test-only-api.js 932 B

12345678910111213141516171819202122232425262728293031
  1. 'use strict';
  2. /* Whether the browser is Chromium-based with MojoJS enabled */
  3. const isChromiumBased = 'MojoInterfaceInterceptor' in self;
  4. /* Whether the browser is WebKit-based with internal test-only API enabled */
  5. const isWebKitBased = !isChromiumBased && 'internals' in self;
  6. /**
  7. * Loads a script in a window or worker.
  8. *
  9. * @param {string} path - A script path
  10. * @returns {Promise}
  11. */
  12. function loadScript(path) {
  13. if (typeof document === 'undefined') {
  14. // Workers (importScripts is synchronous and may throw.)
  15. importScripts(path);
  16. return Promise.resolve();
  17. } else {
  18. // Window
  19. const script = document.createElement('script');
  20. script.src = path;
  21. script.async = false;
  22. const p = new Promise((resolve, reject) => {
  23. script.onload = () => { resolve(); };
  24. script.onerror = e => { reject(`Error loading ${path}`); };
  25. })
  26. document.head.appendChild(script);
  27. return p;
  28. }
  29. }