tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

eval-dynamic-import-worker.js (1268B)


      1 // This script is meant to be imported by a module worker. It receives a
      2 // message from the worker and responds with the list of imported modules.
      3 const code = 'const sourcePromise = new Promise(resolve => {' +
      4    '  if (\'DedicatedWorkerGlobalScope\' in self &&' +
      5    '      self instanceof DedicatedWorkerGlobalScope) {' +
      6    '    self.onmessage = e => {' +
      7    '      resolve(e.target);' +
      8    '    };' +
      9    '  } else if (\'SharedWorkerGlobalScope\' in self &&' +
     10    '             self instanceof SharedWorkerGlobalScope) {' +
     11    '    self.onconnect = e => {' +
     12    '      resolve(e.ports[0]);' +
     13    '    };' +
     14    '  } else if (\'ServiceWorkerGlobalScope\' in self &&' +
     15    '             self instanceof ServiceWorkerGlobalScope) {' +
     16    '    self.onmessage = e => {' +
     17    '      resolve(e.source);' +
     18    '    };' +
     19    '  }' +
     20    '});' +
     21    'const importedModulesPromise =' +
     22    '  import(\'./export-on-load-script.js\')' +
     23    '    .then(module => module.importedModules)' +
     24    '    .catch(error => `Failed to do dynamic import: ${error}`);' +
     25    'Promise.all([sourcePromise, importedModulesPromise]).then(results => {' +
     26    '  const [source, importedModules] = results;' +
     27    '  source.postMessage(importedModules);' +
     28    '});';
     29 eval(code);