flash.js 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. /*!
  2. {
  3. "name": "Flash",
  4. "property": "flash",
  5. "tags": ["flash"],
  6. "polyfills": ["shumway"]
  7. }
  8. !*/
  9. /* DOC
  10. Detects Flash support as well as Flash-blocking plugins
  11. */
  12. define(['Modernizr', 'createElement', 'docElement', 'addTest', 'getBody', 'isSVG'], function(Modernizr, createElement, docElement, addTest, getBody, isSVG) {
  13. Modernizr.addAsyncTest(function() {
  14. var attachBody = function(body) {
  15. if (!docElement.contains(body)) {
  16. docElement.appendChild(body);
  17. }
  18. };
  19. var removeFakeBody = function(body) {
  20. // If we’re rockin’ an attached fake body, clean it up
  21. if (body.fake && body.parentNode) {
  22. body.parentNode.removeChild(body);
  23. }
  24. };
  25. var runTest = function(result, embed) {
  26. var bool = !!result;
  27. if (bool) {
  28. bool = new Boolean(bool);
  29. bool.blocked = (result === 'blocked');
  30. }
  31. addTest('flash', function() { return bool; });
  32. if (embed && body.contains(embed)) {
  33. // in case embed has been wrapped, as with ClickToPlugin
  34. while (embed.parentNode !== body) {
  35. embed = embed.parentNode;
  36. }
  37. body.removeChild(embed);
  38. }
  39. };
  40. var easy_detect;
  41. var activex;
  42. // we wrap activex in a try/catch because when Flash is disabled through
  43. // ActiveX controls, it throws an error.
  44. try {
  45. // Pan is an API that exists for Flash objects.
  46. activex = 'ActiveXObject' in window && 'Pan' in new window.ActiveXObject('ShockwaveFlash.ShockwaveFlash');
  47. } catch (e) {}
  48. easy_detect = !(('plugins' in navigator && 'Shockwave Flash' in navigator.plugins) || activex);
  49. if (easy_detect || isSVG) {
  50. runTest(false);
  51. }
  52. else {
  53. // Flash seems to be installed, but it might be blocked. We have to
  54. // actually create an element to see what happens to it.
  55. var embed = createElement('embed');
  56. var body = getBody();
  57. var blockedDetect;
  58. var inline_style;
  59. embed.type = 'application/x-shockwave-flash';
  60. // Need to do this in the body (fake or otherwise) otherwise IE8 complains
  61. body.appendChild(embed);
  62. // Pan doesn't exist in the embed if its IE (its on the ActiveXObject)
  63. // so this check is for all other browsers.
  64. if (!('Pan' in embed) && !activex) {
  65. attachBody(body);
  66. runTest('blocked', embed);
  67. removeFakeBody(body);
  68. return;
  69. }
  70. blockedDetect = function() {
  71. // if we used a fake body originally, we need to restart this test, since
  72. // we haven't been attached to the DOM, and therefore none of the blockers
  73. // have had time to work.
  74. attachBody(body);
  75. if (!docElement.contains(body)) {
  76. body = document.body || body;
  77. embed = createElement('embed');
  78. embed.type = 'application/x-shockwave-flash';
  79. body.appendChild(embed);
  80. return setTimeout(blockedDetect, 1000);
  81. }
  82. if (!docElement.contains(embed)) {
  83. runTest('blocked');
  84. }
  85. else {
  86. inline_style = embed.style.cssText;
  87. if (inline_style !== '') {
  88. // the style of the element has changed automatically. This is a
  89. // really poor heuristic, but for lower end Flash blocks, it the
  90. // only change they can make.
  91. runTest('blocked', embed);
  92. }
  93. else {
  94. runTest(true, embed);
  95. }
  96. }
  97. removeFakeBody(body);
  98. };
  99. // If we have got this far, there is still a chance a userland plugin
  100. // is blocking us (either changing the styles, or automatically removing
  101. // the element). Both of these require us to take a step back for a moment
  102. // to allow for them to get time of the thread, hence a setTimeout.
  103. //
  104. setTimeout(blockedDetect, 10);
  105. }
  106. });
  107. });