workers-blobworkers.js 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. // by jussi-kalliokoski
  2. // This test is asynchronous. Watch out.
  3. // The test will potentially add garbage to console.
  4. (function(){
  5. try {
  6. // we're avoiding using Modernizr._domPrefixes as the prefix capitalization on
  7. // these guys are notoriously peculiar.
  8. var BlobBuilder = window.MozBlobBuilder || window.WebKitBlobBuilder || window.MSBlobBuilder || window.OBlobBuilder || window.BlobBuilder;
  9. var URL = window.MozURL || window.webkitURL || window.MSURL || window.OURL || window.URL;
  10. var data = 'Modernizr',
  11. blob,
  12. bb,
  13. worker,
  14. url,
  15. timeout,
  16. scriptText = 'this.onmessage=function(e){postMessage(e.data)}';
  17. try {
  18. blob = new Blob([scriptText], {type:'text/javascript'});
  19. } catch(e) {
  20. // we'll fall back to the deprecated BlobBuilder
  21. }
  22. if (!blob) {
  23. bb = new BlobBuilder();
  24. bb.append(scriptText);
  25. blob = bb.getBlob();
  26. }
  27. url = URL.createObjectURL(blob);
  28. worker = new Worker(url);
  29. worker.onmessage = function(e) {
  30. Modernizr.addTest('blobworkers', data === e.data);
  31. cleanup();
  32. };
  33. // Just in case...
  34. worker.onerror = fail;
  35. timeout = setTimeout(fail, 200);
  36. worker.postMessage(data);
  37. } catch (e) {
  38. fail();
  39. }
  40. function fail() {
  41. Modernizr.addTest('blobworkers', false);
  42. cleanup();
  43. }
  44. function cleanup() {
  45. if (url) {
  46. URL.revokeObjectURL(url);
  47. }
  48. if (worker) {
  49. worker.terminate();
  50. }
  51. if (timeout) {
  52. clearTimeout(timeout);
  53. }
  54. }
  55. }());