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