dialog.js 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. "use strict";
  2. var __defProp = Object.defineProperty;
  3. var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
  4. var __getOwnPropNames = Object.getOwnPropertyNames;
  5. var __hasOwnProp = Object.prototype.hasOwnProperty;
  6. var __export = (target, all) => {
  7. for (var name in all)
  8. __defProp(target, name, { get: all[name], enumerable: true });
  9. };
  10. var __copyProps = (to, from, except, desc) => {
  11. if (from && typeof from === "object" || typeof from === "function") {
  12. for (let key of __getOwnPropNames(from))
  13. if (!__hasOwnProp.call(to, key) && key !== except)
  14. __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
  15. }
  16. return to;
  17. };
  18. var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
  19. var dialog_exports = {};
  20. __export(dialog_exports, {
  21. Dialog: () => Dialog,
  22. DialogManager: () => DialogManager
  23. });
  24. module.exports = __toCommonJS(dialog_exports);
  25. var import_utils = require("../utils");
  26. var import_instrumentation = require("./instrumentation");
  27. class Dialog extends import_instrumentation.SdkObject {
  28. constructor(page, type, message, onHandle, defaultValue) {
  29. super(page, "dialog");
  30. this._handled = false;
  31. this._page = page;
  32. this._type = type;
  33. this._message = message;
  34. this._onHandle = onHandle;
  35. this._defaultValue = defaultValue || "";
  36. }
  37. page() {
  38. return this._page;
  39. }
  40. type() {
  41. return this._type;
  42. }
  43. message() {
  44. return this._message;
  45. }
  46. defaultValue() {
  47. return this._defaultValue;
  48. }
  49. async accept(promptText) {
  50. (0, import_utils.assert)(!this._handled, "Cannot accept dialog which is already handled!");
  51. this._handled = true;
  52. this._page.browserContext.dialogManager.dialogWillClose(this);
  53. await this._onHandle(true, promptText);
  54. }
  55. async dismiss() {
  56. (0, import_utils.assert)(!this._handled, "Cannot dismiss dialog which is already handled!");
  57. this._handled = true;
  58. this._page.browserContext.dialogManager.dialogWillClose(this);
  59. await this._onHandle(false);
  60. }
  61. async close() {
  62. if (this._type === "beforeunload")
  63. await this.accept();
  64. else
  65. await this.dismiss();
  66. }
  67. }
  68. class DialogManager {
  69. constructor(instrumentation) {
  70. this._dialogHandlers = /* @__PURE__ */ new Set();
  71. this._openedDialogs = /* @__PURE__ */ new Set();
  72. this._instrumentation = instrumentation;
  73. }
  74. dialogDidOpen(dialog) {
  75. for (const frame of dialog.page().frameManager.frames())
  76. frame._invalidateNonStallingEvaluations("JavaScript dialog interrupted evaluation");
  77. this._openedDialogs.add(dialog);
  78. this._instrumentation.onDialog(dialog);
  79. let hasHandlers = false;
  80. for (const handler of this._dialogHandlers) {
  81. if (handler(dialog))
  82. hasHandlers = true;
  83. }
  84. if (!hasHandlers)
  85. dialog.close().then(() => {
  86. });
  87. }
  88. dialogWillClose(dialog) {
  89. this._openedDialogs.delete(dialog);
  90. }
  91. addDialogHandler(handler) {
  92. this._dialogHandlers.add(handler);
  93. }
  94. removeDialogHandler(handler) {
  95. this._dialogHandlers.delete(handler);
  96. if (!this._dialogHandlers.size) {
  97. for (const dialog of this._openedDialogs)
  98. dialog.close().catch(() => {
  99. });
  100. }
  101. }
  102. hasOpenDialogsForPage(page) {
  103. return [...this._openedDialogs].some((dialog) => dialog.page() === page);
  104. }
  105. async closeBeforeUnloadDialogs() {
  106. await Promise.all([...this._openedDialogs].map(async (dialog) => {
  107. if (dialog.type() === "beforeunload")
  108. await dialog.dismiss();
  109. }));
  110. }
  111. }
  112. // Annotate the CommonJS export names for ESM import in node:
  113. 0 && (module.exports = {
  114. Dialog,
  115. DialogManager
  116. });