patchright-core 1.48.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (333) hide show
  1. package/README.md +3 -0
  2. package/ThirdPartyNotices.txt +1548 -0
  3. package/bin/PrintDeps.exe +0 -0
  4. package/bin/README.md +2 -0
  5. package/bin/install_media_pack.ps1 +5 -0
  6. package/bin/reinstall_chrome_beta_linux.sh +40 -0
  7. package/bin/reinstall_chrome_beta_mac.sh +13 -0
  8. package/bin/reinstall_chrome_beta_win.ps1 +24 -0
  9. package/bin/reinstall_chrome_stable_linux.sh +40 -0
  10. package/bin/reinstall_chrome_stable_mac.sh +12 -0
  11. package/bin/reinstall_chrome_stable_win.ps1 +24 -0
  12. package/bin/reinstall_msedge_beta_linux.sh +40 -0
  13. package/bin/reinstall_msedge_beta_mac.sh +11 -0
  14. package/bin/reinstall_msedge_beta_win.ps1 +23 -0
  15. package/bin/reinstall_msedge_dev_linux.sh +40 -0
  16. package/bin/reinstall_msedge_dev_mac.sh +11 -0
  17. package/bin/reinstall_msedge_dev_win.ps1 +23 -0
  18. package/bin/reinstall_msedge_stable_linux.sh +40 -0
  19. package/bin/reinstall_msedge_stable_mac.sh +11 -0
  20. package/bin/reinstall_msedge_stable_win.ps1 +24 -0
  21. package/browsers.json +57 -0
  22. package/cli.js +18 -0
  23. package/index.d.ts +17 -0
  24. package/index.js +33 -0
  25. package/index.mjs +28 -0
  26. package/lib/androidServerImpl.js +69 -0
  27. package/lib/browserServerImpl.js +92 -0
  28. package/lib/cli/driver.js +95 -0
  29. package/lib/cli/program.js +589 -0
  30. package/lib/cli/programWithTestStub.js +67 -0
  31. package/lib/client/accessibility.js +50 -0
  32. package/lib/client/android.js +473 -0
  33. package/lib/client/api.js +285 -0
  34. package/lib/client/artifact.js +79 -0
  35. package/lib/client/browser.js +145 -0
  36. package/lib/client/browserContext.js +583 -0
  37. package/lib/client/browserType.js +241 -0
  38. package/lib/client/cdpSession.js +53 -0
  39. package/lib/client/channelOwner.js +235 -0
  40. package/lib/client/clientHelper.js +57 -0
  41. package/lib/client/clientInstrumentation.js +50 -0
  42. package/lib/client/clock.js +69 -0
  43. package/lib/client/connection.js +333 -0
  44. package/lib/client/consoleMessage.js +55 -0
  45. package/lib/client/coverage.js +41 -0
  46. package/lib/client/dialog.js +57 -0
  47. package/lib/client/download.js +62 -0
  48. package/lib/client/electron.js +135 -0
  49. package/lib/client/elementHandle.js +321 -0
  50. package/lib/client/errors.js +77 -0
  51. package/lib/client/eventEmitter.js +314 -0
  52. package/lib/client/events.js +94 -0
  53. package/lib/client/fetch.js +391 -0
  54. package/lib/client/fileChooser.js +45 -0
  55. package/lib/client/frame.js +504 -0
  56. package/lib/client/harRouter.js +99 -0
  57. package/lib/client/input.js +111 -0
  58. package/lib/client/jsHandle.js +121 -0
  59. package/lib/client/jsonPipe.js +35 -0
  60. package/lib/client/localUtils.js +36 -0
  61. package/lib/client/locator.js +441 -0
  62. package/lib/client/network.js +762 -0
  63. package/lib/client/page.js +770 -0
  64. package/lib/client/playwright.js +80 -0
  65. package/lib/client/selectors.js +67 -0
  66. package/lib/client/stream.js +54 -0
  67. package/lib/client/tracing.js +134 -0
  68. package/lib/client/types.js +24 -0
  69. package/lib/client/video.js +51 -0
  70. package/lib/client/waiter.js +158 -0
  71. package/lib/client/webError.js +37 -0
  72. package/lib/client/worker.js +71 -0
  73. package/lib/client/writableStream.js +54 -0
  74. package/lib/common/socksProxy.js +569 -0
  75. package/lib/common/timeoutSettings.js +73 -0
  76. package/lib/common/types.js +5 -0
  77. package/lib/generated/clockSource.js +7 -0
  78. package/lib/generated/consoleApiSource.js +7 -0
  79. package/lib/generated/injectedScriptSource.js +7 -0
  80. package/lib/generated/pollingRecorderSource.js +7 -0
  81. package/lib/generated/utilityScriptSource.js +7 -0
  82. package/lib/generated/webSocketMockSource.js +7 -0
  83. package/lib/image_tools/colorUtils.js +98 -0
  84. package/lib/image_tools/compare.js +108 -0
  85. package/lib/image_tools/imageChannel.js +70 -0
  86. package/lib/image_tools/stats.js +102 -0
  87. package/lib/inProcessFactory.js +54 -0
  88. package/lib/inprocess.js +20 -0
  89. package/lib/outofprocess.js +67 -0
  90. package/lib/protocol/debug.js +27 -0
  91. package/lib/protocol/serializers.js +173 -0
  92. package/lib/protocol/transport.js +82 -0
  93. package/lib/protocol/validator.js +2760 -0
  94. package/lib/protocol/validatorPrimitives.js +139 -0
  95. package/lib/remote/playwrightConnection.js +274 -0
  96. package/lib/remote/playwrightServer.js +110 -0
  97. package/lib/server/accessibility.js +62 -0
  98. package/lib/server/android/android.js +441 -0
  99. package/lib/server/android/backendAdb.js +172 -0
  100. package/lib/server/artifact.js +104 -0
  101. package/lib/server/bidi/bidiBrowser.js +311 -0
  102. package/lib/server/bidi/bidiChromium.js +124 -0
  103. package/lib/server/bidi/bidiConnection.js +206 -0
  104. package/lib/server/bidi/bidiExecutionContext.js +159 -0
  105. package/lib/server/bidi/bidiFirefox.js +104 -0
  106. package/lib/server/bidi/bidiInput.js +158 -0
  107. package/lib/server/bidi/bidiNetworkManager.js +338 -0
  108. package/lib/server/bidi/bidiOverCdp.js +103 -0
  109. package/lib/server/bidi/bidiPage.js +529 -0
  110. package/lib/server/bidi/bidiPdf.js +140 -0
  111. package/lib/server/bidi/third_party/bidiDeserializer.js +93 -0
  112. package/lib/server/bidi/third_party/bidiKeyboard.js +238 -0
  113. package/lib/server/bidi/third_party/bidiProtocol.js +139 -0
  114. package/lib/server/bidi/third_party/bidiSerializer.js +144 -0
  115. package/lib/server/bidi/third_party/firefoxPrefs.js +221 -0
  116. package/lib/server/browser.js +148 -0
  117. package/lib/server/browserContext.js +666 -0
  118. package/lib/server/browserType.js +335 -0
  119. package/lib/server/chromium/appIcon.png +0 -0
  120. package/lib/server/chromium/chromium.js +350 -0
  121. package/lib/server/chromium/chromiumSwitches.js +36 -0
  122. package/lib/server/chromium/crAccessibility.js +237 -0
  123. package/lib/server/chromium/crBrowser.js +522 -0
  124. package/lib/server/chromium/crConnection.js +228 -0
  125. package/lib/server/chromium/crCoverage.js +246 -0
  126. package/lib/server/chromium/crDevTools.js +104 -0
  127. package/lib/server/chromium/crDragDrop.js +143 -0
  128. package/lib/server/chromium/crExecutionContext.js +149 -0
  129. package/lib/server/chromium/crInput.js +171 -0
  130. package/lib/server/chromium/crNetworkManager.js +809 -0
  131. package/lib/server/chromium/crPage.js +1235 -0
  132. package/lib/server/chromium/crPdf.js +153 -0
  133. package/lib/server/chromium/crProtocolHelper.js +133 -0
  134. package/lib/server/chromium/crServiceWorker.js +111 -0
  135. package/lib/server/chromium/defaultFontFamilies.js +145 -0
  136. package/lib/server/chromium/videoRecorder.js +155 -0
  137. package/lib/server/clock.js +133 -0
  138. package/lib/server/codegen/csharp.js +299 -0
  139. package/lib/server/codegen/java.js +235 -0
  140. package/lib/server/codegen/javascript.js +223 -0
  141. package/lib/server/codegen/jsonl.js +47 -0
  142. package/lib/server/codegen/language.js +88 -0
  143. package/lib/server/codegen/languages.js +30 -0
  144. package/lib/server/codegen/python.js +265 -0
  145. package/lib/server/codegen/types.js +5 -0
  146. package/lib/server/console.js +57 -0
  147. package/lib/server/cookieStore.js +185 -0
  148. package/lib/server/debugController.js +234 -0
  149. package/lib/server/debugger.js +132 -0
  150. package/lib/server/deviceDescriptors.js +26 -0
  151. package/lib/server/deviceDescriptorsSource.json +1669 -0
  152. package/lib/server/dialog.js +71 -0
  153. package/lib/server/dispatchers/androidDispatcher.js +193 -0
  154. package/lib/server/dispatchers/artifactDispatcher.js +118 -0
  155. package/lib/server/dispatchers/browserContextDispatcher.js +368 -0
  156. package/lib/server/dispatchers/browserDispatcher.js +170 -0
  157. package/lib/server/dispatchers/browserTypeDispatcher.js +55 -0
  158. package/lib/server/dispatchers/cdpSessionDispatcher.js +48 -0
  159. package/lib/server/dispatchers/debugControllerDispatcher.js +103 -0
  160. package/lib/server/dispatchers/dialogDispatcher.js +44 -0
  161. package/lib/server/dispatchers/dispatcher.js +395 -0
  162. package/lib/server/dispatchers/electronDispatcher.js +93 -0
  163. package/lib/server/dispatchers/elementHandlerDispatcher.js +228 -0
  164. package/lib/server/dispatchers/frameDispatcher.js +286 -0
  165. package/lib/server/dispatchers/jsHandleDispatcher.js +97 -0
  166. package/lib/server/dispatchers/jsonPipeDispatcher.js +59 -0
  167. package/lib/server/dispatchers/localUtilsDispatcher.js +413 -0
  168. package/lib/server/dispatchers/networkDispatchers.js +221 -0
  169. package/lib/server/dispatchers/pageDispatcher.js +367 -0
  170. package/lib/server/dispatchers/playwrightDispatcher.js +107 -0
  171. package/lib/server/dispatchers/selectorsDispatcher.js +36 -0
  172. package/lib/server/dispatchers/streamDispatcher.js +62 -0
  173. package/lib/server/dispatchers/tracingDispatcher.js +54 -0
  174. package/lib/server/dispatchers/webSocketRouteDispatcher.js +189 -0
  175. package/lib/server/dispatchers/writableStreamDispatcher.js +58 -0
  176. package/lib/server/dom.js +845 -0
  177. package/lib/server/download.js +60 -0
  178. package/lib/server/electron/electron.js +296 -0
  179. package/lib/server/electron/loader.js +57 -0
  180. package/lib/server/errors.js +68 -0
  181. package/lib/server/fetch.js +656 -0
  182. package/lib/server/fileChooser.js +42 -0
  183. package/lib/server/fileUploadUtils.js +75 -0
  184. package/lib/server/firefox/ffAccessibility.js +216 -0
  185. package/lib/server/firefox/ffBrowser.js +460 -0
  186. package/lib/server/firefox/ffConnection.js +168 -0
  187. package/lib/server/firefox/ffExecutionContext.js +135 -0
  188. package/lib/server/firefox/ffInput.js +150 -0
  189. package/lib/server/firefox/ffNetworkManager.js +233 -0
  190. package/lib/server/firefox/ffPage.js +559 -0
  191. package/lib/server/firefox/firefox.js +99 -0
  192. package/lib/server/formData.js +75 -0
  193. package/lib/server/frameSelectors.js +171 -0
  194. package/lib/server/frames.js +1808 -0
  195. package/lib/server/har/harRecorder.js +139 -0
  196. package/lib/server/har/harTracer.js +542 -0
  197. package/lib/server/helper.js +103 -0
  198. package/lib/server/index.js +114 -0
  199. package/lib/server/input.js +310 -0
  200. package/lib/server/instrumentation.js +70 -0
  201. package/lib/server/isomorphic/utilityScriptSerializers.js +226 -0
  202. package/lib/server/javascript.js +299 -0
  203. package/lib/server/launchApp.js +91 -0
  204. package/lib/server/macEditingCommands.js +139 -0
  205. package/lib/server/network.js +617 -0
  206. package/lib/server/page.js +819 -0
  207. package/lib/server/pipeTransport.js +85 -0
  208. package/lib/server/playwright.js +88 -0
  209. package/lib/server/progress.js +102 -0
  210. package/lib/server/protocolError.js +49 -0
  211. package/lib/server/recorder/contextRecorder.js +299 -0
  212. package/lib/server/recorder/recorderApp.js +196 -0
  213. package/lib/server/recorder/recorderCollection.js +116 -0
  214. package/lib/server/recorder/recorderFrontend.js +5 -0
  215. package/lib/server/recorder/recorderInTraceViewer.js +144 -0
  216. package/lib/server/recorder/recorderRunner.js +155 -0
  217. package/lib/server/recorder/recorderUtils.js +112 -0
  218. package/lib/server/recorder/throttledFile.js +46 -0
  219. package/lib/server/recorder.js +329 -0
  220. package/lib/server/registry/browserFetcher.js +168 -0
  221. package/lib/server/registry/dependencies.js +322 -0
  222. package/lib/server/registry/index.js +1005 -0
  223. package/lib/server/registry/nativeDeps.js +490 -0
  224. package/lib/server/registry/oopDownloadBrowserMain.js +138 -0
  225. package/lib/server/screenshotter.js +348 -0
  226. package/lib/server/selectors.js +73 -0
  227. package/lib/server/socksClientCertificatesInterceptor.js +340 -0
  228. package/lib/server/socksInterceptor.js +100 -0
  229. package/lib/server/trace/recorder/snapshotter.js +172 -0
  230. package/lib/server/trace/recorder/snapshotterInjected.js +493 -0
  231. package/lib/server/trace/recorder/tracing.js +542 -0
  232. package/lib/server/trace/test/inMemorySnapshotter.js +93 -0
  233. package/lib/server/trace/viewer/traceViewer.js +213 -0
  234. package/lib/server/transport.js +191 -0
  235. package/lib/server/types.js +24 -0
  236. package/lib/server/usKeyboardLayout.js +555 -0
  237. package/lib/server/webkit/webkit.js +87 -0
  238. package/lib/server/webkit/wkAccessibility.js +194 -0
  239. package/lib/server/webkit/wkBrowser.js +329 -0
  240. package/lib/server/webkit/wkConnection.js +173 -0
  241. package/lib/server/webkit/wkExecutionContext.js +143 -0
  242. package/lib/server/webkit/wkInput.js +169 -0
  243. package/lib/server/webkit/wkInterceptableRequest.js +162 -0
  244. package/lib/server/webkit/wkPage.js +1219 -0
  245. package/lib/server/webkit/wkProvisionalPage.js +94 -0
  246. package/lib/server/webkit/wkWorkers.js +104 -0
  247. package/lib/third_party/diff_match_patch.js +2222 -0
  248. package/lib/third_party/pixelmatch.js +255 -0
  249. package/lib/utils/ascii.js +31 -0
  250. package/lib/utils/comparators.js +171 -0
  251. package/lib/utils/crypto.js +174 -0
  252. package/lib/utils/debug.js +46 -0
  253. package/lib/utils/debugLogger.js +91 -0
  254. package/lib/utils/env.js +49 -0
  255. package/lib/utils/eventsHelper.js +38 -0
  256. package/lib/utils/expectUtils.js +33 -0
  257. package/lib/utils/fileUtils.js +205 -0
  258. package/lib/utils/happy-eyeballs.js +194 -0
  259. package/lib/utils/headers.js +52 -0
  260. package/lib/utils/hostPlatform.js +133 -0
  261. package/lib/utils/httpServer.js +237 -0
  262. package/lib/utils/index.js +368 -0
  263. package/lib/utils/isomorphic/cssParser.js +250 -0
  264. package/lib/utils/isomorphic/cssTokenizer.js +979 -0
  265. package/lib/utils/isomorphic/locatorGenerators.js +642 -0
  266. package/lib/utils/isomorphic/locatorParser.js +179 -0
  267. package/lib/utils/isomorphic/locatorUtils.js +62 -0
  268. package/lib/utils/isomorphic/mimeType.js +29 -0
  269. package/lib/utils/isomorphic/recorderUtils.js +195 -0
  270. package/lib/utils/isomorphic/selectorParser.js +397 -0
  271. package/lib/utils/isomorphic/stringUtils.js +139 -0
  272. package/lib/utils/isomorphic/traceUtils.js +39 -0
  273. package/lib/utils/isomorphic/urlMatch.js +120 -0
  274. package/lib/utils/linuxUtils.js +78 -0
  275. package/lib/utils/manualPromise.js +109 -0
  276. package/lib/utils/multimap.js +75 -0
  277. package/lib/utils/network.js +160 -0
  278. package/lib/utils/processLauncher.js +248 -0
  279. package/lib/utils/profiler.js +53 -0
  280. package/lib/utils/rtti.js +44 -0
  281. package/lib/utils/semaphore.js +51 -0
  282. package/lib/utils/spawnAsync.js +45 -0
  283. package/lib/utils/stackTrace.js +121 -0
  284. package/lib/utils/task.js +58 -0
  285. package/lib/utils/time.js +37 -0
  286. package/lib/utils/timeoutRunner.js +66 -0
  287. package/lib/utils/traceUtils.js +44 -0
  288. package/lib/utils/userAgent.js +105 -0
  289. package/lib/utils/wsServer.js +127 -0
  290. package/lib/utils/zipFile.js +75 -0
  291. package/lib/utils/zones.js +62 -0
  292. package/lib/utilsBundle.js +82 -0
  293. package/lib/utilsBundleImpl/index.js +53 -0
  294. package/lib/utilsBundleImpl/xdg-open +1066 -0
  295. package/lib/vite/htmlReport/index.html +66 -0
  296. package/lib/vite/recorder/assets/codeMirrorModule-baozm8ur.js +24 -0
  297. package/lib/vite/recorder/assets/codeMirrorModule-ez37Vkbh.css +1 -0
  298. package/lib/vite/recorder/assets/codicon-DCmgc-ay.ttf +0 -0
  299. package/lib/vite/recorder/assets/index-2ElAIWFB.js +42 -0
  300. package/lib/vite/recorder/assets/index-BW-aOBcL.css +1 -0
  301. package/lib/vite/recorder/index.html +29 -0
  302. package/lib/vite/recorder/playwright-logo.svg +9 -0
  303. package/lib/vite/traceViewer/assets/codeMirrorModule-Bh1rfd2w.js +24 -0
  304. package/lib/vite/traceViewer/assets/inspectorTab-7GHnKvSD.js +64 -0
  305. package/lib/vite/traceViewer/assets/testServerConnection-DeE2kSzz.js +1 -0
  306. package/lib/vite/traceViewer/assets/workbench-DPQnTHYP.js +9 -0
  307. package/lib/vite/traceViewer/assets/xtermModule-BeNbaIVa.js +9 -0
  308. package/lib/vite/traceViewer/codeMirrorModule.ez37Vkbh.css +1 -0
  309. package/lib/vite/traceViewer/codicon.DCmgc-ay.ttf +0 -0
  310. package/lib/vite/traceViewer/embedded.BlHoW5LY.js +2 -0
  311. package/lib/vite/traceViewer/embedded.html +18 -0
  312. package/lib/vite/traceViewer/embedded.w7WN2u1R.css +1 -0
  313. package/lib/vite/traceViewer/index.CrbWWHbf.css +1 -0
  314. package/lib/vite/traceViewer/index.DaWVfou1.js +2 -0
  315. package/lib/vite/traceViewer/index.html +29 -0
  316. package/lib/vite/traceViewer/inspectorTab.DLjBDrQR.css +1 -0
  317. package/lib/vite/traceViewer/playwright-logo.svg +9 -0
  318. package/lib/vite/traceViewer/recorder.B_SY1GJM.css +0 -0
  319. package/lib/vite/traceViewer/recorder.C4zxcvd2.js +2 -0
  320. package/lib/vite/traceViewer/recorder.html +17 -0
  321. package/lib/vite/traceViewer/snapshot.html +21 -0
  322. package/lib/vite/traceViewer/sw.bundle.js +3 -0
  323. package/lib/vite/traceViewer/uiMode.CAYqod-m.css +1 -0
  324. package/lib/vite/traceViewer/uiMode.html +20 -0
  325. package/lib/vite/traceViewer/uiMode.mTXWniJb.js +5 -0
  326. package/lib/vite/traceViewer/workbench.D3JVcA9K.css +1 -0
  327. package/lib/vite/traceViewer/xtermModule.DSXBckUd.css +32 -0
  328. package/lib/zipBundle.js +25 -0
  329. package/lib/zipBundleImpl.js +5 -0
  330. package/package.json +44 -0
  331. package/types/protocol.d.ts +21571 -0
  332. package/types/structs.d.ts +45 -0
  333. package/types/types.d.ts +22519 -0
@@ -0,0 +1,1235 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.CRPage = void 0;
7
+ var _crypto = _interopRequireDefault(require("crypto"));
8
+ var _path = _interopRequireDefault(require("path"));
9
+ var _eventsHelper = require("../../utils/eventsHelper");
10
+ var _registry = require("../registry");
11
+ var _stackTrace = require("../../utils/stackTrace");
12
+ var _utils = require("../../utils");
13
+ var dialog = _interopRequireWildcard(require("../dialog"));
14
+ var dom = _interopRequireWildcard(require("../dom"));
15
+ var frames = _interopRequireWildcard(require("../frames"));
16
+ var _helper = require("../helper");
17
+ var network = _interopRequireWildcard(require("../network"));
18
+ var _page = require("../page");
19
+ var _crAccessibility = require("./crAccessibility");
20
+ var _crBrowser = require("./crBrowser");
21
+ var _crCoverage = require("./crCoverage");
22
+ var _crDragDrop = require("./crDragDrop");
23
+ var _crExecutionContext = require("./crExecutionContext");
24
+ var _crInput = require("./crInput");
25
+ var _crNetworkManager = require("./crNetworkManager");
26
+ var _crPdf = require("./crPdf");
27
+ var _crProtocolHelper = require("./crProtocolHelper");
28
+ var _defaultFontFamilies = require("./defaultFontFamilies");
29
+ var _videoRecorder = require("./videoRecorder");
30
+ var _browserContext = require("../browserContext");
31
+ var _errors = require("../errors");
32
+ var _protocolError = require("../protocolError");
33
+ function _getRequireWildcardCache(e) { if ("function" != typeof WeakMap) return null; var r = new WeakMap(), t = new WeakMap(); return (_getRequireWildcardCache = function (e) { return e ? t : r; })(e); }
34
+ function _interopRequireWildcard(e, r) { if (!r && e && e.__esModule) return e; if (null === e || "object" != typeof e && "function" != typeof e) return { default: e }; var t = _getRequireWildcardCache(r); if (t && t.has(e)) return t.get(e); var n = { __proto__: null }, a = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var u in e) if ("default" !== u && Object.prototype.hasOwnProperty.call(e, u)) { var i = a ? Object.getOwnPropertyDescriptor(e, u) : null; i && (i.get || i.set) ? Object.defineProperty(n, u, i) : n[u] = e[u]; } return n.default = e, t && t.set(e, n), n; }
35
+ function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
36
+ // undetected-undetected_playwright-patch - custom imports
37
+
38
+ /**
39
+ * Copyright 2017 Google Inc. All rights reserved.
40
+ * Modifications copyright (c) Microsoft Corporation.
41
+ *
42
+ * Licensed under the Apache License, Version 2.0 (the "License");
43
+ * you may not use this file except in compliance with the License.
44
+ * You may obtain a copy of the License at
45
+ *
46
+ * http://www.apache.org/licenses/LICENSE-2.0
47
+ *
48
+ * Unless required by applicable law or agreed to in writing, software
49
+ * distributed under the License is distributed on an "AS IS" BASIS,
50
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
51
+ * See the License for the specific language governing permissions and
52
+ * limitations under the License.
53
+ */
54
+
55
+ const UTILITY_WORLD_NAME = '__playwright_utility_world__';
56
+ class CRPage {
57
+ static mainFrameSession(page) {
58
+ const crPage = page._delegate;
59
+ return crPage._mainFrameSession;
60
+ }
61
+ constructor(client, targetId, browserContext, opener, bits) {
62
+ this._mainFrameSession = void 0;
63
+ this._sessions = new Map();
64
+ this._page = void 0;
65
+ this.rawMouse = void 0;
66
+ this.rawKeyboard = void 0;
67
+ this.rawTouchscreen = void 0;
68
+ this._targetId = void 0;
69
+ this._opener = void 0;
70
+ this._networkManager = void 0;
71
+ this._pdf = void 0;
72
+ this._coverage = void 0;
73
+ this._browserContext = void 0;
74
+ this._pagePromise = void 0;
75
+ this._initializedPage = null;
76
+ this._isBackgroundPage = void 0;
77
+ // Holds window features for the next popup being opened via window.open,
78
+ // until the popup target arrives. This could be racy if two oopifs
79
+ // simultaneously call window.open with window features: the order
80
+ // of their Page.windowOpen events is not guaranteed to match the order
81
+ // of new popup targets.
82
+ this._nextWindowOpenPopupFeatures = [];
83
+ this._targetId = targetId;
84
+ this._opener = opener;
85
+ this._isBackgroundPage = bits.isBackgroundPage;
86
+ const dragManager = new _crDragDrop.DragManager(this);
87
+ this.rawKeyboard = new _crInput.RawKeyboardImpl(client, browserContext._browser._platform() === 'mac', dragManager);
88
+ this.rawMouse = new _crInput.RawMouseImpl(this, client, dragManager);
89
+ this.rawTouchscreen = new _crInput.RawTouchscreenImpl(client);
90
+ this._pdf = new _crPdf.CRPDF(client);
91
+ this._coverage = new _crCoverage.CRCoverage(client);
92
+ this._browserContext = browserContext;
93
+ this._page = new _page.Page(this, browserContext);
94
+ this._networkManager = new _crNetworkManager.CRNetworkManager(this._page, null);
95
+ // Sync any browser context state to the network manager. This does not talk over CDP because
96
+ // we have not connected any sessions to the network manager yet.
97
+ this.updateOffline();
98
+ this.updateExtraHTTPHeaders();
99
+ this.updateHttpCredentials();
100
+ this._networkManager.setRequestInterception(true);
101
+ this.initScriptTag = "injected-playwright-init-script-" + _crypto.default.randomBytes(20).toString('hex');
102
+ this._mainFrameSession = new FrameSession(this, client, targetId, null);
103
+ this._sessions.set(targetId, this._mainFrameSession);
104
+ if (opener && !browserContext._options.noDefaultViewport) {
105
+ const features = opener._nextWindowOpenPopupFeatures.shift() || [];
106
+ const viewportSize = _helper.helper.getViewportSizeFromWindowFeatures(features);
107
+ if (viewportSize) this._page._emulatedSize = {
108
+ viewport: viewportSize,
109
+ screen: viewportSize
110
+ };
111
+ }
112
+ // Note: it is important to call |reportAsNew| before resolving pageOrError promise,
113
+ // so that anyone who awaits pageOrError got a ready and reported page.
114
+ this._pagePromise = this._mainFrameSession._initialize(bits.hasUIWindow).then(async r => {
115
+ await this._page.initOpener(this._opener);
116
+ return r;
117
+ }).catch(async e => {
118
+ await this._page.initOpener(this._opener);
119
+ throw e;
120
+ }).then(() => {
121
+ this._initializedPage = this._page;
122
+ this._reportAsNew();
123
+ return this._page;
124
+ }).catch(e => {
125
+ this._reportAsNew(e);
126
+ return e;
127
+ });
128
+ }
129
+ potentiallyUninitializedPage() {
130
+ return this._page;
131
+ }
132
+ _reportAsNew(error) {
133
+ this._page.reportAsNew(error, this._isBackgroundPage ? _crBrowser.CRBrowserContext.CREvents.BackgroundPage : _browserContext.BrowserContext.Events.Page);
134
+ }
135
+ async _forAllFrameSessions(cb) {
136
+ const frameSessions = Array.from(this._sessions.values());
137
+ await Promise.all(frameSessions.map(frameSession => {
138
+ if (frameSession._isMainFrame()) return cb(frameSession);
139
+ return cb(frameSession).catch(e => {
140
+ // Broadcasting a message to the closed iframe should be a noop.
141
+ if ((0, _protocolError.isSessionClosedError)(e)) return;
142
+ throw e;
143
+ });
144
+ }));
145
+ }
146
+ _sessionForFrame(frame) {
147
+ // Frame id equals target id.
148
+ while (!this._sessions.has(frame._id)) {
149
+ const parent = frame.parentFrame();
150
+ if (!parent) throw new Error(`Frame has been detached.`);
151
+ frame = parent;
152
+ }
153
+ return this._sessions.get(frame._id);
154
+ }
155
+ _sessionForHandle(handle) {
156
+ const frame = handle._context.frame;
157
+ return this._sessionForFrame(frame);
158
+ }
159
+ willBeginDownload() {
160
+ this._mainFrameSession._willBeginDownload();
161
+ }
162
+ async pageOrError() {
163
+ return this._pagePromise;
164
+ }
165
+ didClose() {
166
+ for (const session of this._sessions.values()) session.dispose();
167
+ this._page._didClose();
168
+ }
169
+ async navigateFrame(frame, url, referrer) {
170
+ return this._sessionForFrame(frame)._navigate(frame, url, referrer);
171
+ }
172
+ async updateExtraHTTPHeaders() {
173
+ const headers = network.mergeHeaders([this._browserContext._options.extraHTTPHeaders, this._page.extraHTTPHeaders()]);
174
+ await this._networkManager.setExtraHTTPHeaders(headers);
175
+ }
176
+ async updateGeolocation() {
177
+ await this._forAllFrameSessions(frame => frame._updateGeolocation(false));
178
+ }
179
+ async updateOffline() {
180
+ await this._networkManager.setOffline(!!this._browserContext._options.offline);
181
+ }
182
+ async updateHttpCredentials() {
183
+ await this._networkManager.authenticate(this._browserContext._options.httpCredentials || null);
184
+ }
185
+ async updateEmulatedViewportSize(preserveWindowBoundaries) {
186
+ await this._mainFrameSession._updateViewport(preserveWindowBoundaries);
187
+ }
188
+ async bringToFront() {
189
+ await this._mainFrameSession._client.send('Page.bringToFront');
190
+ }
191
+ async updateEmulateMedia() {
192
+ await this._forAllFrameSessions(frame => frame._updateEmulateMedia());
193
+ }
194
+ async updateUserAgent() {
195
+ await this._forAllFrameSessions(frame => frame._updateUserAgent());
196
+ }
197
+ async updateRequestInterception() {
198
+ await this._networkManager.setRequestInterception(this._page.needsRequestInterception());
199
+ }
200
+ async updateFileChooserInterception() {
201
+ await this._forAllFrameSessions(frame => frame._updateFileChooserInterception(false));
202
+ }
203
+ async reload() {
204
+ await this._mainFrameSession._client.send('Page.reload');
205
+ }
206
+ async _go(delta) {
207
+ const history = await this._mainFrameSession._client.send('Page.getNavigationHistory');
208
+ const entry = history.entries[history.currentIndex + delta];
209
+ if (!entry) return false;
210
+ await this._mainFrameSession._client.send('Page.navigateToHistoryEntry', {
211
+ entryId: entry.id
212
+ });
213
+ return true;
214
+ }
215
+ goBack() {
216
+ return this._go(-1);
217
+ }
218
+ goForward() {
219
+ return this._go(+1);
220
+ }
221
+ async requestGC() {
222
+ await this._mainFrameSession._client.send('HeapProfiler.collectGarbage');
223
+ }
224
+ async addInitScript(initScript, world = 'main') {
225
+ this._page.initScripts.push(initScript);
226
+ await this._forAllFrameSessions(frame => frame._evaluateOnNewDocument(initScript, world));
227
+ }
228
+ async removeInitScripts() {
229
+ await this._forAllFrameSessions(frame => frame._removeEvaluatesOnNewDocument());
230
+ }
231
+ async closePage(runBeforeUnload) {
232
+ if (runBeforeUnload) await this._mainFrameSession._client.send('Page.close');else await this._browserContext._browser._closePage(this);
233
+ }
234
+ async setBackgroundColor(color) {
235
+ await this._mainFrameSession._client.send('Emulation.setDefaultBackgroundColorOverride', {
236
+ color
237
+ });
238
+ }
239
+ async takeScreenshot(progress, format, documentRect, viewportRect, quality, fitsViewport, scale) {
240
+ const {
241
+ visualViewport
242
+ } = await this._mainFrameSession._client.send('Page.getLayoutMetrics');
243
+ if (!documentRect) {
244
+ documentRect = {
245
+ x: visualViewport.pageX + viewportRect.x,
246
+ y: visualViewport.pageY + viewportRect.y,
247
+ ..._helper.helper.enclosingIntSize({
248
+ width: viewportRect.width / visualViewport.scale,
249
+ height: viewportRect.height / visualViewport.scale
250
+ })
251
+ };
252
+ }
253
+ // When taking screenshots with documentRect (based on the page content, not viewport),
254
+ // ignore current page scale.
255
+ const clip = {
256
+ ...documentRect,
257
+ scale: viewportRect ? visualViewport.scale : 1
258
+ };
259
+ if (scale === 'css') {
260
+ const deviceScaleFactor = this._browserContext._options.deviceScaleFactor || 1;
261
+ clip.scale /= deviceScaleFactor;
262
+ }
263
+ progress.throwIfAborted();
264
+ const result = await this._mainFrameSession._client.send('Page.captureScreenshot', {
265
+ format,
266
+ quality,
267
+ clip,
268
+ captureBeyondViewport: !fitsViewport
269
+ });
270
+ return Buffer.from(result.data, 'base64');
271
+ }
272
+ async getContentFrame(handle) {
273
+ return this._sessionForHandle(handle)._getContentFrame(handle);
274
+ }
275
+ async getOwnerFrame(handle) {
276
+ return this._sessionForHandle(handle)._getOwnerFrame(handle);
277
+ }
278
+ isElementHandle(remoteObject) {
279
+ return remoteObject.subtype === 'node';
280
+ }
281
+ async getBoundingBox(handle) {
282
+ return this._sessionForHandle(handle)._getBoundingBox(handle);
283
+ }
284
+ async scrollRectIntoViewIfNeeded(handle, rect) {
285
+ return this._sessionForHandle(handle)._scrollRectIntoViewIfNeeded(handle, rect);
286
+ }
287
+ async setScreencastOptions(options) {
288
+ if (options) {
289
+ await this._mainFrameSession._startScreencast(this, {
290
+ format: 'jpeg',
291
+ quality: options.quality,
292
+ maxWidth: options.width,
293
+ maxHeight: options.height
294
+ });
295
+ } else {
296
+ await this._mainFrameSession._stopScreencast(this);
297
+ }
298
+ }
299
+ rafCountForStablePosition() {
300
+ return 1;
301
+ }
302
+ async getContentQuads(handle) {
303
+ return this._sessionForHandle(handle)._getContentQuads(handle);
304
+ }
305
+ async setInputFiles(handle, files) {
306
+ await handle.evaluateInUtility(([injected, node, files]) => injected.setInputFiles(node, files), files);
307
+ }
308
+ async setInputFilePaths(handle, files) {
309
+ const frame = await handle.ownerFrame();
310
+ if (!frame) throw new Error('Cannot set input files to detached input element');
311
+ const parentSession = this._sessionForFrame(frame);
312
+ await parentSession._client.send('DOM.setFileInputFiles', {
313
+ objectId: handle._objectId,
314
+ files
315
+ });
316
+ }
317
+ async adoptElementHandle(handle, to) {
318
+ return this._sessionForHandle(handle)._adoptElementHandle(handle, to);
319
+ }
320
+ async getAccessibilityTree(needle) {
321
+ return (0, _crAccessibility.getAccessibilityTree)(this._mainFrameSession._client, needle);
322
+ }
323
+ async inputActionEpilogue() {
324
+ await this._mainFrameSession._client.send('Page.enable').catch(e => {});
325
+ }
326
+ async resetForReuse() {}
327
+ async pdf(options) {
328
+ return this._pdf.generate(options);
329
+ }
330
+ coverage() {
331
+ return this._coverage;
332
+ }
333
+ async getFrameElement(frame) {
334
+ let parent = frame.parentFrame();
335
+ if (!parent) throw new Error('Frame has been detached.');
336
+ const parentSession = this._sessionForFrame(parent);
337
+ const {
338
+ backendNodeId
339
+ } = await parentSession._client.send('DOM.getFrameOwner', {
340
+ frameId: frame._id
341
+ }).catch(e => {
342
+ if (e instanceof Error && e.message.includes('Frame with the given id was not found.')) (0, _stackTrace.rewriteErrorMessage)(e, 'Frame has been detached.');
343
+ throw e;
344
+ });
345
+ parent = frame.parentFrame();
346
+ if (!parent) throw new Error('Frame has been detached.');
347
+ return parentSession._adoptBackendNodeId(backendNodeId, await parent._mainContext());
348
+ }
349
+ shouldToggleStyleSheetToSyncAnimations() {
350
+ return false;
351
+ }
352
+ async exposeBinding(binding) {
353
+ await this._forAllFrameSessions(frame => frame._initBinding(binding));
354
+ await Promise.all(this._page.frames().map(frame => frame.evaluateExpression(binding.source).catch(e => {})));
355
+ }
356
+ async removeExposedBindings() {
357
+ await this._forAllFrameSessions(frame => frame._removeExposedBindings());
358
+ }
359
+ }
360
+ exports.CRPage = CRPage;
361
+ class FrameSession {
362
+ constructor(crPage, client, targetId, parentSession) {
363
+ this._client = void 0;
364
+ this._crPage = void 0;
365
+ this._page = void 0;
366
+ this._parentSession = void 0;
367
+ this._childSessions = new Set();
368
+ this._contextIdToContext = new Map();
369
+ this._eventListeners = [];
370
+ this._targetId = void 0;
371
+ this._firstNonInitialNavigationCommittedPromise = void 0;
372
+ this._firstNonInitialNavigationCommittedFulfill = () => {};
373
+ this._firstNonInitialNavigationCommittedReject = e => {};
374
+ this._windowId = void 0;
375
+ // Marks the oopif session that remote -> local transition has happened in the parent.
376
+ // See Target.detachedFromTarget handler for details.
377
+ this._swappedIn = false;
378
+ this._videoRecorder = null;
379
+ this._screencastId = null;
380
+ this._screencastClients = new Set();
381
+ this._evaluateOnNewDocumentIdentifiers = [];
382
+ this._metricsOverride = void 0;
383
+ this._workerSessions = new Map();
384
+ this._exposedBindingNames = [];
385
+ this._evaluateOnNewDocumentScripts = [];
386
+ this._parsedExecutionContextIds = [];
387
+ this._exposedBindingScripts = [];
388
+ this._client = client;
389
+ this._crPage = crPage;
390
+ this._page = crPage._page;
391
+ this._targetId = targetId;
392
+ this._parentSession = parentSession;
393
+ if (parentSession) parentSession._childSessions.add(this);
394
+ this._firstNonInitialNavigationCommittedPromise = new Promise((f, r) => {
395
+ this._firstNonInitialNavigationCommittedFulfill = f;
396
+ this._firstNonInitialNavigationCommittedReject = r;
397
+ });
398
+ }
399
+ _isMainFrame() {
400
+ return this._targetId === this._crPage._targetId;
401
+ }
402
+ _addRendererListeners() {
403
+ this._client._sendMayFail("Debugger.enable", {});
404
+ _eventsHelper.eventsHelper.addEventListener(this._client, 'Debugger.scriptParsed', event => {
405
+ if (!this._parsedExecutionContextIds.includes(event.executionContextId)) this._parsedExecutionContextIds.push(event.executionContextId);
406
+ });
407
+ this._eventListeners.push(...[_eventsHelper.eventsHelper.addEventListener(this._client, 'Log.entryAdded', event => this._onLogEntryAdded(event)), _eventsHelper.eventsHelper.addEventListener(this._client, 'Page.fileChooserOpened', event => this._onFileChooserOpened(event)), _eventsHelper.eventsHelper.addEventListener(this._client, 'Page.frameAttached', event => this._onFrameAttached(event.frameId, event.parentFrameId)), _eventsHelper.eventsHelper.addEventListener(this._client, 'Page.frameDetached', event => this._onFrameDetached(event.frameId, event.reason)), _eventsHelper.eventsHelper.addEventListener(this._client, 'Page.frameNavigated', event => this._onFrameNavigated(event.frame, false)), _eventsHelper.eventsHelper.addEventListener(this._client, 'Page.frameRequestedNavigation', event => this._onFrameRequestedNavigation(event)), _eventsHelper.eventsHelper.addEventListener(this._client, 'Page.javascriptDialogOpening', event => this._onDialog(event)), _eventsHelper.eventsHelper.addEventListener(this._client, 'Page.navigatedWithinDocument', event => this._onFrameNavigatedWithinDocument(event.frameId, event.url)), _eventsHelper.eventsHelper.addEventListener(this._client, 'Runtime.bindingCalled', event => this._onBindingCalled(event)), _eventsHelper.eventsHelper.addEventListener(this._client, 'Runtime.consoleAPICalled', event => this._onConsoleAPI(event)), _eventsHelper.eventsHelper.addEventListener(this._client, 'Runtime.exceptionThrown', exception => this._handleException(exception.exceptionDetails)), _eventsHelper.eventsHelper.addEventListener(this._client, 'Runtime.executionContextCreated', event => this._onExecutionContextCreated(event.context)), _eventsHelper.eventsHelper.addEventListener(this._client, 'Runtime.executionContextDestroyed', event => this._onExecutionContextDestroyed(event.executionContextId)), _eventsHelper.eventsHelper.addEventListener(this._client, 'Runtime.executionContextsCleared', event => this._onExecutionContextsCleared()), _eventsHelper.eventsHelper.addEventListener(this._client, 'Target.attachedToTarget', event => this._onAttachedToTarget(event)), _eventsHelper.eventsHelper.addEventListener(this._client, 'Target.detachedFromTarget', event => this._onDetachedFromTarget(event))]);
408
+ }
409
+ _addBrowserListeners() {
410
+ this._eventListeners.push(...[_eventsHelper.eventsHelper.addEventListener(this._client, 'Inspector.targetCrashed', event => this._onTargetCrashed()), _eventsHelper.eventsHelper.addEventListener(this._client, 'Page.screencastFrame', event => this._onScreencastFrame(event)), _eventsHelper.eventsHelper.addEventListener(this._client, 'Page.windowOpen', event => this._onWindowOpen(event))]);
411
+ }
412
+ async _initialize(hasUIWindow) {
413
+ const isSettingStorageState = this._page._browserContext.isSettingStorageState();
414
+ if (!isSettingStorageState && hasUIWindow && !this._crPage._browserContext._browser.isClank() && !this._crPage._browserContext._options.noDefaultViewport) {
415
+ const {
416
+ windowId
417
+ } = await this._client.send('Browser.getWindowForTarget');
418
+ this._windowId = windowId;
419
+ }
420
+ let screencastOptions;
421
+ if (!isSettingStorageState && this._isMainFrame() && this._crPage._browserContext._options.recordVideo && hasUIWindow) {
422
+ const screencastId = (0, _utils.createGuid)();
423
+ const outputFile = _path.default.join(this._crPage._browserContext._options.recordVideo.dir, screencastId + '.webm');
424
+ screencastOptions = {
425
+ // validateBrowserContextOptions ensures correct video size.
426
+ ...this._crPage._browserContext._options.recordVideo.size,
427
+ outputFile
428
+ };
429
+ await this._crPage._browserContext._ensureVideosPath();
430
+ // Note: it is important to start video recorder before sending Page.startScreencast,
431
+ // and it is equally important to send Page.startScreencast before sending Runtime.runIfWaitingForDebugger.
432
+ await this._createVideoRecorder(screencastId, screencastOptions);
433
+ this._crPage.pageOrError().then(p => {
434
+ if (p instanceof Error) this._stopVideoRecording().catch(() => {});
435
+ });
436
+ }
437
+ let lifecycleEventsEnabled;
438
+ if (!this._isMainFrame()) this._addRendererListeners();
439
+ this._addBrowserListeners();
440
+ const promises = [this._client.send('Page.enable'), this._client.send('Page.getFrameTree').then(({
441
+ frameTree
442
+ }) => {
443
+ if (this._isMainFrame()) {
444
+ this._handleFrameTree(frameTree);
445
+ this._addRendererListeners();
446
+ }
447
+ const isInitialEmptyPage = this._isMainFrame() && this._page.mainFrame().url() === ':';
448
+ if (isInitialEmptyPage) {
449
+ // Ignore lifecycle events, worlds and bindings for the initial empty page. It is never the final page
450
+ // hence we are going to get more lifecycle updates after the actual navigation has
451
+ // started (even if the target url is about:blank).
452
+ lifecycleEventsEnabled.catch(e => {}).then(() => {
453
+ this._eventListeners.push(_eventsHelper.eventsHelper.addEventListener(this._client, 'Page.lifecycleEvent', event => this._onLifecycleEvent(event)));
454
+ });
455
+ } else {
456
+ const localFrames = this._isMainFrame() ? this._page.frames() : [this._page._frameManager.frame(this._targetId)];
457
+ for (const frame of localFrames) {
458
+ this._page._frameManager.frame(frame._id)._context("utility");
459
+ for (const binding of this._crPage._browserContext._pageBindings.values()) frame.evaluateExpression(binding.source).catch(e => {});
460
+ for (const source of this._crPage._browserContext.initScripts) frame.evaluateExpression(source).catch(e => {});
461
+ }
462
+ this._firstNonInitialNavigationCommittedFulfill();
463
+ this._eventListeners.push(_eventsHelper.eventsHelper.addEventListener(this._client, 'Page.lifecycleEvent', event => this._onLifecycleEvent(event)));
464
+ }
465
+ }), this._client.send('Log.enable', {}), lifecycleEventsEnabled = this._client.send('Page.setLifecycleEventsEnabled', {
466
+ enabled: true
467
+ }), this._client.send('Page.addScriptToEvaluateOnNewDocument', {
468
+ source: '',
469
+ worldName: UTILITY_WORLD_NAME
470
+ }), this._crPage._networkManager.addSession(this._client, undefined, this._isMainFrame()), this._client.send('Target.setAutoAttach', {
471
+ autoAttach: true,
472
+ waitForDebuggerOnStart: true,
473
+ flatten: true
474
+ })];
475
+ if (!isSettingStorageState) {
476
+ if (this._isMainFrame()) promises.push(this._client.send('Emulation.setFocusEmulationEnabled', {
477
+ enabled: true
478
+ }));
479
+ const options = this._crPage._browserContext._options;
480
+ if (options.bypassCSP) promises.push(this._client.send('Page.setBypassCSP', {
481
+ enabled: true
482
+ }));
483
+ if (options.ignoreHTTPSErrors || options.internalIgnoreHTTPSErrors) promises.push(this._client.send('Security.setIgnoreCertificateErrors', {
484
+ ignore: true
485
+ }));
486
+ if (this._isMainFrame()) promises.push(this._updateViewport());
487
+ if (options.hasTouch) promises.push(this._client.send('Emulation.setTouchEmulationEnabled', {
488
+ enabled: true
489
+ }));
490
+ if (options.javaScriptEnabled === false) promises.push(this._client.send('Emulation.setScriptExecutionDisabled', {
491
+ value: true
492
+ }));
493
+ if (options.userAgent || options.locale) promises.push(this._updateUserAgent());
494
+ if (options.locale) promises.push(emulateLocale(this._client, options.locale));
495
+ if (options.timezoneId) promises.push(emulateTimezone(this._client, options.timezoneId));
496
+ if (!this._crPage._browserContext._browser.options.headful) promises.push(this._setDefaultFontFamilies(this._client));
497
+ promises.push(this._updateGeolocation(true));
498
+ promises.push(this._updateEmulateMedia());
499
+ promises.push(this._updateFileChooserInterception(true));
500
+ for (const binding of this._crPage._page.allBindings()) promises.push(this._initBinding(binding));
501
+ for (const initScript of this._crPage._browserContext.initScripts) promises.push(this._evaluateOnNewDocument(initScript, 'main'));
502
+ for (const initScript of this._crPage._page.initScripts) promises.push(this._evaluateOnNewDocument(initScript, 'main'));
503
+ if (screencastOptions) promises.push(this._startVideoRecording(screencastOptions));
504
+ }
505
+ if (!(this._crPage._page._pageBindings.size || this._crPage._browserContext._pageBindings.size)) promises.push(this._client.send('Runtime.runIfWaitingForDebugger'));
506
+ promises.push(this._firstNonInitialNavigationCommittedPromise);
507
+ await Promise.all(promises);
508
+ if (this._crPage._page._pageBindings.size || this._crPage._browserContext._pageBindings.size) await this._client.send('Runtime.runIfWaitingForDebugger');
509
+ }
510
+ dispose() {
511
+ this._firstNonInitialNavigationCommittedReject(new _errors.TargetClosedError());
512
+ for (const childSession of this._childSessions) childSession.dispose();
513
+ if (this._parentSession) this._parentSession._childSessions.delete(this);
514
+ _eventsHelper.eventsHelper.removeEventListeners(this._eventListeners);
515
+ this._crPage._networkManager.removeSession(this._client);
516
+ this._crPage._sessions.delete(this._targetId);
517
+ this._client.dispose();
518
+ }
519
+ async _navigate(frame, url, referrer) {
520
+ const response = await this._client.send('Page.navigate', {
521
+ url,
522
+ referrer,
523
+ frameId: frame._id,
524
+ referrerPolicy: 'unsafeUrl'
525
+ });
526
+ this._client._sendMayFail('Page.waitForDebugger');
527
+ if (response.errorText) throw new frames.NavigationAbortedError(response.loaderId, `${response.errorText} at ${url}`);
528
+ return {
529
+ newDocumentId: response.loaderId
530
+ };
531
+ }
532
+ async _onLifecycleEvent(event) {
533
+ if (this._eventBelongsToStaleFrame(event.frameId)) return;
534
+ if (event.name === 'load') this._page._frameManager.frameLifecycleEvent(event.frameId, 'load');else if (event.name === 'DOMContentLoaded') this._page._frameManager.frameLifecycleEvent(event.frameId, 'domcontentloaded');
535
+ var document = await this._client._sendMayFail("DOM.getDocument");
536
+ if (!document) return;
537
+ var query = await this._client._sendMayFail("DOM.querySelectorAll", {
538
+ nodeId: document.root.nodeId,
539
+ selector: "[class=" + this._crPage.initScriptTag + "]"
540
+ });
541
+ if (!query) return;
542
+ for (const nodeId of query.nodeIds) await this._client._sendMayFail("DOM.removeNode", {
543
+ nodeId: nodeId
544
+ });
545
+ await this._client._sendMayFail('Runtime.runIfWaitingForDebugger');
546
+ // ensuring execution context
547
+ try {
548
+ await this._page._frameManager.frame(this._targetId)._context("utility");
549
+ } catch {}
550
+ ;
551
+ }
552
+ _handleFrameTree(frameTree) {
553
+ this._onFrameAttached(frameTree.frame.id, frameTree.frame.parentId || null);
554
+ this._onFrameNavigated(frameTree.frame, true);
555
+ if (!frameTree.childFrames) return;
556
+ for (const child of frameTree.childFrames) this._handleFrameTree(child);
557
+ }
558
+ _eventBelongsToStaleFrame(frameId) {
559
+ const frame = this._page._frameManager.frame(frameId);
560
+ // Subtree may be already gone because some ancestor navigation destroyed the oopif.
561
+ if (!frame) return true;
562
+ // When frame goes remote, parent process may still send some events
563
+ // related to the local frame before it sends frameDetached.
564
+ // In this case, we already have a new session for this frame, so events
565
+ // in the old session should be ignored.
566
+ const session = this._crPage._sessionForFrame(frame);
567
+ return session && session !== this && !session._swappedIn;
568
+ }
569
+ _onFrameAttached(frameId, parentFrameId) {
570
+ const frameSession = this._crPage._sessions.get(frameId);
571
+ if (frameSession && frameId !== this._targetId) {
572
+ // This is a remote -> local frame transition.
573
+ frameSession._swappedIn = true;
574
+ const frame = this._page._frameManager.frame(frameId);
575
+ // Frame or even a whole subtree may be already gone, because some ancestor did navigate.
576
+ if (frame) this._page._frameManager.removeChildFramesRecursively(frame);
577
+ return;
578
+ }
579
+ if (parentFrameId && !this._page._frameManager.frame(parentFrameId)) {
580
+ // Parent frame may be gone already because some ancestor frame navigated and
581
+ // destroyed the whole subtree of some oopif, while oopif's process is still sending us events.
582
+ // Be careful to not confuse this with "main frame navigated cross-process" scenario
583
+ // where parentFrameId is null.
584
+ return;
585
+ }
586
+ this._page._frameManager.frameAttached(frameId, parentFrameId);
587
+ }
588
+ async _onFrameNavigated(framePayload, initial) {
589
+ if (this._eventBelongsToStaleFrame(framePayload.id)) return;
590
+ this._page._frameManager.frameCommittedNewDocumentNavigation(framePayload.id, framePayload.url + (framePayload.urlFragment || ''), framePayload.name || '', framePayload.loaderId, initial);
591
+ if (!initial) this._firstNonInitialNavigationCommittedFulfill();
592
+ var document = await this._client._sendMayFail("DOM.getDocument");
593
+ if (!document) return;
594
+ var query = await this._client._sendMayFail("DOM.querySelectorAll", {
595
+ nodeId: document.root.nodeId,
596
+ selector: "[class=" + this._crPage.initScriptTag + "]"
597
+ });
598
+ if (!query) return;
599
+ for (const nodeId of query.nodeIds) await this._client._sendMayFail("DOM.removeNode", {
600
+ nodeId: nodeId
601
+ });
602
+ await this._client._sendMayFail('Runtime.runIfWaitingForDebugger');
603
+ // ensuring execution context
604
+ try {
605
+ await this._page._frameManager.frame(this._targetId)._context("utility");
606
+ } catch {}
607
+ ;
608
+ }
609
+ _onFrameRequestedNavigation(payload) {
610
+ if (this._eventBelongsToStaleFrame(payload.frameId)) return;
611
+ if (payload.disposition === 'currentTab') this._page._frameManager.frameRequestedNavigation(payload.frameId);
612
+ }
613
+ _onFrameNavigatedWithinDocument(frameId, url) {
614
+ if (this._eventBelongsToStaleFrame(frameId)) return;
615
+ this._page._frameManager.frameCommittedSameDocumentNavigation(frameId, url);
616
+ }
617
+ _onFrameDetached(frameId, reason) {
618
+ if (this._crPage._sessions.has(frameId)) {
619
+ // This is a local -> remote frame transition, where
620
+ // Page.frameDetached arrives after Target.attachedToTarget.
621
+ // We've already handled the new target and frame reattach - nothing to do here.
622
+ return;
623
+ }
624
+ if (reason === 'swap') {
625
+ // This is a local -> remote frame transition, where
626
+ // Page.frameDetached arrives before Target.attachedToTarget.
627
+ // We should keep the frame in the tree, and it will be used for the new target.
628
+ const frame = this._page._frameManager.frame(frameId);
629
+ if (frame) this._page._frameManager.removeChildFramesRecursively(frame);
630
+ return;
631
+ }
632
+ // Just a regular frame detach.
633
+ this._page._frameManager.frameDetached(frameId);
634
+ }
635
+ _onExecutionContextCreated(contextPayload) {
636
+ for (const name of this._exposedBindingNames) this._client._sendMayFail('Runtime.addBinding', {
637
+ name: name,
638
+ executionContextId: contextPayload.id
639
+ });
640
+ const frame = contextPayload.auxData ? this._page._frameManager.frame(contextPayload.auxData.frameId) : null;
641
+ if (contextPayload.auxData.type == "worker") throw new Error("ExecutionContext is worker");
642
+ if (!frame || this._eventBelongsToStaleFrame(frame._id)) return;
643
+ const delegate = new _crExecutionContext.CRExecutionContext(this._client, contextPayload);
644
+ let worldName = contextPayload.name;
645
+ const context = new dom.FrameExecutionContext(delegate, frame, worldName);
646
+ context[contextDelegateSymbol] = delegate;
647
+ if (worldName) frame._contextCreated(worldName, context);
648
+ this._contextIdToContext.set(contextPayload.id, context);
649
+ for (const source of this._exposedBindingScripts) {
650
+ this._client._sendMayFail("Runtime.evaluate", {
651
+ expression: source,
652
+ contextId: contextPayload.id,
653
+ awaitPromise: true
654
+ });
655
+ }
656
+ }
657
+ _onExecutionContextDestroyed(executionContextId) {
658
+ const context = this._contextIdToContext.get(executionContextId);
659
+ if (!context) return;
660
+ this._contextIdToContext.delete(executionContextId);
661
+ context.frame._contextDestroyed(context);
662
+ }
663
+ _onExecutionContextsCleared() {
664
+ for (const contextId of Array.from(this._contextIdToContext.keys())) this._onExecutionContextDestroyed(contextId);
665
+ }
666
+ async _onAttachedToTarget(event) {
667
+ var _this$_page$_frameMan;
668
+ const session = this._client.createChildSession(event.sessionId);
669
+ if (event.targetInfo.type === 'iframe') {
670
+ // Frame id equals target id.
671
+ const targetId = event.targetInfo.targetId;
672
+ const frame = this._page._frameManager.frame(targetId);
673
+ if (!frame) return; // Subtree may be already gone due to renderer/browser race.
674
+ this._page._frameManager.removeChildFramesRecursively(frame);
675
+ const frameSession = new FrameSession(this._crPage, session, targetId, this);
676
+ this._crPage._sessions.set(targetId, frameSession);
677
+ frameSession._initialize(false).catch(e => e);
678
+ return;
679
+ }
680
+ if (event.targetInfo.type !== 'worker') {
681
+ session.detach().catch(() => {});
682
+ return;
683
+ }
684
+ const url = event.targetInfo.url;
685
+ const worker = new _page.Worker(this._page, url);
686
+ this._page._addWorker(event.sessionId, worker);
687
+ this._workerSessions.set(event.sessionId, session);
688
+ session.once('Runtime.executionContextCreated', async event => {
689
+ worker._createExecutionContext(new _crExecutionContext.CRExecutionContext(session, event.context));
690
+ });
691
+ var globalThis = await session._sendMayFail('Runtime.evaluate', {
692
+ expression: "globalThis",
693
+ serializationOptions: {
694
+ serialization: "idOnly"
695
+ }
696
+ });
697
+ if (globalThis && globalThis.result) {
698
+ var globalThisObjId = globalThis.result.objectId;
699
+ var executionContextId = parseInt(globalThisObjId.split('.')[1], 10);
700
+ worker._createExecutionContext(new _crExecutionContext.CRExecutionContext(session, {
701
+ id: executionContextId
702
+ }));
703
+ }
704
+ // This might fail if the target is closed before we initialize.
705
+ // TODO: attribute workers to the right frame.
706
+ this._crPage._networkManager.addSession(session, (_this$_page$_frameMan = this._page._frameManager.frame(this._targetId)) !== null && _this$_page$_frameMan !== void 0 ? _this$_page$_frameMan : undefined).catch(() => {});
707
+ session._sendMayFail('Runtime.runIfWaitingForDebugger');
708
+ session._sendMayFail('Target.setAutoAttach', {
709
+ autoAttach: true,
710
+ waitForDebuggerOnStart: true,
711
+ flatten: true
712
+ });
713
+ session.on('Target.attachedToTarget', event => this._onAttachedToTarget(event));
714
+ session.on('Target.detachedFromTarget', event => this._onDetachedFromTarget(event));
715
+ session.on('Runtime.consoleAPICalled', event => {
716
+ const args = event.args.map(o => worker._existingExecutionContext.createHandle(o));
717
+ this._page._addConsoleMessage(event.type, args, (0, _crProtocolHelper.toConsoleMessageLocation)(event.stackTrace));
718
+ });
719
+ session.on('Runtime.exceptionThrown', exception => this._page.emitOnContextOnceInitialized(_browserContext.BrowserContext.Events.PageError, (0, _crProtocolHelper.exceptionToError)(exception.exceptionDetails), this._page));
720
+ }
721
+ _onDetachedFromTarget(event) {
722
+ // This might be a worker...
723
+ const workerSession = this._workerSessions.get(event.sessionId);
724
+ if (workerSession) {
725
+ workerSession.dispose();
726
+ this._page._removeWorker(event.sessionId);
727
+ return;
728
+ }
729
+
730
+ // ... or an oopif.
731
+ const childFrameSession = this._crPage._sessions.get(event.targetId);
732
+ if (!childFrameSession) return;
733
+
734
+ // Usually, we get frameAttached in this session first and mark child as swappedIn.
735
+ if (childFrameSession._swappedIn) {
736
+ childFrameSession.dispose();
737
+ return;
738
+ }
739
+
740
+ // However, sometimes we get detachedFromTarget before frameAttached.
741
+ // In this case we don't know whether this is a remote frame detach,
742
+ // or just a remote -> local transition. In the latter case, frameAttached
743
+ // is already inflight, so let's make a safe roundtrip to ensure it arrives.
744
+ this._client.send('Page.enable').catch(e => null).then(() => {
745
+ // Child was not swapped in - that means frameAttached did not happen and
746
+ // this is remote detach rather than remote -> local swap.
747
+ if (!childFrameSession._swappedIn) this._page._frameManager.frameDetached(event.targetId);
748
+ childFrameSession.dispose();
749
+ });
750
+ }
751
+ _onWindowOpen(event) {
752
+ this._crPage._nextWindowOpenPopupFeatures.push(event.windowFeatures);
753
+ }
754
+ async _onConsoleAPI(event) {
755
+ if (event.executionContextId === 0) {
756
+ // DevTools protocol stores the last 1000 console messages. These
757
+ // messages are always reported even for removed execution contexts. In
758
+ // this case, they are marked with executionContextId = 0 and are
759
+ // reported upon enabling Runtime agent.
760
+ //
761
+ // Ignore these messages since:
762
+ // - there's no execution context we can use to operate with message
763
+ // arguments
764
+ // - these messages are reported before Playwright clients can subscribe
765
+ // to the 'console'
766
+ // page event.
767
+ //
768
+ // @see https://github.com/GoogleChrome/puppeteer/issues/3865
769
+ return;
770
+ }
771
+ const context = this._contextIdToContext.get(event.executionContextId);
772
+ if (!context) return;
773
+ const values = event.args.map(arg => context.createHandle(arg));
774
+ this._page._addConsoleMessage(event.type, values, (0, _crProtocolHelper.toConsoleMessageLocation)(event.stackTrace));
775
+ }
776
+ async _onBindingCalled(event) {
777
+ const pageOrError = await this._crPage.pageOrError();
778
+ if (!(pageOrError instanceof Error)) {
779
+ const context = this._contextIdToContext.get(event.executionContextId);
780
+ if (context) await this._page._onBindingCalled(event.payload, context);else await this._page._onBindingCalled(event.payload, await this._page.mainFrame()._mainContext()); // This might be a bit sketchy but it works for now
781
+ }
782
+ }
783
+ _onDialog(event) {
784
+ if (!this._page._frameManager.frame(this._targetId)) return; // Our frame/subtree may be gone already.
785
+ this._page.emitOnContext(_browserContext.BrowserContext.Events.Dialog, new dialog.Dialog(this._page, event.type, event.message, async (accept, promptText) => {
786
+ await this._client.send('Page.handleJavaScriptDialog', {
787
+ accept,
788
+ promptText
789
+ });
790
+ }, event.defaultPrompt));
791
+ }
792
+ _handleException(exceptionDetails) {
793
+ this._page.emitOnContextOnceInitialized(_browserContext.BrowserContext.Events.PageError, (0, _crProtocolHelper.exceptionToError)(exceptionDetails), this._page);
794
+ }
795
+ async _onTargetCrashed() {
796
+ this._client._markAsCrashed();
797
+ this._page._didCrash();
798
+ }
799
+ _onLogEntryAdded(event) {
800
+ const {
801
+ level,
802
+ text,
803
+ args,
804
+ source,
805
+ url,
806
+ lineNumber
807
+ } = event.entry;
808
+ if (args) args.map(arg => (0, _crProtocolHelper.releaseObject)(this._client, arg.objectId));
809
+ if (source !== 'worker') {
810
+ const location = {
811
+ url: url || '',
812
+ lineNumber: lineNumber || 0,
813
+ columnNumber: 0
814
+ };
815
+ this._page._addConsoleMessage(level, [], location, text);
816
+ }
817
+ }
818
+ async _onFileChooserOpened(event) {
819
+ if (!event.backendNodeId) return;
820
+ const frame = this._page._frameManager.frame(event.frameId);
821
+ if (!frame) return;
822
+ let handle;
823
+ try {
824
+ const utilityContext = await frame._utilityContext();
825
+ handle = await this._adoptBackendNodeId(event.backendNodeId, utilityContext);
826
+ } catch (e) {
827
+ // During async processing, frame/context may go away. We should not throw.
828
+ return;
829
+ }
830
+ await this._page._onFileChooserOpened(handle);
831
+ }
832
+ _willBeginDownload() {
833
+ const originPage = this._crPage._initializedPage;
834
+ if (!originPage) {
835
+ // Resume the page creation with an error. The page will automatically close right
836
+ // after the download begins.
837
+ this._firstNonInitialNavigationCommittedReject(new Error('Starting new page download'));
838
+ }
839
+ }
840
+ _onScreencastFrame(payload) {
841
+ this._page.throttleScreencastFrameAck(() => {
842
+ this._client.send('Page.screencastFrameAck', {
843
+ sessionId: payload.sessionId
844
+ }).catch(() => {});
845
+ });
846
+ const buffer = Buffer.from(payload.data, 'base64');
847
+ this._page.emit(_page.Page.Events.ScreencastFrame, {
848
+ buffer,
849
+ frameSwapWallTime: payload.metadata.timestamp ? payload.metadata.timestamp * 1000 : undefined,
850
+ width: payload.metadata.deviceWidth,
851
+ height: payload.metadata.deviceHeight
852
+ });
853
+ }
854
+ async _createVideoRecorder(screencastId, options) {
855
+ (0, _utils.assert)(!this._screencastId);
856
+ const ffmpegPath = _registry.registry.findExecutable('ffmpeg').executablePathOrDie(this._page.attribution.playwright.options.sdkLanguage);
857
+ this._videoRecorder = await _videoRecorder.VideoRecorder.launch(this._crPage._page, ffmpegPath, options);
858
+ this._screencastId = screencastId;
859
+ }
860
+ async _startVideoRecording(options) {
861
+ const screencastId = this._screencastId;
862
+ (0, _utils.assert)(screencastId);
863
+ this._page.once(_page.Page.Events.Close, () => this._stopVideoRecording().catch(() => {}));
864
+ const gotFirstFrame = new Promise(f => this._client.once('Page.screencastFrame', f));
865
+ await this._startScreencast(this._videoRecorder, {
866
+ format: 'jpeg',
867
+ quality: 90,
868
+ maxWidth: options.width,
869
+ maxHeight: options.height
870
+ });
871
+ // Wait for the first frame before reporting video to the client.
872
+ gotFirstFrame.then(() => {
873
+ this._crPage._browserContext._browser._videoStarted(this._crPage._browserContext, screencastId, options.outputFile, this._crPage.pageOrError());
874
+ });
875
+ }
876
+ async _stopVideoRecording() {
877
+ if (!this._screencastId) return;
878
+ const screencastId = this._screencastId;
879
+ this._screencastId = null;
880
+ const recorder = this._videoRecorder;
881
+ this._videoRecorder = null;
882
+ await this._stopScreencast(recorder);
883
+ await recorder.stop().catch(() => {});
884
+ // Keep the video artifact in the map until encoding is fully finished, if the context
885
+ // starts closing before the video is fully written to disk it will wait for it.
886
+ const video = this._crPage._browserContext._browser._takeVideo(screencastId);
887
+ video === null || video === void 0 || video.reportFinished();
888
+ }
889
+ async _startScreencast(client, options = {}) {
890
+ this._screencastClients.add(client);
891
+ if (this._screencastClients.size === 1) await this._client.send('Page.startScreencast', options);
892
+ }
893
+ async _stopScreencast(client) {
894
+ this._screencastClients.delete(client);
895
+ if (!this._screencastClients.size) await this._client._sendMayFail('Page.stopScreencast');
896
+ }
897
+ async _updateGeolocation(initial) {
898
+ const geolocation = this._crPage._browserContext._options.geolocation;
899
+ if (!initial || geolocation) await this._client.send('Emulation.setGeolocationOverride', geolocation || {});
900
+ }
901
+ async _updateViewport(preserveWindowBoundaries) {
902
+ if (this._crPage._browserContext._browser.isClank()) return;
903
+ (0, _utils.assert)(this._isMainFrame());
904
+ const options = this._crPage._browserContext._options;
905
+ const emulatedSize = this._page.emulatedSize();
906
+ if (emulatedSize === null) return;
907
+ const viewportSize = emulatedSize.viewport;
908
+ const screenSize = emulatedSize.screen;
909
+ const isLandscape = screenSize.width > screenSize.height;
910
+ const metricsOverride = {
911
+ mobile: !!options.isMobile,
912
+ width: viewportSize.width,
913
+ height: viewportSize.height,
914
+ screenWidth: screenSize.width,
915
+ screenHeight: screenSize.height,
916
+ deviceScaleFactor: options.deviceScaleFactor || 1,
917
+ screenOrientation: !!options.isMobile ? isLandscape ? {
918
+ angle: 90,
919
+ type: 'landscapePrimary'
920
+ } : {
921
+ angle: 0,
922
+ type: 'portraitPrimary'
923
+ } : {
924
+ angle: 0,
925
+ type: 'landscapePrimary'
926
+ },
927
+ dontSetVisibleSize: preserveWindowBoundaries
928
+ };
929
+ if (JSON.stringify(this._metricsOverride) === JSON.stringify(metricsOverride)) return;
930
+ const promises = [this._client.send('Emulation.setDeviceMetricsOverride', metricsOverride)];
931
+ if (!preserveWindowBoundaries && this._windowId) {
932
+ let insets = {
933
+ width: 0,
934
+ height: 0
935
+ };
936
+ if (this._crPage._browserContext._browser.options.headful) {
937
+ // TODO: popup windows have their own insets.
938
+ insets = {
939
+ width: 24,
940
+ height: 88
941
+ };
942
+ if (process.platform === 'win32') insets = {
943
+ width: 16,
944
+ height: 88
945
+ };else if (process.platform === 'linux') insets = {
946
+ width: 8,
947
+ height: 85
948
+ };else if (process.platform === 'darwin') insets = {
949
+ width: 2,
950
+ height: 80
951
+ };
952
+ if (this._crPage._browserContext.isPersistentContext()) {
953
+ // FIXME: Chrome bug: OOPIF router is confused when hit target is
954
+ // outside browser window.
955
+ // Account for the infobar here to work around the bug.
956
+ insets.height += 46;
957
+ }
958
+ }
959
+ promises.push(this.setWindowBounds({
960
+ width: viewportSize.width + insets.width,
961
+ height: viewportSize.height + insets.height
962
+ }));
963
+ }
964
+ await Promise.all(promises);
965
+ this._metricsOverride = metricsOverride;
966
+ }
967
+ async windowBounds() {
968
+ const {
969
+ bounds
970
+ } = await this._client.send('Browser.getWindowBounds', {
971
+ windowId: this._windowId
972
+ });
973
+ return bounds;
974
+ }
975
+ async setWindowBounds(bounds) {
976
+ return await this._client.send('Browser.setWindowBounds', {
977
+ windowId: this._windowId,
978
+ bounds
979
+ });
980
+ }
981
+ async _updateEmulateMedia() {
982
+ const emulatedMedia = this._page.emulatedMedia();
983
+ // Empty string disables the override.
984
+ const media = emulatedMedia.media === 'no-override' ? '' : emulatedMedia.media;
985
+ const colorScheme = emulatedMedia.colorScheme === 'no-override' ? '' : emulatedMedia.colorScheme;
986
+ const reducedMotion = emulatedMedia.reducedMotion === 'no-override' ? '' : emulatedMedia.reducedMotion;
987
+ const forcedColors = emulatedMedia.forcedColors === 'no-override' ? '' : emulatedMedia.forcedColors;
988
+ const features = [{
989
+ name: 'prefers-color-scheme',
990
+ value: colorScheme
991
+ }, {
992
+ name: 'prefers-reduced-motion',
993
+ value: reducedMotion
994
+ }, {
995
+ name: 'forced-colors',
996
+ value: forcedColors
997
+ }];
998
+ await this._client.send('Emulation.setEmulatedMedia', {
999
+ media,
1000
+ features
1001
+ });
1002
+ }
1003
+ async _updateUserAgent() {
1004
+ const options = this._crPage._browserContext._options;
1005
+ await this._client.send('Emulation.setUserAgentOverride', {
1006
+ userAgent: options.userAgent || '',
1007
+ acceptLanguage: options.locale,
1008
+ userAgentMetadata: calculateUserAgentMetadata(options)
1009
+ });
1010
+ }
1011
+ async _setDefaultFontFamilies(session) {
1012
+ const fontFamilies = _defaultFontFamilies.platformToFontFamilies[this._crPage._browserContext._browser._platform()];
1013
+ await session.send('Page.setFontFamilies', fontFamilies);
1014
+ }
1015
+ async _updateFileChooserInterception(initial) {
1016
+ const enabled = this._page.fileChooserIntercepted();
1017
+ if (initial && !enabled) return;
1018
+ await this._client.send('Page.setInterceptFileChooserDialog', {
1019
+ enabled
1020
+ }).catch(() => {}); // target can be closed.
1021
+ }
1022
+ async _evaluateOnNewDocument(initScript, world) {
1023
+ this._evaluateOnNewDocumentScripts.push(initScript);
1024
+ }
1025
+ async _removeEvaluatesOnNewDocument() {
1026
+ this._evaluateOnNewDocumentScripts = [];
1027
+ }
1028
+ async _getContentFrame(handle) {
1029
+ const nodeInfo = await this._client.send('DOM.describeNode', {
1030
+ objectId: handle._objectId
1031
+ });
1032
+ if (!nodeInfo || typeof nodeInfo.node.frameId !== 'string') return null;
1033
+ return this._page._frameManager.frame(nodeInfo.node.frameId);
1034
+ }
1035
+ async _getOwnerFrame(handle) {
1036
+ // document.documentElement has frameId of the owner frame.
1037
+ const documentElement = await handle.evaluateHandle(node => {
1038
+ const doc = node;
1039
+ if (doc.documentElement && doc.documentElement.ownerDocument === doc) return doc.documentElement;
1040
+ return node.ownerDocument ? node.ownerDocument.documentElement : null;
1041
+ });
1042
+ if (!documentElement) return null;
1043
+ if (!documentElement._objectId) return null;
1044
+ const nodeInfo = await this._client.send('DOM.describeNode', {
1045
+ objectId: documentElement._objectId
1046
+ });
1047
+ const frameId = nodeInfo && typeof nodeInfo.node.frameId === 'string' ? nodeInfo.node.frameId : null;
1048
+ documentElement.dispose();
1049
+ return frameId;
1050
+ }
1051
+ async _getBoundingBox(handle) {
1052
+ const result = await this._client._sendMayFail('DOM.getBoxModel', {
1053
+ objectId: handle._objectId
1054
+ });
1055
+ if (!result) return null;
1056
+ const quad = result.model.border;
1057
+ const x = Math.min(quad[0], quad[2], quad[4], quad[6]);
1058
+ const y = Math.min(quad[1], quad[3], quad[5], quad[7]);
1059
+ const width = Math.max(quad[0], quad[2], quad[4], quad[6]) - x;
1060
+ const height = Math.max(quad[1], quad[3], quad[5], quad[7]) - y;
1061
+ const position = await this._framePosition();
1062
+ if (!position) return null;
1063
+ return {
1064
+ x: x + position.x,
1065
+ y: y + position.y,
1066
+ width,
1067
+ height
1068
+ };
1069
+ }
1070
+ async _framePosition() {
1071
+ const frame = this._page._frameManager.frame(this._targetId);
1072
+ if (!frame) return null;
1073
+ if (frame === this._page.mainFrame()) return {
1074
+ x: 0,
1075
+ y: 0
1076
+ };
1077
+ const element = await frame.frameElement();
1078
+ const box = await element.boundingBox();
1079
+ return box;
1080
+ }
1081
+ async _scrollRectIntoViewIfNeeded(handle, rect) {
1082
+ return await this._client.send('DOM.scrollIntoViewIfNeeded', {
1083
+ objectId: handle._objectId,
1084
+ rect
1085
+ }).then(() => 'done').catch(e => {
1086
+ if (e instanceof Error && e.message.includes('Node does not have a layout object')) return 'error:notvisible';
1087
+ if (e instanceof Error && e.message.includes('Node is detached from document')) return 'error:notconnected';
1088
+ throw e;
1089
+ });
1090
+ }
1091
+ async _getContentQuads(handle) {
1092
+ const result = await this._client._sendMayFail('DOM.getContentQuads', {
1093
+ objectId: handle._objectId
1094
+ });
1095
+ if (!result) return null;
1096
+ const position = await this._framePosition();
1097
+ if (!position) return null;
1098
+ return result.quads.map(quad => [{
1099
+ x: quad[0] + position.x,
1100
+ y: quad[1] + position.y
1101
+ }, {
1102
+ x: quad[2] + position.x,
1103
+ y: quad[3] + position.y
1104
+ }, {
1105
+ x: quad[4] + position.x,
1106
+ y: quad[5] + position.y
1107
+ }, {
1108
+ x: quad[6] + position.x,
1109
+ y: quad[7] + position.y
1110
+ }]);
1111
+ }
1112
+ async _adoptElementHandle(handle, to) {
1113
+ const nodeInfo = await this._client.send('DOM.describeNode', {
1114
+ objectId: handle._objectId
1115
+ });
1116
+ return this._adoptBackendNodeId(nodeInfo.node.backendNodeId, to);
1117
+ }
1118
+ async _adoptBackendNodeId(backendNodeId, to) {
1119
+ const result = await this._client._sendMayFail('DOM.resolveNode', {
1120
+ backendNodeId,
1121
+ executionContextId: to._delegate._contextId
1122
+ });
1123
+ if (!result || result.object.subtype === 'null') throw new Error(dom.kUnableToAdoptErrorMessage);
1124
+ return to.createHandle(result.object).asElement();
1125
+ }
1126
+ async _initBinding(binding = _page.PageBinding) {
1127
+ var result = await this._client._sendMayFail('Page.createIsolatedWorld', {
1128
+ frameId: this._targetId,
1129
+ grantUniveralAccess: true,
1130
+ worldName: "utility"
1131
+ });
1132
+ if (!result) return;
1133
+ var isolatedContextId = result.executionContextId;
1134
+ var globalThis = await this._client._sendMayFail('Runtime.evaluate', {
1135
+ expression: "globalThis",
1136
+ serializationOptions: {
1137
+ serialization: "idOnly"
1138
+ }
1139
+ });
1140
+ if (!globalThis) return;
1141
+ var globalThisObjId = globalThis["result"]['objectId'];
1142
+ var mainContextId = parseInt(globalThisObjId.split('.')[1], 10);
1143
+ await Promise.all([this._client._sendMayFail('Runtime.addBinding', {
1144
+ name: binding.name
1145
+ }), this._client._sendMayFail('Runtime.addBinding', {
1146
+ name: binding.name,
1147
+ executionContextId: mainContextId
1148
+ }), this._client._sendMayFail('Runtime.addBinding', {
1149
+ name: binding.name,
1150
+ executionContextId: isolatedContextId
1151
+ })
1152
+ // this._client._sendMayFail("Runtime.evaluate", { expression: binding.source, contextId: mainContextId, awaitPromise: true })
1153
+ ]);
1154
+ this._exposedBindingNames.push(binding.name);
1155
+ this._exposedBindingScripts.push(binding.source);
1156
+ await this._crPage.addInitScript(binding.source);
1157
+ //this._client._sendMayFail('Runtime.runIfWaitingForDebugger')
1158
+ }
1159
+ async _removeExposedBindings() {
1160
+ const toRetain = [];
1161
+ const toRemove = [];
1162
+ for (const name of this._exposedBindingNames) (name.startsWith('__pw_') ? toRetain : toRemove).push(name);
1163
+ this._exposedBindingNames = toRetain;
1164
+ await Promise.all(toRemove.map(name => this._client.send('Runtime.removeBinding', {
1165
+ name
1166
+ })));
1167
+ }
1168
+ }
1169
+ async function emulateLocale(session, locale) {
1170
+ try {
1171
+ await session.send('Emulation.setLocaleOverride', {
1172
+ locale
1173
+ });
1174
+ } catch (exception) {
1175
+ // All pages in the same renderer share locale. All such pages belong to the same
1176
+ // context and if locale is overridden for one of them its value is the same as
1177
+ // we are trying to set so it's not a problem.
1178
+ if (exception.message.includes('Another locale override is already in effect')) return;
1179
+ throw exception;
1180
+ }
1181
+ }
1182
+ async function emulateTimezone(session, timezoneId) {
1183
+ try {
1184
+ await session.send('Emulation.setTimezoneOverride', {
1185
+ timezoneId: timezoneId
1186
+ });
1187
+ } catch (exception) {
1188
+ if (exception.message.includes('Timezone override is already in effect')) return;
1189
+ if (exception.message.includes('Invalid timezone')) throw new Error(`Invalid timezone ID: ${timezoneId}`);
1190
+ throw exception;
1191
+ }
1192
+ }
1193
+ const contextDelegateSymbol = Symbol('delegate');
1194
+
1195
+ // Chromium reference: https://source.chromium.org/chromium/chromium/src/+/main:components/embedder_support/user_agent_utils.cc;l=434;drc=70a6711e08e9f9e0d8e4c48e9ba5cab62eb010c2
1196
+ function calculateUserAgentMetadata(options) {
1197
+ const ua = options.userAgent;
1198
+ if (!ua) return undefined;
1199
+ const metadata = {
1200
+ mobile: !!options.isMobile,
1201
+ model: '',
1202
+ architecture: 'x64',
1203
+ platform: 'Windows',
1204
+ platformVersion: ''
1205
+ };
1206
+ const androidMatch = ua.match(/Android (\d+(\.\d+)?(\.\d+)?)/);
1207
+ const iPhoneMatch = ua.match(/iPhone OS (\d+(_\d+)?)/);
1208
+ const iPadMatch = ua.match(/iPad; CPU OS (\d+(_\d+)?)/);
1209
+ const macOSMatch = ua.match(/Mac OS X (\d+(_\d+)?(_\d+)?)/);
1210
+ const windowsMatch = ua.match(/Windows\D+(\d+(\.\d+)?(\.\d+)?)/);
1211
+ if (androidMatch) {
1212
+ metadata.platform = 'Android';
1213
+ metadata.platformVersion = androidMatch[1];
1214
+ metadata.architecture = 'arm';
1215
+ } else if (iPhoneMatch) {
1216
+ metadata.platform = 'iOS';
1217
+ metadata.platformVersion = iPhoneMatch[1];
1218
+ metadata.architecture = 'arm';
1219
+ } else if (iPadMatch) {
1220
+ metadata.platform = 'iOS';
1221
+ metadata.platformVersion = iPadMatch[1];
1222
+ metadata.architecture = 'arm';
1223
+ } else if (macOSMatch) {
1224
+ metadata.platform = 'macOS';
1225
+ metadata.platformVersion = macOSMatch[1];
1226
+ if (!ua.includes('Intel')) metadata.architecture = 'arm';
1227
+ } else if (windowsMatch) {
1228
+ metadata.platform = 'Windows';
1229
+ metadata.platformVersion = windowsMatch[1];
1230
+ } else if (ua.toLowerCase().includes('linux')) {
1231
+ metadata.platform = 'Linux';
1232
+ }
1233
+ if (ua.includes('ARM')) metadata.architecture = 'arm';
1234
+ return metadata;
1235
+ }