@letsscrapedata/controller 0.0.52 → 0.0.53

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.
package/dist/index.js CHANGED
@@ -1603,7 +1603,11 @@ var PlaywrightBrowserContext = class extends EventEmitter2 {
1603
1603
  };
1604
1604
 
1605
1605
  // src/playwright/browser.ts
1606
- var PlaywrightBrowser = class extends EventEmitter3 {
1606
+ var PlaywrightBrowser = class _PlaywrightBrowser extends EventEmitter3 {
1607
+ static #supportedBrowserTypes = ["chromium", "firefox", "webkit"];
1608
+ static doesSupport(browserType) {
1609
+ return _PlaywrightBrowser.#supportedBrowserTypes.includes(browserType);
1610
+ }
1607
1611
  #browser;
1608
1612
  #browserIdx;
1609
1613
  #pid;
@@ -1632,7 +1636,7 @@ var PlaywrightBrowser = class extends EventEmitter3 {
1632
1636
  return this.#options.maxPageFreeSeconds ? this.#options.maxPageFreeSeconds : 900;
1633
1637
  }
1634
1638
  // constructor: called only by LsdBrowserController.launch/connect
1635
- constructor(browser, browerType, browserCreateMethod, options, browserIdx = 0, pid = 0) {
1639
+ constructor(browser, browserType, browserCreateMethod, options, browserIdx = 0, pid = 0) {
1636
1640
  if (!browser || typeof browser.contexts !== "function") {
1637
1641
  throw new Error(`Invalid playwright browser parameter`);
1638
1642
  }
@@ -1644,7 +1648,10 @@ var PlaywrightBrowser = class extends EventEmitter3 {
1644
1648
  this.#createTime = getCurrentUnixTime3();
1645
1649
  this.#lsdBrowserContexts = [];
1646
1650
  this.#browserControllerType = "playwright";
1647
- this.#browserType = browerType;
1651
+ this.#browserType = browserType;
1652
+ if (!_PlaywrightBrowser.#supportedBrowserTypes.includes(browserType)) {
1653
+ throw new Error(`Browser controller ${this.#browserControllerType} doesnot support browserType ${browserType}`);
1654
+ }
1648
1655
  this.#browserCreationMethod = browserCreateMethod;
1649
1656
  this.#headless = headless;
1650
1657
  this.#proxy = options?.proxy ? Object.assign({}, options.proxy) : null;
@@ -3284,7 +3291,11 @@ var PuppeteerBrowserContext = class extends EventEmitter5 {
3284
3291
  };
3285
3292
 
3286
3293
  // src/puppeteer/browser.ts
3287
- var PuppeteerBrowser = class extends EventEmitter6 {
3294
+ var PuppeteerBrowser = class _PuppeteerBrowser extends EventEmitter6 {
3295
+ static #supportedBrowserTypes = ["chromium"];
3296
+ static doesSupport(browserType) {
3297
+ return _PuppeteerBrowser.#supportedBrowserTypes.includes(browserType);
3298
+ }
3288
3299
  #browser;
3289
3300
  #browserIdx;
3290
3301
  #pid;
@@ -3316,7 +3327,7 @@ var PuppeteerBrowser = class extends EventEmitter6 {
3316
3327
  return this.#options.userAgent ? this.#options.userAgent : "";
3317
3328
  }
3318
3329
  // constructor: called only by LsdBrowserController.launch/connect
3319
- constructor(browser, browerType, browserCreateMethod, options, browserIdx = 0, pid = 0) {
3330
+ constructor(browser, browserType, browserCreateMethod, options, browserIdx = 0, pid = 0) {
3320
3331
  if (!browser || typeof browser.browserContexts !== "function") {
3321
3332
  throw new Error(`Invalid puppeteer browser parameter`);
3322
3333
  }
@@ -3328,7 +3339,10 @@ var PuppeteerBrowser = class extends EventEmitter6 {
3328
3339
  this.#createTime = getCurrentUnixTime6();
3329
3340
  this.#lsdBrowserContexts = [];
3330
3341
  this.#browserControllerType = "puppeteer";
3331
- this.#browserType = browerType;
3342
+ this.#browserType = browserType;
3343
+ if (!_PuppeteerBrowser.#supportedBrowserTypes.includes(browserType)) {
3344
+ throw new Error(`Browser controller ${this.#browserControllerType} doesnot support browserType ${browserType}`);
3345
+ }
3332
3346
  this.#browserCreationMethod = browserCreateMethod;
3333
3347
  this.#headless = headless;
3334
3348
  this.#proxy = options?.proxy ? Object.assign({}, options.proxy) : null;
@@ -3823,149 +3837,1923 @@ var CheerioPage = class extends EventEmitter7 {
3823
3837
  // src/controller/controller.ts
3824
3838
  import os from "os";
3825
3839
  import puppeteer from "puppeteer";
3826
- import playwright, { request as apiRequest } from "playwright";
3827
- import puppeteerExtra from "puppeteer-extra";
3828
- import * as playwrightExtra from "playwright-extra";
3829
- import StealthPlugin from "puppeteer-extra-plugin-stealth";
3830
- import { getPidsListeningOnPort, unreachable as unreachable5 } from "@letsscrapedata/utils";
3831
- var LsdBrowserController = class _LsdBrowserController {
3832
- static #forbidConstructor = false;
3833
- #puppeteer;
3834
- #playwrightBrowserTypes;
3835
- #nextBrowserIdx;
3836
- /**
3837
- * Possible values are 'aix', 'darwin', 'freebsd','linux', 'openbsd', 'sunos', and 'win32'.
3838
- */
3839
- #osPlatform;
3840
- constructor() {
3841
- if (_LsdBrowserController.#forbidConstructor) {
3842
- throw new Error("Only one LsdBrowserController instance can be created!");
3840
+ import playwright, { request as apiRequestInPlaywright } from "playwright";
3841
+ import patchright from "patchright";
3842
+
3843
+ // src/patchright/browser.ts
3844
+ import EventEmitter10 from "events";
3845
+ import { getCurrentUnixTime as getCurrentUnixTime9, getPerformanceOfPidTree as getPerformanceOfPidTree3 } from "@letsscrapedata/utils";
3846
+
3847
+ // src/patchright/context.ts
3848
+ import EventEmitter9 from "events";
3849
+ import { getCurrentUnixTime as getCurrentUnixTime8, sleep as sleep3 } from "@letsscrapedata/utils";
3850
+
3851
+ // src/patchright/page.ts
3852
+ import EventEmitter8 from "events";
3853
+ import { getCurrentUnixTime as getCurrentUnixTime7, unreachable as unreachable6 } from "@letsscrapedata/utils";
3854
+
3855
+ // src/patchright/element.ts
3856
+ import { unreachable as unreachable5 } from "@letsscrapedata/utils";
3857
+ var PatchrightElement = class _PatchrightElement {
3858
+ #frame;
3859
+ #locator;
3860
+ constructor(locator, frame) {
3861
+ if (!frame.locator || !locator.click) {
3862
+ throw new Error("Invalid paras in new PatchrightElement");
3843
3863
  }
3844
- this.#puppeteer = puppeteer;
3845
- const { chromium: chromium2, firefox: firefox2, webkit: webkit2 } = playwright;
3846
- this.#playwrightBrowserTypes = { chromium: chromium2, firefox: firefox2, webkit: webkit2 };
3847
- this.#osPlatform = os.platform();
3848
- this.#nextBrowserIdx = 1;
3849
- _LsdBrowserController.#forbidConstructor = true;
3864
+ this.#frame = frame;
3865
+ this.#locator = locator;
3850
3866
  }
3851
- #playwrightBrowserType(browserType, connectFlag = false) {
3852
- if (browserType === "chromium") {
3853
- return this.#playwrightBrowserTypes.chromium;
3854
- } else if (connectFlag) {
3855
- throw new Error(`playwright only can connect to chromium browser, not support ${browserType} browser`);
3856
- } else if (browserType === "firefox") {
3857
- return this.#playwrightBrowserTypes.firefox;
3858
- } else if (browserType === "webkit") {
3859
- return this.#playwrightBrowserTypes.webkit;
3860
- } else {
3861
- throw new Error(`Invalid playwright browserType ${browserType}`);
3862
- }
3867
+ async attribute(attributeName) {
3868
+ const attributeValue = await this.#locator.getAttribute(attributeName);
3869
+ return attributeValue ? attributeValue : "";
3863
3870
  }
3864
- #puppeteerProduct(browserType) {
3865
- if (browserType === "chromium") {
3866
- return "chrome";
3867
- } else {
3868
- throw new Error(`Invalid puppeteer product ${browserType}`);
3871
+ async attributeNames() {
3872
+ const names = await this.#locator.evaluate((node) => node.getAttributeNames());
3873
+ return names;
3874
+ }
3875
+ async dataset() {
3876
+ try {
3877
+ const dataset = await this.#locator.evaluate((node) => node.dataset);
3878
+ return dataset;
3879
+ } catch (err) {
3880
+ return {};
3869
3881
  }
3870
3882
  }
3871
- setPuppeteerNode(puppeteer2) {
3872
- if (puppeteer2) {
3873
- this.#puppeteer = puppeteer2;
3874
- } else {
3875
- this.#puppeteer = puppeteerExtra.use(StealthPlugin());
3883
+ async evaluate(func, args) {
3884
+ try {
3885
+ const frame = this.#frame;
3886
+ ;
3887
+ if (typeof frame.parentFrame === "function") {
3888
+ return await frame.evaluate(func, args);
3889
+ } else {
3890
+ const locator = this.#frame.owner();
3891
+ return await locator.evaluate(func, args);
3892
+ }
3893
+ } catch (err) {
3894
+ logerr(err);
3895
+ return "";
3876
3896
  }
3877
- return true;
3878
3897
  }
3879
- setPlaywrightBrowserType(browserType, playwrightBrowserType) {
3880
- switch (browserType) {
3881
- case "chromium":
3882
- if (playwrightBrowserType) {
3883
- this.#playwrightBrowserTypes.chromium = playwrightBrowserType;
3898
+ /*
3899
+ async #getChildFrame(parentFrame: Frame, iframeOption: IframeOption): Promise<Frame | null> {
3900
+ if (!parentFrame) {
3901
+ throw new Error("Invalid parent frame");
3902
+ }
3903
+
3904
+ let { src = "" } = iframeOption;
3905
+ if (!src) {
3906
+ throw new Error("Invalid src in IframeOption");
3907
+ }
3908
+
3909
+ // src: use childFrames()
3910
+ const childFrames = parentFrame.childFrames();
3911
+ for (const childFrame of childFrames) {
3912
+ const url = childFrame.url();
3913
+ if (typeof src === "string") {
3914
+ // src: string
3915
+ if (url.startsWith(src)) {
3916
+ return childFrame;
3917
+ } else if (url.toLowerCase().startsWith(src)) {
3918
+ return childFrame;
3919
+ }
3884
3920
  } else {
3885
- this.#playwrightBrowserTypes.chromium = playwrightExtra.chromium.use(StealthPlugin());
3921
+ // src: RegExp
3922
+ if (url.match(src)) {
3923
+ return childFrame;
3924
+ }
3886
3925
  }
3887
- break;
3888
- case "firefox":
3889
- if (playwrightBrowserType) {
3890
- this.#playwrightBrowserTypes.firefox = playwrightBrowserType;
3891
- } else {
3892
- this.#playwrightBrowserTypes.firefox = playwrightExtra.firefox.use(StealthPlugin());
3926
+ }
3927
+
3928
+ return null;
3929
+ }
3930
+ */
3931
+ async #getChildFrameLocator(parent, iframeOption) {
3932
+ return parent.frameLocator(getIframeSelector(iframeOption));
3933
+ }
3934
+ async #getDescendantFrame(parent, iframeOptions) {
3935
+ try {
3936
+ if (iframeOptions.length <= 0) {
3937
+ return null;
3938
+ }
3939
+ let frameLocator = parent.frameLocator(getIframeSelector(iframeOptions[0]));
3940
+ for (const iframeOption of iframeOptions.slice(1)) {
3941
+ if (!frameLocator) {
3942
+ return null;
3893
3943
  }
3894
- break;
3895
- case "webkit":
3896
- if (playwrightBrowserType) {
3897
- this.#playwrightBrowserTypes.webkit = playwrightBrowserType;
3944
+ frameLocator = await this.#getChildFrameLocator(frameLocator, iframeOption);
3945
+ }
3946
+ return frameLocator;
3947
+ } catch (err) {
3948
+ throw new Error(`No child iframe: ${JSON.stringify(iframeOptions)}`);
3949
+ }
3950
+ }
3951
+ async #findElementHandles(selector, absolute = false, iframeOptions = []) {
3952
+ let parent = absolute ? this.#frame : this.#locator;
3953
+ let frame = this.#frame;
3954
+ const retObj = { frame, locators: [] };
3955
+ if (iframeOptions.length > 0) {
3956
+ const childFrame = await this.#getDescendantFrame(frame, iframeOptions);
3957
+ if (!childFrame) {
3958
+ return retObj;
3959
+ }
3960
+ retObj.frame = childFrame;
3961
+ parent = childFrame;
3962
+ }
3963
+ try {
3964
+ let locators = [];
3965
+ if (selector.startsWith("./") || selector.startsWith("/") || selector.startsWith("..")) {
3966
+ locators = await parent.locator(`xpath=${selector}`).all();
3967
+ } else {
3968
+ if (selector !== ".") {
3969
+ locators = await parent.locator(selector).all();
3898
3970
  } else {
3899
- this.#playwrightBrowserTypes.webkit = playwrightExtra.webkit.use(StealthPlugin());
3971
+ locators = [this.#locator];
3900
3972
  }
3901
- break;
3902
- default:
3903
- unreachable5(browserType);
3973
+ }
3974
+ retObj.locators = locators;
3975
+ return retObj;
3976
+ } catch (err) {
3977
+ loginfo(err);
3978
+ return retObj;
3904
3979
  }
3905
- return true;
3906
3980
  }
3907
- async launch(browserControllerType, browserType, options) {
3908
- let {
3909
- closeFreePagesIntervalSeconds = 300,
3910
- maxBrowserContextsPerBrowser = 10,
3911
- maxPagesPerBrowserContext = 20,
3912
- maxPageFreeSeconds = 900,
3913
- maxViewportOfNewPage = true,
3914
- proxy = null,
3915
- timeout = 3e4,
3916
- args = [],
3917
- executablePath = "",
3918
- maxWindowSize = true,
3919
- headless = true,
3920
- minBrowserContexts = 1,
3921
- // incognito
3922
- proxyPerBrowserContext = false,
3923
- userDataDir = "",
3924
- userAgent = ""
3925
- } = options ? options : {};
3926
- let browserPid = 0;
3927
- const incognito = typeof options?.incognito === "boolean" ? options.incognito : browserControllerType === "puppeteer" ? false : true;
3928
- const actOptions = { closeFreePagesIntervalSeconds, maxBrowserContextsPerBrowser, maxPagesPerBrowserContext, maxPageFreeSeconds, maxViewportOfNewPage, proxy, timeout, args, executablePath, maxWindowSize, headless, minBrowserContexts, incognito, proxyPerBrowserContext, userDataDir, userAgent };
3929
- let idx = args.findIndex((arg) => arg.toLowerCase().startsWith("--incoginto"));
3930
- if (idx >= 0) {
3931
- logwarn(`Please use options.incognito instead when launching new browser.`);
3932
- args.splice(idx, 1);
3933
- }
3934
- idx = args.findIndex((arg) => arg.toLowerCase().startsWith("--proxy-server"));
3935
- if (idx >= 0) {
3936
- logwarn(`Please use options.proxy instead when launching new browser.`);
3937
- args.splice(idx, 1);
3981
+ async findElement(selectorOrXpath, iframeOptions = [], absolute = false) {
3982
+ const selectors = typeof selectorOrXpath === "string" ? [selectorOrXpath] : selectorOrXpath;
3983
+ if (!Array.isArray(selectors)) {
3984
+ throw new Error(`Invalid selectorOrXpath ${selectorOrXpath} in findElement`);
3938
3985
  }
3939
- idx = args.findIndex((arg) => arg.toLowerCase().startsWith("--user-data-dir"));
3940
- if (idx >= 0) {
3941
- logwarn(`Please use options.userDataDir instead when launching new browser.`);
3942
- args.splice(idx, 1);
3986
+ for (const selector of selectors) {
3987
+ const { frame, locators } = await this.#findElementHandles(selector, absolute, iframeOptions);
3988
+ if (locators.length > 0) {
3989
+ const playwrightElement = new _PatchrightElement(locators[0], frame);
3990
+ return playwrightElement;
3991
+ }
3943
3992
  }
3944
- idx = args.findIndex((arg) => arg.toLowerCase().startsWith("--start-maximized"));
3945
- if (idx >= 0) {
3946
- logwarn(`Please use options.maxWindowSize instead when launching new browser.`);
3947
- args.splice(idx, 1);
3993
+ return null;
3994
+ }
3995
+ async findElements(selectorOrXpath, iframeOptions = [], absolute = false) {
3996
+ const selectors = typeof selectorOrXpath === "string" ? [selectorOrXpath] : selectorOrXpath;
3997
+ if (!Array.isArray(selectors)) {
3998
+ throw new Error(`Invalid selectorOrXpath ${selectorOrXpath} in findElements`);
3948
3999
  }
3949
- let lsdBrowser;
3950
- if (browserControllerType === "playwright") {
3951
- const launchOptions = { headless, timeout };
3952
- if (executablePath) {
3953
- launchOptions.executablePath = executablePath;
3954
- }
3955
- if (maxWindowSize) {
3956
- args.push("--start-maximized");
3957
- }
3958
- if (proxy?.proxyUrl && proxy.proxyUrl !== "local") {
3959
- const { proxyUrl: server, username, password } = proxy;
3960
- launchOptions.proxy = { server, username, password };
3961
- } else if (proxyPerBrowserContext && browserType === "chromium" && this.#osPlatform.startsWith("win")) {
3962
- launchOptions.proxy = { server: "proxyPerBrowserContext" };
4000
+ for (const selector of selectors) {
4001
+ const { frame, locators } = await this.#findElementHandles(selector, absolute, iframeOptions);
4002
+ if (locators.length > 0) {
4003
+ const playwrightElements = locators.map((locator) => new _PatchrightElement(locator, frame));
4004
+ return playwrightElements;
3963
4005
  }
3964
- if (browserType === "chromium") {
3965
- if (incognito) {
3966
- args.push("--incognito");
3967
- } else if (userDataDir) {
3968
- args.push(`--user-data-dir=${userDataDir}`);
4006
+ }
4007
+ return [];
4008
+ }
4009
+ async hasAttribute(attributeName) {
4010
+ const hasFlag = await this.#locator.evaluate((node, attr) => node.hasAttribute(attr), attributeName);
4011
+ return hasFlag;
4012
+ }
4013
+ async innerHtml() {
4014
+ const html = await this.#locator.innerHTML();
4015
+ return html;
4016
+ }
4017
+ async innerText(onlyChild = false) {
4018
+ let text = "";
4019
+ if (onlyChild) {
4020
+ text = await this.#locator.evaluate((node) => {
4021
+ let child = node.firstChild;
4022
+ let texts = [];
4023
+ while (child) {
4024
+ if (child.nodeType == 3) {
4025
+ texts.push(child.data);
4026
+ }
4027
+ child = child.nextSibling;
4028
+ }
4029
+ return texts.join(" ");
4030
+ });
4031
+ } else {
4032
+ text = await this.#locator.innerText();
4033
+ }
4034
+ return text;
4035
+ }
4036
+ async outerHtml() {
4037
+ const html = await this.#locator.evaluate((node) => node.outerHTML);
4038
+ return html;
4039
+ }
4040
+ async textContent() {
4041
+ const text = await this.#locator.textContent();
4042
+ return text ? text : "";
4043
+ }
4044
+ async click(options = {}) {
4045
+ const { button, clickCount: count, delay, position: offset, clickType = "click" } = options;
4046
+ const actOptions = { button, count, delay, offset };
4047
+ if (clickType === "click") {
4048
+ await this.#locator.click(actOptions);
4049
+ } else if (clickType === "evaluate") {
4050
+ await this.#locator.evaluate(async (ev) => await ev.click());
4051
+ } else {
4052
+ unreachable5(clickType);
4053
+ }
4054
+ return true;
4055
+ }
4056
+ async focus() {
4057
+ await this.#locator.focus();
4058
+ return true;
4059
+ }
4060
+ async hover() {
4061
+ await this.#locator.hover();
4062
+ return true;
4063
+ }
4064
+ async input(value, options = {}) {
4065
+ const { delay = 0, replace = false, enter = false } = options;
4066
+ if (replace) {
4067
+ await this.#locator.click({ button: "left", clickCount: 3 });
4068
+ }
4069
+ if (delay > 0) {
4070
+ await this.#locator.fill(value);
4071
+ } else {
4072
+ await this.#locator.fill(value);
4073
+ }
4074
+ if (enter) {
4075
+ await this.#locator.press("Enter");
4076
+ }
4077
+ return true;
4078
+ }
4079
+ async press(key, options = {}) {
4080
+ await this.#locator.press(key, options);
4081
+ return true;
4082
+ }
4083
+ async screenshot(options) {
4084
+ return await this.#locator.screenshot(options);
4085
+ }
4086
+ async scrollIntoView() {
4087
+ await this.#locator.scrollIntoViewIfNeeded();
4088
+ return true;
4089
+ }
4090
+ async select(options) {
4091
+ const { type, values = [], labels = [], indexes = [] } = options;
4092
+ switch (type) {
4093
+ case "value":
4094
+ if (values.length > 0) {
4095
+ await this.#locator.selectOption(values);
4096
+ }
4097
+ break;
4098
+ case "label":
4099
+ if (labels.length > 0) {
4100
+ await this.#locator.selectOption(labels.map((label) => {
4101
+ return { label };
4102
+ }));
4103
+ }
4104
+ break;
4105
+ case "index":
4106
+ if (indexes.length > 0) {
4107
+ const indexValues = await this.#locator.evaluate(
4108
+ (node, indexes2) => {
4109
+ const options2 = node.options;
4110
+ const len = options2.length;
4111
+ const vals = [];
4112
+ for (const index of indexes2.filter((i) => i >= 0 && i < len)) {
4113
+ vals.push(options2[index].value);
4114
+ }
4115
+ return vals;
4116
+ },
4117
+ indexes
4118
+ );
4119
+ if (indexValues.length > 0) {
4120
+ await this.#locator.selectOption(indexValues);
4121
+ }
4122
+ }
4123
+ break;
4124
+ default:
4125
+ unreachable5(type);
4126
+ }
4127
+ return true;
4128
+ }
4129
+ async setAttribute(attributeName, newValue) {
4130
+ await this.#locator.evaluate((node, argvs) => {
4131
+ node.setAttribute(argvs[0], argvs[1]);
4132
+ }, [attributeName, newValue]);
4133
+ return true;
4134
+ }
4135
+ _origElement() {
4136
+ return this.#locator;
4137
+ }
4138
+ };
4139
+
4140
+ // src/patchright/page.ts
4141
+ var PatchrightPage = class extends EventEmitter8 {
4142
+ #lsdBrowserContext;
4143
+ #page;
4144
+ #status;
4145
+ #pageId;
4146
+ #closeWhenFree;
4147
+ #resquestInterceptionOptions;
4148
+ #responseInterceptionOptions;
4149
+ #client;
4150
+ #responseCb;
4151
+ #hasValidUrl(page) {
4152
+ const url = page.url();
4153
+ return url.toLowerCase().startsWith("http");
4154
+ }
4155
+ async #clearCookies(page) {
4156
+ if (!this.#hasValidUrl(page)) {
4157
+ throw new Error("Please open related url before clearing cookies");
4158
+ }
4159
+ const browserContext = this.#lsdBrowserContext._origBrowserContext();
4160
+ if (!browserContext) {
4161
+ throw new Error(`Invalid LsdBrowserContext`);
4162
+ }
4163
+ const cookieItems = await this.#getCookies(page);
4164
+ const domainSet = new Set(cookieItems.map((c) => c.domain));
4165
+ if (domainSet.size !== 1) {
4166
+ logwarn(`Domains in clearCookies: ${Array.from(domainSet.values())}`);
4167
+ }
4168
+ for (const domain of domainSet.values()) {
4169
+ await browserContext.clearCookies({ domain });
4170
+ }
4171
+ return true;
4172
+ }
4173
+ async #getCookies(page) {
4174
+ if (!this.#hasValidUrl(page)) {
4175
+ throw new Error("Please open related url before getting cookies");
4176
+ }
4177
+ const browserContext = this.#lsdBrowserContext._origBrowserContext();
4178
+ if (!browserContext) {
4179
+ throw new Error(`Invalid LsdBrowserContext`);
4180
+ }
4181
+ const url = page.url();
4182
+ const origCookies = await browserContext.cookies(url);
4183
+ const cookies = origCookies.map((origCookie) => {
4184
+ const { name, value, domain, path, expires, httpOnly, secure, sameSite = "Lax" } = origCookie;
4185
+ return { name, value, domain, path, expires, httpOnly, secure, sameSite };
4186
+ });
4187
+ return cookies;
4188
+ }
4189
+ async #setCookies(page, cookies) {
4190
+ if (!page) {
4191
+ throw new Error("No valid page");
4192
+ }
4193
+ if (Array.isArray(cookies) && cookies.length > 0 && cookies.every((c) => typeof c.name === "string")) {
4194
+ const browserContext = this.#lsdBrowserContext._origBrowserContext();
4195
+ if (!browserContext) {
4196
+ throw new Error(`Invalid LsdBrowserContext`);
4197
+ }
4198
+ await browserContext.addCookies(cookies);
4199
+ return true;
4200
+ } else {
4201
+ return false;
4202
+ }
4203
+ }
4204
+ async #clearLocalStorage(page) {
4205
+ if (!this.#hasValidUrl(page)) {
4206
+ throw new Error("Please open related url before clearing localStorage");
4207
+ }
4208
+ await page.evaluate(() => window.localStorage.clear());
4209
+ return true;
4210
+ }
4211
+ async #getLocalStorage(page) {
4212
+ if (!this.#hasValidUrl(page)) {
4213
+ throw new Error("Please open related url before getting localStorage");
4214
+ }
4215
+ const localStorageStr = await page.evaluate(() => JSON.stringify(window.localStorage));
4216
+ const localStorageObj = JSON.parse(localStorageStr);
4217
+ const localStorageItems = Object.keys(localStorageObj).map((name) => ({ name, value: localStorageObj[name] }));
4218
+ const url = new URL(page.url());
4219
+ return [{ origin: url.origin, localStorage: localStorageItems }];
4220
+ }
4221
+ async #setLocalStorage(page, localStorageItems) {
4222
+ if (!this.#hasValidUrl(page)) {
4223
+ throw new Error("Please open related url before setting localStorage");
4224
+ }
4225
+ await page.evaluate((items) => {
4226
+ for (const item of items) {
4227
+ window.localStorage.setItem(item.name, item.value);
4228
+ }
4229
+ }, localStorageItems);
4230
+ return true;
4231
+ }
4232
+ async #clearIndexedDB(page) {
4233
+ if (!this.#hasValidUrl(page)) {
4234
+ throw new Error("Please open related url before clearing indexedDB");
4235
+ }
4236
+ await page.evaluate(async () => {
4237
+ for (const db of await indexedDB.databases?.() || []) {
4238
+ if (db.name)
4239
+ indexedDB.deleteDatabase(db.name);
4240
+ }
4241
+ });
4242
+ return true;
4243
+ }
4244
+ /*
4245
+ async #getChildFrame(parentFrame: Frame, iframeOption: IframeOption): Promise<Frame | null> {
4246
+ if (!parentFrame) {
4247
+ throw new Error("Invalid parent frame");
4248
+ }
4249
+
4250
+ let { src = "" } = iframeOption;
4251
+ if (!src) {
4252
+ throw new Error("Invalid src in IframeOption");
4253
+ }
4254
+
4255
+ // src: use childFrames()
4256
+ const childFrames = parentFrame.childFrames();
4257
+ for (const childFrame of childFrames) {
4258
+ const url = childFrame.url();
4259
+ if (typeof src === "string") {
4260
+ // src: string
4261
+ if (url.startsWith(src)) {
4262
+ return childFrame;
4263
+ } else if (url.toLowerCase().startsWith(src)) {
4264
+ return childFrame;
4265
+ }
4266
+ } else {
4267
+ // src: RegExp
4268
+ if (url.match(src)) {
4269
+ return childFrame;
4270
+ }
4271
+ }
4272
+ }
4273
+
4274
+ return null;
4275
+ }
4276
+ */
4277
+ async #findDescendantFrame(src, id) {
4278
+ if (!this.#page) {
4279
+ throw new Error("No valid page");
4280
+ }
4281
+ const frames = this.#page.frames();
4282
+ for (const frame of frames) {
4283
+ const url = frame.url();
4284
+ if (typeof src === "string" && src) {
4285
+ if (url.startsWith(src)) {
4286
+ return frame;
4287
+ } else if (url.toLowerCase().startsWith(src)) {
4288
+ return frame;
4289
+ }
4290
+ } else if (src instanceof RegExp) {
4291
+ if (url.match(src)) {
4292
+ return frame;
4293
+ }
4294
+ } else if (id) {
4295
+ const element = await frame.frameElement();
4296
+ if (element) {
4297
+ const frameId = await frame.evaluate(([ele, attr]) => ele.getAttribute(attr), [element, "id"]);
4298
+ if (frameId === id) {
4299
+ return frame;
4300
+ }
4301
+ }
4302
+ }
4303
+ }
4304
+ return null;
4305
+ }
4306
+ async #getChildFrameLocator(parent, iframeOption) {
4307
+ return parent.frameLocator(getIframeSelector(iframeOption));
4308
+ }
4309
+ async #getDescendantFrame(mainFrame, iframeOptions) {
4310
+ try {
4311
+ if (iframeOptions.length <= 0) {
4312
+ return null;
4313
+ }
4314
+ if (iframeOptions.length === 1 && !iframeOptions[0].selector) {
4315
+ const { src = "", id = "" } = iframeOptions[0];
4316
+ const frame = await this.#findDescendantFrame(src, id);
4317
+ return frame;
4318
+ } else {
4319
+ let frameLocator = mainFrame.frameLocator(getIframeSelector(iframeOptions[0]));
4320
+ for (const iframeOption of iframeOptions.slice(1)) {
4321
+ if (!frameLocator) {
4322
+ return null;
4323
+ }
4324
+ frameLocator = await this.#getChildFrameLocator(frameLocator, iframeOption);
4325
+ }
4326
+ return frameLocator;
4327
+ }
4328
+ } catch (err) {
4329
+ throw new Error(`No child iframe: ${JSON.stringify(iframeOptions)}`);
4330
+ }
4331
+ }
4332
+ async #findElementHandles(selector, iframeOptions = []) {
4333
+ if (!this.#page) {
4334
+ throw new Error("No valid page");
4335
+ }
4336
+ let frame = this.#page.mainFrame();
4337
+ const retObj = { frame, locators: [] };
4338
+ if (iframeOptions.length > 0) {
4339
+ frame = await this.#getDescendantFrame(frame, iframeOptions);
4340
+ if (!frame) {
4341
+ return retObj;
4342
+ }
4343
+ retObj.frame = frame;
4344
+ }
4345
+ try {
4346
+ let locators = [];
4347
+ if (selector.startsWith("./") || selector.startsWith("/") || selector.startsWith("..")) {
4348
+ locators = await frame.locator(`xpath=${selector}`).all();
4349
+ } else {
4350
+ if (selector !== ".") {
4351
+ locators = await frame.locator(selector).all();
4352
+ } else {
4353
+ throw new Error("Cannot use selector '.' on page");
4354
+ }
4355
+ }
4356
+ retObj.locators = locators;
4357
+ return retObj;
4358
+ } catch (err) {
4359
+ loginfo(err);
4360
+ return retObj;
4361
+ }
4362
+ }
4363
+ #addPageOn() {
4364
+ if (!this.#page) {
4365
+ throw new Error("No valid page");
4366
+ }
4367
+ const page = this.#page;
4368
+ const pageId = this.#pageId;
4369
+ page.on("close", async () => {
4370
+ loginfo(`##browser ${pageId} closed`);
4371
+ if (!page.pageInfo) {
4372
+ logerr(`Logic error in page.on("close")`);
4373
+ }
4374
+ this.emit("pageClose");
4375
+ this.#lsdBrowserContext.emit("pageClose", this);
4376
+ });
4377
+ page.on("popup", (p) => {
4378
+ if (p) {
4379
+ let evtData = null;
4380
+ const pageInfo = p.pageInfo;
4381
+ let popupPageId = "page";
4382
+ if (pageInfo) {
4383
+ const { browserIdx, browserContextIdx, pageIdx } = pageInfo;
4384
+ popupPageId = `page-${browserIdx}-${browserContextIdx}-${pageIdx}`;
4385
+ pageInfo.openType = "popup";
4386
+ evtData = this.browserContext().page(pageIdx);
4387
+ if (evtData && page.pageInfo?.taskId) {
4388
+ pageInfo.relatedId = page.pageInfo.taskId;
4389
+ }
4390
+ } else {
4391
+ logerr(`##browser ${pageId} has popup without page.pageInfo`);
4392
+ }
4393
+ loginfo(`##browser ${pageId} has popup ${popupPageId}`);
4394
+ this.emit("pagePopup", evtData);
4395
+ } else {
4396
+ logerr(`##browser ${pageId} has popup page with null page`);
4397
+ }
4398
+ });
4399
+ }
4400
+ constructor(browserContext, page, pageInfo) {
4401
+ if (!browserContext.pages || !page?.goto) {
4402
+ throw new Error("Invalid paras in new LsdPage");
4403
+ }
4404
+ super();
4405
+ this.#lsdBrowserContext = browserContext;
4406
+ this.#page = page;
4407
+ this.#status = "free";
4408
+ const currentTime = getCurrentUnixTime7();
4409
+ const { browserIdx = 0, browserContextIdx = 0, pageIdx = 0, openType = "other", openTime = currentTime, lastStatusUpdateTime = currentTime, taskId = 0, relatedId = 0, misc = {} } = pageInfo ? pageInfo : {};
4410
+ this.#page.pageInfo = { browserIdx, browserContextIdx, pageIdx, openType, openTime, lastStatusUpdateTime, taskId, relatedId, misc };
4411
+ this.#pageId = `page-${browserIdx}-${browserContextIdx}-${pageIdx}`;
4412
+ this.#closeWhenFree = false;
4413
+ this.#resquestInterceptionOptions = [];
4414
+ this.#responseInterceptionOptions = [];
4415
+ this.#client = null;
4416
+ this.#responseCb = null;
4417
+ this.#addPageOn();
4418
+ }
4419
+ async addPreloadScript(scriptOrFunc, arg) {
4420
+ if (!this.#page) {
4421
+ throw new Error("No valid page");
4422
+ }
4423
+ if (typeof scriptOrFunc === "string") {
4424
+ await this.#page.addInitScript({ content: scriptOrFunc });
4425
+ } else if (typeof scriptOrFunc === "function") {
4426
+ await this.#page.addInitScript(scriptOrFunc, arg);
4427
+ } else {
4428
+ throw new Error(`Invalid type of scriptOrFunc ${typeof scriptOrFunc}`);
4429
+ }
4430
+ return true;
4431
+ }
4432
+ async addScriptTag(options) {
4433
+ if (!this.#page) {
4434
+ throw new Error("No valid page");
4435
+ }
4436
+ return this.#page.addScriptTag(options);
4437
+ }
4438
+ apiContext() {
4439
+ return this.browserContext().apiContext();
4440
+ }
4441
+ async bringToFront() {
4442
+ if (!this.#page) {
4443
+ throw new Error("No valid page");
4444
+ }
4445
+ await this.#page.bringToFront();
4446
+ return true;
4447
+ }
4448
+ browserContext() {
4449
+ return this.#lsdBrowserContext;
4450
+ }
4451
+ async clearCookies() {
4452
+ if (!this.#page) {
4453
+ throw new Error("No valid page");
4454
+ }
4455
+ return await this.#clearCookies(this.#page);
4456
+ }
4457
+ async clearLocalStorage() {
4458
+ if (!this.#page) {
4459
+ throw new Error("No valid page");
4460
+ }
4461
+ return await this.#clearLocalStorage(this.#page);
4462
+ }
4463
+ async clearRequestInterceptions() {
4464
+ if (!this.#page) {
4465
+ throw new Error("No valid page");
4466
+ }
4467
+ await this.#page.unrouteAll();
4468
+ return true;
4469
+ }
4470
+ async clearResponseInterceptions() {
4471
+ if (!this.#page) {
4472
+ throw new Error("No valid page");
4473
+ }
4474
+ try {
4475
+ if (this.#responseInterceptionOptions.length > 0) {
4476
+ if (this.#responseCb) {
4477
+ this.#page.removeListener("response", this.#responseCb);
4478
+ }
4479
+ this.#responseInterceptionOptions = [];
4480
+ }
4481
+ return true;
4482
+ } catch (err) {
4483
+ logerr(err);
4484
+ return false;
4485
+ }
4486
+ }
4487
+ async clearStateData() {
4488
+ if (!this.#page) {
4489
+ throw new Error("No valid page");
4490
+ }
4491
+ await this.#clearCookies(this.#page);
4492
+ await this.#clearIndexedDB(this.#page);
4493
+ return await this.#clearLocalStorage(this.#page);
4494
+ }
4495
+ async close() {
4496
+ if (this.#status === "closed") {
4497
+ logwarn(`Page ${this.#pageId} is already closed.`);
4498
+ return true;
4499
+ } else if (this.#status === "busy") {
4500
+ throw new Error(`Page ${this.#pageId} cannot be closed because it is busy.`);
4501
+ }
4502
+ if (!this.#page) {
4503
+ throw new Error("No valid page");
4504
+ }
4505
+ await this.#page.close();
4506
+ this.#page = null;
4507
+ this.#status = "closed";
4508
+ return true;
4509
+ }
4510
+ closeWhenFree() {
4511
+ return this.#closeWhenFree;
4512
+ }
4513
+ async content(iframeOptions = []) {
4514
+ if (!this.#page) {
4515
+ throw new Error("No valid page");
4516
+ }
4517
+ let content = "";
4518
+ if (iframeOptions.length > 0) {
4519
+ const frameLocator = await this.#getDescendantFrame(this.#page.mainFrame(), iframeOptions);
4520
+ if (frameLocator) {
4521
+ content = await frameLocator.locator(":root").evaluate(() => document.documentElement.outerHTML);
4522
+ }
4523
+ } else {
4524
+ content = await this.#page.content();
4525
+ }
4526
+ return content;
4527
+ }
4528
+ async cookies() {
4529
+ if (!this.#page) {
4530
+ throw new Error("No valid page");
4531
+ }
4532
+ return this.#getCookies(this.#page);
4533
+ }
4534
+ async documentHeight() {
4535
+ if (!this.#page) {
4536
+ throw new Error("No valid page");
4537
+ }
4538
+ const height = await this.#page.evaluate(() => document.documentElement.scrollHeight);
4539
+ return height;
4540
+ }
4541
+ async evaluate(func, args) {
4542
+ if (!this.#page) {
4543
+ throw new Error("No valid page");
4544
+ }
4545
+ return this.#page.evaluate(func, args);
4546
+ }
4547
+ async exposeFunction(name, callbackFunction) {
4548
+ if (!this.#page) {
4549
+ throw new Error("No valid page");
4550
+ }
4551
+ await this.#page.exposeFunction(name, callbackFunction);
4552
+ return;
4553
+ }
4554
+ async findElement(selectorOrXpath, iframeOptions = []) {
4555
+ if (!this.#page) {
4556
+ throw new Error("No valid page");
4557
+ }
4558
+ const selectors = typeof selectorOrXpath === "string" ? [selectorOrXpath] : selectorOrXpath;
4559
+ if (!Array.isArray(selectors)) {
4560
+ throw new Error(`Invalid selectorOrXpath ${selectorOrXpath} in findElement`);
4561
+ }
4562
+ for (const selector of selectors) {
4563
+ const { frame, locators } = await this.#findElementHandles(selector, iframeOptions);
4564
+ if (locators.length > 0) {
4565
+ const playwrightElement = new PatchrightElement(locators[0], frame);
4566
+ return playwrightElement;
4567
+ }
4568
+ }
4569
+ return null;
4570
+ }
4571
+ async findElements(selectorOrXpath, iframeOptions = []) {
4572
+ if (!this.#page) {
4573
+ throw new Error("No valid page");
4574
+ }
4575
+ const selectors = typeof selectorOrXpath === "string" ? [selectorOrXpath] : selectorOrXpath;
4576
+ if (!Array.isArray(selectors)) {
4577
+ throw new Error(`Invalid selectorOrXpath ${selectorOrXpath} in findElements`);
4578
+ }
4579
+ for (const selector of selectors) {
4580
+ const { frame, locators } = await this.#findElementHandles(selector, iframeOptions);
4581
+ if (locators.length > 0) {
4582
+ const playwrightElements = locators.map((locator) => new PatchrightElement(locator, frame));
4583
+ return playwrightElements;
4584
+ }
4585
+ }
4586
+ return [];
4587
+ }
4588
+ async free() {
4589
+ if (this.#status === "free") {
4590
+ logwarn(`Page ${this.#pageId} is already free.`);
4591
+ }
4592
+ this.#status = "free";
4593
+ await this.clearRequestInterceptions();
4594
+ await this.clearResponseInterceptions();
4595
+ return true;
4596
+ }
4597
+ #getWaitUntil(origWaitUntil) {
4598
+ if (origWaitUntil === "networkidle0" || origWaitUntil === "networkidle2") {
4599
+ return "networkidle";
4600
+ } else {
4601
+ return origWaitUntil;
4602
+ }
4603
+ }
4604
+ async goto(url, options) {
4605
+ if (!this.#page) {
4606
+ throw new Error("No valid page");
4607
+ }
4608
+ if (options) {
4609
+ const { referer, timeout, waitUntil = "load" } = options;
4610
+ const newOptions = {};
4611
+ if (referer) {
4612
+ newOptions.referer = referer;
4613
+ }
4614
+ if (timeout) {
4615
+ newOptions.timeout = timeout;
4616
+ }
4617
+ newOptions.waitUntil = this.#getWaitUntil(waitUntil);
4618
+ await this.#page.goto(url, newOptions);
4619
+ } else {
4620
+ await this.#page.goto(url);
4621
+ }
4622
+ return true;
4623
+ }
4624
+ id() {
4625
+ return this.#pageId;
4626
+ }
4627
+ isFree() {
4628
+ return this.#status === "free";
4629
+ }
4630
+ async localStroage() {
4631
+ if (!this.#page) {
4632
+ throw new Error("No valid page");
4633
+ }
4634
+ return this.#getLocalStorage(this.#page);
4635
+ }
4636
+ load() {
4637
+ throw new Error("Not supported in PatchrightPage.");
4638
+ }
4639
+ mainFrame() {
4640
+ if (!this.#page) {
4641
+ throw new Error("No valid page");
4642
+ }
4643
+ return this.#page.mainFrame();
4644
+ }
4645
+ async maximizeViewport() {
4646
+ const height = await this.pageHeight();
4647
+ const width = await this.pageWidth();
4648
+ return await this.setViewportSize({ height, width });
4649
+ }
4650
+ async pageHeight() {
4651
+ if (!this.#page) {
4652
+ throw new Error("No valid page");
4653
+ }
4654
+ const bodyHeight = await this.#page.evaluate(() => document.body.scrollHeight);
4655
+ const documentHeight = await this.#page.evaluate(() => document.documentElement.scrollHeight);
4656
+ const windowHeight = await this.#page.evaluate(() => window.outerHeight);
4657
+ const pageHeight = Math.max(bodyHeight, documentHeight, windowHeight);
4658
+ return pageHeight;
4659
+ }
4660
+ pageInfo() {
4661
+ if (!this.#page) {
4662
+ throw new Error("No valid page");
4663
+ }
4664
+ return Object.assign({}, this.#page.pageInfo);
4665
+ }
4666
+ async pageWidth() {
4667
+ if (!this.#page) {
4668
+ throw new Error("No valid page");
4669
+ }
4670
+ const offsetWidth = await this.#page.evaluate(() => document.documentElement.offsetWidth);
4671
+ const windowWidth = await this.#page.evaluate(() => window.outerWidth);
4672
+ const pageWidth = Math.max(offsetWidth, windowWidth);
4673
+ return pageWidth;
4674
+ }
4675
+ async pdf(options) {
4676
+ if (!this.#page) {
4677
+ throw new Error("No valid page");
4678
+ }
4679
+ const buffer = await this.#page.pdf(options);
4680
+ return buffer;
4681
+ }
4682
+ async screenshot(options) {
4683
+ if (!this.#page) {
4684
+ throw new Error("No valid page");
4685
+ }
4686
+ return await this.#page.screenshot(options);
4687
+ }
4688
+ async scrollBy(x, y) {
4689
+ if (!this.#page) {
4690
+ throw new Error("No valid page");
4691
+ }
4692
+ await this.#page.evaluate(
4693
+ ([x2, y2]) => {
4694
+ window.scrollBy(x2, y2);
4695
+ },
4696
+ [x, y]
4697
+ );
4698
+ return true;
4699
+ }
4700
+ async scrollTo(x, y) {
4701
+ if (!this.#page) {
4702
+ throw new Error("No valid page");
4703
+ }
4704
+ await this.#page.evaluate(
4705
+ ([x2, y2]) => {
4706
+ window.scrollTo(x2, y2);
4707
+ },
4708
+ [x, y]
4709
+ );
4710
+ return true;
4711
+ }
4712
+ async sendCDPMessage(method, params = null, detach = true) {
4713
+ if (!this.#client) {
4714
+ const origContext = this.browserContext()._origBrowserContext();
4715
+ if (!origContext) {
4716
+ throw new Error(`Invalid playwright browserContext`);
4717
+ }
4718
+ this.#client = await origContext.newCDPSession(this.#page);
4719
+ }
4720
+ if (!this.#client) {
4721
+ throw new Error("No valid CDP session to send message");
4722
+ }
4723
+ const response = params ? await this.#client.send(method, params) : await this.#client.send(method);
4724
+ if (detach) {
4725
+ await this.#client.detach();
4726
+ this.#client = null;
4727
+ }
4728
+ return response;
4729
+ }
4730
+ setCloseWhenFree(closeWhenFree) {
4731
+ this.#closeWhenFree = closeWhenFree;
4732
+ return true;
4733
+ }
4734
+ async setCookies(cookies) {
4735
+ if (!this.#page) {
4736
+ throw new Error("No valid page");
4737
+ }
4738
+ return await this.#setCookies(this.#page, cookies);
4739
+ }
4740
+ async setExtraHTTPHeaders(headers) {
4741
+ if (!this.#page) {
4742
+ throw new Error("No valid page");
4743
+ }
4744
+ await this.#page.setExtraHTTPHeaders(headers);
4745
+ return true;
4746
+ }
4747
+ async setLocalStroage(localStorageItems) {
4748
+ if (!this.#page) {
4749
+ throw new Error("No valid page");
4750
+ }
4751
+ return await this.#setLocalStorage(this.#page, localStorageItems);
4752
+ }
4753
+ setPageInfo(pageInfo) {
4754
+ if (!this.#page?.pageInfo) {
4755
+ throw new Error("No valid page or pageInfo");
4756
+ }
4757
+ if (!pageInfo) {
4758
+ throw new Error("Invalid paras in setPageInfo");
4759
+ }
4760
+ const actPageInfo = this.#page.pageInfo;
4761
+ const { lastStatusUpdateTime, taskId, relatedId, misc } = pageInfo;
4762
+ if (typeof lastStatusUpdateTime === "number") {
4763
+ actPageInfo.lastStatusUpdateTime = lastStatusUpdateTime;
4764
+ }
4765
+ if (typeof taskId === "number") {
4766
+ actPageInfo.taskId = taskId;
4767
+ }
4768
+ if (typeof relatedId === "number") {
4769
+ actPageInfo.relatedId = relatedId;
4770
+ }
4771
+ if (misc && typeof misc === "object") {
4772
+ for (const key of Object.keys(misc)) {
4773
+ actPageInfo.misc[key] = misc[key];
4774
+ }
4775
+ }
4776
+ return true;
4777
+ }
4778
+ #checkRequestMatch(request, requestMatch) {
4779
+ try {
4780
+ if (!request) {
4781
+ return false;
4782
+ }
4783
+ const { methods, postData, resourceTypes, url } = requestMatch;
4784
+ if (methods && !methods.includes(request.method().toUpperCase())) {
4785
+ return false;
4786
+ }
4787
+ if (resourceTypes && !resourceTypes.includes(request.resourceType())) {
4788
+ return false;
4789
+ }
4790
+ if (url && !request.url().match(url)) {
4791
+ return false;
4792
+ }
4793
+ const origData = request.postData();
4794
+ const data = origData ? origData : "";
4795
+ if (postData && !data.match(postData)) {
4796
+ return false;
4797
+ }
4798
+ return true;
4799
+ } catch (err) {
4800
+ logerr(err);
4801
+ return false;
4802
+ }
4803
+ }
4804
+ async setRequestInterception(options) {
4805
+ if (!this.#page) {
4806
+ throw new Error("No valid page");
4807
+ }
4808
+ const actOptions = Array.isArray(options) ? options : [options];
4809
+ if (actOptions.length <= 0) {
4810
+ logwarn("Invalid paras in setRequestInterception");
4811
+ return false;
4812
+ }
4813
+ const firstRequestInterception = this.#resquestInterceptionOptions.length <= 0;
4814
+ for (const option of actOptions) {
4815
+ switch (option.action) {
4816
+ case "abort":
4817
+ case "fulfill":
4818
+ this.#resquestInterceptionOptions.push(option);
4819
+ break;
4820
+ default:
4821
+ unreachable6(option.action);
4822
+ }
4823
+ }
4824
+ if (firstRequestInterception && this.#resquestInterceptionOptions.length > 0) {
4825
+ this.#page.route("**", async (route) => {
4826
+ try {
4827
+ for (const option of actOptions) {
4828
+ const { requestMatch, action, fulfill } = option;
4829
+ const request = route.request();
4830
+ const matchedFlag = !requestMatch || this.#checkRequestMatch(request, requestMatch);
4831
+ if (matchedFlag) {
4832
+ switch (action) {
4833
+ case "abort":
4834
+ await route.abort();
4835
+ break;
4836
+ case "fulfill":
4837
+ const body = fulfill ? fulfill : `<html><body><h1>${request.url()}</h1></body></html>`;
4838
+ route.fulfill({
4839
+ status: 200,
4840
+ // contentType: "text/html; charset=utf-8", // "text/plain",
4841
+ body
4842
+ });
4843
+ break;
4844
+ default:
4845
+ unreachable6(action);
4846
+ }
4847
+ return true;
4848
+ } else {
4849
+ }
4850
+ }
4851
+ await route.continue();
4852
+ return true;
4853
+ } catch (err) {
4854
+ logerr(err);
4855
+ return false;
4856
+ }
4857
+ });
4858
+ }
4859
+ return true;
4860
+ }
4861
+ async #responseListener(response) {
4862
+ try {
4863
+ const pageUrl = this.#page ? this.#page.url() : "";
4864
+ if (!response.ok()) {
4865
+ return;
4866
+ }
4867
+ const request = response.request();
4868
+ if (!request) {
4869
+ return;
4870
+ }
4871
+ for (const option of this.#responseInterceptionOptions) {
4872
+ const { requestMatch, responseMatch, responseItems, handler, handlerOptions = {} } = option;
4873
+ let matchedFlag = !requestMatch || this.#checkRequestMatch(request, requestMatch);
4874
+ if (matchedFlag && responseMatch) {
4875
+ const { minLength, maxLength } = responseMatch;
4876
+ const text = await response.text();
4877
+ const len = text.length;
4878
+ if (minLength && minLength > 0 && len < minLength || maxLength && maxLength > 0 && len > maxLength) {
4879
+ matchedFlag = false;
4880
+ }
4881
+ }
4882
+ if (!matchedFlag) {
4883
+ continue;
4884
+ }
4885
+ if (Array.isArray(responseItems)) {
4886
+ const requestMethod = request.method();
4887
+ const requestUrl = request.url();
4888
+ const reqData2 = request.postData();
4889
+ const requestData = reqData2 ? reqData2 : "";
4890
+ const responseData = await response.text();
4891
+ responseItems.push({
4892
+ pageUrl,
4893
+ requestMethod,
4894
+ requestUrl,
4895
+ requestData,
4896
+ responseData
4897
+ });
4898
+ loginfo(`##browser cache matched response: ${requestUrl}`);
4899
+ }
4900
+ if (typeof handler === "function") {
4901
+ const pageData = { pageUrl, cookies: "" };
4902
+ await handler(response, handlerOptions, pageData);
4903
+ }
4904
+ }
4905
+ return;
4906
+ } catch (err) {
4907
+ logerr(err);
4908
+ return;
4909
+ }
4910
+ }
4911
+ async setResponseInterception(options) {
4912
+ if (!this.#page) {
4913
+ throw new Error("No valid page");
4914
+ }
4915
+ const actOptions = Array.isArray(options) ? options : [options];
4916
+ if (actOptions.length <= 0) {
4917
+ logwarn("Invalid paras in setResponseInterception");
4918
+ return false;
4919
+ }
4920
+ const firstResponseInterception = this.#responseInterceptionOptions.length <= 0;
4921
+ for (const option of actOptions) {
4922
+ if (option?.responseItems || option?.handler) {
4923
+ this.#responseInterceptionOptions.push(option);
4924
+ } else {
4925
+ throw new Error(`Invalid ResponseInterceptionOption`);
4926
+ }
4927
+ }
4928
+ if (firstResponseInterception && this.#responseInterceptionOptions.length > 0) {
4929
+ this.#responseCb = this.#responseListener.bind(this);
4930
+ this.#page.on("response", this.#responseCb);
4931
+ }
4932
+ return true;
4933
+ }
4934
+ async setStateData(stateData) {
4935
+ return await this.#lsdBrowserContext.setStateData(stateData);
4936
+ }
4937
+ async setUserAgent(userAgent) {
4938
+ if (userAgent) {
4939
+ throw new Error(`Patchright does not support page.setUserAgent by now`);
4940
+ }
4941
+ return false;
4942
+ }
4943
+ async setViewportSize(viewPortSize) {
4944
+ if (!this.#page) {
4945
+ throw new Error("No valid page");
4946
+ }
4947
+ await this.#page.setViewportSize(viewPortSize);
4948
+ return true;
4949
+ }
4950
+ async stateData() {
4951
+ if (!this.#page) {
4952
+ throw new Error("No valid page");
4953
+ }
4954
+ const cookies = await this.#getCookies(this.#page);
4955
+ const localStorage = await this.#getLocalStorage(this.#page);
4956
+ return { cookies, localStorage };
4957
+ }
4958
+ status() {
4959
+ return this.#status;
4960
+ }
4961
+ async title() {
4962
+ if (!this.#page) {
4963
+ throw new Error("No valid page");
4964
+ }
4965
+ return await this.#page.title();
4966
+ }
4967
+ url() {
4968
+ if (!this.#page) {
4969
+ throw new Error("No valid page");
4970
+ }
4971
+ return this.#page.url();
4972
+ }
4973
+ use() {
4974
+ if (this.#status === "busy") {
4975
+ throw new Error(`Page ${this.#pageId} is already busy!!!`);
4976
+ }
4977
+ this.#status = "busy";
4978
+ return true;
4979
+ }
4980
+ async waitForElement(selector, options = {}) {
4981
+ if (!this.#page) {
4982
+ throw new Error("No valid page");
4983
+ }
4984
+ const locator = this.#page.locator(selector);
4985
+ const { timeout = 3e4, state = "visible" } = options;
4986
+ await locator.waitFor({ state, timeout });
4987
+ return true;
4988
+ }
4989
+ async waitForNavigation(options) {
4990
+ if (!this.#page) {
4991
+ throw new Error("No valid page");
4992
+ }
4993
+ const { url = "", timeout = 3e4, waitUntil = "load" } = options;
4994
+ const newWaitUntil = this.#getWaitUntil(waitUntil);
4995
+ if (url) {
4996
+ await this.#page.waitForURL(url, { timeout, waitUntil: newWaitUntil });
4997
+ } else if (newWaitUntil === "commit") {
4998
+ throw new Error("commit is not supported in PatchrightPage.waitForNavigation");
4999
+ } else {
5000
+ await this.#page.waitForLoadState(newWaitUntil, { timeout });
5001
+ }
5002
+ return true;
5003
+ }
5004
+ async windowMember(keys) {
5005
+ if (!this.#page) {
5006
+ throw new Error("No valid page");
5007
+ }
5008
+ if (!this.#page || !Array.isArray(keys) || keys.length <= 0 || keys.length > 20) {
5009
+ return "";
5010
+ }
5011
+ const content = await this.#page.evaluate(
5012
+ (keys2) => {
5013
+ let retObj = window;
5014
+ for (const key of keys2) {
5015
+ if (!key) {
5016
+ break;
5017
+ } else if (typeof retObj !== "object" || !retObj) {
5018
+ return "";
5019
+ } else {
5020
+ retObj = retObj[key];
5021
+ }
5022
+ }
5023
+ if (typeof retObj === "string") {
5024
+ return retObj;
5025
+ } else if (typeof retObj === "number") {
5026
+ return String(retObj);
5027
+ } else if (typeof retObj === "boolean") {
5028
+ return String(Number(retObj));
5029
+ } else if (!retObj) {
5030
+ return "";
5031
+ } else if (typeof retObj === "object") {
5032
+ try {
5033
+ return JSON.stringify(retObj);
5034
+ } catch (err) {
5035
+ return "";
5036
+ }
5037
+ } else if (typeof retObj === "bigint") {
5038
+ return String(retObj);
5039
+ } else {
5040
+ return "";
5041
+ }
5042
+ },
5043
+ keys
5044
+ );
5045
+ return content;
5046
+ }
5047
+ _origPage() {
5048
+ return this.#page;
5049
+ }
5050
+ };
5051
+
5052
+ // src/patchright/api.ts
5053
+ var PatchrightApiContext = class {
5054
+ #apiRequestContext;
5055
+ #status;
5056
+ constructor(apiRequestContext) {
5057
+ this.#apiRequestContext = apiRequestContext;
5058
+ this.#status = "normal";
5059
+ }
5060
+ async fetch(url, options = {}) {
5061
+ if (this.#status !== "normal") {
5062
+ throw new Error(`ApiContext has already been destroyed`);
5063
+ }
5064
+ const apiResponse = await this.#apiRequestContext.fetch(url, options);
5065
+ const headers = apiResponse.headers();
5066
+ const status = apiResponse.status();
5067
+ const statusText = apiResponse.statusText();
5068
+ const text = await apiResponse.text();
5069
+ const responseUrl = apiResponse.url();
5070
+ return { headers, status, statusText, text, url: responseUrl };
5071
+ }
5072
+ async stateData() {
5073
+ if (this.#status !== "normal") {
5074
+ throw new Error(`ApiContext has already been destroyed`);
5075
+ }
5076
+ const storageState = await this.#apiRequestContext.storageState();
5077
+ const { cookies, origins: localStorage } = storageState;
5078
+ return { cookies, localStorage };
5079
+ }
5080
+ async destroy() {
5081
+ await this.#apiRequestContext.dispose();
5082
+ this.#status = "destroyed";
5083
+ return true;
5084
+ }
5085
+ };
5086
+
5087
+ // src/patchright/context.ts
5088
+ var PatchrightBrowserContext = class extends EventEmitter9 {
5089
+ #lsdBrowser;
5090
+ #browserIdx;
5091
+ #browserContextIdx;
5092
+ #browserContext;
5093
+ #browserContextCreationMethod;
5094
+ #apiContext;
5095
+ #createTime;
5096
+ #lastStatusUpdateTime;
5097
+ #status;
5098
+ #incognito;
5099
+ #proxy;
5100
+ #maxPagesPerBrowserContext;
5101
+ #maxPageFreeSeconds;
5102
+ #maxViewportOfNewPage;
5103
+ #lsdPages;
5104
+ #nextPageIdx;
5105
+ #gettingPage;
5106
+ async #initPages() {
5107
+ if (!this.#browserContext) {
5108
+ throw new Error("Invalid browserContext");
5109
+ }
5110
+ const pages = this.#browserContext.pages();
5111
+ const openType = this.#lsdBrowser.browserCreationMethod();
5112
+ const lastStatusUpdateTime = getCurrentUnixTime8();
5113
+ for (const page of pages) {
5114
+ const pageInfo = { browserIdx: this.#browserIdx, browserContextIdx: this.#browserContextIdx, pageIdx: this.#nextPageIdx++, openType, openTime: this.#createTime, lastStatusUpdateTime, taskId: 0, relatedId: 0, misc: {} };
5115
+ const lsdPage = new PatchrightPage(this, page, pageInfo);
5116
+ if (this.#maxViewportOfNewPage) {
5117
+ await lsdPage.maximizeViewport();
5118
+ }
5119
+ this.#lsdPages.push(lsdPage);
5120
+ loginfo(`##browser ${lsdPage.id()} ${openType}ed`);
5121
+ }
5122
+ }
5123
+ constructor(lsdBrowser, browserContext, browserContextCreationMethod, incognito = false, proxy = null, browserIdx = 0, browserContextIdx = 0, maxPagesPerBrowserContext = 20, maxPageFreeSeconds = 0, maxViewportOfNewPage = true) {
5124
+ if (!lsdBrowser || typeof lsdBrowser.browserContexts !== "function") {
5125
+ throw new Error(`Invalid lsdBrowser parameter`);
5126
+ }
5127
+ if (!browserContext || typeof browserContext.setOffline !== "function") {
5128
+ throw new Error(`Invalid playwright browserContext parameter`);
5129
+ }
5130
+ super();
5131
+ this.#lsdBrowser = lsdBrowser;
5132
+ this.#browserIdx = browserIdx;
5133
+ this.#browserContextIdx = browserContextIdx;
5134
+ this.#browserContext = browserContext;
5135
+ this.#browserContextCreationMethod = browserContextCreationMethod;
5136
+ const apiRequestContext = browserContext.request;
5137
+ this.#apiContext = new PatchrightApiContext(apiRequestContext);
5138
+ const currentTime = getCurrentUnixTime8();
5139
+ this.#createTime = currentTime;
5140
+ this.#lastStatusUpdateTime = currentTime;
5141
+ this.#status = "free";
5142
+ this.#incognito = incognito === false ? false : true;
5143
+ this.#proxy = proxy?.proxyUrl ? proxy : null;
5144
+ this.#maxPagesPerBrowserContext = maxPagesPerBrowserContext;
5145
+ this.#maxPageFreeSeconds = maxPageFreeSeconds;
5146
+ this.#maxViewportOfNewPage = maxViewportOfNewPage;
5147
+ this.#lsdPages = [];
5148
+ this.#nextPageIdx = 1;
5149
+ this.#gettingPage = false;
5150
+ this.#initPages();
5151
+ browserContext.on("page", async (page) => {
5152
+ const pageInfo = page.pageInfo;
5153
+ if (pageInfo) {
5154
+ const { browserIdx: browserIdx2, browserContextIdx: browserContextIdx2, pageIdx } = pageInfo;
5155
+ logwarn(`##browser page-${browserIdx2}-${browserContextIdx2}-${pageIdx} has been already created`);
5156
+ } else {
5157
+ const currentTime2 = getCurrentUnixTime8();
5158
+ const pageInfo2 = { browserIdx: this.#browserIdx, browserContextIdx: this.#browserContextIdx, pageIdx: this.#nextPageIdx++, openType: "other", openTime: currentTime2, lastStatusUpdateTime: currentTime2, taskId: 0, relatedId: 0, misc: {} };
5159
+ const lsdPage = new PatchrightPage(this, page, pageInfo2);
5160
+ if (this.#maxViewportOfNewPage) {
5161
+ await lsdPage.maximizeViewport();
5162
+ }
5163
+ this.#lsdPages.push(lsdPage);
5164
+ loginfo(`##page ${lsdPage.id()} created`);
5165
+ }
5166
+ });
5167
+ browserContext.on("close", (bc) => {
5168
+ if (browserContext !== bc) {
5169
+ logerr(`##browser different browserContext in browserContext.on("close")`);
5170
+ }
5171
+ this.#lsdBrowser.emit("browserContextClose", this);
5172
+ });
5173
+ this.on("pageClose", (lsdPage) => {
5174
+ if (!(lsdPage instanceof PatchrightPage)) {
5175
+ logerr(`Invalid data in LsdBrowserContext.on("pageClose)`);
5176
+ return;
5177
+ }
5178
+ const idx = this.#lsdPages.findIndex((p) => p === lsdPage);
5179
+ if (idx < 0) {
5180
+ logerr(`Invalid lsdPage in LsdBrowserContext.on("pageClose)`);
5181
+ return;
5182
+ }
5183
+ this.#lsdPages.splice(idx, 1);
5184
+ return;
5185
+ });
5186
+ }
5187
+ apiContext() {
5188
+ return this.#apiContext;
5189
+ }
5190
+ browser() {
5191
+ return this.#lsdBrowser;
5192
+ }
5193
+ async close() {
5194
+ if (this.#browserContext) {
5195
+ this.#status = "closed";
5196
+ this.#lastStatusUpdateTime = getCurrentUnixTime8();
5197
+ loginfo(`browserContext ${this.id()} closed at ${this.#lastStatusUpdateTime}`);
5198
+ await this.#browserContext.close();
5199
+ }
5200
+ return true;
5201
+ }
5202
+ async #tryToGetGettingLock() {
5203
+ let i = 0;
5204
+ for (i = 0; i < 50; i++) {
5205
+ if (!this.#gettingPage) {
5206
+ this.#gettingPage = true;
5207
+ return true;
5208
+ } else {
5209
+ await sleep3(200);
5210
+ }
5211
+ }
5212
+ logwarn(`Cannot get the gettingLock.`);
5213
+ return false;
5214
+ }
5215
+ #freeGettingLock() {
5216
+ if (!this.#gettingPage) {
5217
+ logwarn(`Getting lock is already free now.`);
5218
+ }
5219
+ this.#gettingPage = false;
5220
+ }
5221
+ async closeFreePages(maxPageFreeSeconds = 0) {
5222
+ if (maxPageFreeSeconds <= 0) {
5223
+ maxPageFreeSeconds = this.#maxPageFreeSeconds;
5224
+ }
5225
+ if (maxPageFreeSeconds <= 0) {
5226
+ logwarn(`Please set valid maxPageFreeSeconds to close free pages`);
5227
+ return false;
5228
+ }
5229
+ const gotLock = await this.#tryToGetGettingLock();
5230
+ if (!gotLock) {
5231
+ return false;
5232
+ }
5233
+ try {
5234
+ const maxUpdateTime = getCurrentUnixTime8() - this.#maxPageFreeSeconds;
5235
+ let freePages = this.#lsdPages.filter((p) => p.isFree() && p.pageInfo().lastStatusUpdateTime < maxUpdateTime);
5236
+ if (freePages.length === this.#lsdPages.length) {
5237
+ freePages = freePages.slice(1);
5238
+ }
5239
+ for (const lsdPage of freePages) {
5240
+ await lsdPage.close();
5241
+ }
5242
+ this.#freeGettingLock();
5243
+ return true;
5244
+ } catch (err) {
5245
+ logerr(err);
5246
+ this.#freeGettingLock();
5247
+ return false;
5248
+ }
5249
+ }
5250
+ creationMethod() {
5251
+ return this.#browserContextCreationMethod;
5252
+ }
5253
+ doesMeetBrowserContextRequirements(browserContextRequirements) {
5254
+ if (!this.#lsdBrowser.doesMeetBrowserContextRequirements(browserContextRequirements)) {
5255
+ return false;
5256
+ }
5257
+ const { browserIncognitos: incognitos } = browserContextRequirements;
5258
+ return incognitos.length === 0 || incognitos.includes(this.#incognito);
5259
+ }
5260
+ async getPage(always = false) {
5261
+ if (!this.#browserContext) {
5262
+ throw new Error("Invalid browserContext");
5263
+ }
5264
+ const gotLock = await this.#tryToGetGettingLock();
5265
+ if (!gotLock) {
5266
+ return null;
5267
+ }
5268
+ try {
5269
+ let lsdPage = this.#lsdPages.find((p) => p.isFree());
5270
+ if (lsdPage) {
5271
+ lsdPage.use();
5272
+ this.#freeGettingLock();
5273
+ return lsdPage;
5274
+ }
5275
+ if (this.#lsdPages.length >= this.#maxPagesPerBrowserContext && !always) {
5276
+ this.#freeGettingLock();
5277
+ return null;
5278
+ }
5279
+ const page = await this.#browserContext.newPage();
5280
+ await sleep3(2e3);
5281
+ const pageInfo = page.pageInfo;
5282
+ if (!pageInfo) {
5283
+ throw new Error(`Logic error in getPage`);
5284
+ } else {
5285
+ pageInfo.openType = "newpage";
5286
+ }
5287
+ lsdPage = this.#lsdPages.find((p) => p.isFree());
5288
+ if (lsdPage) {
5289
+ lsdPage.use();
5290
+ this.#freeGettingLock();
5291
+ return lsdPage;
5292
+ } else {
5293
+ this.#freeGettingLock();
5294
+ return null;
5295
+ }
5296
+ } catch (err) {
5297
+ logerr(err);
5298
+ this.#freeGettingLock();
5299
+ return null;
5300
+ }
5301
+ }
5302
+ free(clearStateData = false) {
5303
+ if (this.#status === "busy") {
5304
+ this.#status = "free";
5305
+ this.#lastStatusUpdateTime = getCurrentUnixTime8();
5306
+ if (clearStateData) {
5307
+ }
5308
+ return true;
5309
+ } else {
5310
+ return false;
5311
+ }
5312
+ }
5313
+ hasFreePage(pageNum = 1) {
5314
+ if (this.#maxPagesPerBrowserContext - this.#lsdPages.length > pageNum) {
5315
+ return true;
5316
+ } else if (this.#maxPagesPerBrowserContext - this.#lsdPages.filter((p) => !p.isFree()).length > pageNum) {
5317
+ return true;
5318
+ } else {
5319
+ return false;
5320
+ }
5321
+ }
5322
+ id() {
5323
+ return `browserContext-${this.#browserIdx}-${this.#browserContextIdx}`;
5324
+ }
5325
+ isFree() {
5326
+ return this.#status === "free";
5327
+ }
5328
+ isIncognito() {
5329
+ return this.#incognito;
5330
+ }
5331
+ page(pageIdx) {
5332
+ const lsdPage = this.#lsdPages.find((p) => p.pageInfo().pageIdx === pageIdx);
5333
+ return lsdPage ? lsdPage : null;
5334
+ }
5335
+ pages() {
5336
+ return this.#lsdPages;
5337
+ }
5338
+ proxy() {
5339
+ return this.#proxy;
5340
+ }
5341
+ async setStateData(stateData) {
5342
+ if (!this.#browserContext) {
5343
+ throw new Error("No valid browserContext");
5344
+ }
5345
+ try {
5346
+ const { cookies, localStorage: localStorageOrigins } = stateData;
5347
+ const page = await this.getPage();
5348
+ if (!page) {
5349
+ return false;
5350
+ }
5351
+ const origPage = page._origPage();
5352
+ if (cookies.length > 0) {
5353
+ await this.#browserContext.addCookies(cookies);
5354
+ }
5355
+ if (localStorageOrigins.length > 0) {
5356
+ await origPage.route("**/*", async (route) => {
5357
+ const request = route.request();
5358
+ await route.fulfill({
5359
+ status: 200,
5360
+ // contentType: "text/html; charset=utf-8", // "text/plain",
5361
+ body: `<html><body><h1>${request.url()}</h1></body></html>`
5362
+ });
5363
+ });
5364
+ for (const localStorageOrigin of localStorageOrigins) {
5365
+ const { origin, localStorage } = localStorageOrigin;
5366
+ await origPage.goto(origin);
5367
+ await origPage.evaluate((localStorageItems) => {
5368
+ for (const item of localStorageItems) {
5369
+ window.localStorage.setItem(item.name, item.value);
5370
+ }
5371
+ }, localStorage);
5372
+ }
5373
+ }
5374
+ await sleep3(2e3);
5375
+ await origPage.unrouteAll();
5376
+ await page.free();
5377
+ return true;
5378
+ } catch (err) {
5379
+ logerr(err);
5380
+ return false;
5381
+ }
5382
+ }
5383
+ status() {
5384
+ return this.#status;
5385
+ }
5386
+ use() {
5387
+ if (this.#status === "free") {
5388
+ this.#status = "busy";
5389
+ this.#lastStatusUpdateTime = getCurrentUnixTime8();
5390
+ return true;
5391
+ } else {
5392
+ return false;
5393
+ }
5394
+ }
5395
+ _origBrowserContext() {
5396
+ return this.#browserContext;
5397
+ }
5398
+ };
5399
+
5400
+ // src/patchright/browser.ts
5401
+ var PatchrightBrowser = class _PatchrightBrowser extends EventEmitter10 {
5402
+ static #supportedBrowserTypes = ["chromium", "firefox", "webkit"];
5403
+ static doesSupport(browserType) {
5404
+ return _PatchrightBrowser.#supportedBrowserTypes.includes(browserType);
5405
+ }
5406
+ #browser;
5407
+ #browserIdx;
5408
+ #pid;
5409
+ #createTime;
5410
+ #lsdBrowserContexts;
5411
+ #browserControllerType;
5412
+ #browserType;
5413
+ #browserCreationMethod;
5414
+ #headless;
5415
+ #options;
5416
+ #proxy;
5417
+ /**
5418
+ * launch: actual path of executable app
5419
+ * connect: ""
5420
+ */
5421
+ #executablePath;
5422
+ #nextBrowserContextIdx;
5423
+ #closeFreePagesIntervalId;
5424
+ #maxBrowserContextsPerBrowser() {
5425
+ return this.#options.maxBrowserContextsPerBrowser ? this.#options.maxBrowserContextsPerBrowser : 10;
5426
+ }
5427
+ #maxPagesPerBrowserContext() {
5428
+ return this.#options.maxPagesPerBrowserContext ? this.#options.maxPagesPerBrowserContext : 20;
5429
+ }
5430
+ #maxPageFreeSeconds() {
5431
+ return this.#options.maxPageFreeSeconds ? this.#options.maxPageFreeSeconds : 900;
5432
+ }
5433
+ // constructor: called only by LsdBrowserController.launch/connect
5434
+ constructor(browser, browserType, browserCreateMethod, options, browserIdx = 0, pid = 0) {
5435
+ if (!browser || typeof browser.contexts !== "function") {
5436
+ throw new Error(`Invalid playwright browser parameter`);
5437
+ }
5438
+ super();
5439
+ const { closeFreePagesIntervalSeconds = 300, maxPageFreeSeconds = 900, maxViewportOfNewPage = true, headless = false, executablePath = "" } = options;
5440
+ this.#browser = browser;
5441
+ this.#browserIdx = browserIdx;
5442
+ this.#pid = pid;
5443
+ this.#createTime = getCurrentUnixTime9();
5444
+ this.#lsdBrowserContexts = [];
5445
+ this.#browserControllerType = "playwright";
5446
+ this.#browserType = browserType;
5447
+ if (!_PatchrightBrowser.#supportedBrowserTypes.includes(browserType)) {
5448
+ throw new Error(`Browser controller ${this.#browserControllerType} doesnot support browserType ${browserType}`);
5449
+ }
5450
+ this.#browserCreationMethod = browserCreateMethod;
5451
+ this.#headless = headless;
5452
+ this.#proxy = options?.proxy ? Object.assign({}, options.proxy) : null;
5453
+ this.#options = Object.assign({}, options, { closeFreePagesIntervalSeconds, maxPageFreeSeconds, maxViewportOfNewPage, headless, executablePath, proxy: this.#proxy });
5454
+ this.#executablePath = executablePath;
5455
+ this.#nextBrowserContextIdx = 1;
5456
+ this.#closeFreePagesIntervalId = null;
5457
+ loginfo(`##browser ${this.id()} ${this.#browserCreationMethod}ed by ${this.#browserControllerType}`);
5458
+ const browserContexts = browser.contexts();
5459
+ if (browserContexts.length > 0) {
5460
+ logwarn(`There are ${browserContexts.length} new browserContexts when playwright launches new browser`);
5461
+ }
5462
+ const incognito = typeof options?.incognito === "boolean" ? options.incognito : true;
5463
+ for (const browserContext of browserContexts) {
5464
+ const lsdBrowserContext = new PatchrightBrowserContext(this, browserContext, "launch", incognito, this.#proxy, this.#browserIdx, this.#nextBrowserContextIdx++, this.#maxPagesPerBrowserContext(), this.#maxPageFreeSeconds(), maxViewportOfNewPage);
5465
+ this.#lsdBrowserContexts.push(lsdBrowserContext);
5466
+ loginfo(`##browserContext ${lsdBrowserContext.id()} ${this.#browserCreationMethod}ed`);
5467
+ }
5468
+ browser.on("disconnected", () => {
5469
+ loginfo(`##browser ${this.id()} disconnected`);
5470
+ if (this.#lsdBrowserContexts.length > 0) {
5471
+ logerr(`${this.id()} has browserContexts when disconnected`);
5472
+ }
5473
+ });
5474
+ this.on("browserContextClose", (lsdBrowserContext) => {
5475
+ if (!(lsdBrowserContext instanceof PatchrightBrowserContext)) {
5476
+ logerr(`Invalid data in LsdBrowser.on("browserContextClose)`);
5477
+ return;
5478
+ }
5479
+ const idx = this.#lsdBrowserContexts.findIndex((bc) => bc === lsdBrowserContext);
5480
+ if (idx < 0) {
5481
+ logerr(`Invalid lsdBrowserContext in LsdBrowser.on("browserContextClose)`);
5482
+ return;
5483
+ }
5484
+ loginfo(`##browserContext ${lsdBrowserContext.id()} closed
5485
+ `);
5486
+ this.#lsdBrowserContexts.splice(idx, 1);
5487
+ if (this.#lsdBrowserContexts.length === 0) {
5488
+ loginfo(`##browser ${this.id()} has no browserContexts now`);
5489
+ }
5490
+ return;
5491
+ });
5492
+ if (closeFreePagesIntervalSeconds > 0 && maxPageFreeSeconds > 0) {
5493
+ this.#closeFreePagesIntervalId = setInterval(async () => {
5494
+ await this.#closeFreePagesHandler();
5495
+ }, closeFreePagesIntervalSeconds * 1e3);
5496
+ }
5497
+ }
5498
+ async #closeFreePagesHandler() {
5499
+ for (const lsdBrowserContext of this.#lsdBrowserContexts) {
5500
+ await lsdBrowserContext.closeFreePages();
5501
+ }
5502
+ }
5503
+ async newBrowserContext(options) {
5504
+ if (this.#lsdBrowserContexts.length >= this.#maxBrowserContextsPerBrowser()) {
5505
+ logwarn(`##browser ${this.id()} can not create more new browserContext`);
5506
+ return null;
5507
+ }
5508
+ const browserContextOptions = {};
5509
+ if (this.#options.maxWindowSize) {
5510
+ browserContextOptions.viewport = null;
5511
+ }
5512
+ const proxy = options?.proxy ? Object.assign({}, options.proxy) : this.#proxy;
5513
+ if (proxy) {
5514
+ const { proxyUrl: server, username, password } = proxy;
5515
+ browserContextOptions.proxy = { server, username, password };
5516
+ }
5517
+ if (options?.userAgent) {
5518
+ browserContextOptions.userAgent = options.userAgent;
5519
+ }
5520
+ const browserContext = await this.#browser.newContext(browserContextOptions);
5521
+ const { maxViewportOfNewPage = this.#options.maxViewportOfNewPage } = options ? options : {};
5522
+ const lsdBrowserContext = new PatchrightBrowserContext(this, browserContext, "new", true, proxy, this.#browserIdx, this.#nextBrowserContextIdx++, this.#maxPagesPerBrowserContext(), this.#maxPageFreeSeconds(), maxViewportOfNewPage);
5523
+ this.#lsdBrowserContexts.push(lsdBrowserContext);
5524
+ loginfo(`##browser ${lsdBrowserContext.id()} created`);
5525
+ return lsdBrowserContext;
5526
+ }
5527
+ async close() {
5528
+ if (this.#closeFreePagesIntervalId) {
5529
+ clearInterval(this.#closeFreePagesIntervalId);
5530
+ }
5531
+ for (const lsdBrowserContext of this.#lsdBrowserContexts) {
5532
+ await lsdBrowserContext.close();
5533
+ }
5534
+ await this.#browser.close();
5535
+ return true;
5536
+ }
5537
+ browserContexts() {
5538
+ return this.#lsdBrowserContexts;
5539
+ }
5540
+ browserControllerType() {
5541
+ return this.#browserControllerType;
5542
+ }
5543
+ browserCreationMethod() {
5544
+ return this.#browserCreationMethod;
5545
+ }
5546
+ browserType() {
5547
+ return this.#browserType;
5548
+ }
5549
+ createTime() {
5550
+ return this.#createTime;
5551
+ }
5552
+ doesMeetBrowserContextRequirements(browserContextRequirements) {
5553
+ const { browserControllerTypes, browserTypes, browserHeadlesses } = browserContextRequirements;
5554
+ return (browserControllerTypes.length === 0 || browserControllerTypes.includes(this.#browserControllerType)) && (browserTypes.length === 0 || browserTypes.includes(this.#browserType)) && (browserHeadlesses.length === 0 || browserHeadlesses.includes(this.#headless));
5555
+ }
5556
+ executablePath() {
5557
+ return this.#executablePath;
5558
+ }
5559
+ id() {
5560
+ return `browser-${this.#browserType}-${this.#browserIdx}`;
5561
+ }
5562
+ isConnected() {
5563
+ return this.#browser.isConnected();
5564
+ }
5565
+ isHeadless() {
5566
+ return this.#headless;
5567
+ }
5568
+ options() {
5569
+ return this.#options;
5570
+ }
5571
+ pid() {
5572
+ return this.#pid;
5573
+ }
5574
+ async pidUsage() {
5575
+ if (this.#pid > 0) {
5576
+ const usage = await getPerformanceOfPidTree3(this.#pid, "MB");
5577
+ return usage;
5578
+ } else {
5579
+ return { cpu: 0, memory: 0 };
5580
+ }
5581
+ }
5582
+ proxy() {
5583
+ return this.#proxy;
5584
+ }
5585
+ async version() {
5586
+ const version = await this.#browser.version();
5587
+ return version;
5588
+ }
5589
+ _origBrowser() {
5590
+ return this.#browser;
5591
+ }
5592
+ };
5593
+
5594
+ // src/controller/controller.ts
5595
+ import { getPidsListeningOnPort, unreachable as unreachable7 } from "@letsscrapedata/utils";
5596
+ var LsdBrowserController = class _LsdBrowserController {
5597
+ static #forbidConstructor = false;
5598
+ #puppeteer;
5599
+ #playwrightBrowserTypes;
5600
+ #patchrightBrowserTypes;
5601
+ #nextBrowserIdx;
5602
+ /**
5603
+ * Possible values are 'aix', 'darwin', 'freebsd','linux', 'openbsd', 'sunos', and 'win32'.
5604
+ */
5605
+ #osPlatform;
5606
+ constructor() {
5607
+ if (_LsdBrowserController.#forbidConstructor) {
5608
+ throw new Error("Only one LsdBrowserController instance can be created!");
5609
+ }
5610
+ this.#puppeteer = puppeteer;
5611
+ this.#playwrightBrowserTypes = {
5612
+ chromium: playwright.chromium,
5613
+ firefox: playwright.firefox,
5614
+ webkit: playwright.webkit
5615
+ };
5616
+ this.#patchrightBrowserTypes = {
5617
+ chromium: patchright.chromium,
5618
+ firefox: patchright.firefox,
5619
+ webkit: patchright.webkit
5620
+ };
5621
+ this.#osPlatform = os.platform();
5622
+ this.#nextBrowserIdx = 1;
5623
+ _LsdBrowserController.#forbidConstructor = true;
5624
+ }
5625
+ #playwrightBrowserType(browserType, connectFlag = false) {
5626
+ if (browserType === "chromium") {
5627
+ return this.#playwrightBrowserTypes.chromium;
5628
+ } else if (connectFlag) {
5629
+ throw new Error(`playwright only can connect to chromium browser, not support ${browserType} browser`);
5630
+ } else if (browserType === "firefox") {
5631
+ return this.#playwrightBrowserTypes.firefox;
5632
+ } else if (browserType === "webkit") {
5633
+ return this.#playwrightBrowserTypes.webkit;
5634
+ } else {
5635
+ throw new Error(`Invalid playwright browserType ${browserType}`);
5636
+ }
5637
+ }
5638
+ #patchrightBrowserType(browserType, connectFlag = false) {
5639
+ if (browserType === "chromium") {
5640
+ return this.#patchrightBrowserTypes.chromium;
5641
+ } else if (connectFlag) {
5642
+ throw new Error(`patchright only can connect to chromium browser, not support ${browserType} browser`);
5643
+ } else if (browserType === "firefox") {
5644
+ return this.#patchrightBrowserTypes.firefox;
5645
+ } else if (browserType === "webkit") {
5646
+ return this.#patchrightBrowserTypes.webkit;
5647
+ } else {
5648
+ throw new Error(`Invalid patchright browserType ${browserType}`);
5649
+ }
5650
+ }
5651
+ #puppeteerProduct(browserType) {
5652
+ if (browserType === "chromium") {
5653
+ return "chrome";
5654
+ } else {
5655
+ throw new Error(`Invalid puppeteer product ${browserType}`);
5656
+ }
5657
+ }
5658
+ setBrowserPlugin(browserControllerType, browserType, plugin) {
5659
+ if (browserControllerType === "puppeteer") {
5660
+ if (!PuppeteerBrowser.doesSupport(browserType)) {
5661
+ throw new Error(`BrowserControllerType ${browserControllerType} doesnot support browserType ${browserType}`);
5662
+ }
5663
+ this.#puppeteer = plugin;
5664
+ } else if (browserControllerType === "playwright") {
5665
+ if (!PlaywrightBrowser.doesSupport(browserType)) {
5666
+ throw new Error(`BrowserControllerType ${browserControllerType} doesnot support browserType ${browserType}`);
5667
+ }
5668
+ switch (browserType) {
5669
+ case "chromium":
5670
+ case "firefox":
5671
+ case "webkit":
5672
+ this.#playwrightBrowserTypes[browserType] = plugin;
5673
+ break;
5674
+ default:
5675
+ unreachable7(browserType);
5676
+ }
5677
+ } else if (browserControllerType === "patchright") {
5678
+ if (!PatchrightBrowser.doesSupport(browserType)) {
5679
+ throw new Error(`BrowserControllerType ${browserControllerType} doesnot support browserType ${browserType}`);
5680
+ }
5681
+ switch (browserType) {
5682
+ case "chromium":
5683
+ case "firefox":
5684
+ case "webkit":
5685
+ this.#patchrightBrowserTypes[browserType] = plugin;
5686
+ break;
5687
+ default:
5688
+ unreachable7(browserType);
5689
+ }
5690
+ } else {
5691
+ unreachable7(browserControllerType);
5692
+ }
5693
+ return true;
5694
+ }
5695
+ async launch(browserControllerType, browserType, options) {
5696
+ let {
5697
+ closeFreePagesIntervalSeconds = 300,
5698
+ maxBrowserContextsPerBrowser = 10,
5699
+ maxPagesPerBrowserContext = 20,
5700
+ maxPageFreeSeconds = 900,
5701
+ maxViewportOfNewPage = true,
5702
+ proxy = null,
5703
+ timeout = 3e4,
5704
+ args = [],
5705
+ executablePath = "",
5706
+ maxWindowSize = true,
5707
+ headless = true,
5708
+ minBrowserContexts = 1,
5709
+ // incognito
5710
+ proxyPerBrowserContext = false,
5711
+ userDataDir = "",
5712
+ userAgent = ""
5713
+ } = options ? options : {};
5714
+ let browserPid = 0;
5715
+ const incognito = typeof options?.incognito === "boolean" ? options.incognito : browserControllerType === "puppeteer" ? false : true;
5716
+ const actOptions = { closeFreePagesIntervalSeconds, maxBrowserContextsPerBrowser, maxPagesPerBrowserContext, maxPageFreeSeconds, maxViewportOfNewPage, proxy, timeout, args, executablePath, maxWindowSize, headless, minBrowserContexts, incognito, proxyPerBrowserContext, userDataDir, userAgent };
5717
+ let idx = args.findIndex((arg) => arg.toLowerCase().startsWith("--incoginto"));
5718
+ if (idx >= 0) {
5719
+ logwarn(`Please use options.incognito instead when launching new browser.`);
5720
+ args.splice(idx, 1);
5721
+ }
5722
+ idx = args.findIndex((arg) => arg.toLowerCase().startsWith("--proxy-server"));
5723
+ if (idx >= 0) {
5724
+ logwarn(`Please use options.proxy instead when launching new browser.`);
5725
+ args.splice(idx, 1);
5726
+ }
5727
+ idx = args.findIndex((arg) => arg.toLowerCase().startsWith("--user-data-dir"));
5728
+ if (idx >= 0) {
5729
+ logwarn(`Please use options.userDataDir instead when launching new browser.`);
5730
+ args.splice(idx, 1);
5731
+ }
5732
+ idx = args.findIndex((arg) => arg.toLowerCase().startsWith("--start-maximized"));
5733
+ if (idx >= 0) {
5734
+ logwarn(`Please use options.maxWindowSize instead when launching new browser.`);
5735
+ args.splice(idx, 1);
5736
+ }
5737
+ let lsdBrowser;
5738
+ if (browserControllerType === "playwright") {
5739
+ const launchOptions = { headless, timeout };
5740
+ if (executablePath) {
5741
+ launchOptions.executablePath = executablePath;
5742
+ }
5743
+ if (maxWindowSize) {
5744
+ args.push("--start-maximized");
5745
+ }
5746
+ if (proxy?.proxyUrl && proxy.proxyUrl !== "local") {
5747
+ const { proxyUrl: server, username, password } = proxy;
5748
+ launchOptions.proxy = { server, username, password };
5749
+ } else if (proxyPerBrowserContext && browserType === "chromium" && this.#osPlatform.startsWith("win")) {
5750
+ launchOptions.proxy = { server: "proxyPerBrowserContext" };
5751
+ }
5752
+ if (browserType === "chromium") {
5753
+ if (incognito) {
5754
+ args.push("--incognito");
5755
+ } else if (userDataDir) {
5756
+ args.push(`--user-data-dir=${userDataDir}`);
3969
5757
  }
3970
5758
  }
3971
5759
  if (args.length > 0) {
@@ -3988,6 +5776,47 @@ var LsdBrowserController = class _LsdBrowserController {
3988
5776
  const browser = await playwrightBrowserType.launch(launchOptions);
3989
5777
  lsdBrowser = new PlaywrightBrowser(browser, browserType, "launch", actOptions, this.#nextBrowserIdx++, browserPid);
3990
5778
  }
5779
+ } else if (browserControllerType === "patchright") {
5780
+ const launchOptions = { headless, timeout };
5781
+ if (executablePath) {
5782
+ launchOptions.executablePath = executablePath;
5783
+ }
5784
+ if (maxWindowSize) {
5785
+ args.push("--start-maximized");
5786
+ }
5787
+ if (proxy?.proxyUrl && proxy.proxyUrl !== "local") {
5788
+ const { proxyUrl: server, username, password } = proxy;
5789
+ launchOptions.proxy = { server, username, password };
5790
+ } else if (proxyPerBrowserContext && browserType === "chromium" && this.#osPlatform.startsWith("win")) {
5791
+ launchOptions.proxy = { server: "proxyPerBrowserContext" };
5792
+ }
5793
+ if (browserType === "chromium") {
5794
+ if (incognito) {
5795
+ args.push("--incognito");
5796
+ } else if (userDataDir) {
5797
+ args.push(`--user-data-dir=${userDataDir}`);
5798
+ }
5799
+ }
5800
+ if (args.length > 0) {
5801
+ launchOptions.args = args;
5802
+ }
5803
+ const patchrightBrowserType = this.#patchrightBrowserType(browserType);
5804
+ if (!actOptions.executablePath) {
5805
+ actOptions.executablePath = patchrightBrowserType.executablePath();
5806
+ }
5807
+ if (options.launchMethod === "launchServer") {
5808
+ const browserServer = await patchrightBrowserType.launchServer(launchOptions);
5809
+ const process = browserServer.process();
5810
+ if (process?.pid) {
5811
+ browserPid = process.pid;
5812
+ }
5813
+ const wsEndpoint = browserServer.wsEndpoint();
5814
+ const browser = await patchrightBrowserType.connect(wsEndpoint);
5815
+ lsdBrowser = new PatchrightBrowser(browser, browserType, "launch", actOptions, this.#nextBrowserIdx++, browserPid);
5816
+ } else {
5817
+ const browser = await patchrightBrowserType.launch(launchOptions);
5818
+ lsdBrowser = new PatchrightBrowser(browser, browserType, "launch", actOptions, this.#nextBrowserIdx++, browserPid);
5819
+ }
3991
5820
  } else if (browserControllerType === "puppeteer") {
3992
5821
  const product = this.#puppeteerProduct(browserType);
3993
5822
  const launchOptions = { headless, timeout, product };
@@ -4066,6 +5895,11 @@ var LsdBrowserController = class _LsdBrowserController {
4066
5895
  const browser = await playwrightBrowserType.connectOverCDP(browserUrl);
4067
5896
  const lsdBrowser = new PlaywrightBrowser(browser, browserType, "connect", actOptions, this.#nextBrowserIdx++, browserPid);
4068
5897
  return lsdBrowser;
5898
+ } else if (browserControllerType === "patchright") {
5899
+ const patchrightBrowserType = this.#patchrightBrowserType(browserType, true);
5900
+ const browser = await patchrightBrowserType.connectOverCDP(browserUrl);
5901
+ const lsdBrowser = new PatchrightBrowser(browser, browserType, "connect", actOptions, this.#nextBrowserIdx++, browserPid);
5902
+ return lsdBrowser;
4069
5903
  } else if (browserControllerType === "puppeteer") {
4070
5904
  this.#puppeteerProduct(browserType);
4071
5905
  const browser = await this.#puppeteer.connect({ browserURL: browserUrl });
@@ -4078,7 +5912,7 @@ var LsdBrowserController = class _LsdBrowserController {
4078
5912
  }
4079
5913
  async newApiContext(options = {}) {
4080
5914
  const { proxy, stateData, userAgent, timeout, ignoreHTTPSErrors, extraHTTPHeaders, httpCredentials } = options;
4081
- const apiRequestContext = await apiRequest.newContext({
5915
+ const apiRequestContext = await apiRequestInPlaywright.newContext({
4082
5916
  proxy: proxy ? { server: proxy.proxyUrl, username: proxy.username, password: proxy.password } : void 0,
4083
5917
  storageState: stateData ? { cookies: stateData.cookies, origins: stateData.localStorage } : void 0,
4084
5918
  userAgent,