loader.js 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. "use strict";
  2. /**
  3. * Copyright (c) Microsoft Corporation.
  4. *
  5. * Licensed under the Apache License, Version 2.0 (the "License");
  6. * you may not use this file except in compliance with the License.
  7. * You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. */
  17. const {
  18. app
  19. } = require('electron');
  20. const {
  21. chromiumSwitches
  22. } = require('../chromium/chromiumSwitches');
  23. // [Electron, -r, loader.js, --inspect=0, --remote-debugging-port=0, ...args]
  24. process.argv.splice(1, 4);
  25. for (const arg of chromiumSwitches) {
  26. const match = arg.match(/--([^=]*)=?(.*)/);
  27. app.commandLine.appendSwitch(match[1], match[2]);
  28. }
  29. // Defer ready event.
  30. const originalWhenReady = app.whenReady();
  31. const originalEmit = app.emit.bind(app);
  32. let readyEventArgs;
  33. app.emit = (event, ...args) => {
  34. if (event === 'ready') {
  35. readyEventArgs = args;
  36. return app.listenerCount('ready') > 0;
  37. }
  38. return originalEmit(event, ...args);
  39. };
  40. let isReady = false;
  41. let whenReadyCallback;
  42. const whenReadyPromise = new Promise(f => whenReadyCallback = f);
  43. app.isReady = () => isReady;
  44. app.whenReady = () => whenReadyPromise;
  45. globalThis.__playwright_run = async () => {
  46. // Wait for app to be ready to avoid browser initialization races.
  47. const event = await originalWhenReady;
  48. isReady = true;
  49. whenReadyCallback(event);
  50. originalEmit('ready', ...readyEventArgs);
  51. };