@linxin666/dsh-client-ui-browser-view 0.4.11 → 0.4.13
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/README.i18n.yaml +2 -2
- package/README.md +43 -12
- package/README.zh.md +43 -12
- package/icon.svg +3 -0
- package/lib/client.js +1733 -17
- package/lib/client.js.map +1 -1
- package/lib/index.js +363 -48
- package/lib/types/client/BrowserViewSettingsCard.d.ts +40 -0
- package/lib/types/client/PluginSettingsCard.d.ts +255 -0
- package/lib/types/client/RangeBubble.d.ts +46 -0
- package/lib/types/client/SegmentGroup.d.ts +28 -0
- package/lib/types/client/config.d.ts +6 -0
- package/lib/types/client/index.d.ts +10 -0
- package/lib/types/client/locales.d.ts +26 -0
- package/lib/types/client/plugin-card-seat.d.ts +110 -0
- package/lib/types/client/settings-entry-form.d.ts +49 -0
- package/lib/types/client/settings-form.d.ts +235 -0
- package/lib/types/core/protocol.d.ts +7 -1
- package/lib/types/core/tabs.d.ts +20 -0
- package/lib/types/host/cdp-endpoint.d.ts +27 -0
- package/lib/types/host/config.d.ts +26 -0
- package/lib/types/host/service.d.ts +25 -9
- package/lib/types/index.d.ts +10 -1
- package/locale/en.json +6 -0
- package/locale/zh.json +6 -0
- package/package.json +14 -2
package/lib/index.js
CHANGED
|
@@ -653,11 +653,14 @@ var TabManager = class {
|
|
|
653
653
|
viewport;
|
|
654
654
|
ringSize;
|
|
655
655
|
now;
|
|
656
|
+
discoversTargets;
|
|
657
|
+
pinViewports;
|
|
656
658
|
onFrame;
|
|
657
659
|
onTabsChanged;
|
|
658
660
|
tabs = /* @__PURE__ */ new Map();
|
|
659
661
|
bySession = /* @__PURE__ */ new Map();
|
|
660
662
|
unsubscribers = [];
|
|
663
|
+
attaching = /* @__PURE__ */ new Map();
|
|
661
664
|
active = null;
|
|
662
665
|
disposed = false;
|
|
663
666
|
/**
|
|
@@ -672,6 +675,8 @@ var TabManager = class {
|
|
|
672
675
|
};
|
|
673
676
|
this.ringSize = options.ringSize ?? 500;
|
|
674
677
|
this.now = options.now ?? Date.now;
|
|
678
|
+
this.discoversTargets = options.discoverTargets ?? false;
|
|
679
|
+
this.pinViewports = options.pinViewport ?? true;
|
|
675
680
|
this.onFrame = options.onFrame;
|
|
676
681
|
this.onTabsChanged = options.onTabsChanged;
|
|
677
682
|
this.subscribe();
|
|
@@ -720,19 +725,77 @@ var TabManager = class {
|
|
|
720
725
|
async createTab(options = {}) {
|
|
721
726
|
const targetId = (await this.transport.send("Target.createTarget", { url: "about:blank" })).targetId;
|
|
722
727
|
if (typeof targetId !== "string" || targetId === "") throw new Error("the browser did not return a target id for the new tab");
|
|
728
|
+
const record = await this.attachTarget({
|
|
729
|
+
targetId,
|
|
730
|
+
type: "page",
|
|
731
|
+
url: "about:blank",
|
|
732
|
+
title: ""
|
|
733
|
+
}, "created");
|
|
734
|
+
record.ownerSessionId = options.sessionId;
|
|
735
|
+
this.active = targetId;
|
|
736
|
+
if (this.pinViewports) await this.pinViewport(record);
|
|
737
|
+
if (options.url !== void 0 && options.url !== "") await this.navigate(targetId, options.url);
|
|
738
|
+
this.onTabsChanged?.();
|
|
739
|
+
return this.view(record);
|
|
740
|
+
}
|
|
741
|
+
/** Discover every existing page and keep following pages created later. */
|
|
742
|
+
async discoverExistingTabs() {
|
|
743
|
+
if (!this.discoversTargets || this.disposed) return;
|
|
744
|
+
await this.transport.send("Target.setDiscoverTargets", { discover: true });
|
|
745
|
+
const result = await this.transport.send("Target.getTargets");
|
|
746
|
+
for (const info of result.targetInfos ?? []) {
|
|
747
|
+
if (info.type !== "page") continue;
|
|
748
|
+
await this.attachTarget(info, "adopted").catch(() => void 0);
|
|
749
|
+
}
|
|
750
|
+
this.onTabsChanged?.();
|
|
751
|
+
}
|
|
752
|
+
/** Adopt one target-created event without racing an explicit create/attach. */
|
|
753
|
+
async adoptTarget(info) {
|
|
754
|
+
if (this.disposed || info.type !== "page") return;
|
|
755
|
+
await this.attachTarget(info, "adopted");
|
|
756
|
+
this.onTabsChanged?.();
|
|
757
|
+
}
|
|
758
|
+
/** Attach a page exactly once, coalescing discovery and Target.createTarget. */
|
|
759
|
+
async attachTarget(info, origin) {
|
|
760
|
+
const targetId = info.targetId;
|
|
761
|
+
if (typeof targetId !== "string" || targetId === "") throw new Error("the browser did not return a target id");
|
|
762
|
+
const existing = this.tabs.get(targetId);
|
|
763
|
+
if (existing !== void 0) {
|
|
764
|
+
if (origin === "created") existing.origin = "created";
|
|
765
|
+
return existing;
|
|
766
|
+
}
|
|
767
|
+
const inFlight = this.attaching.get(targetId);
|
|
768
|
+
if (inFlight !== void 0) {
|
|
769
|
+
const record = await inFlight;
|
|
770
|
+
if (origin === "created") record.origin = "created";
|
|
771
|
+
return record;
|
|
772
|
+
}
|
|
773
|
+
const task = this.attachNewTarget(targetId, info, origin);
|
|
774
|
+
this.attaching.set(targetId, task);
|
|
775
|
+
try {
|
|
776
|
+
return await task;
|
|
777
|
+
} finally {
|
|
778
|
+
this.attaching.delete(targetId);
|
|
779
|
+
}
|
|
780
|
+
}
|
|
781
|
+
/** Build and instrument one previously unseen page record. */
|
|
782
|
+
async attachNewTarget(targetId, info, origin) {
|
|
783
|
+
if (this.disposed) throw new Error("the tab manager is disposed");
|
|
723
784
|
const sessionId = (await this.transport.send("Target.attachToTarget", {
|
|
724
785
|
targetId,
|
|
725
786
|
flatten: true
|
|
726
787
|
})).sessionId;
|
|
727
788
|
if (typeof sessionId !== "string" || sessionId === "") throw new Error(`the browser did not return a session for tab ${targetId}`);
|
|
789
|
+
if (this.disposed) throw new Error("the tab manager is disposed");
|
|
728
790
|
const record = {
|
|
729
791
|
id: targetId,
|
|
730
792
|
sessionId,
|
|
731
|
-
url: "about:blank",
|
|
732
|
-
title: "",
|
|
793
|
+
url: typeof info.url === "string" ? info.url : "about:blank",
|
|
794
|
+
title: typeof info.title === "string" ? info.title : "",
|
|
733
795
|
loading: false,
|
|
734
796
|
loaderId: void 0,
|
|
735
|
-
ownerSessionId:
|
|
797
|
+
ownerSessionId: void 0,
|
|
798
|
+
origin,
|
|
736
799
|
console: [],
|
|
737
800
|
network: [],
|
|
738
801
|
pendingRequests: /* @__PURE__ */ new Map(),
|
|
@@ -742,12 +805,27 @@ var TabManager = class {
|
|
|
742
805
|
};
|
|
743
806
|
this.tabs.set(targetId, record);
|
|
744
807
|
this.bySession.set(sessionId, record);
|
|
745
|
-
this.active = targetId;
|
|
746
|
-
|
|
747
|
-
|
|
748
|
-
|
|
808
|
+
if (this.active === null) this.active = targetId;
|
|
809
|
+
try {
|
|
810
|
+
await this.enableDomains(record);
|
|
811
|
+
await this.refreshDocumentInfo(record);
|
|
812
|
+
return record;
|
|
813
|
+
} catch (error) {
|
|
814
|
+
this.tabs.delete(targetId);
|
|
815
|
+
this.bySession.delete(sessionId);
|
|
816
|
+
if (this.active === targetId) this.active = [...this.tabs.keys()].at(-1) ?? null;
|
|
817
|
+
await this.transport.send("Target.detachFromTarget", { sessionId }).catch(() => void 0);
|
|
818
|
+
throw error;
|
|
819
|
+
}
|
|
820
|
+
}
|
|
821
|
+
/** Forget a target the real browser closed. */
|
|
822
|
+
removeTarget(targetId) {
|
|
823
|
+
const record = this.tabs.get(targetId);
|
|
824
|
+
if (record === void 0) return;
|
|
825
|
+
this.tabs.delete(targetId);
|
|
826
|
+
this.bySession.delete(record.sessionId);
|
|
827
|
+
if (this.active === targetId) this.active = [...this.tabs.keys()].at(-1) ?? null;
|
|
749
828
|
this.onTabsChanged?.();
|
|
750
|
-
return this.view(record);
|
|
751
829
|
}
|
|
752
830
|
/**
|
|
753
831
|
* Pin CSS-pixel viewport metrics so frames, boxes, and clicks share one
|
|
@@ -808,7 +886,7 @@ var TabManager = class {
|
|
|
808
886
|
async closeTab(tabId) {
|
|
809
887
|
const record = this.requireRecord(tabId);
|
|
810
888
|
await this.stopScreencast(tabId).catch(() => void 0);
|
|
811
|
-
await this.transport.send("Target.closeTarget", { targetId: record.id }).
|
|
889
|
+
if ((await this.transport.send("Target.closeTarget", { targetId: record.id })).success === false) throw new Error(`the browser refused to close tab "${record.id}"`);
|
|
812
890
|
this.tabs.delete(record.id);
|
|
813
891
|
this.bySession.delete(record.sessionId);
|
|
814
892
|
if (this.active === record.id) this.active = [...this.tabs.keys()].at(-1) ?? null;
|
|
@@ -867,16 +945,30 @@ var TabManager = class {
|
|
|
867
945
|
const data = await this.captureScreenshot(tabId);
|
|
868
946
|
const record = this.requireRecord(tabId);
|
|
869
947
|
record.frameSeq += 1;
|
|
948
|
+
const viewport = await this.frameViewport(record);
|
|
870
949
|
const frame = {
|
|
871
950
|
tabId: record.id,
|
|
872
951
|
seq: record.frameSeq,
|
|
873
|
-
width:
|
|
874
|
-
height:
|
|
952
|
+
width: viewport.width,
|
|
953
|
+
height: viewport.height,
|
|
875
954
|
data
|
|
876
955
|
};
|
|
877
956
|
this.onFrame?.(frame);
|
|
878
957
|
return frame;
|
|
879
958
|
}
|
|
959
|
+
/** Read the real CSS viewport without changing it in external-browser mode. */
|
|
960
|
+
async frameViewport(record) {
|
|
961
|
+
if (this.pinViewports) return this.viewport;
|
|
962
|
+
try {
|
|
963
|
+
const metrics = await this.transport.send("Page.getLayoutMetrics", {}, record.sessionId);
|
|
964
|
+
const viewport = metrics.cssLayoutViewport ?? metrics.layoutViewport;
|
|
965
|
+
if (typeof viewport?.clientWidth === "number" && typeof viewport.clientHeight === "number" && viewport.clientWidth > 0 && viewport.clientHeight > 0) return {
|
|
966
|
+
width: Math.round(viewport.clientWidth),
|
|
967
|
+
height: Math.round(viewport.clientHeight)
|
|
968
|
+
};
|
|
969
|
+
} catch {}
|
|
970
|
+
return this.viewport;
|
|
971
|
+
}
|
|
880
972
|
/**
|
|
881
973
|
* Console entries captured so far, oldest first.
|
|
882
974
|
* @param tabId - target tab.
|
|
@@ -934,7 +1026,8 @@ var TabManager = class {
|
|
|
934
1026
|
id: record.id,
|
|
935
1027
|
url: record.url,
|
|
936
1028
|
title: record.title,
|
|
937
|
-
loading: record.loading
|
|
1029
|
+
loading: record.loading,
|
|
1030
|
+
origin: record.origin
|
|
938
1031
|
};
|
|
939
1032
|
if (record.ownerSessionId !== void 0) view.sessionId = record.ownerSessionId;
|
|
940
1033
|
return view;
|
|
@@ -978,6 +1071,28 @@ var TabManager = class {
|
|
|
978
1071
|
const bySession = (sessionId) => {
|
|
979
1072
|
return sessionId === void 0 ? void 0 : this.bySession.get(sessionId);
|
|
980
1073
|
};
|
|
1074
|
+
if (this.discoversTargets) {
|
|
1075
|
+
this.unsubscribers.push(this.transport.on("Target.targetCreated", (params) => {
|
|
1076
|
+
const info = params.targetInfo;
|
|
1077
|
+
if (info !== void 0) this.adoptTarget(info).catch(() => void 0);
|
|
1078
|
+
}));
|
|
1079
|
+
this.unsubscribers.push(this.transport.on("Target.targetInfoChanged", (params) => {
|
|
1080
|
+
const info = params.targetInfo;
|
|
1081
|
+
if (info === void 0 || typeof info.targetId !== "string") return;
|
|
1082
|
+
const record = this.tabs.get(info.targetId);
|
|
1083
|
+
if (record === void 0) {
|
|
1084
|
+
this.adoptTarget(info).catch(() => void 0);
|
|
1085
|
+
return;
|
|
1086
|
+
}
|
|
1087
|
+
if (typeof info.url === "string") record.url = info.url;
|
|
1088
|
+
if (typeof info.title === "string") record.title = info.title;
|
|
1089
|
+
this.onTabsChanged?.();
|
|
1090
|
+
}));
|
|
1091
|
+
this.unsubscribers.push(this.transport.on("Target.targetDestroyed", (params) => {
|
|
1092
|
+
const targetId = params.targetId;
|
|
1093
|
+
if (typeof targetId === "string") this.removeTarget(targetId);
|
|
1094
|
+
}));
|
|
1095
|
+
}
|
|
981
1096
|
this.unsubscribers.push(this.transport.on("Page.frameNavigated", (params, sessionId) => {
|
|
982
1097
|
const frame = params.frame;
|
|
983
1098
|
if (frame === void 0) return;
|
|
@@ -1387,6 +1502,74 @@ function isLoopbackTarget(url) {
|
|
|
1387
1502
|
return isLoopbackHostname(parsed.hostname);
|
|
1388
1503
|
}
|
|
1389
1504
|
//#endregion
|
|
1505
|
+
//#region src/host/cdp-endpoint.ts
|
|
1506
|
+
/**
|
|
1507
|
+
* Loopback-only discovery of an external Chrome DevTools endpoint.
|
|
1508
|
+
* @module @linxin666/dsh-client-ui-browser-view/host/cdp-endpoint
|
|
1509
|
+
*/
|
|
1510
|
+
function assertLoopbackUrl(url, protocols, label) {
|
|
1511
|
+
if (!protocols.includes(url.protocol)) throw new Error(`${label} must use ${protocols.join(" or ")}`);
|
|
1512
|
+
if (!isLoopbackHostname(url.hostname)) throw new Error(`${label} must name a loopback host (localhost, 127/8, or [::1])`);
|
|
1513
|
+
if (url.username !== "" || url.password !== "") throw new Error(`${label} must not contain credentials`);
|
|
1514
|
+
}
|
|
1515
|
+
/** Parse and normalize the configured HTTP endpoint, failing closed. */
|
|
1516
|
+
function parseCdpEndpoint(raw) {
|
|
1517
|
+
let url;
|
|
1518
|
+
try {
|
|
1519
|
+
url = new URL(raw);
|
|
1520
|
+
} catch {
|
|
1521
|
+
throw new Error("cdpEndpoint must be an absolute HTTP URL such as http://127.0.0.1:9222");
|
|
1522
|
+
}
|
|
1523
|
+
assertLoopbackUrl(url, ["http:"], "cdpEndpoint");
|
|
1524
|
+
if (url.port === "") throw new Error("cdpEndpoint must include an explicit port");
|
|
1525
|
+
if (url.pathname !== "" && url.pathname !== "/" || url.search !== "" || url.hash !== "") throw new Error("cdpEndpoint must be an origin without a path, query, or fragment");
|
|
1526
|
+
return {
|
|
1527
|
+
baseUrl: url.origin,
|
|
1528
|
+
versionUrl: new URL("/json/version", url.origin).toString()
|
|
1529
|
+
};
|
|
1530
|
+
}
|
|
1531
|
+
/** Validate the WebSocket published by the local discovery document. */
|
|
1532
|
+
function parseCdpWebSocketUrl(raw) {
|
|
1533
|
+
let url;
|
|
1534
|
+
try {
|
|
1535
|
+
url = new URL(raw);
|
|
1536
|
+
} catch {
|
|
1537
|
+
throw new Error("the CDP discovery document returned an invalid webSocketDebuggerUrl");
|
|
1538
|
+
}
|
|
1539
|
+
assertLoopbackUrl(url, ["ws:", "wss:"], "webSocketDebuggerUrl");
|
|
1540
|
+
if (url.port === "") throw new Error("webSocketDebuggerUrl must include an explicit port");
|
|
1541
|
+
return url.toString();
|
|
1542
|
+
}
|
|
1543
|
+
/** Fetch and validate the external browser's discovery document. */
|
|
1544
|
+
async function fetchCdpVersion(endpoint, options = {}) {
|
|
1545
|
+
const fetchImpl = options.fetchImpl ?? fetch;
|
|
1546
|
+
const controller = new AbortController();
|
|
1547
|
+
const timer = setTimeout(() => {
|
|
1548
|
+
controller.abort();
|
|
1549
|
+
}, options.timeoutMs ?? 5e3);
|
|
1550
|
+
try {
|
|
1551
|
+
const response = await fetchImpl(endpoint.versionUrl, {
|
|
1552
|
+
signal: controller.signal,
|
|
1553
|
+
redirect: "error"
|
|
1554
|
+
});
|
|
1555
|
+
if (!response.ok) throw new Error(`discovery returned HTTP ${response.status}`);
|
|
1556
|
+
const value = await response.json();
|
|
1557
|
+
if (typeof value.webSocketDebuggerUrl !== "string" || value.webSocketDebuggerUrl === "") throw new Error("discovery did not return webSocketDebuggerUrl");
|
|
1558
|
+
const browser = value.Browser ?? value.browser;
|
|
1559
|
+
return {
|
|
1560
|
+
webSocketDebuggerUrl: parseCdpWebSocketUrl(value.webSocketDebuggerUrl),
|
|
1561
|
+
...typeof browser === "string" && browser !== "" ? { browser } : {}
|
|
1562
|
+
};
|
|
1563
|
+
} finally {
|
|
1564
|
+
clearTimeout(timer);
|
|
1565
|
+
}
|
|
1566
|
+
}
|
|
1567
|
+
/** Add the real-browser startup constraints to any external connection error. */
|
|
1568
|
+
function realBrowserConnectionError(endpoint, error) {
|
|
1569
|
+
const reason = error instanceof Error ? error.message : String(error);
|
|
1570
|
+
return /* @__PURE__ */ new Error(`Could not connect to the real browser at ${endpoint.baseUrl}: ${reason}. Start Chrome/Edge with --remote-debugging-port and a non-default --user-data-dir. Chrome 136+ ignores remote debugging switches on its default data directory. No isolated-browser fallback was attempted.`);
|
|
1571
|
+
}
|
|
1572
|
+
//#endregion
|
|
1390
1573
|
//#region src/core/actions.ts
|
|
1391
1574
|
/** Dispatch one expression in the page and return its plain-JSON value. */
|
|
1392
1575
|
async function evaluateJson(transport, sessionId, expression) {
|
|
@@ -1730,8 +1913,8 @@ async function scrollBy(scope, tabId, delta) {
|
|
|
1730
1913
|
*
|
|
1731
1914
|
* @module @linxin666/dsh-client-ui-browser-view/host/service
|
|
1732
1915
|
*/
|
|
1733
|
-
/** Start
|
|
1734
|
-
async function
|
|
1916
|
+
/** Start the default isolated Chrome and wrap it in a session. */
|
|
1917
|
+
async function openIsolatedSession(options) {
|
|
1735
1918
|
const chrome = await ChromeProcess.launch({ viewport: options.viewport });
|
|
1736
1919
|
let cdp;
|
|
1737
1920
|
try {
|
|
@@ -1748,6 +1931,7 @@ async function openDefaultSession(options) {
|
|
|
1748
1931
|
onTabsChanged: options.onTabsChanged
|
|
1749
1932
|
});
|
|
1750
1933
|
return {
|
|
1934
|
+
mode: "isolated",
|
|
1751
1935
|
transport: cdp,
|
|
1752
1936
|
tabs,
|
|
1753
1937
|
chrome,
|
|
@@ -1758,6 +1942,44 @@ async function openDefaultSession(options) {
|
|
|
1758
1942
|
}
|
|
1759
1943
|
};
|
|
1760
1944
|
}
|
|
1945
|
+
/** Connect to an existing real browser without owning its process or tabs. */
|
|
1946
|
+
async function openExternalSession(options) {
|
|
1947
|
+
const endpoint = parseCdpEndpoint(options.cdpEndpoint ?? "");
|
|
1948
|
+
let cdp;
|
|
1949
|
+
let tabs;
|
|
1950
|
+
try {
|
|
1951
|
+
const version = await fetchCdpVersion(endpoint);
|
|
1952
|
+
cdp = await CdpClient.connect(version.webSocketDebuggerUrl, { timeoutMs: 5e3 });
|
|
1953
|
+
tabs = new TabManager({
|
|
1954
|
+
transport: cdp,
|
|
1955
|
+
jpegQuality: options.jpegQuality,
|
|
1956
|
+
viewport: options.viewport,
|
|
1957
|
+
pinViewport: false,
|
|
1958
|
+
discoverTargets: true,
|
|
1959
|
+
onFrame: options.onFrame,
|
|
1960
|
+
onTabsChanged: options.onTabsChanged
|
|
1961
|
+
});
|
|
1962
|
+
await tabs.discoverExistingTabs();
|
|
1963
|
+
const attachedTabs = tabs;
|
|
1964
|
+
return {
|
|
1965
|
+
mode: "cdp",
|
|
1966
|
+
transport: cdp,
|
|
1967
|
+
tabs: attachedTabs,
|
|
1968
|
+
async dispose() {
|
|
1969
|
+
await attachedTabs.dispose();
|
|
1970
|
+
cdp?.close();
|
|
1971
|
+
}
|
|
1972
|
+
};
|
|
1973
|
+
} catch (error) {
|
|
1974
|
+
if (tabs !== void 0) await tabs.dispose().catch(() => void 0);
|
|
1975
|
+
cdp?.close();
|
|
1976
|
+
throw realBrowserConnectionError(endpoint, error);
|
|
1977
|
+
}
|
|
1978
|
+
}
|
|
1979
|
+
/** Resolve a factory that honors the selected mode without fallback. */
|
|
1980
|
+
function openConfiguredSession(options) {
|
|
1981
|
+
return options.mode === "cdp" ? openExternalSession(options) : openIsolatedSession(options);
|
|
1982
|
+
}
|
|
1761
1983
|
/**
|
|
1762
1984
|
* The plugin's single browser service.
|
|
1763
1985
|
*/
|
|
@@ -1766,6 +1988,7 @@ var BrowserViewService = class {
|
|
|
1766
1988
|
viewport;
|
|
1767
1989
|
now;
|
|
1768
1990
|
openSession;
|
|
1991
|
+
resolveConfig;
|
|
1769
1992
|
authenticatedUrl;
|
|
1770
1993
|
sinks = /* @__PURE__ */ new Set();
|
|
1771
1994
|
bindings = /* @__PURE__ */ new Map();
|
|
@@ -1792,6 +2015,7 @@ var BrowserViewService = class {
|
|
|
1792
2015
|
opening;
|
|
1793
2016
|
openingTab;
|
|
1794
2017
|
lastError;
|
|
2018
|
+
generation = 0;
|
|
1795
2019
|
disposed = false;
|
|
1796
2020
|
/**
|
|
1797
2021
|
* @param options - quality, viewport, clock, and the browser factory seam.
|
|
@@ -1803,7 +2027,8 @@ var BrowserViewService = class {
|
|
|
1803
2027
|
height: DEFAULT_VIEWPORT.height
|
|
1804
2028
|
};
|
|
1805
2029
|
this.now = options.now ?? Date.now;
|
|
1806
|
-
this.openSession = options.openSession ??
|
|
2030
|
+
this.openSession = options.openSession ?? openConfiguredSession;
|
|
2031
|
+
this.resolveConfig = options.resolveConfig ?? (() => ({ mode: "isolated" }));
|
|
1807
2032
|
this.authenticatedUrl = options.authenticatedUrl;
|
|
1808
2033
|
}
|
|
1809
2034
|
/** Whether a browser is currently running. */
|
|
@@ -1812,11 +2037,18 @@ var BrowserViewService = class {
|
|
|
1812
2037
|
}
|
|
1813
2038
|
/** The current panel-facing state; safe to call before anything started. */
|
|
1814
2039
|
state() {
|
|
2040
|
+
const config = this.resolveConfig();
|
|
1815
2041
|
const state = {
|
|
1816
2042
|
tabs: this.session?.tabs.list() ?? [],
|
|
1817
2043
|
activeTabId: this.session === void 0 ? null : this.displayedOrDefault(),
|
|
1818
|
-
running: this.session !== void 0
|
|
2044
|
+
running: this.session !== void 0,
|
|
2045
|
+
mode: config.mode
|
|
1819
2046
|
};
|
|
2047
|
+
if (config.mode === "cdp" && config.cdpEndpoint !== void 0) try {
|
|
2048
|
+
state.cdpEndpoint = parseCdpEndpoint(config.cdpEndpoint).baseUrl;
|
|
2049
|
+
} catch {
|
|
2050
|
+
state.cdpEndpoint = config.cdpEndpoint;
|
|
2051
|
+
}
|
|
1820
2052
|
if (this.lastError !== void 0) state.message = this.lastError;
|
|
1821
2053
|
return state;
|
|
1822
2054
|
}
|
|
@@ -1832,7 +2064,7 @@ var BrowserViewService = class {
|
|
|
1832
2064
|
kind: "hello",
|
|
1833
2065
|
state: this.state()
|
|
1834
2066
|
});
|
|
1835
|
-
this.startStreaming();
|
|
2067
|
+
this.startStreaming().catch(() => void 0);
|
|
1836
2068
|
return () => {
|
|
1837
2069
|
this.sinks.delete(sink);
|
|
1838
2070
|
if (this.sinks.size === 0) this.stopStreaming();
|
|
@@ -1871,6 +2103,7 @@ var BrowserViewService = class {
|
|
|
1871
2103
|
return;
|
|
1872
2104
|
}
|
|
1873
2105
|
case "close-tab":
|
|
2106
|
+
if ((await this.requireTabs()).tab(command.tabId).origin === "adopted") throw new Error("an adopted real-browser tab can only be closed in the browser or by browser_close with an explicit tab_id");
|
|
1874
2107
|
await this.closeTab(command.tabId);
|
|
1875
2108
|
return;
|
|
1876
2109
|
default:
|
|
@@ -1921,12 +2154,17 @@ var BrowserViewService = class {
|
|
|
1921
2154
|
*/
|
|
1922
2155
|
async closeOwnTabs(sessionId, tabId) {
|
|
1923
2156
|
if (sessionId === void 0) throw new Error("browser_close needs the calling session: with no session there is no tab this call may close");
|
|
1924
|
-
if (this.session === void 0)
|
|
1925
|
-
|
|
2157
|
+
if (this.session === void 0) {
|
|
2158
|
+
if (tabId === void 0 || tabId === "" || this.resolveConfig().mode !== "cdp") return [];
|
|
2159
|
+
await this.requireSession();
|
|
2160
|
+
}
|
|
2161
|
+
const session = this.session;
|
|
2162
|
+
if (session === void 0) return [];
|
|
2163
|
+
const tabs = session.tabs;
|
|
1926
2164
|
if (tabId !== void 0 && tabId !== "") {
|
|
1927
2165
|
const found = tabs.list().find((tab) => tab.id === tabId);
|
|
1928
2166
|
if (found === void 0) throw new Error(`unknown tab "${tabId}"; call browser_tabs to see the open tabs`);
|
|
1929
|
-
if (found.sessionId !== sessionId) throw new Error(`tab "${tabId}" was not opened by this session, so it is not this session's to close`);
|
|
2167
|
+
if (session.mode !== "cdp" && found.sessionId !== sessionId) throw new Error(`tab "${tabId}" was not opened by this session, so it is not this session's to close`);
|
|
1930
2168
|
await this.closeTab(found.id);
|
|
1931
2169
|
return [found.id];
|
|
1932
2170
|
}
|
|
@@ -2246,12 +2484,20 @@ var BrowserViewService = class {
|
|
|
2246
2484
|
}
|
|
2247
2485
|
/** Open tabs, for `browser_tabs`. */
|
|
2248
2486
|
async listTabs() {
|
|
2249
|
-
if (this.session === void 0)
|
|
2487
|
+
if (this.session === void 0) {
|
|
2488
|
+
if (this.resolveConfig().mode !== "cdp") return {
|
|
2489
|
+
tabs: [],
|
|
2490
|
+
activeTabId: null
|
|
2491
|
+
};
|
|
2492
|
+
await this.requireSession();
|
|
2493
|
+
}
|
|
2494
|
+
const session = this.session;
|
|
2495
|
+
if (session === void 0) return {
|
|
2250
2496
|
tabs: [],
|
|
2251
2497
|
activeTabId: null
|
|
2252
2498
|
};
|
|
2253
2499
|
return {
|
|
2254
|
-
tabs:
|
|
2500
|
+
tabs: session.tabs.list(),
|
|
2255
2501
|
activeTabId: this.displayedOrDefault()
|
|
2256
2502
|
};
|
|
2257
2503
|
}
|
|
@@ -2263,6 +2509,26 @@ var BrowserViewService = class {
|
|
|
2263
2509
|
this.sinks.delete(sink);
|
|
2264
2510
|
}
|
|
2265
2511
|
}
|
|
2512
|
+
/** Apply a settings edit: detach the old source and restart an attached panel's stream. */
|
|
2513
|
+
async reconfigure() {
|
|
2514
|
+
if (this.disposed) return;
|
|
2515
|
+
this.generation += 1;
|
|
2516
|
+
const session = this.session;
|
|
2517
|
+
this.session = void 0;
|
|
2518
|
+
this.opening = void 0;
|
|
2519
|
+
this.bindings.clear();
|
|
2520
|
+
this.authTopUpSpent.clear();
|
|
2521
|
+
this.displayedTabId = null;
|
|
2522
|
+
this.selectionPinned = false;
|
|
2523
|
+
this.lastError = void 0;
|
|
2524
|
+
if (session !== void 0) await session.dispose();
|
|
2525
|
+
this.broadcast({
|
|
2526
|
+
v: 1,
|
|
2527
|
+
kind: "state",
|
|
2528
|
+
state: this.state()
|
|
2529
|
+
});
|
|
2530
|
+
if (this.sinks.size > 0) await this.startStreaming().catch(() => void 0);
|
|
2531
|
+
}
|
|
2266
2532
|
/**
|
|
2267
2533
|
* Tear everything down: stop pushing, stop streaming, close the socket, kill
|
|
2268
2534
|
* the browser.
|
|
@@ -2271,6 +2537,8 @@ var BrowserViewService = class {
|
|
|
2271
2537
|
async dispose() {
|
|
2272
2538
|
if (this.disposed) return;
|
|
2273
2539
|
this.disposed = true;
|
|
2540
|
+
this.generation += 1;
|
|
2541
|
+
this.opening = void 0;
|
|
2274
2542
|
const sinks = [...this.sinks];
|
|
2275
2543
|
this.sinks.clear();
|
|
2276
2544
|
for (const sink of sinks) try {
|
|
@@ -2304,15 +2572,21 @@ var BrowserViewService = class {
|
|
|
2304
2572
|
async requireSession() {
|
|
2305
2573
|
if (this.disposed) throw new Error("the browser view service is disposed");
|
|
2306
2574
|
if (this.session !== void 0) return this.session;
|
|
2307
|
-
this.opening
|
|
2308
|
-
|
|
2309
|
-
|
|
2310
|
-
|
|
2575
|
+
if (this.opening === void 0) this.opening = this.startSession();
|
|
2576
|
+
const opening = this.opening;
|
|
2577
|
+
try {
|
|
2578
|
+
return await opening;
|
|
2579
|
+
} finally {
|
|
2580
|
+
if (this.opening === opening) this.opening = void 0;
|
|
2581
|
+
}
|
|
2311
2582
|
}
|
|
2312
2583
|
/** Start the browser once, recording failures for the panel. */
|
|
2313
2584
|
async startSession() {
|
|
2585
|
+
const generation = this.generation;
|
|
2586
|
+
const config = this.resolveConfig();
|
|
2314
2587
|
try {
|
|
2315
2588
|
const session = await this.openSession({
|
|
2589
|
+
...config,
|
|
2316
2590
|
jpegQuality: this.jpegQuality,
|
|
2317
2591
|
viewport: this.viewport,
|
|
2318
2592
|
onFrame: (frame) => {
|
|
@@ -2330,6 +2604,10 @@ var BrowserViewService = class {
|
|
|
2330
2604
|
});
|
|
2331
2605
|
}
|
|
2332
2606
|
});
|
|
2607
|
+
if (generation !== this.generation || this.disposed) {
|
|
2608
|
+
await session.dispose();
|
|
2609
|
+
throw new Error("the browser configuration changed while the session was opening");
|
|
2610
|
+
}
|
|
2333
2611
|
this.session = session;
|
|
2334
2612
|
this.lastError = void 0;
|
|
2335
2613
|
this.broadcast({
|
|
@@ -2339,19 +2617,21 @@ var BrowserViewService = class {
|
|
|
2339
2617
|
});
|
|
2340
2618
|
return session;
|
|
2341
2619
|
} catch (error) {
|
|
2342
|
-
|
|
2343
|
-
|
|
2344
|
-
|
|
2345
|
-
|
|
2346
|
-
|
|
2347
|
-
|
|
2348
|
-
|
|
2620
|
+
if (generation === this.generation && !this.disposed) {
|
|
2621
|
+
this.lastError = error instanceof Error ? error.message : String(error);
|
|
2622
|
+
this.broadcast({
|
|
2623
|
+
v: 1,
|
|
2624
|
+
kind: "error",
|
|
2625
|
+
code: config.mode === "cdp" ? "browser-connect-failed" : "browser-launch-failed",
|
|
2626
|
+
message: this.lastError
|
|
2627
|
+
});
|
|
2628
|
+
}
|
|
2349
2629
|
throw error;
|
|
2350
2630
|
}
|
|
2351
2631
|
}
|
|
2352
2632
|
/** Start the screencast for the tab the panel watches. */
|
|
2353
2633
|
async startStreaming() {
|
|
2354
|
-
const session = this.session;
|
|
2634
|
+
const session = this.session ?? (this.resolveConfig().mode === "cdp" && this.sinks.size > 0 ? await this.requireSession() : void 0);
|
|
2355
2635
|
if (session === void 0 || this.sinks.size === 0) return;
|
|
2356
2636
|
const tabId = this.displayedOrDefault();
|
|
2357
2637
|
if (tabId === null) return;
|
|
@@ -10003,7 +10283,11 @@ const TAB_VIEW_SCHEMA = {
|
|
|
10003
10283
|
type: "boolean",
|
|
10004
10284
|
required: true
|
|
10005
10285
|
},
|
|
10006
|
-
sessionId: { type: "string" }
|
|
10286
|
+
sessionId: { type: "string" },
|
|
10287
|
+
origin: {
|
|
10288
|
+
type: "string",
|
|
10289
|
+
description: "created when DSH opened the target; adopted when it already existed in the real browser."
|
|
10290
|
+
}
|
|
10007
10291
|
}
|
|
10008
10292
|
};
|
|
10009
10293
|
/**
|
|
@@ -10016,7 +10300,7 @@ function createBrowserTools(deps) {
|
|
|
10016
10300
|
return [
|
|
10017
10301
|
defineTool({
|
|
10018
10302
|
name: "browser_navigate",
|
|
10019
|
-
description: "Navigate the
|
|
10303
|
+
description: "Navigate the selected browser tab to a URL and report the resulting page (url, title). This is a real Chromium either launched by the host or attached over CDP, so the page renders, runs scripts, and makes network requests exactly as it would for a user. Use it to inspect a local page or site, to verify rendered markup, or to set up a page before a snapshot, click, or screenshot. After navigating, call browser_snapshot before clicking anything: refs from before the navigation no longer exist. Without new_tab the call reuses this session's current tab, so the console and network buffers start over with the new document. Set new_tab to true to keep the current page and open the URL in an additional tab (browser_tabs then lists both). 中文触发词:打开网页、浏览器里看一下、访问这个地址、渲染页面、新标签页。",
|
|
10020
10304
|
parameters: {
|
|
10021
10305
|
url: {
|
|
10022
10306
|
type: "string",
|
|
@@ -10448,7 +10732,7 @@ title: ${value.title}
|
|
|
10448
10732
|
}),
|
|
10449
10733
|
defineTool({
|
|
10450
10734
|
name: "browser_tabs",
|
|
10451
|
-
description: "List the browser tabs this plugin currently has open, with their ids, urls, titles, and loading state. Call it when you need a tab id, when you are unsure whether a browser is running at all, or after the user closed a tab in the panel.
|
|
10735
|
+
description: "List the browser tabs this plugin currently has open, with their ids, urls, titles, and loading state. Call it when you need a tab id, when you are unsure whether a browser is running at all, or after the user closed a tab in the panel. In isolated mode it never starts a browser; in real-browser mode it connects to the configured CDP endpoint and discovers existing page tabs. 中文触发词:有多少标签页、当前浏览器状态、打开着哪些页面。",
|
|
10452
10736
|
parameters: {},
|
|
10453
10737
|
output: {
|
|
10454
10738
|
schema: {
|
|
@@ -10479,7 +10763,7 @@ title: ${value.title}
|
|
|
10479
10763
|
}),
|
|
10480
10764
|
defineTool({
|
|
10481
10765
|
name: "browser_close",
|
|
10482
|
-
description: "Close
|
|
10766
|
+
description: "Close browser tabs within the ownership rules of the selected mode. Call it when the work in the browser is finished: a tab left open is a page still running on the host and a chip still sitting in the panel, and the next thing anyone sees is somebody else's leftover. Without tab_id it closes only tabs this session opened. In isolated mode an explicit tab_id must also belong to this session; in real-browser mode an explicit tab_id may close any listed page, including an adopted one. A session that owns no tab and omits tab_id closes nothing and still succeeds, so cleanup is safe to make unconditionally. 中文触发词:关掉网页、关闭标签页、收尾清理。",
|
|
10483
10767
|
parameters: { tab_id: {
|
|
10484
10768
|
type: "string",
|
|
10485
10769
|
description: "Tab id from browser_tabs or a previous result. Omit to close every tab this session opened."
|
|
@@ -10504,6 +10788,20 @@ title: ${value.title}
|
|
|
10504
10788
|
];
|
|
10505
10789
|
}
|
|
10506
10790
|
//#endregion
|
|
10791
|
+
//#region src/host/config.ts
|
|
10792
|
+
/** Settings schema exported through the host loader entry. */
|
|
10793
|
+
const Config = Schema.object({
|
|
10794
|
+
realBrowserEnabled: Schema.boolean().default(false).volatile().description("Connect the browser tools to a real Chrome/Edge over CDP instead of launching an isolated temporary browser. Off by default."),
|
|
10795
|
+
cdpEndpoint: Schema.string().default("http://127.0.0.1:9222").volatile().description("Loopback CDP endpoint only. Chrome 136+ ignores remote debugging on its default data directory; use a browser profile explicitly started with remote debugging and a non-default --user-data-dir.")
|
|
10796
|
+
});
|
|
10797
|
+
/** Read the current values, including edits committed without a plugin remount. */
|
|
10798
|
+
function effectiveConfig(config) {
|
|
10799
|
+
return {
|
|
10800
|
+
realBrowserEnabled: config?.realBrowserEnabled?.get() ?? false,
|
|
10801
|
+
cdpEndpoint: config?.cdpEndpoint?.get() ?? "http://127.0.0.1:9222"
|
|
10802
|
+
};
|
|
10803
|
+
}
|
|
10804
|
+
//#endregion
|
|
10507
10805
|
//#region src/index.ts
|
|
10508
10806
|
/** Stable cordis plugin name. */
|
|
10509
10807
|
const name = "browser-view";
|
|
@@ -10513,16 +10811,25 @@ const inject = ["webServer", "tools"];
|
|
|
10513
10811
|
* Mount the routes, the tools, and the browser lifetime.
|
|
10514
10812
|
* @param ctx - host plugin context carrying webServer and tools.
|
|
10515
10813
|
*/
|
|
10516
|
-
function apply(ctx) {
|
|
10517
|
-
const service = new BrowserViewService({
|
|
10518
|
-
|
|
10519
|
-
|
|
10520
|
-
|
|
10521
|
-
|
|
10522
|
-
|
|
10523
|
-
|
|
10814
|
+
function apply(ctx, config) {
|
|
10815
|
+
const service = new BrowserViewService({
|
|
10816
|
+
resolveConfig: () => {
|
|
10817
|
+
const value = effectiveConfig(config);
|
|
10818
|
+
return value.realBrowserEnabled ? {
|
|
10819
|
+
mode: "cdp",
|
|
10820
|
+
cdpEndpoint: value.cdpEndpoint
|
|
10821
|
+
} : { mode: "isolated" };
|
|
10822
|
+
},
|
|
10823
|
+
authenticatedUrl: (url) => {
|
|
10824
|
+
const connection = ctx.get("connection");
|
|
10825
|
+
if (connection === void 0 || typeof connection.authenticatedUrl !== "function") return void 0;
|
|
10826
|
+
try {
|
|
10827
|
+
return connection.authenticatedUrl(url);
|
|
10828
|
+
} catch {
|
|
10829
|
+
return;
|
|
10830
|
+
}
|
|
10524
10831
|
}
|
|
10525
|
-
}
|
|
10832
|
+
});
|
|
10526
10833
|
const saveImage = async (input) => {
|
|
10527
10834
|
const attachments = ctx.get("attachments");
|
|
10528
10835
|
if (attachments === void 0 || typeof attachments.saveImage !== "function") throw new Error("browser_screenshot needs the host attachment service to store the image; this host mounts none");
|
|
@@ -10547,9 +10854,17 @@ function apply(ctx) {
|
|
|
10547
10854
|
for (const dispose of disposers) dispose();
|
|
10548
10855
|
};
|
|
10549
10856
|
}, "browser-view: tools");
|
|
10857
|
+
ctx.effect(() => {
|
|
10858
|
+
const dispose = ctx.on("loader/volatile-update", (paths) => {
|
|
10859
|
+
if (paths.some((path) => path[0] === "realBrowserEnabled" || path[0] === "cdpEndpoint")) service.reconfigure().catch(() => void 0);
|
|
10860
|
+
});
|
|
10861
|
+
return () => {
|
|
10862
|
+
dispose();
|
|
10863
|
+
};
|
|
10864
|
+
}, "browser-view: settings");
|
|
10550
10865
|
ctx.effect(() => () => {
|
|
10551
10866
|
service.dispose();
|
|
10552
10867
|
}, "browser-view: browser");
|
|
10553
10868
|
}
|
|
10554
10869
|
//#endregion
|
|
10555
|
-
export { apply, inject, name };
|
|
10870
|
+
export { Config, apply, inject, name };
|