@xbrowser/cli 1.33.0 → 1.35.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/{browser-3LBSVDSS.js → browser-KY7AIRAB.js} +3 -2
- package/dist/{browser-23BMBCH2.js → browser-MLKP72XP.js} +3 -3
- package/dist/{browser-D7RFPTD4.js → browser-RZ7DPBI3.js} +3 -3
- package/dist/cdp-driver-CCG3OJUQ.js +49 -0
- package/dist/{cdp-driver-B5KVVI4O.js → cdp-driver-HBMCD7NU.js} +2 -2
- package/dist/{cdp-driver-GE6Y6LPI.js → cdp-driver-MJD7YIQI.js} +2 -2
- package/dist/{cdp-driver-6G77XM4H.js → cdp-driver-YJU4GRSV.js} +203 -95
- package/dist/chunk-3SZTCI33.js +147 -0
- package/dist/{chunk-OVAUGFM6.js → chunk-43U5VDLB.js} +8 -2
- package/dist/chunk-53VZHESL.js +150 -0
- package/dist/{chunk-NAIHOVDZ.js → chunk-63526WET.js} +2 -8
- package/dist/{chunk-YX5KVWGX.js → chunk-6DS3T6UK.js} +203 -95
- package/dist/{chunk-DDHHVII7.js → chunk-BLCBCESM.js} +8 -2
- package/dist/{chunk-SH6OTPXN.js → chunk-C5ZPXPD7.js} +31 -7
- package/dist/chunk-EGOWDZXP.js +149 -0
- package/dist/chunk-GNLZWHSP.js +1380 -0
- package/dist/{chunk-5MYC6LVL.js → chunk-GYGVHWG3.js} +279 -1460
- package/dist/{chunk-2KPI5GKP.js → chunk-IYCB2KJL.js} +2 -8
- package/dist/chunk-JYYKMSFH.js +39 -0
- package/dist/{chunk-TBJUSUKF.js → chunk-NBT2CE3H.js} +140 -96
- package/dist/{chunk-ACPMC47K.js → chunk-QRFM4NOV.js} +1025 -202
- package/dist/chunk-RGL2YTWQ.js +64 -0
- package/dist/{chunk-BND5O5KC.js → chunk-SQLICOPZ.js} +177 -240
- package/dist/{chunk-O5PASNI7.js → chunk-UUW5EGES.js} +139 -237
- package/dist/{chunk-43TEMG4U.js → chunk-WGS3Y6ZV.js} +3 -1
- package/dist/{chunk-AKT3WLZM.js → chunk-Y2GRCINH.js} +1004 -183
- package/dist/{chunk-H25ECCYJ.js → chunk-ZCZELQCS.js} +203 -95
- package/dist/cli.js +2237 -278
- package/dist/{daemon-client-7D6EZNOE.js → daemon-client-OXKRGLPQ.js} +32 -8
- package/dist/{daemon-client-HEGAXWGK.js → daemon-client-Z3NKNZRL.js} +5 -1
- package/dist/daemon-main.js +2214 -159
- package/dist/human-interaction-BGAZC3JR.js +12 -0
- package/dist/human-interaction-TPKPEE7A.js +11 -0
- package/dist/human-interaction-ZSD3IX6Q.js +12 -0
- package/dist/index.d.ts +225 -94
- package/dist/index.js +2880 -267
- package/dist/{launcher-TOLF23ET.js → launcher-AUJK5QQM.js} +1 -1
- package/dist/{launcher-ZYB32LSZ.js → launcher-QBAZ6F5L.js} +1 -1
- package/dist/mistake-ledger-2B4N54CO.js +29 -0
- package/dist/{recovery-ORHJSBDN.js → recovery-5AGY7CV3.js} +4 -5
- package/dist/{recovery-JFGV3DBX.js → recovery-7NRRHLS4.js} +4 -4
- package/dist/{recovery-XDKN63O5.js → recovery-TIBRFGRP.js} +4 -4
- package/dist/run-routes-SJKVZ34L.js +26 -0
- package/dist/run-routes-TFNV5BBR.js +24 -0
- package/dist/{server-5JHB4TML.js → server-HUSX7EFE.js} +5 -4
- package/dist/{server-6CTC3RHV.js → server-KSP5H4HD.js} +5 -3
- package/dist/{session-replayer-IGRRA2QC.js → session-replayer-4TCLQI76.js} +1 -1
- package/dist/viewer-routes-YOS5GTCV.js +11 -0
- package/package.json +1 -1
- package/dist/chunk-QCV2PI7U.js +0 -184
- package/dist/human-interaction-ISMMOJOK.js +0 -8
- package/dist/human-interaction-STTZZZ5N.js +0 -8
- package/dist/human-interaction-UXFYV7DU.js +0 -8
|
@@ -21,9 +21,10 @@ import {
|
|
|
21
21
|
saveSessionDiskMeta,
|
|
22
22
|
setActivePage,
|
|
23
23
|
touchSession
|
|
24
|
-
} from "./chunk-
|
|
24
|
+
} from "./chunk-GNLZWHSP.js";
|
|
25
|
+
import "./chunk-GYGVHWG3.js";
|
|
25
26
|
import "./chunk-SMYSNO4M.js";
|
|
26
|
-
import "./chunk-
|
|
27
|
+
import "./chunk-IYCB2KJL.js";
|
|
27
28
|
import "./chunk-GDKLH7ZY.js";
|
|
28
29
|
import "./chunk-KFQGP6VL.js";
|
|
29
30
|
export {
|
|
@@ -21,11 +21,11 @@ import {
|
|
|
21
21
|
saveSessionDiskMeta,
|
|
22
22
|
setActivePage,
|
|
23
23
|
touchSession
|
|
24
|
-
} from "./chunk-
|
|
25
|
-
import "./chunk-
|
|
24
|
+
} from "./chunk-43U5VDLB.js";
|
|
25
|
+
import "./chunk-ZCZELQCS.js";
|
|
26
26
|
import "./chunk-A7NEA4PA.js";
|
|
27
27
|
import "./chunk-SMYSNO4M.js";
|
|
28
|
-
import "./chunk-
|
|
28
|
+
import "./chunk-IYCB2KJL.js";
|
|
29
29
|
import "./chunk-GDKLH7ZY.js";
|
|
30
30
|
import "./chunk-KFQGP6VL.js";
|
|
31
31
|
export {
|
|
@@ -21,10 +21,10 @@ import {
|
|
|
21
21
|
saveSessionDiskMeta,
|
|
22
22
|
setActivePage,
|
|
23
23
|
touchSession
|
|
24
|
-
} from "./chunk-
|
|
25
|
-
import "./chunk-
|
|
24
|
+
} from "./chunk-BLCBCESM.js";
|
|
25
|
+
import "./chunk-6DS3T6UK.js";
|
|
26
26
|
import "./chunk-SMYSNO4M.js";
|
|
27
|
-
import "./chunk-
|
|
27
|
+
import "./chunk-IYCB2KJL.js";
|
|
28
28
|
import "./chunk-GDKLH7ZY.js";
|
|
29
29
|
import "./chunk-NEE6EMYA.js";
|
|
30
30
|
import "./chunk-6UJU2FJK.js";
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
import {
|
|
2
|
+
CDPConnection,
|
|
3
|
+
CDPProtocolError,
|
|
4
|
+
XBBrowserImpl,
|
|
5
|
+
XBCDPSessionImpl,
|
|
6
|
+
XBContextImpl,
|
|
7
|
+
XBElementHandleImpl,
|
|
8
|
+
XBKeyboardImpl,
|
|
9
|
+
XBLocatorImpl,
|
|
10
|
+
XBMouseImpl,
|
|
11
|
+
XBPageImpl,
|
|
12
|
+
checkActionable,
|
|
13
|
+
launch,
|
|
14
|
+
scrollIntoView,
|
|
15
|
+
waitForActionable,
|
|
16
|
+
waitForNetworkIdle
|
|
17
|
+
} from "./chunk-GYGVHWG3.js";
|
|
18
|
+
import "./chunk-SMYSNO4M.js";
|
|
19
|
+
import {
|
|
20
|
+
connectToCDP,
|
|
21
|
+
findChrome,
|
|
22
|
+
getCDPTargets,
|
|
23
|
+
killChrome,
|
|
24
|
+
launchChrome
|
|
25
|
+
} from "./chunk-IYCB2KJL.js";
|
|
26
|
+
import "./chunk-GDKLH7ZY.js";
|
|
27
|
+
import "./chunk-KFQGP6VL.js";
|
|
28
|
+
export {
|
|
29
|
+
CDPConnection,
|
|
30
|
+
CDPProtocolError,
|
|
31
|
+
XBBrowserImpl,
|
|
32
|
+
XBCDPSessionImpl,
|
|
33
|
+
XBContextImpl,
|
|
34
|
+
XBElementHandleImpl,
|
|
35
|
+
XBKeyboardImpl,
|
|
36
|
+
XBLocatorImpl,
|
|
37
|
+
XBMouseImpl,
|
|
38
|
+
XBPageImpl,
|
|
39
|
+
checkActionable,
|
|
40
|
+
connectToCDP,
|
|
41
|
+
findChrome,
|
|
42
|
+
getCDPTargets,
|
|
43
|
+
killChrome,
|
|
44
|
+
launch,
|
|
45
|
+
launchChrome,
|
|
46
|
+
scrollIntoView,
|
|
47
|
+
waitForActionable,
|
|
48
|
+
waitForNetworkIdle
|
|
49
|
+
};
|
|
@@ -14,7 +14,7 @@ import {
|
|
|
14
14
|
scrollIntoView,
|
|
15
15
|
waitForActionable,
|
|
16
16
|
waitForNetworkIdle
|
|
17
|
-
} from "./chunk-
|
|
17
|
+
} from "./chunk-ZCZELQCS.js";
|
|
18
18
|
import "./chunk-A7NEA4PA.js";
|
|
19
19
|
import "./chunk-SMYSNO4M.js";
|
|
20
20
|
import {
|
|
@@ -23,7 +23,7 @@ import {
|
|
|
23
23
|
getCDPTargets,
|
|
24
24
|
killChrome,
|
|
25
25
|
launchChrome
|
|
26
|
-
} from "./chunk-
|
|
26
|
+
} from "./chunk-IYCB2KJL.js";
|
|
27
27
|
import "./chunk-GDKLH7ZY.js";
|
|
28
28
|
import "./chunk-KFQGP6VL.js";
|
|
29
29
|
export {
|
|
@@ -14,7 +14,7 @@ import {
|
|
|
14
14
|
scrollIntoView,
|
|
15
15
|
waitForActionable,
|
|
16
16
|
waitForNetworkIdle
|
|
17
|
-
} from "./chunk-
|
|
17
|
+
} from "./chunk-6DS3T6UK.js";
|
|
18
18
|
import "./chunk-SMYSNO4M.js";
|
|
19
19
|
import {
|
|
20
20
|
connectToCDP,
|
|
@@ -22,7 +22,7 @@ import {
|
|
|
22
22
|
getCDPTargets,
|
|
23
23
|
killChrome,
|
|
24
24
|
launchChrome
|
|
25
|
-
} from "./chunk-
|
|
25
|
+
} from "./chunk-IYCB2KJL.js";
|
|
26
26
|
import "./chunk-GDKLH7ZY.js";
|
|
27
27
|
import "./chunk-6UJU2FJK.js";
|
|
28
28
|
import "./chunk-KFQGP6VL.js";
|
|
@@ -13,7 +13,7 @@ import {
|
|
|
13
13
|
getCDPTargets,
|
|
14
14
|
killChrome,
|
|
15
15
|
launchChrome
|
|
16
|
-
} from "./chunk-
|
|
16
|
+
} from "./chunk-63526WET.js";
|
|
17
17
|
import {
|
|
18
18
|
__require
|
|
19
19
|
} from "./chunk-3RG5ZIWI.js";
|
|
@@ -1881,6 +1881,13 @@ function setupPageEvents(options) {
|
|
|
1881
1881
|
options.emit("framenavigated", options.mainFrame());
|
|
1882
1882
|
})
|
|
1883
1883
|
);
|
|
1884
|
+
options.addSubscription(
|
|
1885
|
+
options.conn.subscribe("Page.navigatedWithinDocument", options.sessionId, (params) => {
|
|
1886
|
+
const p = params;
|
|
1887
|
+
const url = p?.url ?? p?.frame?.url;
|
|
1888
|
+
if (url) options.setUrl(url);
|
|
1889
|
+
})
|
|
1890
|
+
);
|
|
1884
1891
|
options.addSubscription(
|
|
1885
1892
|
options.conn.subscribe("Page.loadEventFired", options.sessionId, () => {
|
|
1886
1893
|
options.markLoadFired();
|
|
@@ -2189,6 +2196,15 @@ var NavigationService = class {
|
|
|
2189
2196
|
if (result.errorText) {
|
|
2190
2197
|
throw new Error(`Navigation failed: ${result.errorText}`);
|
|
2191
2198
|
}
|
|
2199
|
+
if (!result.loaderId) {
|
|
2200
|
+
this.options.setUrl(url);
|
|
2201
|
+
return {
|
|
2202
|
+
status: () => 200,
|
|
2203
|
+
ok: () => true,
|
|
2204
|
+
url: () => url,
|
|
2205
|
+
headers: () => ({})
|
|
2206
|
+
};
|
|
2207
|
+
}
|
|
2192
2208
|
if (waitUntil === "commit") {
|
|
2193
2209
|
this.options.setUrl(url);
|
|
2194
2210
|
return {
|
|
@@ -2211,42 +2227,41 @@ var NavigationService = class {
|
|
|
2211
2227
|
};
|
|
2212
2228
|
}
|
|
2213
2229
|
async goBack(opts = {}) {
|
|
2214
|
-
|
|
2215
|
-
const navHistory = await this.options.conn.send("Page.getNavigationHistory", void 0, this.options.sessionId);
|
|
2216
|
-
if (navHistory.currentIndex > 0) {
|
|
2217
|
-
const prevUrl = navHistory.entries[navHistory.currentIndex - 1]?.url;
|
|
2218
|
-
if (prevUrl && prevUrl !== "about:blank") {
|
|
2219
|
-
await this.options.conn.send("Page.navigate", { url: prevUrl }, this.options.sessionId);
|
|
2220
|
-
await this.options.waitForLoadState(opts.waitUntil ?? "domcontentloaded", opts.timeout ?? 1e4).catch(() => {
|
|
2221
|
-
});
|
|
2222
|
-
this.options.setUrl(prevUrl);
|
|
2223
|
-
return;
|
|
2224
|
-
}
|
|
2225
|
-
}
|
|
2226
|
-
} catch {
|
|
2227
|
-
}
|
|
2228
|
-
await this.options.evaluate("() => history.back()");
|
|
2229
|
-
await this.options.waitForTimeout(3e3);
|
|
2230
|
-
this.options.setUrl(await this.options.evaluate("location.href").catch(() => this.options.currentUrl()));
|
|
2230
|
+
await this.traverseHistory(-1, opts);
|
|
2231
2231
|
}
|
|
2232
2232
|
async goForward(opts = {}) {
|
|
2233
|
-
|
|
2234
|
-
|
|
2235
|
-
|
|
2236
|
-
|
|
2237
|
-
|
|
2238
|
-
|
|
2239
|
-
|
|
2240
|
-
|
|
2241
|
-
|
|
2233
|
+
await this.traverseHistory(1, opts);
|
|
2234
|
+
}
|
|
2235
|
+
async traverseHistory(offset, { timeout = 1e4, waitUntil = "domcontentloaded" }) {
|
|
2236
|
+
if (this.options.isClosed()) throw new Error("Page is closed");
|
|
2237
|
+
const { conn, sessionId } = this.options;
|
|
2238
|
+
const getHistory = () => conn.send(
|
|
2239
|
+
"Page.getNavigationHistory",
|
|
2240
|
+
void 0,
|
|
2241
|
+
sessionId
|
|
2242
|
+
);
|
|
2243
|
+
const history = await getHistory();
|
|
2244
|
+
const entry = history.entries[history.currentIndex + offset];
|
|
2245
|
+
if (!entry) return;
|
|
2246
|
+
await conn.send("Page.navigateToHistoryEntry", { entryId: entry.id }, sessionId);
|
|
2247
|
+
const deadline = Date.now() + timeout;
|
|
2248
|
+
do {
|
|
2249
|
+
const updated = await getHistory();
|
|
2250
|
+
if (updated.entries[updated.currentIndex]?.id === entry.id) {
|
|
2251
|
+
const document = await this.options.evaluate(
|
|
2252
|
+
"({ url: location.href, ready: document.readyState })"
|
|
2253
|
+
).catch(() => null);
|
|
2254
|
+
if (document?.url === updated.entries[updated.currentIndex].url && (waitUntil === "commit" || (waitUntil === "load" ? document.ready === "complete" : document.ready !== "loading"))) {
|
|
2255
|
+
this.options.setUrl(document.url);
|
|
2256
|
+
if (waitUntil === "networkidle") {
|
|
2257
|
+
await this.options.waitForLoadState(waitUntil, Math.max(0, deadline - Date.now()));
|
|
2258
|
+
}
|
|
2242
2259
|
return;
|
|
2243
2260
|
}
|
|
2244
2261
|
}
|
|
2245
|
-
|
|
2246
|
-
}
|
|
2247
|
-
|
|
2248
|
-
await this.options.waitForTimeout(3e3);
|
|
2249
|
-
this.options.setUrl(await this.options.evaluate("location.href").catch(() => this.options.currentUrl()));
|
|
2262
|
+
await this.options.waitForTimeout(50);
|
|
2263
|
+
} while (Date.now() < deadline);
|
|
2264
|
+
throw new Error(`History navigation timed out after ${timeout}ms`);
|
|
2250
2265
|
}
|
|
2251
2266
|
async reload(opts = {}) {
|
|
2252
2267
|
this.options.state.loadFired = false;
|
|
@@ -2929,7 +2944,6 @@ var XBPageImpl = class _XBPageImpl {
|
|
|
2929
2944
|
setUrl: (url) => {
|
|
2930
2945
|
this._url = url;
|
|
2931
2946
|
},
|
|
2932
|
-
currentUrl: () => this.url(),
|
|
2933
2947
|
waitForLoadState: (state, timeout) => this.waitForLoadState(state, timeout),
|
|
2934
2948
|
waitForTimeout: (ms) => this.waitForTimeout(ms),
|
|
2935
2949
|
evaluate: (script) => this.evaluate(script)
|
|
@@ -3778,7 +3792,23 @@ var XBContextImpl = class {
|
|
|
3778
3792
|
_emitter = new EventEmitter2();
|
|
3779
3793
|
_browser;
|
|
3780
3794
|
contextId;
|
|
3781
|
-
|
|
3795
|
+
/**
|
|
3796
|
+
* 单一规范注册表(dup 回归修复):targetId → wrapper。
|
|
3797
|
+
*
|
|
3798
|
+
* 同一 target 的 wrapper 有三条并发创建路径——newPage() 显式创建、
|
|
3799
|
+
* setAutoAttach 对新建 target 的自动附着事件、Target.attachToTarget 的
|
|
3800
|
+
* attachedToTarget 回显事件(CDP 规范:显式 attach 也会发该事件)。旧实现
|
|
3801
|
+
* 用数组在 `await page._init()` 之后去重,事件在 _init 期间到达时全部穿透
|
|
3802
|
+
* ——headless E2E 实测同一 target 出 3 个 wrapper,且随事件顺序抖动。
|
|
3803
|
+
*
|
|
3804
|
+
* 现在所有路径先**同步预约**(`_reserveTarget` + 同步 set 进
|
|
3805
|
+
* `_pendingRegistrations`),再异步初始化;后到者直接 join 同一个注册
|
|
3806
|
+
* Promise 拿同一个 wrapper,事件顺序无关。注册 Promise 覆盖到 init
|
|
3807
|
+
* scripts 应用完成——join 者拿到的必然是完整初始化的 wrapper。
|
|
3808
|
+
*/
|
|
3809
|
+
_pages = /* @__PURE__ */ new Map();
|
|
3810
|
+
/** in-flight 注册(targetId → 注册 Promise),完成后移除并落入 _pages */
|
|
3811
|
+
_pendingRegistrations = /* @__PURE__ */ new Map();
|
|
3782
3812
|
closed = false;
|
|
3783
3813
|
options;
|
|
3784
3814
|
targetAttachedHandler = null;
|
|
@@ -3805,28 +3835,30 @@ var XBContextImpl = class {
|
|
|
3805
3835
|
async newPage() {
|
|
3806
3836
|
if (this.closed) throw new Error("Context is closed");
|
|
3807
3837
|
const { targetId } = await this._browser._createTarget(this.contextId);
|
|
3808
|
-
|
|
3809
|
-
|
|
3810
|
-
|
|
3811
|
-
|
|
3812
|
-
|
|
3813
|
-
|
|
3814
|
-
|
|
3815
|
-
|
|
3816
|
-
|
|
3817
|
-
|
|
3818
|
-
|
|
3819
|
-
|
|
3820
|
-
|
|
3821
|
-
await page._setExtraHTTPHeaders(this.options.extraHTTPHeaders);
|
|
3838
|
+
let pending = this._reserveTarget(targetId);
|
|
3839
|
+
let explicitSessionId;
|
|
3840
|
+
if (!pending) {
|
|
3841
|
+
const sessionId = await this._browser._attachToTarget(targetId);
|
|
3842
|
+
pending = this._reserveTarget(targetId);
|
|
3843
|
+
if (!pending) {
|
|
3844
|
+
const registration = this._registerTarget(targetId, sessionId);
|
|
3845
|
+
this._pendingRegistrations.set(targetId, registration);
|
|
3846
|
+
pending = registration;
|
|
3847
|
+
explicitSessionId = void 0;
|
|
3848
|
+
} else {
|
|
3849
|
+
explicitSessionId = sessionId;
|
|
3850
|
+
}
|
|
3822
3851
|
}
|
|
3823
|
-
|
|
3824
|
-
|
|
3852
|
+
const page = await pending;
|
|
3853
|
+
if (explicitSessionId && page.sessionId !== explicitSessionId) {
|
|
3854
|
+
this._browser._detachFromTarget(explicitSessionId).catch(() => {
|
|
3855
|
+
});
|
|
3825
3856
|
}
|
|
3857
|
+
await this._applyNewPageOptions(page);
|
|
3826
3858
|
return page;
|
|
3827
3859
|
}
|
|
3828
3860
|
pages() {
|
|
3829
|
-
return [...this._pages];
|
|
3861
|
+
return [...this._pages.values()];
|
|
3830
3862
|
}
|
|
3831
3863
|
browser() {
|
|
3832
3864
|
return this._browser;
|
|
@@ -3834,11 +3866,12 @@ var XBContextImpl = class {
|
|
|
3834
3866
|
async close() {
|
|
3835
3867
|
if (this.closed) return;
|
|
3836
3868
|
this.closed = true;
|
|
3837
|
-
for (const page of this._pages) {
|
|
3869
|
+
for (const page of this._pages.values()) {
|
|
3838
3870
|
await page.close().catch(() => {
|
|
3839
3871
|
});
|
|
3840
3872
|
}
|
|
3841
|
-
this._pages
|
|
3873
|
+
this._pages.clear();
|
|
3874
|
+
this._pendingRegistrations.clear();
|
|
3842
3875
|
if (this.targetAttachedHandler) {
|
|
3843
3876
|
this.conn.off("Target.attachedToTarget", this.targetAttachedHandler);
|
|
3844
3877
|
this.targetAttachedHandler = null;
|
|
@@ -3859,14 +3892,23 @@ var XBContextImpl = class {
|
|
|
3859
3892
|
}
|
|
3860
3893
|
async addInitScript(script) {
|
|
3861
3894
|
this._initScripts.push(script);
|
|
3862
|
-
for (const page of this._pages) {
|
|
3895
|
+
for (const page of this._pages.values()) {
|
|
3863
3896
|
await page.addInitScript(script);
|
|
3864
3897
|
}
|
|
3865
3898
|
}
|
|
3866
3899
|
// ── Cookies ─────────────────────────────────────────────────
|
|
3900
|
+
/**
|
|
3901
|
+
* Real CDP browserContextId (from Target.createBrowserContext), or undefined
|
|
3902
|
+
* for the default context (contextId 'default' / '' is not a CDP id — passing
|
|
3903
|
+
* it to Storage.* makes CDP reject the whole call).
|
|
3904
|
+
*/
|
|
3905
|
+
get storageContextId() {
|
|
3906
|
+
return this.contextId && this.contextId !== "default" ? this.contextId : void 0;
|
|
3907
|
+
}
|
|
3867
3908
|
async cookies(urls) {
|
|
3868
3909
|
const urlList = typeof urls === "string" ? [urls] : urls;
|
|
3869
|
-
const
|
|
3910
|
+
const bcId = this.storageContextId;
|
|
3911
|
+
const params = bcId ? { ...urlList ? { urls: urlList } : {}, browserContextId: bcId } : urlList ? { urls: urlList } : void 0;
|
|
3870
3912
|
try {
|
|
3871
3913
|
const result = await this.conn.send("Storage.getCookies", params);
|
|
3872
3914
|
return result.cookies;
|
|
@@ -3890,15 +3932,18 @@ var XBContextImpl = class {
|
|
|
3890
3932
|
secure: c.secure,
|
|
3891
3933
|
sameSite: c.sameSite
|
|
3892
3934
|
}));
|
|
3935
|
+
const bcId = this.storageContextId;
|
|
3936
|
+
const params = bcId ? { cookies: cdpCookies, browserContextId: bcId } : { cookies: cdpCookies };
|
|
3893
3937
|
try {
|
|
3894
|
-
await this.conn.send("Storage.setCookies",
|
|
3938
|
+
await this.conn.send("Storage.setCookies", params);
|
|
3895
3939
|
} catch {
|
|
3896
3940
|
await this.conn.send("Network.setCookies", { cookies: cdpCookies });
|
|
3897
3941
|
}
|
|
3898
3942
|
}
|
|
3899
3943
|
async clearCookies() {
|
|
3944
|
+
const bcId = this.storageContextId;
|
|
3900
3945
|
try {
|
|
3901
|
-
await this.conn.send("Storage.clearCookies");
|
|
3946
|
+
await this.conn.send("Storage.clearCookies", bcId ? { browserContextId: bcId } : void 0);
|
|
3902
3947
|
} catch {
|
|
3903
3948
|
await this.conn.send("Network.clearBrowserCookies");
|
|
3904
3949
|
}
|
|
@@ -3910,18 +3955,92 @@ var XBContextImpl = class {
|
|
|
3910
3955
|
this._emitter.off(event, handler);
|
|
3911
3956
|
}
|
|
3912
3957
|
/**
|
|
3913
|
-
*
|
|
3914
|
-
*
|
|
3915
|
-
* pages from the user's
|
|
3916
|
-
* so they appear in `context.pages()
|
|
3958
|
+
* Attach an existing target (discovered via Target.getTargets / HTTP
|
|
3959
|
+
* /json/list) and register exactly one wrapper for it. Used by
|
|
3960
|
+
* XBBrowserImpl.discoverContexts() to wire up pages from the user's
|
|
3961
|
+
* existing browser session so they appear in `context.pages()`.
|
|
3962
|
+
*
|
|
3963
|
+
* 显式 attach 同样会触发 attachedToTarget 回显事件(auto-attach 也会对
|
|
3964
|
+
* 既有 target 补附着)——全部经注册表预约去重。attach 失败(如 target
|
|
3965
|
+
* 已被其他客户端占用)返回 null:context 仍注册,后续 newPage() 可用。
|
|
3917
3966
|
*/
|
|
3918
|
-
|
|
3919
|
-
|
|
3920
|
-
|
|
3921
|
-
|
|
3922
|
-
|
|
3967
|
+
async _attachExistingPage(targetId) {
|
|
3968
|
+
let pending = this._reserveTarget(targetId);
|
|
3969
|
+
let explicitSessionId;
|
|
3970
|
+
if (!pending) {
|
|
3971
|
+
let sessionId;
|
|
3972
|
+
try {
|
|
3973
|
+
sessionId = await this._browser._attachToTarget(targetId);
|
|
3974
|
+
} catch {
|
|
3975
|
+
return null;
|
|
3976
|
+
}
|
|
3977
|
+
pending = this._reserveTarget(targetId);
|
|
3978
|
+
if (!pending) {
|
|
3979
|
+
const registration = this._registerTarget(targetId, sessionId);
|
|
3980
|
+
this._pendingRegistrations.set(targetId, registration);
|
|
3981
|
+
pending = registration;
|
|
3982
|
+
} else {
|
|
3983
|
+
explicitSessionId = sessionId;
|
|
3984
|
+
}
|
|
3985
|
+
}
|
|
3986
|
+
try {
|
|
3987
|
+
const page = await pending;
|
|
3988
|
+
if (explicitSessionId && page.sessionId !== explicitSessionId) {
|
|
3989
|
+
this._browser._detachFromTarget(explicitSessionId).catch(() => {
|
|
3990
|
+
});
|
|
3991
|
+
}
|
|
3992
|
+
return page;
|
|
3993
|
+
} catch {
|
|
3994
|
+
return null;
|
|
3995
|
+
}
|
|
3923
3996
|
}
|
|
3924
3997
|
// ── Private ─────────────────────────────────────────────────
|
|
3998
|
+
/**
|
|
3999
|
+
* 同步查询/预约 target:已注册返回其 resolved Promise;in-flight 返回
|
|
4000
|
+
* 注册 Promise(join 同一 wrapper);否则返回 null,调用方须同步发起
|
|
4001
|
+
* 注册并 set 进 `_pendingRegistrations`(在任意 await 之前)。
|
|
4002
|
+
*/
|
|
4003
|
+
_reserveTarget(targetId) {
|
|
4004
|
+
const registered = this._pages.get(targetId);
|
|
4005
|
+
if (registered) return Promise.resolve(registered);
|
|
4006
|
+
return this._pendingRegistrations.get(targetId) ?? null;
|
|
4007
|
+
}
|
|
4008
|
+
/**
|
|
4009
|
+
* 执行注册:创建 wrapper → _init → 应用 init scripts → 入注册表。
|
|
4010
|
+
* Promise 覆盖到 init scripts 应用完成;成功/失败都从 in-flight 表移除
|
|
4011
|
+
* (失败向上抛给所有 join 者,后续可重新预约重试)。
|
|
4012
|
+
*/
|
|
4013
|
+
async _registerTarget(targetId, sessionId) {
|
|
4014
|
+
try {
|
|
4015
|
+
const page = new XBPageImpl(this.conn, sessionId, targetId, this, this._browser);
|
|
4016
|
+
await page._init();
|
|
4017
|
+
for (const script of this._initScripts) {
|
|
4018
|
+
await page.addInitScript(script).catch(() => {
|
|
4019
|
+
});
|
|
4020
|
+
}
|
|
4021
|
+
this._pages.set(targetId, page);
|
|
4022
|
+
page.on("close", () => {
|
|
4023
|
+
if (this._pages.get(targetId) === page) this._pages.delete(targetId);
|
|
4024
|
+
});
|
|
4025
|
+
this.forwardPageEvents(page);
|
|
4026
|
+
return page;
|
|
4027
|
+
} finally {
|
|
4028
|
+
this._pendingRegistrations.delete(targetId);
|
|
4029
|
+
}
|
|
4030
|
+
}
|
|
4031
|
+
/** newPage 的上下文选项(viewport/UA/headers)——自建与 join 路径共用 */
|
|
4032
|
+
async _applyNewPageOptions(page) {
|
|
4033
|
+
if (this.options.viewport) {
|
|
4034
|
+
await page.setViewportSize(this.options.viewport).catch(() => {
|
|
4035
|
+
});
|
|
4036
|
+
}
|
|
4037
|
+
if (this.options.userAgent) {
|
|
4038
|
+
await page._setUserAgent(this.options.userAgent);
|
|
4039
|
+
}
|
|
4040
|
+
if (this.options.extraHTTPHeaders) {
|
|
4041
|
+
await page._setExtraHTTPHeaders(this.options.extraHTTPHeaders);
|
|
4042
|
+
}
|
|
4043
|
+
}
|
|
3925
4044
|
/** Forward page-level events (request, response, etc.) to context listeners */
|
|
3926
4045
|
forwardPageEvents(page) {
|
|
3927
4046
|
const forward = (event) => {
|
|
@@ -3937,30 +4056,25 @@ var XBContextImpl = class {
|
|
|
3937
4056
|
setupAutoAttach() {
|
|
3938
4057
|
this.targetAttachedHandler = (paramsRaw) => {
|
|
3939
4058
|
const params = paramsRaw;
|
|
3940
|
-
if (this.contextId !== "default" && params.targetInfo.browserContextId !== this.contextId) return;
|
|
3941
4059
|
if (params.targetInfo.type !== "page") return;
|
|
3942
|
-
const
|
|
3943
|
-
|
|
3944
|
-
|
|
3945
|
-
if (
|
|
3946
|
-
|
|
3947
|
-
|
|
3948
|
-
params.sessionId,
|
|
3949
|
-
params.targetInfo.targetId,
|
|
3950
|
-
this,
|
|
3951
|
-
this._browser
|
|
3952
|
-
);
|
|
4060
|
+
const bcId = params.targetInfo.browserContextId;
|
|
4061
|
+
if (this.contextId === "default") {
|
|
4062
|
+
if (bcId) return;
|
|
4063
|
+
} else if (bcId !== this.contextId) {
|
|
4064
|
+
return;
|
|
4065
|
+
}
|
|
3953
4066
|
this.conn.send("Runtime.runIfWaitingForDebugger", void 0, params.sessionId).catch(() => {
|
|
3954
4067
|
});
|
|
3955
|
-
|
|
3956
|
-
|
|
3957
|
-
|
|
3958
|
-
|
|
4068
|
+
if (this._reserveTarget(params.targetInfo.targetId)) return;
|
|
4069
|
+
const registration = this._registerTarget(params.targetInfo.targetId, params.sessionId);
|
|
4070
|
+
this._pendingRegistrations.set(params.targetInfo.targetId, registration);
|
|
4071
|
+
registration.then(
|
|
4072
|
+
(page) => {
|
|
4073
|
+
this._emitter.emit("page", page);
|
|
4074
|
+
},
|
|
4075
|
+
() => {
|
|
3959
4076
|
}
|
|
3960
|
-
|
|
3961
|
-
this.forwardPageEvents(page);
|
|
3962
|
-
this._emitter.emit("page", page);
|
|
3963
|
-
});
|
|
4077
|
+
);
|
|
3964
4078
|
};
|
|
3965
4079
|
this.conn.on("Target.attachedToTarget", this.targetAttachedHandler);
|
|
3966
4080
|
}
|
|
@@ -4031,7 +4145,7 @@ var XBBrowserImpl = class {
|
|
|
4031
4145
|
this._exitHandler = null;
|
|
4032
4146
|
}
|
|
4033
4147
|
if (this.childProcess) {
|
|
4034
|
-
const { killChrome: killChrome2 } = await import("./launcher-
|
|
4148
|
+
const { killChrome: killChrome2 } = await import("./launcher-AUJK5QQM.js");
|
|
4035
4149
|
await killChrome2(this.childProcess, this.tmpDir);
|
|
4036
4150
|
}
|
|
4037
4151
|
await this.conn.close();
|
|
@@ -4211,7 +4325,7 @@ var XBBrowserImpl = class {
|
|
|
4211
4325
|
if (pageTargets.length === 0 && httpFallbackUrl) {
|
|
4212
4326
|
console.log(`[discoverContexts] Target.getTargets returned ${targetInfos.length} targets (0 page type). Falling back to HTTP /json/list at ${httpFallbackUrl}`);
|
|
4213
4327
|
try {
|
|
4214
|
-
const { getCDPTargets: getCDPTargets2 } = await import("./launcher-
|
|
4328
|
+
const { getCDPTargets: getCDPTargets2 } = await import("./launcher-AUJK5QQM.js");
|
|
4215
4329
|
const httpPages = await getCDPTargets2(httpFallbackUrl);
|
|
4216
4330
|
console.log(`[discoverContexts] HTTP /json/list returned ${httpPages.length} pages`);
|
|
4217
4331
|
for (const p of httpPages) {
|
|
@@ -4243,13 +4357,7 @@ var XBBrowserImpl = class {
|
|
|
4243
4357
|
if (this._contexts.has(ctxId)) continue;
|
|
4244
4358
|
const context = new XBContextImpl(this.conn, ctxId, this, {});
|
|
4245
4359
|
for (const p of pages) {
|
|
4246
|
-
|
|
4247
|
-
const sessionId = await this._attachToTarget(p.targetId);
|
|
4248
|
-
const page = new XBPageImpl(this.conn, sessionId, p.targetId, context, this);
|
|
4249
|
-
await page._init();
|
|
4250
|
-
context._addDiscoveredPage(page);
|
|
4251
|
-
} catch {
|
|
4252
|
-
}
|
|
4360
|
+
await context._attachExistingPage(p.targetId).catch(() => null);
|
|
4253
4361
|
}
|
|
4254
4362
|
this._contexts.set(ctxId, { contextId: ctxId, context });
|
|
4255
4363
|
}
|