importScripts_worker.js 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. /**
  2. * Any copyright is dedicated to the Public Domain.
  3. * http://creativecommons.org/publicdomain/zero/1.0/
  4. */
  5. // Try no args. This shouldn't do anything.
  6. importScripts();
  7. // This caused security exceptions in the past, make sure it doesn't!
  8. var constructor = {}.constructor;
  9. importScripts("importScripts_worker_imported1.js");
  10. // Try to call a function defined in the imported script.
  11. importedScriptFunction();
  12. function tryBadScripts() {
  13. var badScripts = [
  14. // Has a syntax error
  15. "importScripts_worker_imported3.js",
  16. // Throws an exception
  17. "importScripts_worker_imported4.js",
  18. // Shouldn't exist!
  19. "http://example.com/non-existing/importScripts_worker_foo.js",
  20. // Not a valid url
  21. "http://notadomain::notafile aword"
  22. ];
  23. for (var i = 0; i < badScripts.length; i++) {
  24. var caughtException = false;
  25. var url = badScripts[i];
  26. try {
  27. importScripts(url);
  28. }
  29. catch (e) {
  30. caughtException = true;
  31. }
  32. if (!caughtException) {
  33. throw "Bad script didn't throw exception: " + url;
  34. }
  35. }
  36. }
  37. const url = "data:text/plain,const startResponse = 'started';";
  38. importScripts(url);
  39. onmessage = function(event) {
  40. switch (event.data) {
  41. case 'start':
  42. importScripts("importScripts_worker_imported2.js");
  43. importedScriptFunction2();
  44. tryBadScripts();
  45. postMessage(startResponse);
  46. break;
  47. case 'stop':
  48. tryBadScripts();
  49. postMessage('stopped');
  50. break;
  51. default:
  52. throw new Error("Bad message: " + event.data);
  53. break;
  54. }
  55. }
  56. tryBadScripts();