browserContext.js 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696
  1. "use strict";
  2. var __create = Object.create;
  3. var __defProp = Object.defineProperty;
  4. var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
  5. var __getOwnPropNames = Object.getOwnPropertyNames;
  6. var __getProtoOf = Object.getPrototypeOf;
  7. var __hasOwnProp = Object.prototype.hasOwnProperty;
  8. var __export = (target, all) => {
  9. for (var name in all)
  10. __defProp(target, name, { get: all[name], enumerable: true });
  11. };
  12. var __copyProps = (to, from, except, desc) => {
  13. if (from && typeof from === "object" || typeof from === "function") {
  14. for (let key of __getOwnPropNames(from))
  15. if (!__hasOwnProp.call(to, key) && key !== except)
  16. __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
  17. }
  18. return to;
  19. };
  20. var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
  21. // If the importer is in node compatibility mode or this is not an ESM
  22. // file that has been converted to a CommonJS file using a Babel-
  23. // compatible transform (i.e. "__esModule" has not been set), then set
  24. // "default" to the CommonJS "module.exports" for node compatibility.
  25. isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
  26. mod
  27. ));
  28. var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
  29. var browserContext_exports = {};
  30. __export(browserContext_exports, {
  31. BrowserContext: () => BrowserContext,
  32. normalizeProxySettings: () => normalizeProxySettings,
  33. validateBrowserContextOptions: () => validateBrowserContextOptions,
  34. verifyClientCertificates: () => verifyClientCertificates,
  35. verifyGeolocation: () => verifyGeolocation
  36. });
  37. module.exports = __toCommonJS(browserContext_exports);
  38. var import_fs = __toESM(require("fs"));
  39. var import_path = __toESM(require("path"));
  40. var import_crypto = require("./utils/crypto");
  41. var import_debug = require("./utils/debug");
  42. var import_clock = require("./clock");
  43. var import_debugger = require("./debugger");
  44. var import_dialog = require("./dialog");
  45. var import_fetch = require("./fetch");
  46. var import_fileUtils = require("./utils/fileUtils");
  47. var import_stackTrace = require("../utils/isomorphic/stackTrace");
  48. var import_harRecorder = require("./har/harRecorder");
  49. var import_helper = require("./helper");
  50. var import_instrumentation = require("./instrumentation");
  51. var network = __toESM(require("./network"));
  52. var import_page = require("./page");
  53. var import_page2 = require("./page");
  54. var import_recorderApp = require("./recorder/recorderApp");
  55. var import_selectors = require("./selectors");
  56. var import_tracing = require("./trace/recorder/tracing");
  57. var rawStorageSource = __toESM(require("../generated/storageScriptSource"));
  58. class BrowserContext extends import_instrumentation.SdkObject {
  59. constructor(browser, options, browserContextId) {
  60. super(browser, "browser-context");
  61. this._pageBindings = /* @__PURE__ */ new Map();
  62. this.requestInterceptors = [];
  63. this._closedStatus = "open";
  64. this._permissions = /* @__PURE__ */ new Map();
  65. this._downloads = /* @__PURE__ */ new Set();
  66. this._origins = /* @__PURE__ */ new Set();
  67. this._harRecorders = /* @__PURE__ */ new Map();
  68. this._tempDirs = [];
  69. this._creatingStorageStatePage = false;
  70. this.initScripts = [];
  71. this._routesInFlight = /* @__PURE__ */ new Set();
  72. this._playwrightBindingExposed = false;
  73. this.attribution.context = this;
  74. this._browser = browser;
  75. this._options = options;
  76. this._browserContextId = browserContextId;
  77. this._isPersistentContext = !browserContextId;
  78. this._closePromise = new Promise((fulfill) => this._closePromiseFulfill = fulfill);
  79. this._selectors = new import_selectors.Selectors(options.selectorEngines || [], options.testIdAttributeName);
  80. this.fetchRequest = new import_fetch.BrowserContextAPIRequestContext(this);
  81. this.tracing = new import_tracing.Tracing(this, browser.options.tracesDir);
  82. this.clock = new import_clock.Clock(this);
  83. this.dialogManager = new import_dialog.DialogManager(this.instrumentation);
  84. }
  85. static {
  86. this.Events = {
  87. Console: "console",
  88. Close: "close",
  89. Page: "page",
  90. // Can't use just 'error' due to node.js special treatment of error events.
  91. // @see https://nodejs.org/api/events.html#events_error_events
  92. PageError: "pageerror",
  93. Request: "request",
  94. Response: "response",
  95. RequestFailed: "requestfailed",
  96. RequestFinished: "requestfinished",
  97. RequestAborted: "requestaborted",
  98. RequestFulfilled: "requestfulfilled",
  99. RequestContinued: "requestcontinued",
  100. BeforeClose: "beforeclose",
  101. VideoStarted: "videostarted",
  102. RecorderEvent: "recorderevent"
  103. };
  104. }
  105. isPersistentContext() {
  106. return this._isPersistentContext;
  107. }
  108. selectors() {
  109. return this._selectors;
  110. }
  111. async _initialize() {
  112. if (this.attribution.playwright.options.isInternalPlaywright)
  113. return;
  114. this._debugger = new import_debugger.Debugger(this);
  115. if ((0, import_debug.debugMode)() === "inspector")
  116. await import_recorderApp.RecorderApp.show(this, { pauseOnNextStatement: true });
  117. if (this._debugger.isPaused())
  118. import_recorderApp.RecorderApp.showInspectorNoReply(this);
  119. this._debugger.on(import_debugger.Debugger.Events.PausedStateChanged, () => {
  120. if (this._debugger.isPaused())
  121. import_recorderApp.RecorderApp.showInspectorNoReply(this);
  122. });
  123. if ((0, import_debug.debugMode)() === "console") {
  124. await this.extendInjectedScript(`
  125. function installConsoleApi(injectedScript) { injectedScript.consoleApi.install(); }
  126. module.exports = { default: () => installConsoleApi };
  127. `);
  128. }
  129. if (this._options.serviceWorkers === "block")
  130. await this.addInitScript(void 0, `
  131. if (navigator.serviceWorker) navigator.serviceWorker.register = async () => { console.warn('Service Worker registration blocked by Playwright'); };
  132. `);
  133. if (this._options.permissions)
  134. await this.grantPermissions(this._options.permissions);
  135. }
  136. debugger() {
  137. return this._debugger;
  138. }
  139. async _ensureVideosPath() {
  140. if (this._options.recordVideo)
  141. await (0, import_fileUtils.mkdirIfNeeded)(import_path.default.join(this._options.recordVideo.dir, "dummy"));
  142. }
  143. canResetForReuse() {
  144. if (this._closedStatus !== "open")
  145. return false;
  146. return true;
  147. }
  148. static reusableContextHash(params) {
  149. const paramsCopy = { ...params };
  150. if (paramsCopy.selectorEngines?.length === 0)
  151. delete paramsCopy.selectorEngines;
  152. for (const k of Object.keys(paramsCopy)) {
  153. const key = k;
  154. if (paramsCopy[key] === defaultNewContextParamValues[key])
  155. delete paramsCopy[key];
  156. }
  157. for (const key of paramsThatAllowContextReuse)
  158. delete paramsCopy[key];
  159. return JSON.stringify(paramsCopy);
  160. }
  161. async resetForReuse(progress, params) {
  162. await this.tracing.resetForReuse(progress);
  163. if (params) {
  164. for (const key of paramsThatAllowContextReuse)
  165. this._options[key] = params[key];
  166. if (params.testIdAttributeName)
  167. this.selectors().setTestIdAttributeName(params.testIdAttributeName);
  168. }
  169. let page = this.pages()[0];
  170. const otherPages = this.possiblyUninitializedPages().filter((p) => p !== page);
  171. for (const p of otherPages)
  172. await p.close();
  173. if (page && page.hasCrashed()) {
  174. await page.close();
  175. page = void 0;
  176. }
  177. await page?.mainFrame().gotoImpl(progress, "about:blank", {});
  178. await this.clock.uninstall(progress);
  179. await progress.race(this.setUserAgent(this._options.userAgent));
  180. await progress.race(this.doUpdateDefaultEmulatedMedia());
  181. await progress.race(this.doUpdateDefaultViewport());
  182. await this.setStorageState(progress, this._options.storageState, "resetForReuse");
  183. await page?.resetForReuse(progress);
  184. }
  185. _browserClosed() {
  186. for (const page of this.pages())
  187. page._didClose();
  188. this._didCloseInternal();
  189. }
  190. _didCloseInternal() {
  191. if (this._closedStatus === "closed") {
  192. return;
  193. }
  194. this._clientCertificatesProxy?.close().catch(() => {
  195. });
  196. this.tracing.abort();
  197. if (this._isPersistentContext)
  198. this.onClosePersistent();
  199. this._closePromiseFulfill(new Error("Context closed"));
  200. this.emit(BrowserContext.Events.Close);
  201. }
  202. pages() {
  203. return this.possiblyUninitializedPages().filter((page) => page.initializedOrUndefined());
  204. }
  205. async cookies(urls = []) {
  206. if (urls && !Array.isArray(urls))
  207. urls = [urls];
  208. return await this.doGetCookies(urls);
  209. }
  210. async clearCookies(options) {
  211. const currentCookies = await this.cookies();
  212. await this.doClearCookies();
  213. const matches = (cookie, prop, value) => {
  214. if (!value)
  215. return true;
  216. if (value instanceof RegExp) {
  217. value.lastIndex = 0;
  218. return value.test(cookie[prop]);
  219. }
  220. return cookie[prop] === value;
  221. };
  222. const cookiesToReadd = currentCookies.filter((cookie) => {
  223. return !matches(cookie, "name", options.name) || !matches(cookie, "domain", options.domain) || !matches(cookie, "path", options.path);
  224. });
  225. await this.addCookies(cookiesToReadd);
  226. }
  227. setHTTPCredentials(httpCredentials) {
  228. return this.doSetHTTPCredentials(httpCredentials);
  229. }
  230. getBindingClient(name) {
  231. return this._pageBindings.get(name)?.forClient;
  232. }
  233. async exposePlaywrightBindingIfNeeded() {
  234. if (this._playwrightBindingExposed)
  235. return;
  236. this._playwrightBindingExposed = true;
  237. await this.doExposePlaywrightBinding();
  238. this.bindingsInitScript = import_page2.PageBinding.createInitScript();
  239. this.initScripts.push(this.bindingsInitScript);
  240. await this.doAddInitScript(this.bindingsInitScript);
  241. await this.safeNonStallingEvaluateInAllFrames(this.bindingsInitScript.source, "main");
  242. }
  243. needsPlaywrightBinding() {
  244. return this._playwrightBindingExposed;
  245. }
  246. async exposeBinding(progress, name, needsHandle, playwrightBinding, forClient) {
  247. if (this._pageBindings.has(name))
  248. throw new Error(`Function "${name}" has been already registered`);
  249. for (const page of this.pages()) {
  250. if (page.getBinding(name))
  251. throw new Error(`Function "${name}" has been already registered in one of the pages`);
  252. }
  253. await progress.race(this.exposePlaywrightBindingIfNeeded());
  254. const binding = new import_page2.PageBinding(name, playwrightBinding, needsHandle);
  255. binding.forClient = forClient;
  256. this._pageBindings.set(name, binding);
  257. try {
  258. await progress.race(this.doAddInitScript(binding.initScript));
  259. await progress.race(this.safeNonStallingEvaluateInAllFrames(binding.initScript.source, "main"));
  260. return binding;
  261. } catch (error) {
  262. this._pageBindings.delete(name);
  263. throw error;
  264. }
  265. }
  266. async removeExposedBindings(bindings) {
  267. bindings = bindings.filter((binding) => this._pageBindings.get(binding.name) === binding);
  268. for (const binding of bindings)
  269. this._pageBindings.delete(binding.name);
  270. await this.doRemoveInitScripts(bindings.map((binding) => binding.initScript));
  271. const cleanup = bindings.map((binding) => `{ ${binding.cleanupScript} };
  272. `).join("");
  273. await this.safeNonStallingEvaluateInAllFrames(cleanup, "main");
  274. }
  275. async grantPermissions(permissions, origin) {
  276. let resolvedOrigin = "*";
  277. if (origin) {
  278. const url = new URL(origin);
  279. resolvedOrigin = url.origin;
  280. }
  281. const existing = new Set(this._permissions.get(resolvedOrigin) || []);
  282. permissions.forEach((p) => existing.add(p));
  283. const list = [...existing.values()];
  284. this._permissions.set(resolvedOrigin, list);
  285. await this.doGrantPermissions(resolvedOrigin, list);
  286. }
  287. async clearPermissions() {
  288. this._permissions.clear();
  289. await this.doClearPermissions();
  290. }
  291. async setExtraHTTPHeaders(progress, headers) {
  292. const oldHeaders = this._options.extraHTTPHeaders;
  293. this._options.extraHTTPHeaders = headers;
  294. try {
  295. await progress.race(this.doUpdateExtraHTTPHeaders());
  296. } catch (error) {
  297. this._options.extraHTTPHeaders = oldHeaders;
  298. this.doUpdateExtraHTTPHeaders().catch(() => {
  299. });
  300. throw error;
  301. }
  302. }
  303. async setOffline(progress, offline) {
  304. const oldOffline = this._options.offline;
  305. this._options.offline = offline;
  306. try {
  307. await progress.race(this.doUpdateOffline());
  308. } catch (error) {
  309. this._options.offline = oldOffline;
  310. this.doUpdateOffline().catch(() => {
  311. });
  312. throw error;
  313. }
  314. }
  315. async _loadDefaultContextAsIs(progress) {
  316. if (!this.possiblyUninitializedPages().length) {
  317. const waitForEvent = import_helper.helper.waitForEvent(progress, this, BrowserContext.Events.Page);
  318. await Promise.race([waitForEvent.promise, this._closePromise]);
  319. }
  320. const page = this.possiblyUninitializedPages()[0];
  321. if (!page)
  322. return;
  323. const pageOrError = await progress.race(page.waitForInitializedOrError());
  324. if (pageOrError instanceof Error)
  325. throw pageOrError;
  326. await page.mainFrame()._waitForLoadState(progress, "load");
  327. return page;
  328. }
  329. async _loadDefaultContext(progress) {
  330. const defaultPage = await this._loadDefaultContextAsIs(progress);
  331. if (!defaultPage)
  332. return;
  333. const browserName = this._browser.options.name;
  334. if (this._options.isMobile && browserName === "chromium" || this._options.locale && browserName === "webkit") {
  335. await this.newPage(progress);
  336. await defaultPage.close();
  337. }
  338. }
  339. _authenticateProxyViaHeader() {
  340. const proxy = this._options.proxy || this._browser.options.proxy || { username: void 0, password: void 0 };
  341. const { username, password } = proxy;
  342. if (username) {
  343. this._options.httpCredentials = { username, password };
  344. const token = Buffer.from(`${username}:${password}`).toString("base64");
  345. this._options.extraHTTPHeaders = network.mergeHeaders([
  346. this._options.extraHTTPHeaders,
  347. network.singleHeader("Proxy-Authorization", `Basic ${token}`)
  348. ]);
  349. }
  350. }
  351. _authenticateProxyViaCredentials() {
  352. const proxy = this._options.proxy || this._browser.options.proxy;
  353. if (!proxy)
  354. return;
  355. const { username, password } = proxy;
  356. if (username)
  357. this._options.httpCredentials = { username, password: password || "" };
  358. }
  359. async addInitScript(progress, source) {
  360. const initScript = new import_page.InitScript(source);
  361. this.initScripts.push(initScript);
  362. try {
  363. const promise = this.doAddInitScript(initScript);
  364. if (progress)
  365. await progress.race(promise);
  366. else
  367. await promise;
  368. return initScript;
  369. } catch (error) {
  370. this.removeInitScripts([initScript]).catch(() => {
  371. });
  372. throw error;
  373. }
  374. }
  375. async removeInitScripts(initScripts) {
  376. const set = new Set(initScripts);
  377. this.initScripts = this.initScripts.filter((script) => !set.has(script));
  378. await this.doRemoveInitScripts(initScripts);
  379. }
  380. async addRequestInterceptor(progress, handler) {
  381. this.requestInterceptors.push(handler);
  382. await this.doUpdateRequestInterception();
  383. }
  384. async removeRequestInterceptor(handler) {
  385. const index = this.requestInterceptors.indexOf(handler);
  386. if (index === -1)
  387. return;
  388. this.requestInterceptors.splice(index, 1);
  389. await this.notifyRoutesInFlightAboutRemovedHandler(handler);
  390. await this.doUpdateRequestInterception();
  391. }
  392. isClosingOrClosed() {
  393. return this._closedStatus !== "open";
  394. }
  395. async _deleteAllDownloads() {
  396. await Promise.all(Array.from(this._downloads).map((download) => download.artifact.deleteOnContextClose()));
  397. }
  398. async _deleteAllTempDirs() {
  399. await Promise.all(this._tempDirs.map(async (dir) => await import_fs.default.promises.unlink(dir).catch((e) => {
  400. })));
  401. }
  402. setCustomCloseHandler(handler) {
  403. this._customCloseHandler = handler;
  404. }
  405. async close(options) {
  406. if (this._closedStatus === "open") {
  407. if (options.reason)
  408. this._closeReason = options.reason;
  409. this.emit(BrowserContext.Events.BeforeClose);
  410. this._closedStatus = "closing";
  411. for (const harRecorder of this._harRecorders.values())
  412. await harRecorder.flush();
  413. await this.tracing.flush();
  414. const promises = [];
  415. for (const { context, artifact } of this._browser._idToVideo.values()) {
  416. if (context === this)
  417. promises.push(artifact.finishedPromise());
  418. }
  419. if (this._customCloseHandler) {
  420. await this._customCloseHandler();
  421. } else {
  422. await this.doClose(options.reason);
  423. }
  424. promises.push(this._deleteAllDownloads());
  425. promises.push(this._deleteAllTempDirs());
  426. await Promise.all(promises);
  427. if (!this._customCloseHandler)
  428. this._didCloseInternal();
  429. }
  430. await this._closePromise;
  431. }
  432. async newPage(progress, forStorageState) {
  433. let page;
  434. try {
  435. this._creatingStorageStatePage = !!forStorageState;
  436. page = await progress.race(this.doCreateNewPage());
  437. const pageOrError = await progress.race(page.waitForInitializedOrError());
  438. if (pageOrError instanceof import_page2.Page) {
  439. if (pageOrError.isClosed())
  440. throw new Error("Page has been closed.");
  441. return pageOrError;
  442. }
  443. throw pageOrError;
  444. } catch (error) {
  445. await page?.close({ reason: "Failed to create page" }).catch(() => {
  446. });
  447. throw error;
  448. } finally {
  449. this._creatingStorageStatePage = false;
  450. }
  451. }
  452. addVisitedOrigin(origin) {
  453. this._origins.add(origin);
  454. }
  455. async storageState(progress, indexedDB = false) {
  456. const result = {
  457. cookies: await this.cookies(),
  458. origins: []
  459. };
  460. const originsToSave = new Set(this._origins);
  461. const collectScript = `(() => {
  462. const module = {};
  463. ${rawStorageSource.source}
  464. const script = new (module.exports.StorageScript())(${this._browser.options.name === "firefox"});
  465. return script.collect(${indexedDB});
  466. })()`;
  467. for (const page of this.pages()) {
  468. const origin = page.mainFrame().origin();
  469. if (!origin || !originsToSave.has(origin))
  470. continue;
  471. try {
  472. const storage = await page.mainFrame().nonStallingEvaluateInExistingContext(collectScript, "utility");
  473. if (storage.localStorage.length || storage.indexedDB?.length)
  474. result.origins.push({ origin, localStorage: storage.localStorage, indexedDB: storage.indexedDB });
  475. originsToSave.delete(origin);
  476. } catch {
  477. }
  478. }
  479. if (originsToSave.size) {
  480. const page = await this.newPage(
  481. progress,
  482. true
  483. /* forStorageState */
  484. );
  485. try {
  486. await page.addRequestInterceptor(progress, (route) => {
  487. route.fulfill({ body: "<html></html>" }).catch(() => {
  488. });
  489. }, "prepend");
  490. for (const origin of originsToSave) {
  491. const frame = page.mainFrame();
  492. await frame.gotoImpl(progress, origin, {});
  493. const storage = await progress.race(frame.evaluateExpression(collectScript, { world: "utility" }));
  494. if (storage.localStorage.length || storage.indexedDB?.length)
  495. result.origins.push({ origin, localStorage: storage.localStorage, indexedDB: storage.indexedDB });
  496. }
  497. } finally {
  498. await page.close();
  499. }
  500. }
  501. return result;
  502. }
  503. isCreatingStorageStatePage() {
  504. return this._creatingStorageStatePage;
  505. }
  506. async setStorageState(progress, state, mode) {
  507. let page;
  508. let interceptor;
  509. try {
  510. if (mode !== "initial") {
  511. await progress.race(this.clearCache());
  512. await progress.race(this.doClearCookies());
  513. }
  514. if (state?.cookies)
  515. await progress.race(this.addCookies(state.cookies));
  516. const newOrigins = new Map(state?.origins?.map((p) => [p.origin, p]) || []);
  517. const allOrigins = /* @__PURE__ */ new Set([...this._origins, ...newOrigins.keys()]);
  518. if (allOrigins.size) {
  519. if (mode === "resetForReuse")
  520. page = this.pages()[0];
  521. if (!page)
  522. page = await this.newPage(
  523. progress,
  524. mode !== "resetForReuse"
  525. /* forStorageState */
  526. );
  527. interceptor = (route) => {
  528. route.fulfill({ body: "<html></html>" }).catch(() => {
  529. });
  530. };
  531. await page.addRequestInterceptor(progress, interceptor, "prepend");
  532. for (const origin of allOrigins) {
  533. const frame = page.mainFrame();
  534. await frame.gotoImpl(progress, origin, {});
  535. const restoreScript = `(() => {
  536. const module = {};
  537. ${rawStorageSource.source}
  538. const script = new (module.exports.StorageScript())(${this._browser.options.name === "firefox"});
  539. return script.restore(${JSON.stringify(newOrigins.get(origin))});
  540. })()`;
  541. await progress.race(frame.evaluateExpression(restoreScript, { world: "utility" }));
  542. }
  543. }
  544. this._origins = /* @__PURE__ */ new Set([...newOrigins.keys()]);
  545. } catch (error) {
  546. (0, import_stackTrace.rewriteErrorMessage)(error, `Error setting storage state:
  547. ` + error.message);
  548. throw error;
  549. } finally {
  550. if (mode !== "resetForReuse")
  551. await page?.close();
  552. else if (interceptor)
  553. await page?.removeRequestInterceptor(interceptor);
  554. }
  555. }
  556. async extendInjectedScript(source, arg) {
  557. const installInFrame = (frame) => frame.extendInjectedScript(source, arg).catch(() => {
  558. });
  559. const installInPage = (page) => {
  560. page.on(import_page2.Page.Events.InternalFrameNavigatedToNewDocument, installInFrame);
  561. return Promise.all(page.frames().map(installInFrame));
  562. };
  563. this.on(BrowserContext.Events.Page, installInPage);
  564. return Promise.all(this.pages().map(installInPage));
  565. }
  566. async safeNonStallingEvaluateInAllFrames(expression, world, options = {}) {
  567. await Promise.all(this.pages().map((page) => page.safeNonStallingEvaluateInAllFrames(expression, world, options)));
  568. }
  569. harStart(page, options) {
  570. const harId = (0, import_crypto.createGuid)();
  571. this._harRecorders.set(harId, new import_harRecorder.HarRecorder(this, page, options));
  572. return harId;
  573. }
  574. async harExport(harId) {
  575. const recorder = this._harRecorders.get(harId || "");
  576. return recorder.export();
  577. }
  578. addRouteInFlight(route) {
  579. this._routesInFlight.add(route);
  580. }
  581. removeRouteInFlight(route) {
  582. this._routesInFlight.delete(route);
  583. }
  584. async notifyRoutesInFlightAboutRemovedHandler(handler) {
  585. await Promise.all([...this._routesInFlight].map((route) => route.removeHandler(handler)));
  586. }
  587. }
  588. function validateBrowserContextOptions(options, browserOptions) {
  589. if (options.noDefaultViewport && options.deviceScaleFactor !== void 0)
  590. throw new Error(`"deviceScaleFactor" option is not supported with null "viewport"`);
  591. if (options.noDefaultViewport && !!options.isMobile)
  592. throw new Error(`"isMobile" option is not supported with null "viewport"`);
  593. if (options.acceptDownloads === void 0 && browserOptions.name !== "electron")
  594. options.acceptDownloads = "accept";
  595. else if (options.acceptDownloads === void 0 && browserOptions.name === "electron")
  596. options.acceptDownloads = "internal-browser-default";
  597. if (!options.viewport && !options.noDefaultViewport)
  598. options.viewport = { width: 1280, height: 720 };
  599. if (options.recordVideo) {
  600. if (!options.recordVideo.size) {
  601. if (options.noDefaultViewport) {
  602. options.recordVideo.size = { width: 800, height: 600 };
  603. } else {
  604. const size = options.viewport;
  605. const scale = Math.min(1, 800 / Math.max(size.width, size.height));
  606. options.recordVideo.size = {
  607. width: Math.floor(size.width * scale),
  608. height: Math.floor(size.height * scale)
  609. };
  610. }
  611. }
  612. options.recordVideo.size.width &= ~1;
  613. options.recordVideo.size.height &= ~1;
  614. }
  615. if (options.proxy)
  616. options.proxy = normalizeProxySettings(options.proxy);
  617. verifyGeolocation(options.geolocation);
  618. }
  619. function verifyGeolocation(geolocation) {
  620. if (!geolocation)
  621. return;
  622. geolocation.accuracy = geolocation.accuracy || 0;
  623. const { longitude, latitude, accuracy } = geolocation;
  624. if (longitude < -180 || longitude > 180)
  625. throw new Error(`geolocation.longitude: precondition -180 <= LONGITUDE <= 180 failed.`);
  626. if (latitude < -90 || latitude > 90)
  627. throw new Error(`geolocation.latitude: precondition -90 <= LATITUDE <= 90 failed.`);
  628. if (accuracy < 0)
  629. throw new Error(`geolocation.accuracy: precondition 0 <= ACCURACY failed.`);
  630. }
  631. function verifyClientCertificates(clientCertificates) {
  632. if (!clientCertificates)
  633. return;
  634. for (const cert of clientCertificates) {
  635. if (!cert.origin)
  636. throw new Error(`clientCertificates.origin is required`);
  637. if (!cert.cert && !cert.key && !cert.passphrase && !cert.pfx)
  638. throw new Error("None of cert, key, passphrase or pfx is specified");
  639. if (cert.cert && !cert.key)
  640. throw new Error("cert is specified without key");
  641. if (!cert.cert && cert.key)
  642. throw new Error("key is specified without cert");
  643. if (cert.pfx && (cert.cert || cert.key))
  644. throw new Error("pfx is specified together with cert, key or passphrase");
  645. }
  646. }
  647. function normalizeProxySettings(proxy) {
  648. let { server, bypass } = proxy;
  649. let url;
  650. try {
  651. url = new URL(server);
  652. if (!url.host || !url.protocol)
  653. url = new URL("http://" + server);
  654. } catch (e) {
  655. url = new URL("http://" + server);
  656. }
  657. if (url.protocol === "socks4:" && (proxy.username || proxy.password))
  658. throw new Error(`Socks4 proxy protocol does not support authentication`);
  659. if (url.protocol === "socks5:" && (proxy.username || proxy.password))
  660. throw new Error(`Browser does not support socks5 proxy authentication`);
  661. server = url.protocol + "//" + url.host;
  662. if (bypass)
  663. bypass = bypass.split(",").map((t) => t.trim()).join(",");
  664. return { ...proxy, server, bypass };
  665. }
  666. const paramsThatAllowContextReuse = [
  667. "colorScheme",
  668. "forcedColors",
  669. "reducedMotion",
  670. "contrast",
  671. "screen",
  672. "userAgent",
  673. "viewport",
  674. "testIdAttributeName"
  675. ];
  676. const defaultNewContextParamValues = {
  677. noDefaultViewport: false,
  678. ignoreHTTPSErrors: false,
  679. javaScriptEnabled: true,
  680. bypassCSP: false,
  681. offline: false,
  682. isMobile: false,
  683. hasTouch: false,
  684. acceptDownloads: "accept",
  685. strictSelectors: false,
  686. serviceWorkers: "allow",
  687. locale: "en-US"
  688. };
  689. // Annotate the CommonJS export names for ESM import in node:
  690. 0 && (module.exports = {
  691. BrowserContext,
  692. normalizeProxySettings,
  693. validateBrowserContextOptions,
  694. verifyClientCertificates,
  695. verifyGeolocation
  696. });