feat: 接入网页批三——数据采集家族(14 id)与网页监听三件套
This commit is contained in:
1 parent
2ce0c13f7b
commit
4ac9755416
3 files changed
+161
No files matched your search
@@ -820,6 +820,65 @@ export class BrowserSession implements BrowserLike {
|
||||
return true;
|
||||
}
|
||||
|
||||
startNetworkListening(): boolean {
|
||||
const flags = globalThis as { __rpaNetListening?: boolean; __rpaNetListener?: unknown; __rpaNetCaptures?: Array<{ requestId: string; url: string; status: number; mimeType: string }> };
|
||||
if (!this.win.isDestroyed() && !this.win.webContents.debugger.isAttached()) this.win.webContents.debugger.attach("1.3");
|
||||
this.cdp("Network.enable").catch(() => {});
|
||||
if (flags.__rpaNetListening) return false;
|
||||
flags.__rpaNetListening = true;
|
||||
flags.__rpaNetCaptures = [];
|
||||
const listener = (_event: unknown, method: string, params: Record<string, unknown>) => {
|
||||
if (method !== "Network.responseReceived") return;
|
||||
const response = params.response as { url?: string; status?: number; mimeType?: string } | undefined;
|
||||
const request = params.requestId as string | undefined;
|
||||
if (!response || !request) return;
|
||||
flags.__rpaNetCaptures?.push({ requestId: request, url: response.url ?? "", status: response.status ?? 0, mimeType: response.mimeType ?? "" });
|
||||
if ((flags.__rpaNetCaptures?.length ?? 0) > 2000) flags.__rpaNetCaptures?.shift();
|
||||
};
|
||||
flags.__rpaNetListener = listener;
|
||||
this.win.webContents.debugger.on("message", listener);
|
||||
return true;
|
||||
}
|
||||
|
||||
async getNetworkCaptures(options: { url?: string; wildcard?: boolean; resourceType?: string; body?: boolean } = {}): Promise<Array<Record<string, unknown>>> {
|
||||
const flags = globalThis as { __rpaNetListening?: boolean; __rpaNetCaptures?: Array<{ requestId: string; url: string; status: number; mimeType: string }> };
|
||||
let captures = flags.__rpaNetCaptures ?? [];
|
||||
if (options.resourceType) {
|
||||
const want = options.resourceType.toLowerCase();
|
||||
captures = captures.filter(c => c.mimeType.toLowerCase().includes(want));
|
||||
}
|
||||
if (options.url) {
|
||||
const keyword = options.url;
|
||||
captures = options.wildcard
|
||||
? captures.filter(c => new RegExp("^" + keyword.replace(/[.*+?^${}()|[\]\\]/g, "\\ async currentUrl(): Promise<string> {").replace(/\*/g, ".*")).test(c.url))
|
||||
: captures.filter(c => c.url.includes(keyword));
|
||||
}
|
||||
const result: Array<Record<string, unknown>> = [];
|
||||
for (const c of captures.slice(-200)) {
|
||||
const item: Record<string, unknown> = { url: c.url, status: c.status, mimeType: c.mimeType };
|
||||
if (options.body && /json|text|javascript/i.test(c.mimeType)) {
|
||||
try {
|
||||
const body = await this.cdp<{ body?: string }>("Network.getResponseBody", { requestId: c.requestId });
|
||||
item.body = body.body ?? "";
|
||||
} catch { item.body = ""; }
|
||||
}
|
||||
result.push(item);
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
stopNetworkListening(): boolean {
|
||||
const flags = globalThis as { __rpaNetListening?: boolean; __rpaNetListener?: unknown };
|
||||
if (!flags.__rpaNetListening) return false;
|
||||
if (flags.__rpaNetListener) {
|
||||
this.win.webContents.debugger.removeListener("message", flags.__rpaNetListener as never);
|
||||
}
|
||||
flags.__rpaNetListening = false;
|
||||
flags.__rpaNetListener = undefined;
|
||||
this.cdp("Network.disable").catch(() => {});
|
||||
return true;
|
||||
}
|
||||
|
||||
async currentUrl(): Promise<string> {
|
||||
return this.win.webContents.getURL();
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user