@checkly/playwright-core 1.41.2-beta.0

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