handlerHandleDialog.js 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. import { sendDebugCommand } from '../helper';
  2. const overwriteDialog = (accept, promptText) => `
  3. const realConfirm = window.confirm;
  4. window.confirm = function() {
  5. return ${accept};
  6. };
  7. const realAlert = window.alert;
  8. window.alert = function() {
  9. return ${accept};
  10. };
  11. const realPrompt = window.prompt;
  12. window.prompt = function() {
  13. return ${accept} ? "${promptText}" : null;
  14. }
  15. `;
  16. async function handleDialog({ data, id: blockId }) {
  17. if (!this.settings.debugMode || BROWSER_TYPE !== 'chrome') {
  18. const isScriptExist = this.preloadScripts.some(({ id }) => id === blockId);
  19. if (!isScriptExist) {
  20. const payload = {
  21. id: blockId,
  22. isBlock: true,
  23. name: 'javascript-code',
  24. data: {
  25. everyNewTab: true,
  26. code: overwriteDialog(data.accept, data.promptText),
  27. },
  28. };
  29. this.preloadScripts.push(payload);
  30. await this._sendMessageToTab(payload, {}, true);
  31. }
  32. } else {
  33. this.dialogParams = {
  34. accept: data.accept,
  35. promptText: data.promptText,
  36. };
  37. const methodName = 'Page.javascriptDialogOpening';
  38. if (!this.engine.eventListeners[methodName]) {
  39. this.engine.on(methodName, () => {
  40. sendDebugCommand(
  41. this.activeTab.id,
  42. 'Page.handleJavaScriptDialog',
  43. this.dialogParams
  44. );
  45. });
  46. }
  47. }
  48. return {
  49. data: '',
  50. nextBlockId: this.getBlockConnections(blockId),
  51. };
  52. }
  53. export default handleDialog;