wide-cli 0.3.101126 → 0.5.161126
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.md +6 -5
- package/dist/agent/context.d.ts.map +1 -1
- package/dist/agent/context.js +5 -1
- package/dist/agent/context.js.map +1 -1
- package/dist/agent/index.d.ts +33 -4
- package/dist/agent/index.d.ts.map +1 -1
- package/dist/agent/index.js +213 -79
- package/dist/agent/index.js.map +1 -1
- package/dist/completion.d.ts +13 -0
- package/dist/completion.d.ts.map +1 -0
- package/dist/completion.js +170 -0
- package/dist/completion.js.map +1 -0
- package/dist/config/index.d.ts +4 -1
- package/dist/config/index.d.ts.map +1 -1
- package/dist/config/index.js +35 -7
- package/dist/config/index.js.map +1 -1
- package/dist/config/logger.d.ts +4 -0
- package/dist/config/logger.d.ts.map +1 -0
- package/dist/config/logger.js +13 -0
- package/dist/config/logger.js.map +1 -0
- package/dist/decision/index.d.ts.map +1 -1
- package/dist/decision/index.js +9 -7
- package/dist/decision/index.js.map +1 -1
- package/dist/index.js +205 -40
- package/dist/index.js.map +1 -1
- package/dist/providers/claude.d.ts +2 -2
- package/dist/providers/claude.d.ts.map +1 -1
- package/dist/providers/claude.js +54 -38
- package/dist/providers/claude.js.map +1 -1
- package/dist/providers/gemini.d.ts +2 -2
- package/dist/providers/gemini.d.ts.map +1 -1
- package/dist/providers/gemini.js +50 -34
- package/dist/providers/gemini.js.map +1 -1
- package/dist/providers/index.d.ts +6 -0
- package/dist/providers/index.d.ts.map +1 -1
- package/dist/providers/index.js +26 -7
- package/dist/providers/index.js.map +1 -1
- package/dist/providers/ollama.d.ts +7 -2
- package/dist/providers/ollama.d.ts.map +1 -1
- package/dist/providers/ollama.js +90 -15
- package/dist/providers/ollama.js.map +1 -1
- package/dist/providers/openai.d.ts +2 -2
- package/dist/providers/openai.d.ts.map +1 -1
- package/dist/providers/openai.js +60 -44
- package/dist/providers/openai.js.map +1 -1
- package/dist/providers/retry.d.ts +7 -0
- package/dist/providers/retry.d.ts.map +1 -0
- package/dist/providers/retry.js +72 -0
- package/dist/providers/retry.js.map +1 -0
- package/dist/providers/types.d.ts +2 -1
- package/dist/providers/types.d.ts.map +1 -1
- package/dist/repl/commands.d.ts +18 -0
- package/dist/repl/commands.d.ts.map +1 -0
- package/dist/repl/commands.js +351 -0
- package/dist/repl/commands.js.map +1 -0
- package/dist/repl/history.d.ts +3 -0
- package/dist/repl/history.d.ts.map +1 -0
- package/dist/repl/history.js +57 -0
- package/dist/repl/history.js.map +1 -0
- package/dist/repl/index.d.ts +9 -1
- package/dist/repl/index.d.ts.map +1 -1
- package/dist/repl/index.js +326 -75
- package/dist/repl/index.js.map +1 -1
- package/dist/repl/mentions.d.ts +8 -0
- package/dist/repl/mentions.d.ts.map +1 -0
- package/dist/repl/mentions.js +100 -0
- package/dist/repl/mentions.js.map +1 -0
- package/dist/repl/sessions.d.ts +21 -0
- package/dist/repl/sessions.d.ts.map +1 -0
- package/dist/repl/sessions.js +117 -0
- package/dist/repl/sessions.js.map +1 -0
- package/dist/repl/setup.d.ts +4 -0
- package/dist/repl/setup.d.ts.map +1 -0
- package/dist/repl/setup.js +73 -0
- package/dist/repl/setup.js.map +1 -0
- package/dist/security/fuzzer.d.ts +11 -1
- package/dist/security/fuzzer.d.ts.map +1 -1
- package/dist/security/fuzzer.js +131 -60
- package/dist/security/fuzzer.js.map +1 -1
- package/dist/security/loader.d.ts +15 -1
- package/dist/security/loader.d.ts.map +1 -1
- package/dist/security/loader.js +251 -11
- package/dist/security/loader.js.map +1 -1
- package/dist/security/rules.d.ts +2 -0
- package/dist/security/rules.d.ts.map +1 -1
- package/dist/security/rules.js +108 -15
- package/dist/security/rules.js.map +1 -1
- package/dist/security/sca.d.ts +39 -0
- package/dist/security/sca.d.ts.map +1 -0
- package/dist/security/sca.js +464 -0
- package/dist/security/sca.js.map +1 -0
- package/dist/security/scanner.d.ts +19 -1
- package/dist/security/scanner.d.ts.map +1 -1
- package/dist/security/scanner.js +294 -77
- package/dist/security/scanner.js.map +1 -1
- package/dist/security/secrets.d.ts +5 -0
- package/dist/security/secrets.d.ts.map +1 -0
- package/dist/security/secrets.js +109 -0
- package/dist/security/secrets.js.map +1 -0
- package/dist/security/taint.d.ts +14 -0
- package/dist/security/taint.d.ts.map +1 -0
- package/dist/security/taint.js +517 -0
- package/dist/security/taint.js.map +1 -0
- package/dist/tools/browser.d.ts +63 -0
- package/dist/tools/browser.d.ts.map +1 -1
- package/dist/tools/browser.js +1736 -72
- package/dist/tools/browser.js.map +1 -1
- package/dist/tools/db.d.ts +106 -0
- package/dist/tools/db.d.ts.map +1 -0
- package/dist/tools/db.js +1526 -0
- package/dist/tools/db.js.map +1 -0
- package/dist/tools/fs.d.ts.map +1 -1
- package/dist/tools/fs.js +48 -14
- package/dist/tools/fs.js.map +1 -1
- package/dist/tools/http.d.ts.map +1 -1
- package/dist/tools/http.js +7 -2
- package/dist/tools/http.js.map +1 -1
- package/dist/tools/index.d.ts.map +1 -1
- package/dist/tools/index.js +24 -2
- package/dist/tools/index.js.map +1 -1
- package/dist/tools/risk.d.ts +9 -0
- package/dist/tools/risk.d.ts.map +1 -0
- package/dist/tools/risk.js +156 -0
- package/dist/tools/risk.js.map +1 -0
- package/dist/tools/search.d.ts.map +1 -1
- package/dist/tools/search.js +31 -7
- package/dist/tools/search.js.map +1 -1
- package/dist/tools/security.d.ts.map +1 -1
- package/dist/tools/security.js +26 -8
- package/dist/tools/security.js.map +1 -1
- package/dist/tools/shell.d.ts +8 -1
- package/dist/tools/shell.d.ts.map +1 -1
- package/dist/tools/shell.js +169 -30
- package/dist/tools/shell.js.map +1 -1
- package/dist/tools/types.d.ts +12 -0
- package/dist/tools/types.d.ts.map +1 -1
- package/dist/ui/banner.d.ts +22 -0
- package/dist/ui/banner.d.ts.map +1 -1
- package/dist/ui/banner.js +391 -11
- package/dist/ui/banner.js.map +1 -1
- package/dist/ui/diff.d.ts +13 -1
- package/dist/ui/diff.d.ts.map +1 -1
- package/dist/ui/diff.js +39 -10
- package/dist/ui/diff.js.map +1 -1
- package/dist/ui/spinner.d.ts +2 -0
- package/dist/ui/spinner.d.ts.map +1 -1
- package/dist/ui/spinner.js +22 -9
- package/dist/ui/spinner.js.map +1 -1
- package/package.json +12 -1
- package/.github/workflows/ci.yml +0 -29
- package/assets/logo.png +0 -0
- package/assets/wide-logo.png +0 -0
- package/src/agent/context.ts +0 -44
- package/src/agent/index.ts +0 -166
- package/src/config/index.ts +0 -88
- package/src/config/workspace.ts +0 -39
- package/src/decision/index.ts +0 -528
- package/src/index.ts +0 -180
- package/src/providers/claude.ts +0 -163
- package/src/providers/gemini.ts +0 -147
- package/src/providers/index.ts +0 -52
- package/src/providers/ollama.ts +0 -108
- package/src/providers/openai.ts +0 -159
- package/src/providers/types.ts +0 -29
- package/src/repl/index.ts +0 -100
- package/src/security/fuzzer.ts +0 -140
- package/src/security/loader.ts +0 -129
- package/src/security/payloads.ts +0 -95
- package/src/security/rules.ts +0 -300
- package/src/security/scanner.ts +0 -180
- package/src/tools/browser.ts +0 -528
- package/src/tools/fs.ts +0 -143
- package/src/tools/git.ts +0 -51
- package/src/tools/http.ts +0 -77
- package/src/tools/index.ts +0 -64
- package/src/tools/search.ts +0 -109
- package/src/tools/security.ts +0 -140
- package/src/tools/shell.ts +0 -56
- package/src/tools/types.ts +0 -33
- package/src/ui/banner.ts +0 -17
- package/src/ui/colors.ts +0 -19
- package/src/ui/diff.ts +0 -24
- package/src/ui/markdown.ts +0 -34
- package/src/ui/spinner.ts +0 -53
- package/tests/browser.test.mjs +0 -71
- package/tests/decision.test.mjs +0 -68
- package/tests/loader.test.mjs +0 -24
- package/tests/suite.test.mjs +0 -61
- package/tsconfig.json +0 -20
package/dist/tools/browser.js
CHANGED
|
@@ -1,5 +1,758 @@
|
|
|
1
|
+
import * as crypto from "node:crypto";
|
|
1
2
|
import * as fs from "node:fs";
|
|
3
|
+
import * as http from "node:http";
|
|
4
|
+
import * as os from "node:os";
|
|
2
5
|
import * as path from "node:path";
|
|
6
|
+
import { spawn } from "node:child_process";
|
|
7
|
+
import { createRequire } from "node:module";
|
|
8
|
+
const BROWSER_EMOJI_ZWJ_RE = /(?:\p{Extended_Pictographic}|\p{Emoji_Presentation}|[\u2600-\u27BF])(?:\uFE0F|\uFE0E|[\u{1F3FB}-\u{1F3FF}])?(?:\u200D(?:\p{Extended_Pictographic}|\p{Emoji_Presentation}|[\u2600-\u27BF])(?:\uFE0F|\uFE0E|[\u{1F3FB}-\u{1F3FF}])?)+/gu;
|
|
9
|
+
const BROWSER_EMOJI_KEYCAP_RE = /[0-9#*]\uFE0F?\u20E3/gu;
|
|
10
|
+
const BROWSER_EMOJI_SUPP_RE = /[\u{1F1E6}-\u{1F1FF}\u{1F3FB}-\u{1F3FF}\u{E0020}-\u{E007F}]/gu;
|
|
11
|
+
const BROWSER_EMOJI_PICT_RE = /\p{Extended_Pictographic}(?:\uFE0F|\uFE0E)?/gu;
|
|
12
|
+
const BROWSER_EMOJI_PRES_RE = /\p{Emoji_Presentation}(?:\uFE0F|\uFE0E)?/gu;
|
|
13
|
+
const BROWSER_EMOJI_BMP_RE = /[\u231A-\u23FF\u2600-\u27BF\u2B05-\u2B55][\uFE0E\uFE0F]?|[\u2139\u2194-\u21AA\u24C2\u25AA-\u25FE\u2934-\u2935\u3030\u303D\u3297\u3299]\uFE0F|[\uFE0E\uFE0F\u200D\u20E3]/gu;
|
|
14
|
+
export function stripBrowserEmojis(text) {
|
|
15
|
+
return String(text ?? "")
|
|
16
|
+
.replace(BROWSER_EMOJI_ZWJ_RE, "")
|
|
17
|
+
.replace(BROWSER_EMOJI_KEYCAP_RE, "")
|
|
18
|
+
.replace(BROWSER_EMOJI_SUPP_RE, "")
|
|
19
|
+
.replace(BROWSER_EMOJI_PICT_RE, "")
|
|
20
|
+
.replace(BROWSER_EMOJI_PRES_RE, "")
|
|
21
|
+
.replace(BROWSER_EMOJI_BMP_RE, "")
|
|
22
|
+
.replace(/[\uD800-\uDFFF]/g, "")
|
|
23
|
+
.replace(/[ \t]{2,}/g, " ")
|
|
24
|
+
.trim();
|
|
25
|
+
}
|
|
26
|
+
const HAR_SENSITIVE_HEADERS = new Set([
|
|
27
|
+
"authorization",
|
|
28
|
+
"proxy-authorization",
|
|
29
|
+
"cookie",
|
|
30
|
+
"set-cookie",
|
|
31
|
+
"x-api-key",
|
|
32
|
+
"x-auth-token",
|
|
33
|
+
"x-csrf-token",
|
|
34
|
+
]);
|
|
35
|
+
function redactHarHeaders(headers) {
|
|
36
|
+
const out = [];
|
|
37
|
+
if (Array.isArray(headers)) {
|
|
38
|
+
for (const h of headers) {
|
|
39
|
+
if (!h?.name)
|
|
40
|
+
continue;
|
|
41
|
+
const name = String(h.name);
|
|
42
|
+
out.push({ name, value: HAR_SENSITIVE_HEADERS.has(name.toLowerCase()) ? "[REDACTED]" : String(h.value ?? "") });
|
|
43
|
+
}
|
|
44
|
+
return out;
|
|
45
|
+
}
|
|
46
|
+
if (headers && typeof headers === "object") {
|
|
47
|
+
for (const [name, value] of Object.entries(headers)) {
|
|
48
|
+
out.push({ name, value: HAR_SENSITIVE_HEADERS.has(name.toLowerCase()) ? "[REDACTED]" : String(value ?? "") });
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
return out;
|
|
52
|
+
}
|
|
53
|
+
function parseHarQuery(urlStr) {
|
|
54
|
+
try {
|
|
55
|
+
const parsed = new URL(urlStr);
|
|
56
|
+
const out = [];
|
|
57
|
+
for (const [name, value] of parsed.searchParams.entries()) {
|
|
58
|
+
out.push({ name, value });
|
|
59
|
+
}
|
|
60
|
+
return out;
|
|
61
|
+
}
|
|
62
|
+
catch {
|
|
63
|
+
return [];
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
const CLI_STEALTH_INIT_SCRIPT = `(() => {
|
|
67
|
+
try { Object.defineProperty(navigator, "webdriver", { get: () => undefined, configurable: true }); } catch {}
|
|
68
|
+
try {
|
|
69
|
+
if (!navigator.languages || navigator.languages.length === 0) {
|
|
70
|
+
Object.defineProperty(navigator, "languages", { get: () => ["en-US", "en"], configurable: true });
|
|
71
|
+
}
|
|
72
|
+
} catch {}
|
|
73
|
+
try {
|
|
74
|
+
if (!navigator.plugins || navigator.plugins.length === 0) {
|
|
75
|
+
Object.defineProperty(navigator, "plugins", {
|
|
76
|
+
get: () => [
|
|
77
|
+
{ name: "Chrome PDF Plugin", filename: "internal-pdf-viewer", description: "Portable Document Format" },
|
|
78
|
+
{ name: "Chrome PDF Viewer", filename: "mhjfbmdgcfjbbpaeojofohoefgiehjai", description: "" },
|
|
79
|
+
{ name: "Native Client", filename: "internal-nacl-plugin", description: "" }
|
|
80
|
+
],
|
|
81
|
+
configurable: true
|
|
82
|
+
});
|
|
83
|
+
}
|
|
84
|
+
} catch {}
|
|
85
|
+
try {
|
|
86
|
+
if (!window.chrome) {
|
|
87
|
+
Object.defineProperty(window, "chrome", { value: { runtime: {}, app: { isInstalled: false } }, configurable: true });
|
|
88
|
+
}
|
|
89
|
+
} catch {}
|
|
90
|
+
})();`;
|
|
91
|
+
class NodeCdpWebSocket {
|
|
92
|
+
readyState = 0;
|
|
93
|
+
socket = null;
|
|
94
|
+
rxBuf = Buffer.alloc(0);
|
|
95
|
+
fragOpcode = 0;
|
|
96
|
+
fragChunks = [];
|
|
97
|
+
listeners = {
|
|
98
|
+
open: [],
|
|
99
|
+
message: [],
|
|
100
|
+
error: [],
|
|
101
|
+
close: []
|
|
102
|
+
};
|
|
103
|
+
constructor(urlStr) {
|
|
104
|
+
const u = new URL(urlStr);
|
|
105
|
+
if (u.protocol !== "ws:") {
|
|
106
|
+
queueMicrotask(() => this.failConnect(new Error(`Unsupported WebSocket scheme: ${u.protocol}`)));
|
|
107
|
+
return;
|
|
108
|
+
}
|
|
109
|
+
const key = crypto.randomBytes(16).toString("base64");
|
|
110
|
+
const expectedAccept = crypto
|
|
111
|
+
.createHash("sha1")
|
|
112
|
+
.update(key + "258EAFA5-E914-47DA-95CA-C5AB0DC85B11")
|
|
113
|
+
.digest("base64");
|
|
114
|
+
const req = http.request({
|
|
115
|
+
hostname: u.hostname,
|
|
116
|
+
port: Number(u.port || 80),
|
|
117
|
+
path: `${u.pathname}${u.search}`,
|
|
118
|
+
method: "GET",
|
|
119
|
+
headers: {
|
|
120
|
+
Host: u.host,
|
|
121
|
+
Connection: "Upgrade",
|
|
122
|
+
Upgrade: "websocket",
|
|
123
|
+
"Sec-WebSocket-Key": key,
|
|
124
|
+
"Sec-WebSocket-Version": "13"
|
|
125
|
+
}
|
|
126
|
+
});
|
|
127
|
+
req.on("upgrade", (res, socket, head) => {
|
|
128
|
+
const accept = String(res.headers["sec-websocket-accept"] || "").trim();
|
|
129
|
+
if (accept !== expectedAccept) {
|
|
130
|
+
socket.destroy();
|
|
131
|
+
this.failConnect(new Error("WebSocket handshake validation failed"));
|
|
132
|
+
return;
|
|
133
|
+
}
|
|
134
|
+
this.socket = socket;
|
|
135
|
+
socket.setNoDelay(true);
|
|
136
|
+
this.readyState = 1;
|
|
137
|
+
this.emit("open", {});
|
|
138
|
+
socket.on("data", (chunk) => {
|
|
139
|
+
this.consume(chunk);
|
|
140
|
+
});
|
|
141
|
+
socket.on("error", (err) => {
|
|
142
|
+
if (this.readyState !== 3) {
|
|
143
|
+
this.emit("error", err);
|
|
144
|
+
}
|
|
145
|
+
});
|
|
146
|
+
socket.on("close", () => {
|
|
147
|
+
if (this.readyState !== 3) {
|
|
148
|
+
this.readyState = 3;
|
|
149
|
+
this.emit("close", {});
|
|
150
|
+
}
|
|
151
|
+
});
|
|
152
|
+
if (head && head.length > 0) {
|
|
153
|
+
this.consume(head);
|
|
154
|
+
}
|
|
155
|
+
});
|
|
156
|
+
req.on("response", (res) => {
|
|
157
|
+
res.resume();
|
|
158
|
+
this.failConnect(new Error(`WebSocket upgrade rejected with HTTP ${res.statusCode}`));
|
|
159
|
+
});
|
|
160
|
+
req.on("error", (err) => {
|
|
161
|
+
this.failConnect(err);
|
|
162
|
+
});
|
|
163
|
+
req.end();
|
|
164
|
+
}
|
|
165
|
+
addEventListener(type, handler) {
|
|
166
|
+
if (!this.listeners[type]) {
|
|
167
|
+
this.listeners[type] = [];
|
|
168
|
+
}
|
|
169
|
+
this.listeners[type].push(handler);
|
|
170
|
+
}
|
|
171
|
+
emit(type, ev) {
|
|
172
|
+
const handlers = this.listeners[type];
|
|
173
|
+
if (!handlers || handlers.length === 0)
|
|
174
|
+
return;
|
|
175
|
+
for (const fn of handlers.slice()) {
|
|
176
|
+
try {
|
|
177
|
+
fn(ev);
|
|
178
|
+
}
|
|
179
|
+
catch {
|
|
180
|
+
}
|
|
181
|
+
}
|
|
182
|
+
}
|
|
183
|
+
failConnect(err) {
|
|
184
|
+
if (this.readyState === 3)
|
|
185
|
+
return;
|
|
186
|
+
this.readyState = 3;
|
|
187
|
+
this.emit("error", err);
|
|
188
|
+
this.emit("close", {});
|
|
189
|
+
}
|
|
190
|
+
send(data) {
|
|
191
|
+
if (this.readyState !== 1 || !this.socket || this.socket.destroyed) {
|
|
192
|
+
throw new Error("WebSocket is not open");
|
|
193
|
+
}
|
|
194
|
+
this.sendFrame(0x1, Buffer.from(String(data), "utf8"));
|
|
195
|
+
}
|
|
196
|
+
sendFrame(opcode, payload) {
|
|
197
|
+
if (!this.socket || this.socket.destroyed)
|
|
198
|
+
return;
|
|
199
|
+
const len = payload.length;
|
|
200
|
+
const hdrLen = len < 126 ? 2 : len <= 65535 ? 4 : 10;
|
|
201
|
+
const frame = Buffer.allocUnsafe(hdrLen + 4 + len);
|
|
202
|
+
frame[0] = 0x80 | (opcode & 0x0f);
|
|
203
|
+
if (len < 126) {
|
|
204
|
+
frame[1] = 0x80 | len;
|
|
205
|
+
}
|
|
206
|
+
else if (len <= 65535) {
|
|
207
|
+
frame[1] = 0x80 | 126;
|
|
208
|
+
frame.writeUInt16BE(len, 2);
|
|
209
|
+
}
|
|
210
|
+
else {
|
|
211
|
+
frame[1] = 0x80 | 127;
|
|
212
|
+
frame.writeBigUInt64BE(BigInt(len), 2);
|
|
213
|
+
}
|
|
214
|
+
const mask = crypto.randomBytes(4);
|
|
215
|
+
mask.copy(frame, hdrLen);
|
|
216
|
+
const bodyOffset = hdrLen + 4;
|
|
217
|
+
for (let i = 0; i < len; i++) {
|
|
218
|
+
frame[bodyOffset + i] = payload[i] ^ mask[i & 3];
|
|
219
|
+
}
|
|
220
|
+
this.socket.write(frame);
|
|
221
|
+
}
|
|
222
|
+
consume(chunk) {
|
|
223
|
+
this.rxBuf = this.rxBuf.length > 0 ? Buffer.concat([this.rxBuf, chunk]) : chunk;
|
|
224
|
+
while (this.rxBuf.length >= 2) {
|
|
225
|
+
const b0 = this.rxBuf[0];
|
|
226
|
+
const b1 = this.rxBuf[1];
|
|
227
|
+
const fin = (b0 & 0x80) !== 0;
|
|
228
|
+
const opcode = b0 & 0x0f;
|
|
229
|
+
const masked = (b1 & 0x80) !== 0;
|
|
230
|
+
let len = b1 & 0x7f;
|
|
231
|
+
let offset = 2;
|
|
232
|
+
if (len === 126) {
|
|
233
|
+
if (this.rxBuf.length < offset + 2)
|
|
234
|
+
break;
|
|
235
|
+
len = this.rxBuf.readUInt16BE(offset);
|
|
236
|
+
offset += 2;
|
|
237
|
+
}
|
|
238
|
+
else if (len === 127) {
|
|
239
|
+
if (this.rxBuf.length < offset + 8)
|
|
240
|
+
break;
|
|
241
|
+
len = Number(this.rxBuf.readBigUInt64BE(offset));
|
|
242
|
+
offset += 8;
|
|
243
|
+
}
|
|
244
|
+
let maskKey = null;
|
|
245
|
+
if (masked) {
|
|
246
|
+
if (this.rxBuf.length < offset + 4)
|
|
247
|
+
break;
|
|
248
|
+
maskKey = this.rxBuf.subarray(offset, offset + 4);
|
|
249
|
+
offset += 4;
|
|
250
|
+
}
|
|
251
|
+
if (this.rxBuf.length < offset + len)
|
|
252
|
+
break;
|
|
253
|
+
const payload = Buffer.from(this.rxBuf.subarray(offset, offset + len));
|
|
254
|
+
this.rxBuf = this.rxBuf.subarray(offset + len);
|
|
255
|
+
if (maskKey) {
|
|
256
|
+
for (let i = 0; i < payload.length; i++) {
|
|
257
|
+
payload[i] ^= maskKey[i & 3];
|
|
258
|
+
}
|
|
259
|
+
}
|
|
260
|
+
if (opcode === 0x8) {
|
|
261
|
+
if (this.readyState === 1) {
|
|
262
|
+
this.readyState = 2;
|
|
263
|
+
try {
|
|
264
|
+
this.sendFrame(0x8, Buffer.alloc(0));
|
|
265
|
+
this.socket?.end();
|
|
266
|
+
}
|
|
267
|
+
catch {
|
|
268
|
+
}
|
|
269
|
+
}
|
|
270
|
+
this.readyState = 3;
|
|
271
|
+
this.emit("close", {});
|
|
272
|
+
return;
|
|
273
|
+
}
|
|
274
|
+
if (opcode === 0x9) {
|
|
275
|
+
if (this.readyState === 1) {
|
|
276
|
+
try {
|
|
277
|
+
this.sendFrame(0xa, payload);
|
|
278
|
+
}
|
|
279
|
+
catch {
|
|
280
|
+
}
|
|
281
|
+
}
|
|
282
|
+
continue;
|
|
283
|
+
}
|
|
284
|
+
if (opcode === 0xa) {
|
|
285
|
+
continue;
|
|
286
|
+
}
|
|
287
|
+
if (opcode === 0x1 || opcode === 0x2) {
|
|
288
|
+
this.fragOpcode = opcode;
|
|
289
|
+
this.fragChunks = [payload];
|
|
290
|
+
}
|
|
291
|
+
else if (opcode === 0x0) {
|
|
292
|
+
this.fragChunks.push(payload);
|
|
293
|
+
}
|
|
294
|
+
if (fin && (opcode === 0x0 || opcode === 0x1 || opcode === 0x2)) {
|
|
295
|
+
const full = this.fragChunks.length === 1 ? this.fragChunks[0] : Buffer.concat(this.fragChunks);
|
|
296
|
+
this.fragChunks = [];
|
|
297
|
+
if (this.fragOpcode === 0x1 || this.fragOpcode === 0x2) {
|
|
298
|
+
this.emit("message", { data: full.toString("utf8") });
|
|
299
|
+
}
|
|
300
|
+
}
|
|
301
|
+
}
|
|
302
|
+
}
|
|
303
|
+
close() {
|
|
304
|
+
if (this.readyState === 3)
|
|
305
|
+
return;
|
|
306
|
+
if (this.readyState === 1 && this.socket && !this.socket.destroyed) {
|
|
307
|
+
this.readyState = 2;
|
|
308
|
+
try {
|
|
309
|
+
this.sendFrame(0x8, Buffer.alloc(0));
|
|
310
|
+
}
|
|
311
|
+
catch {
|
|
312
|
+
}
|
|
313
|
+
}
|
|
314
|
+
this.readyState = 3;
|
|
315
|
+
try {
|
|
316
|
+
this.socket?.end();
|
|
317
|
+
this.socket?.destroy();
|
|
318
|
+
}
|
|
319
|
+
catch {
|
|
320
|
+
}
|
|
321
|
+
this.emit("close", {});
|
|
322
|
+
}
|
|
323
|
+
}
|
|
324
|
+
class NativeCdpSession {
|
|
325
|
+
proc = null;
|
|
326
|
+
profileDir = null;
|
|
327
|
+
ws = null;
|
|
328
|
+
nextId = 1;
|
|
329
|
+
pending = new Map();
|
|
330
|
+
eventListeners = new Map();
|
|
331
|
+
networkRequests = new Map();
|
|
332
|
+
harEntries = [];
|
|
333
|
+
consoleEntries = [];
|
|
334
|
+
lastStatusCode = 200;
|
|
335
|
+
async connect(executablePath, proxyUrl, stealth = false) {
|
|
336
|
+
const WS = typeof globalThis.WebSocket === "function" && process.env.WIDE_CLI_FORCE_NODE_WS !== "1"
|
|
337
|
+
? globalThis.WebSocket
|
|
338
|
+
: NodeCdpWebSocket;
|
|
339
|
+
this.profileDir = fs.mkdtempSync(path.join(os.tmpdir(), "wide-cli-cdp-"));
|
|
340
|
+
try {
|
|
341
|
+
const args = [
|
|
342
|
+
"--headless=new",
|
|
343
|
+
"--no-sandbox",
|
|
344
|
+
"--disable-setuid-sandbox",
|
|
345
|
+
"--disable-dev-shm-usage",
|
|
346
|
+
"--remote-allow-origins=*",
|
|
347
|
+
"--no-first-run",
|
|
348
|
+
"--no-default-browser-check",
|
|
349
|
+
"--disable-gpu",
|
|
350
|
+
"--disable-extensions",
|
|
351
|
+
"--disable-background-networking",
|
|
352
|
+
"--remote-debugging-port=0",
|
|
353
|
+
`--user-data-dir=${this.profileDir}`
|
|
354
|
+
];
|
|
355
|
+
if (stealth) {
|
|
356
|
+
args.push("--disable-blink-features=AutomationControlled");
|
|
357
|
+
}
|
|
358
|
+
if (proxyUrl) {
|
|
359
|
+
args.push(`--proxy-server=${proxyUrl}`);
|
|
360
|
+
}
|
|
361
|
+
args.push("about:blank");
|
|
362
|
+
const wsBaseUrl = await new Promise((resolvePromise, rejectPromise) => {
|
|
363
|
+
let settled = false;
|
|
364
|
+
let stderrBuf = "";
|
|
365
|
+
const timer = setTimeout(() => {
|
|
366
|
+
if (!settled) {
|
|
367
|
+
settled = true;
|
|
368
|
+
rejectPromise(new Error("Timed out waiting for browser DevTools endpoint"));
|
|
369
|
+
}
|
|
370
|
+
}, 15000);
|
|
371
|
+
timer.unref?.();
|
|
372
|
+
this.proc = spawn(executablePath, args, {
|
|
373
|
+
stdio: ["ignore", "pipe", "pipe"],
|
|
374
|
+
windowsHide: true
|
|
375
|
+
});
|
|
376
|
+
this.proc.on("error", (err) => {
|
|
377
|
+
clearTimeout(timer);
|
|
378
|
+
if (!settled) {
|
|
379
|
+
settled = true;
|
|
380
|
+
rejectPromise(err);
|
|
381
|
+
}
|
|
382
|
+
});
|
|
383
|
+
this.proc.on("exit", (code) => {
|
|
384
|
+
clearTimeout(timer);
|
|
385
|
+
if (!settled) {
|
|
386
|
+
settled = true;
|
|
387
|
+
rejectPromise(new Error(`Browser exited early (${code}): ${stderrBuf.slice(0, 300)}`));
|
|
388
|
+
}
|
|
389
|
+
});
|
|
390
|
+
this.proc.stderr?.on("data", (chunk) => {
|
|
391
|
+
stderrBuf += chunk.toString("utf8");
|
|
392
|
+
const m = /DevTools listening on (ws:\/\/[^\s]+)/i.exec(stderrBuf);
|
|
393
|
+
if (m && !settled) {
|
|
394
|
+
settled = true;
|
|
395
|
+
clearTimeout(timer);
|
|
396
|
+
resolvePromise(m[1]);
|
|
397
|
+
}
|
|
398
|
+
});
|
|
399
|
+
});
|
|
400
|
+
const parsed = new URL(wsBaseUrl);
|
|
401
|
+
const httpBase = `http://${parsed.host}`;
|
|
402
|
+
let pageWsUrl = "";
|
|
403
|
+
for (let attempt = 0; attempt < 20; attempt++) {
|
|
404
|
+
try {
|
|
405
|
+
const res = await fetch(`${httpBase}/json/list`, { signal: AbortSignal.timeout(2000) });
|
|
406
|
+
if (res.ok) {
|
|
407
|
+
const targets = (await res.json());
|
|
408
|
+
const pageTarget = targets.find((t) => t.type === "page" && t.webSocketDebuggerUrl);
|
|
409
|
+
if (pageTarget?.webSocketDebuggerUrl) {
|
|
410
|
+
pageWsUrl = pageTarget.webSocketDebuggerUrl;
|
|
411
|
+
break;
|
|
412
|
+
}
|
|
413
|
+
}
|
|
414
|
+
}
|
|
415
|
+
catch {
|
|
416
|
+
}
|
|
417
|
+
await new Promise((r) => setTimeout(r, 100));
|
|
418
|
+
}
|
|
419
|
+
if (!pageWsUrl) {
|
|
420
|
+
const created = await fetch(`${httpBase}/json/new?about:blank`, { method: "PUT", signal: AbortSignal.timeout(3000) }).catch(() => null);
|
|
421
|
+
if (created && created.ok) {
|
|
422
|
+
const t = (await created.json());
|
|
423
|
+
pageWsUrl = t?.webSocketDebuggerUrl || "";
|
|
424
|
+
}
|
|
425
|
+
}
|
|
426
|
+
if (!pageWsUrl) {
|
|
427
|
+
throw new Error("Could not acquire CDP page target");
|
|
428
|
+
}
|
|
429
|
+
await new Promise((resolvePromise, rejectPromise) => {
|
|
430
|
+
const timer = setTimeout(() => rejectPromise(new Error("CDP WebSocket connect timeout")), 10000);
|
|
431
|
+
timer.unref?.();
|
|
432
|
+
const ws = new WS(pageWsUrl);
|
|
433
|
+
this.ws = ws;
|
|
434
|
+
ws.addEventListener("open", () => {
|
|
435
|
+
clearTimeout(timer);
|
|
436
|
+
resolvePromise();
|
|
437
|
+
});
|
|
438
|
+
ws.addEventListener("error", () => {
|
|
439
|
+
clearTimeout(timer);
|
|
440
|
+
rejectPromise(new Error("CDP WebSocket connection failed"));
|
|
441
|
+
});
|
|
442
|
+
ws.addEventListener("message", (ev) => {
|
|
443
|
+
this.handleMessage(typeof ev.data === "string" ? ev.data : String(ev.data));
|
|
444
|
+
});
|
|
445
|
+
ws.addEventListener("close", () => {
|
|
446
|
+
for (const [, p] of this.pending) {
|
|
447
|
+
p.reject(new Error("CDP connection closed"));
|
|
448
|
+
}
|
|
449
|
+
this.pending.clear();
|
|
450
|
+
});
|
|
451
|
+
});
|
|
452
|
+
}
|
|
453
|
+
catch (err) {
|
|
454
|
+
await this.dispose();
|
|
455
|
+
throw err;
|
|
456
|
+
}
|
|
457
|
+
await this.send("Page.enable");
|
|
458
|
+
await this.send("Runtime.enable");
|
|
459
|
+
await this.send("Network.enable");
|
|
460
|
+
if (stealth) {
|
|
461
|
+
await this.send("Page.addScriptToEvaluateOnNewDocument", { source: CLI_STEALTH_INIT_SCRIPT }).catch(() => { });
|
|
462
|
+
}
|
|
463
|
+
await this.send("Emulation.setDeviceMetricsOverride", {
|
|
464
|
+
width: 1280,
|
|
465
|
+
height: 800,
|
|
466
|
+
deviceScaleFactor: 1,
|
|
467
|
+
mobile: false
|
|
468
|
+
});
|
|
469
|
+
this.on("Runtime.consoleAPICalled", (params) => {
|
|
470
|
+
const parts = Array.isArray(params?.args)
|
|
471
|
+
? params.args.map((a) => a?.value ?? a?.description ?? "").filter(Boolean)
|
|
472
|
+
: [];
|
|
473
|
+
this.consoleEntries.push({
|
|
474
|
+
level: String(params?.type || "log"),
|
|
475
|
+
text: parts.join(" "),
|
|
476
|
+
timestamp: new Date().toISOString()
|
|
477
|
+
});
|
|
478
|
+
if (this.consoleEntries.length > 200) {
|
|
479
|
+
this.consoleEntries.shift();
|
|
480
|
+
}
|
|
481
|
+
});
|
|
482
|
+
this.on("Runtime.exceptionThrown", (params) => {
|
|
483
|
+
const desc = params?.exceptionDetails?.exception?.description || params?.exceptionDetails?.text || "Runtime exception";
|
|
484
|
+
this.consoleEntries.push({
|
|
485
|
+
level: "error",
|
|
486
|
+
text: String(desc),
|
|
487
|
+
timestamp: new Date().toISOString()
|
|
488
|
+
});
|
|
489
|
+
if (this.consoleEntries.length > 200) {
|
|
490
|
+
this.consoleEntries.shift();
|
|
491
|
+
}
|
|
492
|
+
});
|
|
493
|
+
this.on("Network.requestWillBeSent", (params) => {
|
|
494
|
+
if (!params?.requestId || !params?.request)
|
|
495
|
+
return;
|
|
496
|
+
this.networkRequests.set(params.requestId, {
|
|
497
|
+
startedDateTime: params.wallTime ? new Date(params.wallTime * 1000).toISOString() : new Date().toISOString(),
|
|
498
|
+
startTimestamp: Number(params.timestamp || 0),
|
|
499
|
+
request: params.request,
|
|
500
|
+
response: null,
|
|
501
|
+
endTimestamp: Number(params.timestamp || 0),
|
|
502
|
+
});
|
|
503
|
+
if (this.networkRequests.size > 400) {
|
|
504
|
+
const oldest = this.networkRequests.keys().next().value;
|
|
505
|
+
if (oldest !== undefined)
|
|
506
|
+
this.networkRequests.delete(oldest);
|
|
507
|
+
}
|
|
508
|
+
});
|
|
509
|
+
this.on("Network.responseReceived", (params) => {
|
|
510
|
+
if (params?.type === "Document" && typeof params?.response?.status === "number") {
|
|
511
|
+
this.lastStatusCode = params.response.status;
|
|
512
|
+
}
|
|
513
|
+
const rec = params?.requestId ? this.networkRequests.get(params.requestId) : null;
|
|
514
|
+
if (rec) {
|
|
515
|
+
rec.response = params.response || null;
|
|
516
|
+
rec.endTimestamp = Number(params.timestamp || rec.startTimestamp || 0);
|
|
517
|
+
}
|
|
518
|
+
});
|
|
519
|
+
const finalizeHar = (requestId, endTimestamp, encodedLength, failedText = "") => {
|
|
520
|
+
const rec = requestId ? this.networkRequests.get(requestId) : null;
|
|
521
|
+
if (!rec)
|
|
522
|
+
return;
|
|
523
|
+
this.networkRequests.delete(requestId);
|
|
524
|
+
const reqUrl = String(rec.request?.url || "");
|
|
525
|
+
if (reqUrl.startsWith("data:") || reqUrl.startsWith("blob:"))
|
|
526
|
+
return;
|
|
527
|
+
const endTs = Number(endTimestamp || rec.endTimestamp || rec.startTimestamp || 0);
|
|
528
|
+
const elapsedMs = rec.startTimestamp > 0 && endTs >= rec.startTimestamp ? Math.round((endTs - rec.startTimestamp) * 1000) : 0;
|
|
529
|
+
const resp = rec.response || {};
|
|
530
|
+
this.harEntries.push({
|
|
531
|
+
pageref: "page_1",
|
|
532
|
+
startedDateTime: rec.startedDateTime,
|
|
533
|
+
time: elapsedMs,
|
|
534
|
+
request: {
|
|
535
|
+
method: String(rec.request?.method || "GET"),
|
|
536
|
+
url: reqUrl,
|
|
537
|
+
httpVersion: String(resp.protocol || "HTTP/1.1"),
|
|
538
|
+
cookies: [],
|
|
539
|
+
headers: redactHarHeaders(rec.request?.headers),
|
|
540
|
+
queryString: parseHarQuery(reqUrl),
|
|
541
|
+
headersSize: -1,
|
|
542
|
+
bodySize: typeof rec.request?.postData === "string" ? Buffer.byteLength(rec.request.postData, "utf8") : 0,
|
|
543
|
+
},
|
|
544
|
+
response: {
|
|
545
|
+
status: Number(resp.status || 0),
|
|
546
|
+
statusText: failedText || String(resp.statusText || ""),
|
|
547
|
+
httpVersion: String(resp.protocol || "HTTP/1.1"),
|
|
548
|
+
cookies: [],
|
|
549
|
+
headers: redactHarHeaders(resp.headers),
|
|
550
|
+
content: {
|
|
551
|
+
size: Number(encodedLength || resp.encodedDataLength || 0),
|
|
552
|
+
mimeType: String(resp.mimeType || "x-unknown"),
|
|
553
|
+
},
|
|
554
|
+
redirectURL: "",
|
|
555
|
+
headersSize: -1,
|
|
556
|
+
bodySize: Number(encodedLength || resp.encodedDataLength || 0),
|
|
557
|
+
},
|
|
558
|
+
cache: {},
|
|
559
|
+
timings: { send: 0, wait: elapsedMs, receive: 0 },
|
|
560
|
+
});
|
|
561
|
+
if (this.harEntries.length > 500) {
|
|
562
|
+
this.harEntries.splice(0, this.harEntries.length - 500);
|
|
563
|
+
}
|
|
564
|
+
};
|
|
565
|
+
this.on("Network.loadingFinished", (params) => {
|
|
566
|
+
finalizeHar(params?.requestId, params?.timestamp, params?.encodedDataLength, "");
|
|
567
|
+
});
|
|
568
|
+
this.on("Network.loadingFailed", (params) => {
|
|
569
|
+
finalizeHar(params?.requestId, params?.timestamp, 0, params?.errorText || "Failed");
|
|
570
|
+
});
|
|
571
|
+
}
|
|
572
|
+
handleMessage(raw) {
|
|
573
|
+
let msg;
|
|
574
|
+
try {
|
|
575
|
+
msg = JSON.parse(raw);
|
|
576
|
+
}
|
|
577
|
+
catch {
|
|
578
|
+
return;
|
|
579
|
+
}
|
|
580
|
+
if (typeof msg.id === "number") {
|
|
581
|
+
const p = this.pending.get(msg.id);
|
|
582
|
+
if (p) {
|
|
583
|
+
this.pending.delete(msg.id);
|
|
584
|
+
if (msg.error) {
|
|
585
|
+
p.reject(new Error(msg.error.message || JSON.stringify(msg.error)));
|
|
586
|
+
}
|
|
587
|
+
else {
|
|
588
|
+
p.resolve(msg.result);
|
|
589
|
+
}
|
|
590
|
+
}
|
|
591
|
+
return;
|
|
592
|
+
}
|
|
593
|
+
if (msg.method) {
|
|
594
|
+
const list = this.eventListeners.get(msg.method);
|
|
595
|
+
if (list) {
|
|
596
|
+
for (const fn of list) {
|
|
597
|
+
try {
|
|
598
|
+
fn(msg.params);
|
|
599
|
+
}
|
|
600
|
+
catch {
|
|
601
|
+
}
|
|
602
|
+
}
|
|
603
|
+
}
|
|
604
|
+
}
|
|
605
|
+
}
|
|
606
|
+
on(method, handler) {
|
|
607
|
+
const arr = this.eventListeners.get(method) || [];
|
|
608
|
+
arr.push(handler);
|
|
609
|
+
this.eventListeners.set(method, arr);
|
|
610
|
+
return () => {
|
|
611
|
+
const cur = this.eventListeners.get(method) || [];
|
|
612
|
+
this.eventListeners.set(method, cur.filter((f) => f !== handler));
|
|
613
|
+
};
|
|
614
|
+
}
|
|
615
|
+
send(method, params = {}, timeoutMs = 15000) {
|
|
616
|
+
if (!this.ws || this.ws.readyState !== 1) {
|
|
617
|
+
return Promise.reject(new Error("CDP session is not open"));
|
|
618
|
+
}
|
|
619
|
+
const id = this.nextId++;
|
|
620
|
+
return new Promise((resolvePromise, rejectPromise) => {
|
|
621
|
+
const timer = setTimeout(() => {
|
|
622
|
+
this.pending.delete(id);
|
|
623
|
+
rejectPromise(new Error(`CDP command timed out: ${method}`));
|
|
624
|
+
}, timeoutMs);
|
|
625
|
+
this.pending.set(id, {
|
|
626
|
+
resolve: (val) => {
|
|
627
|
+
clearTimeout(timer);
|
|
628
|
+
resolvePromise(val);
|
|
629
|
+
},
|
|
630
|
+
reject: (err) => {
|
|
631
|
+
clearTimeout(timer);
|
|
632
|
+
rejectPromise(err);
|
|
633
|
+
}
|
|
634
|
+
});
|
|
635
|
+
this.ws.send(JSON.stringify({ id, method, params }));
|
|
636
|
+
});
|
|
637
|
+
}
|
|
638
|
+
async evalValue(expression) {
|
|
639
|
+
const res = await this.send("Runtime.evaluate", {
|
|
640
|
+
expression,
|
|
641
|
+
returnByValue: true,
|
|
642
|
+
awaitPromise: true
|
|
643
|
+
});
|
|
644
|
+
if (res?.exceptionDetails) {
|
|
645
|
+
const msg = res.exceptionDetails.exception?.description || res.exceptionDetails.text || "Evaluation failed";
|
|
646
|
+
throw new Error(String(msg));
|
|
647
|
+
}
|
|
648
|
+
return res?.result?.value;
|
|
649
|
+
}
|
|
650
|
+
async dispose() {
|
|
651
|
+
for (const [, p] of this.pending) {
|
|
652
|
+
try {
|
|
653
|
+
p.reject(new Error("CDP session disposed"));
|
|
654
|
+
}
|
|
655
|
+
catch {
|
|
656
|
+
}
|
|
657
|
+
}
|
|
658
|
+
this.pending.clear();
|
|
659
|
+
try {
|
|
660
|
+
if (this.ws) {
|
|
661
|
+
this.ws.close();
|
|
662
|
+
this.ws = null;
|
|
663
|
+
}
|
|
664
|
+
}
|
|
665
|
+
catch {
|
|
666
|
+
}
|
|
667
|
+
if (this.proc) {
|
|
668
|
+
try {
|
|
669
|
+
this.proc.kill("SIGKILL");
|
|
670
|
+
}
|
|
671
|
+
catch {
|
|
672
|
+
}
|
|
673
|
+
this.proc = null;
|
|
674
|
+
}
|
|
675
|
+
if (this.profileDir) {
|
|
676
|
+
try {
|
|
677
|
+
fs.rmSync(this.profileDir, { recursive: true, force: true });
|
|
678
|
+
}
|
|
679
|
+
catch {
|
|
680
|
+
}
|
|
681
|
+
this.profileDir = null;
|
|
682
|
+
}
|
|
683
|
+
}
|
|
684
|
+
}
|
|
685
|
+
const SNAPSHOT_DOM_SCRIPT = `(() => {
|
|
686
|
+
const prev = document.querySelectorAll('[data-wide-ref]');
|
|
687
|
+
for (const node of prev) node.removeAttribute('data-wide-ref');
|
|
688
|
+
const selector = 'a[href], button, input, select, textarea, [role="button"], [role="link"], [role="textbox"], [onclick], [tabindex]:not([tabindex="-1"])';
|
|
689
|
+
const nodes = Array.from(document.querySelectorAll(selector));
|
|
690
|
+
const elements = [];
|
|
691
|
+
let idx = 1;
|
|
692
|
+
for (const el of nodes) {
|
|
693
|
+
if (!(el instanceof Element)) continue;
|
|
694
|
+
const style = window.getComputedStyle(el);
|
|
695
|
+
if (style.display === 'none' || style.visibility === 'hidden') continue;
|
|
696
|
+
const rect = el.getBoundingClientRect();
|
|
697
|
+
if (rect.width <= 0 && rect.height <= 0 && el.tagName.toLowerCase() !== 'input') continue;
|
|
698
|
+
const tag = el.tagName.toLowerCase();
|
|
699
|
+
const typeAttr = (el.getAttribute('type') || 'text').toLowerCase();
|
|
700
|
+
if (tag === 'input' && typeAttr === 'hidden') continue;
|
|
701
|
+
const ref = '@e' + idx;
|
|
702
|
+
el.setAttribute('data-wide-ref', ref);
|
|
703
|
+
const role = tag === 'a' ? 'link' : tag === 'input' ? ('input[' + typeAttr + ']') : (el.getAttribute('role') || tag);
|
|
704
|
+
const rawText = (el.innerText || el.textContent || '').replace(/\\s+/g, ' ').trim();
|
|
705
|
+
const aria = el.getAttribute('aria-label') || '';
|
|
706
|
+
const placeholder = el.getAttribute('placeholder') || '';
|
|
707
|
+
const name = el.getAttribute('name') || '';
|
|
708
|
+
const val = ('value' in el && typeof el.value === 'string') ? el.value : (el.getAttribute('value') || '');
|
|
709
|
+
const href = tag === 'a' ? (el.getAttribute('href') || '') : '';
|
|
710
|
+
const text = (rawText || aria || placeholder || name || val || role).slice(0, 120);
|
|
711
|
+
elements.push({
|
|
712
|
+
index: idx,
|
|
713
|
+
ref,
|
|
714
|
+
tag,
|
|
715
|
+
role,
|
|
716
|
+
text,
|
|
717
|
+
href,
|
|
718
|
+
name,
|
|
719
|
+
value: val,
|
|
720
|
+
attributes: {
|
|
721
|
+
id: el.id || '',
|
|
722
|
+
name,
|
|
723
|
+
type: typeAttr,
|
|
724
|
+
placeholder,
|
|
725
|
+
href,
|
|
726
|
+
'aria-label': aria
|
|
727
|
+
}
|
|
728
|
+
});
|
|
729
|
+
idx++;
|
|
730
|
+
if (idx > 120) break;
|
|
731
|
+
}
|
|
732
|
+
const bodyText = (document.body ? (document.body.innerText || document.body.textContent || '') : '')
|
|
733
|
+
.replace(/\\s+/g, ' ')
|
|
734
|
+
.trim()
|
|
735
|
+
.slice(0, 8000);
|
|
736
|
+
return {
|
|
737
|
+
url: location.href,
|
|
738
|
+
title: document.title || location.href,
|
|
739
|
+
textContent: bodyText,
|
|
740
|
+
rawHtml: document.documentElement ? document.documentElement.outerHTML.slice(0, 50000) : '',
|
|
741
|
+
elements
|
|
742
|
+
};
|
|
743
|
+
})()`;
|
|
744
|
+
const activeControllers = new Set();
|
|
745
|
+
let cleanupRegistered = false;
|
|
746
|
+
function registerProcessCleanup() {
|
|
747
|
+
if (cleanupRegistered)
|
|
748
|
+
return;
|
|
749
|
+
cleanupRegistered = true;
|
|
750
|
+
process.once("exit", () => {
|
|
751
|
+
for (const ctrl of activeControllers) {
|
|
752
|
+
ctrl.forceDisposeSync();
|
|
753
|
+
}
|
|
754
|
+
});
|
|
755
|
+
}
|
|
3
756
|
export class HeadlessBrowserController {
|
|
4
757
|
state = {
|
|
5
758
|
url: "about:blank",
|
|
@@ -9,37 +762,289 @@ export class HeadlessBrowserController {
|
|
|
9
762
|
textContent: "",
|
|
10
763
|
rawHtml: "",
|
|
11
764
|
formInputs: {},
|
|
12
|
-
history: []
|
|
765
|
+
history: [],
|
|
766
|
+
mode: "http"
|
|
13
767
|
};
|
|
14
|
-
|
|
768
|
+
cdp = null;
|
|
769
|
+
pwBrowser = null;
|
|
770
|
+
pwContext = null;
|
|
771
|
+
pwPage = null;
|
|
772
|
+
consoleBuffer = [];
|
|
773
|
+
cookieJar = new Map();
|
|
774
|
+
stealth = false;
|
|
775
|
+
harEntries = [];
|
|
776
|
+
harStartedAt = "";
|
|
777
|
+
userAgent = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/131.0.0.0 Safari/537.36 Wide-CLI/0.5151126";
|
|
15
778
|
findSystemBrowserExecutable() {
|
|
16
779
|
const candidates = [];
|
|
17
780
|
if (process.platform === "win32") {
|
|
18
781
|
const localAppData = process.env.LOCALAPPDATA || "";
|
|
19
782
|
const programFiles = process.env["ProgramFiles"] || "C:\\Program Files";
|
|
20
783
|
const programFilesX86 = process.env["ProgramFiles(x86)"] || "C:\\Program Files (x86)";
|
|
21
|
-
candidates.push(path.join(
|
|
784
|
+
candidates.push(path.join(programFiles, "Google", "Chrome", "Application", "chrome.exe"), path.join(programFilesX86, "Google", "Chrome", "Application", "chrome.exe"), path.join(localAppData, "Google", "Chrome", "Application", "chrome.exe"), path.join(programFilesX86, "Microsoft", "Edge", "Application", "msedge.exe"), path.join(programFiles, "Microsoft", "Edge", "Application", "msedge.exe"), path.join(localAppData, "Microsoft", "Edge", "Application", "msedge.exe"), path.join(programFiles, "BraveSoftware", "Brave-Browser", "Application", "brave.exe"));
|
|
22
785
|
}
|
|
23
786
|
else if (process.platform === "darwin") {
|
|
24
|
-
candidates.push("/Applications/Google Chrome.app/Contents/MacOS/Google Chrome", "/Applications/Microsoft Edge.app/Contents/MacOS/Microsoft Edge", "/Applications/Chromium.app/Contents/MacOS/Chromium");
|
|
787
|
+
candidates.push("/Applications/Google Chrome.app/Contents/MacOS/Google Chrome", "/Applications/Microsoft Edge.app/Contents/MacOS/Microsoft Edge", "/Applications/Brave Browser.app/Contents/MacOS/Brave Browser", "/Applications/Chromium.app/Contents/MacOS/Chromium");
|
|
25
788
|
}
|
|
26
789
|
else {
|
|
27
|
-
candidates.push("/usr/bin/google-chrome", "/usr/bin/google-chrome-stable", "/usr/bin/chromium", "/usr/bin/chromium-browser", "/usr/bin/microsoft-edge");
|
|
790
|
+
candidates.push("/usr/bin/google-chrome", "/usr/bin/google-chrome-stable", "/usr/bin/chromium", "/usr/bin/chromium-browser", "/usr/bin/microsoft-edge", "/usr/bin/brave-browser");
|
|
28
791
|
}
|
|
29
792
|
for (const p of candidates) {
|
|
30
|
-
if (fs.existsSync(p))
|
|
793
|
+
if (p && fs.existsSync(p))
|
|
31
794
|
return p;
|
|
32
795
|
}
|
|
33
796
|
return null;
|
|
34
797
|
}
|
|
798
|
+
normalizeTargetUrl(rawUrl) {
|
|
799
|
+
const trimmed = String(rawUrl || "").trim();
|
|
800
|
+
if (!trimmed)
|
|
801
|
+
return "about:blank";
|
|
802
|
+
if (/^(?:https?|about|data|file):\/\//i.test(trimmed) || trimmed === "about:blank") {
|
|
803
|
+
return trimmed;
|
|
804
|
+
}
|
|
805
|
+
const scheme = /^(?:localhost|127\.0\.0\.1|\[::1\]|0\.0\.0\.0)(?:[:/]|$)/i.test(trimmed)
|
|
806
|
+
? "http://"
|
|
807
|
+
: "https://";
|
|
808
|
+
return scheme + trimmed;
|
|
809
|
+
}
|
|
810
|
+
async tryLoadPlaywright() {
|
|
811
|
+
if (process.env.WIDE_CLI_BROWSER_HTTP_ONLY === "1")
|
|
812
|
+
return null;
|
|
813
|
+
try {
|
|
814
|
+
const req = createRequire(import.meta.url);
|
|
815
|
+
const baseDir = path.dirname(req.resolve("./browser.js"));
|
|
816
|
+
const siblingIde = path.resolve(baseDir, "../../../../wide-ide/node_modules/playwright-core");
|
|
817
|
+
const siblingWide = path.resolve(baseDir, "../../../../wide/node_modules/playwright-core");
|
|
818
|
+
for (const id of ["playwright-core", siblingIde, siblingWide]) {
|
|
819
|
+
try {
|
|
820
|
+
return req(id);
|
|
821
|
+
}
|
|
822
|
+
catch {
|
|
823
|
+
}
|
|
824
|
+
}
|
|
825
|
+
}
|
|
826
|
+
catch {
|
|
827
|
+
}
|
|
828
|
+
return null;
|
|
829
|
+
}
|
|
830
|
+
async ensureLiveSession() {
|
|
831
|
+
if (this.pwPage)
|
|
832
|
+
return "playwright";
|
|
833
|
+
if (this.cdp)
|
|
834
|
+
return "cdp";
|
|
835
|
+
if (process.env.WIDE_CLI_BROWSER_HTTP_ONLY === "1")
|
|
836
|
+
return "http";
|
|
837
|
+
registerProcessCleanup();
|
|
838
|
+
activeControllers.add(this);
|
|
839
|
+
if (!this.harStartedAt) {
|
|
840
|
+
this.harStartedAt = new Date().toISOString();
|
|
841
|
+
}
|
|
842
|
+
const exe = this.findSystemBrowserExecutable();
|
|
843
|
+
const proxyUrl = process.env.WIDE_CATCHER_PROXY || process.env.HTTPS_PROXY || process.env.HTTP_PROXY || "";
|
|
844
|
+
const pw = await this.tryLoadPlaywright();
|
|
845
|
+
if (pw?.chromium && exe) {
|
|
846
|
+
try {
|
|
847
|
+
const pwArgs = ["--no-sandbox", "--disable-gpu"];
|
|
848
|
+
if (this.stealth) {
|
|
849
|
+
pwArgs.push("--disable-blink-features=AutomationControlled");
|
|
850
|
+
}
|
|
851
|
+
const launchOpts = {
|
|
852
|
+
executablePath: exe,
|
|
853
|
+
headless: true,
|
|
854
|
+
args: pwArgs
|
|
855
|
+
};
|
|
856
|
+
if (proxyUrl) {
|
|
857
|
+
launchOpts.proxy = { server: proxyUrl };
|
|
858
|
+
}
|
|
859
|
+
this.pwBrowser = await pw.chromium.launch(launchOpts);
|
|
860
|
+
const contextOpts = { viewport: { width: 1280, height: 800 } };
|
|
861
|
+
if (this.stealth) {
|
|
862
|
+
contextOpts.userAgent = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/131.0.0.0 Safari/537.36";
|
|
863
|
+
contextOpts.locale = "en-US";
|
|
864
|
+
}
|
|
865
|
+
this.pwContext = await this.pwBrowser.newContext(contextOpts);
|
|
866
|
+
if (this.stealth) {
|
|
867
|
+
await this.pwContext.addInitScript(CLI_STEALTH_INIT_SCRIPT);
|
|
868
|
+
}
|
|
869
|
+
this.pwPage = await this.pwContext.newPage();
|
|
870
|
+
this.pwPage.on("console", (msg) => {
|
|
871
|
+
this.pushConsoleEntry(msg.type(), msg.text());
|
|
872
|
+
});
|
|
873
|
+
this.pwPage.on("pageerror", (err) => {
|
|
874
|
+
this.pushConsoleEntry("error", err.message);
|
|
875
|
+
});
|
|
876
|
+
this.pwPage.on("requestfinished", async (req) => {
|
|
877
|
+
try {
|
|
878
|
+
const reqUrl = String(req.url?.() || "");
|
|
879
|
+
if (reqUrl.startsWith("data:") || reqUrl.startsWith("blob:"))
|
|
880
|
+
return;
|
|
881
|
+
const resp = await req.response().catch(() => null);
|
|
882
|
+
const timing = typeof req.timing === "function" ? req.timing() : {};
|
|
883
|
+
const elapsedMs = timing && typeof timing.responseEnd === "number" && timing.responseEnd > 0 ? Math.round(timing.responseEnd) : 0;
|
|
884
|
+
this.harEntries.push({
|
|
885
|
+
pageref: "page_1",
|
|
886
|
+
startedDateTime: new Date().toISOString(),
|
|
887
|
+
time: elapsedMs,
|
|
888
|
+
request: {
|
|
889
|
+
method: String(req.method?.() || "GET"),
|
|
890
|
+
url: reqUrl,
|
|
891
|
+
httpVersion: "HTTP/1.1",
|
|
892
|
+
cookies: [],
|
|
893
|
+
headers: redactHarHeaders(req.headers?.() || {}),
|
|
894
|
+
queryString: parseHarQuery(reqUrl),
|
|
895
|
+
headersSize: -1,
|
|
896
|
+
bodySize: 0,
|
|
897
|
+
},
|
|
898
|
+
response: {
|
|
899
|
+
status: resp ? Number(resp.status?.() || 0) : 0,
|
|
900
|
+
statusText: resp ? String(resp.statusText?.() || "") : "",
|
|
901
|
+
httpVersion: "HTTP/1.1",
|
|
902
|
+
cookies: [],
|
|
903
|
+
headers: redactHarHeaders(resp ? resp.headers?.() : {}),
|
|
904
|
+
content: { size: 0, mimeType: String((resp && resp.headers?.()?.["content-type"]) || "x-unknown") },
|
|
905
|
+
redirectURL: "",
|
|
906
|
+
headersSize: -1,
|
|
907
|
+
bodySize: 0,
|
|
908
|
+
},
|
|
909
|
+
cache: {},
|
|
910
|
+
timings: { send: 0, wait: elapsedMs, receive: 0 },
|
|
911
|
+
});
|
|
912
|
+
if (this.harEntries.length > 500) {
|
|
913
|
+
this.harEntries.splice(0, this.harEntries.length - 500);
|
|
914
|
+
}
|
|
915
|
+
}
|
|
916
|
+
catch {
|
|
917
|
+
}
|
|
918
|
+
});
|
|
919
|
+
this.pwPage.on("response", (res) => {
|
|
920
|
+
try {
|
|
921
|
+
if (res.request()?.isNavigationRequest?.()) {
|
|
922
|
+
this.state.statusCode = res.status();
|
|
923
|
+
}
|
|
924
|
+
}
|
|
925
|
+
catch {
|
|
926
|
+
}
|
|
927
|
+
});
|
|
928
|
+
this.state.mode = "playwright";
|
|
929
|
+
this.state.stealth = this.stealth;
|
|
930
|
+
return "playwright";
|
|
931
|
+
}
|
|
932
|
+
catch {
|
|
933
|
+
await this.cleanupLiveDrivers();
|
|
934
|
+
}
|
|
935
|
+
}
|
|
936
|
+
if (exe) {
|
|
937
|
+
try {
|
|
938
|
+
const session = new NativeCdpSession();
|
|
939
|
+
await session.connect(exe, proxyUrl || undefined, this.stealth);
|
|
940
|
+
this.cdp = session;
|
|
941
|
+
this.state.mode = "cdp";
|
|
942
|
+
this.state.stealth = this.stealth;
|
|
943
|
+
return "cdp";
|
|
944
|
+
}
|
|
945
|
+
catch {
|
|
946
|
+
await this.cleanupLiveDrivers();
|
|
947
|
+
}
|
|
948
|
+
}
|
|
949
|
+
this.state.mode = "http";
|
|
950
|
+
this.state.stealth = this.stealth;
|
|
951
|
+
return "http";
|
|
952
|
+
}
|
|
953
|
+
pushConsoleEntry(level, text) {
|
|
954
|
+
this.consoleBuffer.push({
|
|
955
|
+
level: String(level || "log"),
|
|
956
|
+
text: String(text || ""),
|
|
957
|
+
timestamp: new Date().toISOString()
|
|
958
|
+
});
|
|
959
|
+
if (this.consoleBuffer.length > 200) {
|
|
960
|
+
this.consoleBuffer.shift();
|
|
961
|
+
}
|
|
962
|
+
}
|
|
963
|
+
async cleanupLiveDrivers() {
|
|
964
|
+
if (this.pwBrowser) {
|
|
965
|
+
try {
|
|
966
|
+
await this.pwBrowser.close();
|
|
967
|
+
}
|
|
968
|
+
catch {
|
|
969
|
+
}
|
|
970
|
+
this.pwPage = null;
|
|
971
|
+
this.pwContext = null;
|
|
972
|
+
this.pwBrowser = null;
|
|
973
|
+
}
|
|
974
|
+
if (this.cdp) {
|
|
975
|
+
await this.cdp.dispose();
|
|
976
|
+
this.cdp = null;
|
|
977
|
+
}
|
|
978
|
+
activeControllers.delete(this);
|
|
979
|
+
}
|
|
980
|
+
forceDisposeSync() {
|
|
981
|
+
try {
|
|
982
|
+
if (this.cdp) {
|
|
983
|
+
void this.cdp.dispose();
|
|
984
|
+
this.cdp = null;
|
|
985
|
+
}
|
|
986
|
+
if (this.pwBrowser) {
|
|
987
|
+
void this.pwBrowser.close();
|
|
988
|
+
this.pwBrowser = null;
|
|
989
|
+
this.pwContext = null;
|
|
990
|
+
this.pwPage = null;
|
|
991
|
+
}
|
|
992
|
+
}
|
|
993
|
+
catch {
|
|
994
|
+
}
|
|
995
|
+
}
|
|
996
|
+
async syncLiveDomState() {
|
|
997
|
+
let snap = null;
|
|
998
|
+
if (this.pwPage) {
|
|
999
|
+
snap = await this.pwPage.evaluate(SNAPSHOT_DOM_SCRIPT);
|
|
1000
|
+
}
|
|
1001
|
+
else if (this.cdp) {
|
|
1002
|
+
snap = await this.cdp.evalValue(SNAPSHOT_DOM_SCRIPT);
|
|
1003
|
+
this.state.statusCode = this.cdp.lastStatusCode;
|
|
1004
|
+
}
|
|
1005
|
+
if (!snap)
|
|
1006
|
+
return;
|
|
1007
|
+
this.state.url = String(snap.url || this.state.url);
|
|
1008
|
+
this.state.title = stripBrowserEmojis(String(snap.title || "")) || this.state.url;
|
|
1009
|
+
this.state.textContent = String(snap.textContent || "");
|
|
1010
|
+
this.state.rawHtml = String(snap.rawHtml || "");
|
|
1011
|
+
this.state.elements = Array.isArray(snap.elements) ? snap.elements : [];
|
|
1012
|
+
}
|
|
1013
|
+
resolveTargetRefOrSelector(target) {
|
|
1014
|
+
const clean = String(target || "").trim();
|
|
1015
|
+
const lower = clean.toLowerCase();
|
|
1016
|
+
if (/^@?e\d+$/i.test(clean)) {
|
|
1017
|
+
const normalizedRef = clean.startsWith("@") ? clean.toLowerCase() : `@${clean.toLowerCase()}`;
|
|
1018
|
+
const el = this.state.elements.find((e) => (e.ref || `@e${e.index}`).toLowerCase() === normalizedRef);
|
|
1019
|
+
return { ref: normalizedRef, matched: el, cssSelector: `[data-wide-ref="${normalizedRef}"]` };
|
|
1020
|
+
}
|
|
1021
|
+
if (/^\[\d+\]$/.test(clean) || /^\d+$/.test(clean)) {
|
|
1022
|
+
const idx = parseInt(clean.replace(/[\[\]]/g, ""), 10);
|
|
1023
|
+
const el = this.state.elements.find((e) => e.index === idx);
|
|
1024
|
+
const ref = el?.ref || `@e${idx}`;
|
|
1025
|
+
return { ref, matched: el, cssSelector: `[data-wide-ref="${ref}"]` };
|
|
1026
|
+
}
|
|
1027
|
+
const byMeta = this.state.elements.find((e) => e.text.toLowerCase().includes(lower) ||
|
|
1028
|
+
(e.name && e.name.toLowerCase().includes(lower)) ||
|
|
1029
|
+
(e.attributes?.placeholder && e.attributes.placeholder.toLowerCase().includes(lower)) ||
|
|
1030
|
+
(e.attributes?.id && e.attributes.id.toLowerCase() === lower));
|
|
1031
|
+
if (byMeta) {
|
|
1032
|
+
const ref = byMeta.ref || `@e${byMeta.index}`;
|
|
1033
|
+
return { ref, matched: byMeta, cssSelector: `[data-wide-ref="${ref}"]` };
|
|
1034
|
+
}
|
|
1035
|
+
if (/^[#.\[]|^[a-z]+\[/i.test(clean)) {
|
|
1036
|
+
return { cssSelector: clean };
|
|
1037
|
+
}
|
|
1038
|
+
return {};
|
|
1039
|
+
}
|
|
35
1040
|
parseHtml(rawHtml) {
|
|
36
|
-
|
|
1041
|
+
const clean = rawHtml
|
|
37
1042
|
.replace(/<script\b[^<]*(?:(?!<\/script>)<[^<]*)*<\/script>/gi, "")
|
|
38
1043
|
.replace(/<style\b[^<]*(?:(?!<\/style>)<[^<]*)*<\/style>/gi, "")
|
|
39
1044
|
.replace(/<noscript\b[^<]*(?:(?!<\/noscript>)<[^<]*)*<\/noscript>/gi, "")
|
|
40
1045
|
.replace(/<svg\b[^<]*(?:(?!<\/svg>)<[^<]*)*<\/svg>/gi, "");
|
|
41
1046
|
const titleMatch = /<title[^>]*>([^<]*)<\/title>/i.exec(clean);
|
|
42
|
-
const title = titleMatch ? titleMatch[1]
|
|
1047
|
+
const title = titleMatch ? stripBrowserEmojis(titleMatch[1]) : "";
|
|
43
1048
|
const elements = [];
|
|
44
1049
|
let elemIdx = 1;
|
|
45
1050
|
const inputRegex = /<input\b([^>]*)>/gi;
|
|
@@ -54,7 +1059,8 @@ export class HeadlessBrowserController {
|
|
|
54
1059
|
const role = `input[${type}]`;
|
|
55
1060
|
const desc = placeholder || name || val || role;
|
|
56
1061
|
elements.push({
|
|
57
|
-
index: elemIdx
|
|
1062
|
+
index: elemIdx,
|
|
1063
|
+
ref: `@e${elemIdx}`,
|
|
58
1064
|
tag: "input",
|
|
59
1065
|
text: desc,
|
|
60
1066
|
role,
|
|
@@ -62,6 +1068,7 @@ export class HeadlessBrowserController {
|
|
|
62
1068
|
value: val,
|
|
63
1069
|
attributes: attrs
|
|
64
1070
|
});
|
|
1071
|
+
elemIdx++;
|
|
65
1072
|
}
|
|
66
1073
|
const tagRegex = /<(a|button|select|textarea)\b([^>]*)>([\s\S]*?)<\/\1>/gi;
|
|
67
1074
|
let tagMatch;
|
|
@@ -73,7 +1080,8 @@ export class HeadlessBrowserController {
|
|
|
73
1080
|
const role = tag === "a" ? "link" : tag;
|
|
74
1081
|
const text = inner || attrs["aria-label"] || attrs["title"] || role;
|
|
75
1082
|
elements.push({
|
|
76
|
-
index: elemIdx
|
|
1083
|
+
index: elemIdx,
|
|
1084
|
+
ref: `@e${elemIdx}`,
|
|
77
1085
|
tag,
|
|
78
1086
|
text,
|
|
79
1087
|
role,
|
|
@@ -81,6 +1089,7 @@ export class HeadlessBrowserController {
|
|
|
81
1089
|
name: attrs.name,
|
|
82
1090
|
attributes: attrs
|
|
83
1091
|
});
|
|
1092
|
+
elemIdx++;
|
|
84
1093
|
}
|
|
85
1094
|
const plainText = clean
|
|
86
1095
|
.replace(/<[^>]+>/g, " ")
|
|
@@ -103,21 +1112,79 @@ export class HeadlessBrowserController {
|
|
|
103
1112
|
}
|
|
104
1113
|
return attrs;
|
|
105
1114
|
}
|
|
1115
|
+
async open(url, opts) {
|
|
1116
|
+
if (typeof opts?.stealth === "boolean" && opts.stealth !== this.stealth) {
|
|
1117
|
+
await this.cleanupLiveDrivers();
|
|
1118
|
+
this.stealth = opts.stealth;
|
|
1119
|
+
}
|
|
1120
|
+
const mode = await this.ensureLiveSession();
|
|
1121
|
+
if (url && url.trim()) {
|
|
1122
|
+
return this.navigate(url);
|
|
1123
|
+
}
|
|
1124
|
+
return `Browser session initialized (engine: ${mode}, url: ${this.state.url}${this.stealth ? ", stealth: on" : ""}).`;
|
|
1125
|
+
}
|
|
106
1126
|
async navigate(url) {
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
1127
|
+
const target = this.normalizeTargetUrl(url);
|
|
1128
|
+
const mode = await this.ensureLiveSession();
|
|
1129
|
+
if (mode === "playwright" && this.pwPage) {
|
|
1130
|
+
try {
|
|
1131
|
+
const res = await this.pwPage.goto(target, { waitUntil: "domcontentloaded", timeout: 25000 });
|
|
1132
|
+
if (res)
|
|
1133
|
+
this.state.statusCode = res.status();
|
|
1134
|
+
await this.pwPage.waitForTimeout(150);
|
|
1135
|
+
this.state.history.push(target);
|
|
1136
|
+
this.state.formInputs = {};
|
|
1137
|
+
await this.syncLiveDomState();
|
|
1138
|
+
return this.renderView();
|
|
1139
|
+
}
|
|
1140
|
+
catch (err) {
|
|
1141
|
+
return `Navigation failed for ${target}: ${err.message}`;
|
|
1142
|
+
}
|
|
1143
|
+
}
|
|
1144
|
+
if (mode === "cdp" && this.cdp) {
|
|
1145
|
+
try {
|
|
1146
|
+
let loaded = false;
|
|
1147
|
+
const offLoad = this.cdp.on("Page.loadEventFired", () => {
|
|
1148
|
+
loaded = true;
|
|
1149
|
+
});
|
|
1150
|
+
await this.cdp.send("Page.navigate", { url: target });
|
|
1151
|
+
const deadline = Date.now() + 15000;
|
|
1152
|
+
while (!loaded && Date.now() < deadline) {
|
|
1153
|
+
const ready = await this.cdp.evalValue("document.readyState").catch(() => "loading");
|
|
1154
|
+
if (ready === "complete" || ready === "interactive") {
|
|
1155
|
+
loaded = true;
|
|
1156
|
+
break;
|
|
1157
|
+
}
|
|
1158
|
+
await new Promise((r) => setTimeout(r, 100));
|
|
1159
|
+
}
|
|
1160
|
+
offLoad();
|
|
1161
|
+
await new Promise((r) => setTimeout(r, 150));
|
|
1162
|
+
this.state.history.push(target);
|
|
1163
|
+
this.state.formInputs = {};
|
|
1164
|
+
await this.syncLiveDomState();
|
|
1165
|
+
return this.renderView();
|
|
1166
|
+
}
|
|
1167
|
+
catch (err) {
|
|
1168
|
+
return `Navigation failed for ${target}: ${err.message}`;
|
|
1169
|
+
}
|
|
110
1170
|
}
|
|
111
1171
|
const controller = new AbortController();
|
|
112
1172
|
const timer = setTimeout(() => controller.abort(), 20000);
|
|
1173
|
+
const reqStartedAt = Date.now();
|
|
1174
|
+
const startedIso = new Date(reqStartedAt).toISOString();
|
|
1175
|
+
if (!this.harStartedAt)
|
|
1176
|
+
this.harStartedAt = startedIso;
|
|
113
1177
|
try {
|
|
114
|
-
const
|
|
115
|
-
|
|
116
|
-
"
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
1178
|
+
const headers = {
|
|
1179
|
+
"User-Agent": this.stealth
|
|
1180
|
+
? "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/131.0.0.0 Safari/537.36"
|
|
1181
|
+
: this.userAgent,
|
|
1182
|
+
Accept: "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8"
|
|
1183
|
+
};
|
|
1184
|
+
if (this.cookieJar.size > 0) {
|
|
1185
|
+
headers.Cookie = Array.from(this.cookieJar.entries()).map(([k, v]) => `${k}=${v}`).join("; ");
|
|
1186
|
+
}
|
|
1187
|
+
const res = await fetch(target, { headers, signal: controller.signal });
|
|
121
1188
|
clearTimeout(timer);
|
|
122
1189
|
this.state.statusCode = res.status;
|
|
123
1190
|
this.state.url = res.url || target;
|
|
@@ -125,9 +1192,48 @@ export class HeadlessBrowserController {
|
|
|
125
1192
|
this.state.history.push(this.state.url);
|
|
126
1193
|
this.state.formInputs = {};
|
|
127
1194
|
const parsed = this.parseHtml(this.state.rawHtml);
|
|
128
|
-
this.state.title = parsed.title || this.state.url;
|
|
1195
|
+
this.state.title = stripBrowserEmojis(parsed.title) || this.state.url;
|
|
129
1196
|
this.state.elements = parsed.elements;
|
|
130
1197
|
this.state.textContent = parsed.textContent;
|
|
1198
|
+
const elapsedMs = Math.max(0, Date.now() - reqStartedAt);
|
|
1199
|
+
const respHeadersObj = {};
|
|
1200
|
+
res.headers.forEach((v, k) => {
|
|
1201
|
+
respHeadersObj[k] = v;
|
|
1202
|
+
});
|
|
1203
|
+
this.harEntries.push({
|
|
1204
|
+
pageref: "page_1",
|
|
1205
|
+
startedDateTime: startedIso,
|
|
1206
|
+
time: elapsedMs,
|
|
1207
|
+
request: {
|
|
1208
|
+
method: "GET",
|
|
1209
|
+
url: this.state.url,
|
|
1210
|
+
httpVersion: "HTTP/1.1",
|
|
1211
|
+
cookies: [],
|
|
1212
|
+
headers: redactHarHeaders(headers),
|
|
1213
|
+
queryString: parseHarQuery(this.state.url),
|
|
1214
|
+
headersSize: -1,
|
|
1215
|
+
bodySize: 0,
|
|
1216
|
+
},
|
|
1217
|
+
response: {
|
|
1218
|
+
status: res.status,
|
|
1219
|
+
statusText: res.statusText || "",
|
|
1220
|
+
httpVersion: "HTTP/1.1",
|
|
1221
|
+
cookies: [],
|
|
1222
|
+
headers: redactHarHeaders(respHeadersObj),
|
|
1223
|
+
content: {
|
|
1224
|
+
size: Buffer.byteLength(this.state.rawHtml, "utf8"),
|
|
1225
|
+
mimeType: res.headers.get("content-type") || "text/html",
|
|
1226
|
+
},
|
|
1227
|
+
redirectURL: "",
|
|
1228
|
+
headersSize: -1,
|
|
1229
|
+
bodySize: Buffer.byteLength(this.state.rawHtml, "utf8"),
|
|
1230
|
+
},
|
|
1231
|
+
cache: {},
|
|
1232
|
+
timings: { send: 0, wait: elapsedMs, receive: 0 },
|
|
1233
|
+
});
|
|
1234
|
+
if (this.harEntries.length > 500) {
|
|
1235
|
+
this.harEntries.splice(0, this.harEntries.length - 500);
|
|
1236
|
+
}
|
|
131
1237
|
return this.renderView();
|
|
132
1238
|
}
|
|
133
1239
|
catch (err) {
|
|
@@ -135,33 +1241,66 @@ export class HeadlessBrowserController {
|
|
|
135
1241
|
return `Navigation failed for ${target}: ${err.message}`;
|
|
136
1242
|
}
|
|
137
1243
|
}
|
|
1244
|
+
async snapshot() {
|
|
1245
|
+
if (this.pwPage || this.cdp) {
|
|
1246
|
+
await this.syncLiveDomState();
|
|
1247
|
+
}
|
|
1248
|
+
return this.renderView();
|
|
1249
|
+
}
|
|
138
1250
|
async click(target) {
|
|
139
|
-
if (this.
|
|
140
|
-
|
|
1251
|
+
if (this.pwPage) {
|
|
1252
|
+
const resolved = this.resolveTargetRefOrSelector(target);
|
|
1253
|
+
if (resolved.cssSelector) {
|
|
1254
|
+
await this.pwPage.locator(resolved.cssSelector).first().click({ timeout: 8000 });
|
|
1255
|
+
await this.pwPage.waitForTimeout(250);
|
|
1256
|
+
await this.syncLiveDomState();
|
|
1257
|
+
return `Clicked ${resolved.ref || target}.\n\n${this.renderView()}`;
|
|
1258
|
+
}
|
|
1259
|
+
const loc = this.pwPage.getByText(target, { exact: false }).first();
|
|
1260
|
+
await loc.click({ timeout: 8000 });
|
|
1261
|
+
await this.pwPage.waitForTimeout(250);
|
|
1262
|
+
await this.syncLiveDomState();
|
|
1263
|
+
return `Clicked '${target}'.\n\n${this.renderView()}`;
|
|
141
1264
|
}
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
const
|
|
146
|
-
|
|
1265
|
+
if (this.cdp) {
|
|
1266
|
+
const resolved = this.resolveTargetRefOrSelector(target);
|
|
1267
|
+
const selector = resolved.cssSelector || "";
|
|
1268
|
+
const coords = await this.cdp.evalValue(`(() => {
|
|
1269
|
+
let el = ${selector ? `document.querySelector(${JSON.stringify(selector)})` : "null"};
|
|
1270
|
+
if (!el) {
|
|
1271
|
+
const needle = ${JSON.stringify(target.trim().toLowerCase())};
|
|
1272
|
+
const list = Array.from(document.querySelectorAll('a,button,input,select,textarea,[role="button"],[role="link"]'));
|
|
1273
|
+
el = list.find((n) => (n.innerText || n.textContent || n.getAttribute('aria-label') || '').toLowerCase().includes(needle)) || null;
|
|
147
1274
|
}
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
1275
|
+
if (!el) return { found: false };
|
|
1276
|
+
el.scrollIntoView({ block: 'center', inline: 'center' });
|
|
1277
|
+
const r = el.getBoundingClientRect();
|
|
1278
|
+
return { found: true, x: r.left + r.width / 2, y: r.top + r.height / 2 };
|
|
1279
|
+
})()`);
|
|
1280
|
+
if (!coords?.found) {
|
|
1281
|
+
const available = this.state.elements.slice(0, 10).map((e) => `${e.ref || `[${e.index}]`} ${e.role}: ${e.text.slice(0, 25)}`).join(", ");
|
|
1282
|
+
return `Element '${target}' not found. Available elements: ${available}`;
|
|
152
1283
|
}
|
|
1284
|
+
const cx = Math.max(1, Math.round(coords.x));
|
|
1285
|
+
const cy = Math.max(1, Math.round(coords.y));
|
|
1286
|
+
await this.cdp.send("Input.dispatchMouseEvent", { type: "mousePressed", x: cx, y: cy, button: "left", clickCount: 1 });
|
|
1287
|
+
await this.cdp.send("Input.dispatchMouseEvent", { type: "mouseReleased", x: cx, y: cy, button: "left", clickCount: 1 });
|
|
1288
|
+
await new Promise((r) => setTimeout(r, 300));
|
|
1289
|
+
await this.syncLiveDomState();
|
|
1290
|
+
return `Clicked ${resolved.ref || target}.\n\n${this.renderView()}`;
|
|
153
1291
|
}
|
|
154
|
-
if (
|
|
155
|
-
|
|
156
|
-
(e.name && e.name.toLowerCase().includes(cleanTarget)));
|
|
1292
|
+
if (this.state.elements.length === 0) {
|
|
1293
|
+
return "No interactive elements on the current page. Please navigate first.";
|
|
157
1294
|
}
|
|
1295
|
+
const resolved = this.resolveTargetRefOrSelector(target);
|
|
1296
|
+
const matched = resolved.matched;
|
|
158
1297
|
if (!matched) {
|
|
159
|
-
const available = this.state.elements.slice(0, 10).map(e => `[${e.index}] ${e.role}: ${e.text.slice(0, 25)}`).join(", ");
|
|
1298
|
+
const available = this.state.elements.slice(0, 10).map((e) => `[${e.index}] ${e.role}: ${e.text.slice(0, 25)}`).join(", ");
|
|
160
1299
|
return `Element '${target}' not found. Available elements: ${available}`;
|
|
161
1300
|
}
|
|
162
1301
|
if (matched.href) {
|
|
163
|
-
const
|
|
164
|
-
return await this.navigate(
|
|
1302
|
+
const resolvedUrl = new URL(matched.href, this.state.url).toString();
|
|
1303
|
+
return await this.navigate(resolvedUrl);
|
|
165
1304
|
}
|
|
166
1305
|
if (matched.tag === "button" || (matched.tag === "input" && ["submit", "button"].includes(matched.attributes.type || ""))) {
|
|
167
1306
|
if (Object.keys(this.state.formInputs).length > 0) {
|
|
@@ -197,21 +1336,52 @@ export class HeadlessBrowserController {
|
|
|
197
1336
|
}
|
|
198
1337
|
return `Clicked [${matched.index}] ${matched.role}: ${matched.text}`;
|
|
199
1338
|
}
|
|
200
|
-
fill(target, text, submit = false) {
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
1339
|
+
async fill(target, text, submit = false) {
|
|
1340
|
+
if (this.pwPage) {
|
|
1341
|
+
const resolved = this.resolveTargetRefOrSelector(target);
|
|
1342
|
+
if (!resolved.cssSelector) {
|
|
1343
|
+
return `Input element '${target}' not found in live page snapshot.`;
|
|
1344
|
+
}
|
|
1345
|
+
const loc = this.pwPage.locator(resolved.cssSelector).first();
|
|
1346
|
+
await loc.fill(text, { timeout: 8000 });
|
|
1347
|
+
if (submit) {
|
|
1348
|
+
await loc.press("Enter");
|
|
1349
|
+
await this.pwPage.waitForTimeout(250);
|
|
1350
|
+
}
|
|
1351
|
+
await this.syncLiveDomState();
|
|
1352
|
+
return `Filled ${resolved.ref || target} with value '${text}'${submit ? " and submitted" : ""}.`;
|
|
206
1353
|
}
|
|
207
|
-
|
|
208
|
-
const
|
|
209
|
-
|
|
210
|
-
|
|
1354
|
+
if (this.cdp) {
|
|
1355
|
+
const resolved = this.resolveTargetRefOrSelector(target);
|
|
1356
|
+
const selector = resolved.cssSelector || "";
|
|
1357
|
+
const focused = await this.cdp.evalValue(`(() => {
|
|
1358
|
+
let el = ${selector ? `document.querySelector(${JSON.stringify(selector)})` : "null"};
|
|
1359
|
+
if (!el) {
|
|
1360
|
+
const needle = ${JSON.stringify(target.trim().toLowerCase())};
|
|
1361
|
+
const list = Array.from(document.querySelectorAll('input,textarea'));
|
|
1362
|
+
el = list.find((n) => ((n.name || '') + ' ' + (n.placeholder || '') + ' ' + (n.getAttribute('aria-label') || '')).toLowerCase().includes(needle)) || null;
|
|
1363
|
+
}
|
|
1364
|
+
if (!el) return false;
|
|
1365
|
+
el.focus();
|
|
1366
|
+
el.value = ${JSON.stringify(text)};
|
|
1367
|
+
el.dispatchEvent(new Event('input', { bubbles: true }));
|
|
1368
|
+
el.dispatchEvent(new Event('change', { bubbles: true }));
|
|
1369
|
+
return true;
|
|
1370
|
+
})()`);
|
|
1371
|
+
if (!focused) {
|
|
1372
|
+
return `Input element '${target}' not found.`;
|
|
211
1373
|
}
|
|
1374
|
+
if (submit) {
|
|
1375
|
+
await this.press("Enter");
|
|
1376
|
+
}
|
|
1377
|
+
await this.syncLiveDomState();
|
|
1378
|
+
return `Filled ${resolved.ref || target} with value '${text}'${submit ? " and submitted" : ""}.`;
|
|
212
1379
|
}
|
|
1380
|
+
const resolved = this.resolveTargetRefOrSelector(target);
|
|
1381
|
+
let matched = resolved.matched;
|
|
1382
|
+
const cleanTarget = target.trim().toLowerCase();
|
|
213
1383
|
if (!matched) {
|
|
214
|
-
matched = this.state.elements.find(e => ["input", "textarea"].includes(e.tag) && (e.text.toLowerCase().includes(cleanTarget) ||
|
|
1384
|
+
matched = this.state.elements.find((e) => ["input", "textarea"].includes(e.tag) && (e.text.toLowerCase().includes(cleanTarget) ||
|
|
215
1385
|
(e.name && e.name.toLowerCase().includes(cleanTarget)) ||
|
|
216
1386
|
(e.attributes.placeholder && e.attributes.placeholder.toLowerCase().includes(cleanTarget))));
|
|
217
1387
|
}
|
|
@@ -221,10 +1391,141 @@ export class HeadlessBrowserController {
|
|
|
221
1391
|
return this.click("submit");
|
|
222
1392
|
}
|
|
223
1393
|
const desc = matched ? `[${matched.index}] ${matched.role}` : fieldName;
|
|
224
|
-
return
|
|
1394
|
+
return `Filled ${desc} with value '${text}'.`;
|
|
1395
|
+
}
|
|
1396
|
+
async press(key) {
|
|
1397
|
+
if (this.pwPage) {
|
|
1398
|
+
await this.pwPage.keyboard.press(key);
|
|
1399
|
+
await this.pwPage.waitForTimeout(200);
|
|
1400
|
+
await this.syncLiveDomState();
|
|
1401
|
+
return `Pressed key '${key}'.`;
|
|
1402
|
+
}
|
|
1403
|
+
if (this.cdp) {
|
|
1404
|
+
const keyCode = key === "Enter" ? 13 : key === "Tab" ? 9 : key === "Escape" ? 27 : 0;
|
|
1405
|
+
await this.cdp.send("Input.dispatchKeyEvent", { type: "keyDown", key, windowsVirtualKeyCode: keyCode });
|
|
1406
|
+
await this.cdp.send("Input.dispatchKeyEvent", { type: "keyUp", key, windowsVirtualKeyCode: keyCode });
|
|
1407
|
+
await new Promise((r) => setTimeout(r, 200));
|
|
1408
|
+
await this.syncLiveDomState();
|
|
1409
|
+
return `Pressed key '${key}'.`;
|
|
1410
|
+
}
|
|
1411
|
+
if (key.toLowerCase() === "enter" && Object.keys(this.state.formInputs).length > 0) {
|
|
1412
|
+
return this.click("submit");
|
|
1413
|
+
}
|
|
1414
|
+
return `Simulated key press '${key}' on ${this.state.url}.`;
|
|
1415
|
+
}
|
|
1416
|
+
async hover(target) {
|
|
1417
|
+
if (this.pwPage) {
|
|
1418
|
+
const resolved = this.resolveTargetRefOrSelector(target);
|
|
1419
|
+
if (resolved.cssSelector) {
|
|
1420
|
+
await this.pwPage.locator(resolved.cssSelector).first().hover({ timeout: 6000 });
|
|
1421
|
+
return `Hovered over ${resolved.ref || target}.`;
|
|
1422
|
+
}
|
|
1423
|
+
}
|
|
1424
|
+
if (this.cdp) {
|
|
1425
|
+
const resolved = this.resolveTargetRefOrSelector(target);
|
|
1426
|
+
const selector = resolved.cssSelector || target;
|
|
1427
|
+
const coords = await this.cdp.evalValue(`(() => {
|
|
1428
|
+
const el = document.querySelector(${JSON.stringify(selector)});
|
|
1429
|
+
if (!el) return null;
|
|
1430
|
+
el.scrollIntoView({ block: 'center' });
|
|
1431
|
+
const r = el.getBoundingClientRect();
|
|
1432
|
+
return { x: r.left + r.width / 2, y: r.top + r.height / 2 };
|
|
1433
|
+
})()`);
|
|
1434
|
+
if (coords) {
|
|
1435
|
+
await this.cdp.send("Input.dispatchMouseEvent", { type: "mouseMoved", x: Math.round(coords.x), y: Math.round(coords.y) });
|
|
1436
|
+
return `Hovered over ${resolved.ref || target}.`;
|
|
1437
|
+
}
|
|
1438
|
+
}
|
|
1439
|
+
return `Hovered over '${target}' on ${this.state.url}.`;
|
|
1440
|
+
}
|
|
1441
|
+
async select(target, value) {
|
|
1442
|
+
if (this.pwPage) {
|
|
1443
|
+
const resolved = this.resolveTargetRefOrSelector(target);
|
|
1444
|
+
if (resolved.cssSelector) {
|
|
1445
|
+
await this.pwPage.locator(resolved.cssSelector).first().selectOption(value);
|
|
1446
|
+
await this.syncLiveDomState();
|
|
1447
|
+
return `Selected '${value}' on ${resolved.ref || target}.`;
|
|
1448
|
+
}
|
|
1449
|
+
}
|
|
1450
|
+
if (this.cdp) {
|
|
1451
|
+
const resolved = this.resolveTargetRefOrSelector(target);
|
|
1452
|
+
const selector = resolved.cssSelector || target;
|
|
1453
|
+
const ok = await this.cdp.evalValue(`(() => {
|
|
1454
|
+
const el = document.querySelector(${JSON.stringify(selector)});
|
|
1455
|
+
if (!el || el.tagName.toLowerCase() !== 'select') return false;
|
|
1456
|
+
el.value = ${JSON.stringify(value)};
|
|
1457
|
+
el.dispatchEvent(new Event('change', { bubbles: true }));
|
|
1458
|
+
return true;
|
|
1459
|
+
})()`);
|
|
1460
|
+
if (!ok)
|
|
1461
|
+
return `Select element '${target}' not found.`;
|
|
1462
|
+
await this.syncLiveDomState();
|
|
1463
|
+
return `Selected '${value}' on ${resolved.ref || target}.`;
|
|
1464
|
+
}
|
|
1465
|
+
this.state.formInputs[target] = value;
|
|
1466
|
+
return `Selected '${value}' for '${target}'.`;
|
|
1467
|
+
}
|
|
1468
|
+
async scroll(direction = "down", pixels = 500) {
|
|
1469
|
+
const js = direction === "top"
|
|
1470
|
+
? "window.scrollTo(0, 0)"
|
|
1471
|
+
: direction === "bottom"
|
|
1472
|
+
? "window.scrollTo(0, document.body.scrollHeight)"
|
|
1473
|
+
: `window.scrollBy(0, ${direction === "up" ? -Math.abs(pixels) : Math.abs(pixels)})`;
|
|
1474
|
+
if (this.pwPage) {
|
|
1475
|
+
await this.pwPage.evaluate(js);
|
|
1476
|
+
return `Scrolled ${direction} (${pixels}px).`;
|
|
1477
|
+
}
|
|
1478
|
+
if (this.cdp) {
|
|
1479
|
+
await this.cdp.evalValue(js);
|
|
1480
|
+
return `Scrolled ${direction} (${pixels}px).`;
|
|
1481
|
+
}
|
|
1482
|
+
return `Scrolled ${direction} in viewport state.`;
|
|
1483
|
+
}
|
|
1484
|
+
async wait(opts) {
|
|
1485
|
+
const ms = Math.min(Math.max(Number(opts.ms || 1000), 50), 15000);
|
|
1486
|
+
if (opts.selector) {
|
|
1487
|
+
if (this.pwPage) {
|
|
1488
|
+
await this.pwPage.waitForSelector(opts.selector, { timeout: ms });
|
|
1489
|
+
await this.syncLiveDomState();
|
|
1490
|
+
return `Selector '${opts.selector}' appeared.`;
|
|
1491
|
+
}
|
|
1492
|
+
if (this.cdp) {
|
|
1493
|
+
const deadline = Date.now() + ms;
|
|
1494
|
+
while (Date.now() < deadline) {
|
|
1495
|
+
const found = await this.cdp.evalValue(`Boolean(document.querySelector(${JSON.stringify(opts.selector)}))`).catch(() => false);
|
|
1496
|
+
if (found) {
|
|
1497
|
+
await this.syncLiveDomState();
|
|
1498
|
+
return `Selector '${opts.selector}' appeared.`;
|
|
1499
|
+
}
|
|
1500
|
+
await new Promise((r) => setTimeout(r, 150));
|
|
1501
|
+
}
|
|
1502
|
+
throw new Error(`Timed out waiting for selector '${opts.selector}'`);
|
|
1503
|
+
}
|
|
1504
|
+
}
|
|
1505
|
+
if (opts.text) {
|
|
1506
|
+
if (this.pwPage) {
|
|
1507
|
+
await this.pwPage.getByText(opts.text).first().waitFor({ timeout: ms });
|
|
1508
|
+
await this.syncLiveDomState();
|
|
1509
|
+
return `Text '${opts.text}' appeared.`;
|
|
1510
|
+
}
|
|
1511
|
+
if (this.cdp) {
|
|
1512
|
+
const deadline = Date.now() + ms;
|
|
1513
|
+
while (Date.now() < deadline) {
|
|
1514
|
+
const hasText = await this.cdp.evalValue(`document.body ? document.body.innerText.includes(${JSON.stringify(opts.text)}) : false`).catch(() => false);
|
|
1515
|
+
if (hasText) {
|
|
1516
|
+
await this.syncLiveDomState();
|
|
1517
|
+
return `Text '${opts.text}' appeared.`;
|
|
1518
|
+
}
|
|
1519
|
+
await new Promise((r) => setTimeout(r, 150));
|
|
1520
|
+
}
|
|
1521
|
+
throw new Error(`Timed out waiting for text '${opts.text}'`);
|
|
1522
|
+
}
|
|
1523
|
+
}
|
|
1524
|
+
await new Promise((r) => setTimeout(r, ms));
|
|
1525
|
+
return `Waited ${ms}ms.`;
|
|
225
1526
|
}
|
|
226
1527
|
extract(query = "") {
|
|
227
|
-
if (!this.state.rawHtml) {
|
|
1528
|
+
if (!this.state.rawHtml && !this.state.textContent) {
|
|
228
1529
|
return "No webpage currently loaded. Use browser_navigate first.";
|
|
229
1530
|
}
|
|
230
1531
|
const lines = [
|
|
@@ -237,8 +1538,8 @@ export class HeadlessBrowserController {
|
|
|
237
1538
|
const qLower = query.toLowerCase();
|
|
238
1539
|
const matched = this.state.textContent
|
|
239
1540
|
.split(/(?<=\.|\n)/)
|
|
240
|
-
.map(s => s.trim())
|
|
241
|
-
.filter(s => s.toLowerCase().includes(qLower));
|
|
1541
|
+
.map((s) => s.trim())
|
|
1542
|
+
.filter((s) => s.toLowerCase().includes(qLower));
|
|
242
1543
|
if (matched.length > 0) {
|
|
243
1544
|
lines.push(`Content matching '${query}':`);
|
|
244
1545
|
lines.push(...matched.slice(0, 25));
|
|
@@ -254,6 +1555,26 @@ export class HeadlessBrowserController {
|
|
|
254
1555
|
}
|
|
255
1556
|
return lines.join("\n");
|
|
256
1557
|
}
|
|
1558
|
+
async captureScreenshotFile(outputDir) {
|
|
1559
|
+
let pngBuffer = null;
|
|
1560
|
+
if (this.pwPage) {
|
|
1561
|
+
pngBuffer = await this.pwPage.screenshot({ type: "png" });
|
|
1562
|
+
}
|
|
1563
|
+
else if (this.cdp) {
|
|
1564
|
+
const res = await this.cdp.send("Page.captureScreenshot", { format: "png" });
|
|
1565
|
+
if (res?.data) {
|
|
1566
|
+
pngBuffer = Buffer.from(res.data, "base64");
|
|
1567
|
+
}
|
|
1568
|
+
}
|
|
1569
|
+
if (!pngBuffer)
|
|
1570
|
+
return null;
|
|
1571
|
+
const baseDir = outputDir ? path.join(outputDir, ".wide", "screenshots") : os.tmpdir();
|
|
1572
|
+
fs.mkdirSync(baseDir, { recursive: true });
|
|
1573
|
+
const outPath = path.join(baseDir, `wide-browser-${Date.now()}.png`);
|
|
1574
|
+
fs.writeFileSync(outPath, pngBuffer);
|
|
1575
|
+
this.state.lastScreenshotPath = outPath;
|
|
1576
|
+
return outPath;
|
|
1577
|
+
}
|
|
257
1578
|
screenshot() {
|
|
258
1579
|
const lines = [
|
|
259
1580
|
"+----------------------------------------------------------------+",
|
|
@@ -268,23 +1589,78 @@ export class HeadlessBrowserController {
|
|
|
268
1589
|
lines.push(`| [${String(el.index).padStart(2, "0")}] ${role} | ${text} |`);
|
|
269
1590
|
}
|
|
270
1591
|
lines.push("+----------------------------------------------------------------+");
|
|
1592
|
+
if (this.state.lastScreenshotPath) {
|
|
1593
|
+
lines.push(`Screenshot saved: ${this.state.lastScreenshotPath}`);
|
|
1594
|
+
}
|
|
271
1595
|
return lines.join("\n");
|
|
272
1596
|
}
|
|
1597
|
+
getConsole(clear = false) {
|
|
1598
|
+
const merged = this.cdp ? [...this.consoleBuffer, ...this.cdp.consoleEntries] : [...this.consoleBuffer];
|
|
1599
|
+
if (clear) {
|
|
1600
|
+
this.consoleBuffer = [];
|
|
1601
|
+
if (this.cdp)
|
|
1602
|
+
this.cdp.consoleEntries = [];
|
|
1603
|
+
}
|
|
1604
|
+
return merged;
|
|
1605
|
+
}
|
|
1606
|
+
async cookies(action = "get", cookie) {
|
|
1607
|
+
if (action === "clear") {
|
|
1608
|
+
this.cookieJar.clear();
|
|
1609
|
+
if (this.pwContext)
|
|
1610
|
+
await this.pwContext.clearCookies();
|
|
1611
|
+
if (this.cdp)
|
|
1612
|
+
await this.cdp.send("Network.clearBrowserCookies");
|
|
1613
|
+
return "Cleared browser cookies.";
|
|
1614
|
+
}
|
|
1615
|
+
if (action === "set" && cookie?.name) {
|
|
1616
|
+
const targetUrl = cookie.url || (this.state.url.startsWith("http") ? this.state.url : "http://127.0.0.1/");
|
|
1617
|
+
this.cookieJar.set(cookie.name, cookie.value);
|
|
1618
|
+
if (this.pwContext) {
|
|
1619
|
+
await this.pwContext.addCookies([{ name: cookie.name, value: cookie.value, url: targetUrl }]);
|
|
1620
|
+
}
|
|
1621
|
+
else if (this.cdp) {
|
|
1622
|
+
await this.cdp.send("Network.setCookie", { name: cookie.name, value: cookie.value, url: targetUrl });
|
|
1623
|
+
}
|
|
1624
|
+
return `Set cookie '${cookie.name}'.`;
|
|
1625
|
+
}
|
|
1626
|
+
if (this.pwContext) {
|
|
1627
|
+
const list = await this.pwContext.cookies();
|
|
1628
|
+
return JSON.stringify(list, null, 2);
|
|
1629
|
+
}
|
|
1630
|
+
if (this.cdp) {
|
|
1631
|
+
const res = await this.cdp.send("Network.getCookies");
|
|
1632
|
+
return JSON.stringify(res?.cookies || [], null, 2);
|
|
1633
|
+
}
|
|
1634
|
+
return JSON.stringify(Array.from(this.cookieJar.entries()).map(([name, value]) => ({ name, value })), null, 2);
|
|
1635
|
+
}
|
|
1636
|
+
async evalJs(script) {
|
|
1637
|
+
if (this.pwPage) {
|
|
1638
|
+
const val = await this.pwPage.evaluate(script);
|
|
1639
|
+
return typeof val === "string" ? val : JSON.stringify(val, null, 2);
|
|
1640
|
+
}
|
|
1641
|
+
if (this.cdp) {
|
|
1642
|
+
const val = await this.cdp.evalValue(script);
|
|
1643
|
+
return typeof val === "string" ? val : JSON.stringify(val, null, 2);
|
|
1644
|
+
}
|
|
1645
|
+
throw new Error("JavaScript evaluation requires an active Chromium/Edge browser session (call browser_open or browser_navigate first).");
|
|
1646
|
+
}
|
|
273
1647
|
renderView() {
|
|
274
1648
|
const out = [
|
|
275
1649
|
`Page Title: ${this.state.title}`,
|
|
276
1650
|
`Current URL: ${this.state.url}`,
|
|
277
1651
|
`Status Code: ${this.state.statusCode}`,
|
|
1652
|
+
`Engine: ${this.state.mode || "http"}`,
|
|
278
1653
|
"",
|
|
279
|
-
"Interactive Elements:"
|
|
1654
|
+
"Interactive Elements (target by @eN ref, [N], or label):"
|
|
280
1655
|
];
|
|
281
1656
|
if (this.state.elements.length === 0) {
|
|
282
1657
|
out.push("(No interactive elements found)");
|
|
283
1658
|
}
|
|
284
1659
|
else {
|
|
285
|
-
for (const el of this.state.elements.slice(0,
|
|
1660
|
+
for (const el of this.state.elements.slice(0, 35)) {
|
|
1661
|
+
const ref = el.ref || `@e${el.index}`;
|
|
286
1662
|
const extra = el.href ? ` -> ${el.href}` : "";
|
|
287
|
-
out.push(`[${el.index}] ${el.role}: ${el.text}${extra}`);
|
|
1663
|
+
out.push(`[${el.index}] (${ref}) ${el.role}: ${el.text}${extra}`);
|
|
288
1664
|
}
|
|
289
1665
|
}
|
|
290
1666
|
out.push("");
|
|
@@ -292,8 +1668,38 @@ export class HeadlessBrowserController {
|
|
|
292
1668
|
out.push(this.state.textContent.slice(0, 1500) || "(Empty content)");
|
|
293
1669
|
return out.join("\n");
|
|
294
1670
|
}
|
|
1671
|
+
exportHar(opts) {
|
|
1672
|
+
const cdpEntries = this.cdp?.harEntries || [];
|
|
1673
|
+
const combined = [...cdpEntries, ...this.harEntries];
|
|
1674
|
+
const filter = String(opts?.urlFilter || "").trim().toLowerCase();
|
|
1675
|
+
const limit = Math.max(1, Math.min(500, Number(opts?.limit) || 100));
|
|
1676
|
+
const filtered = filter
|
|
1677
|
+
? combined.filter((e) => String(e?.request?.url || "").toLowerCase().includes(filter))
|
|
1678
|
+
: combined;
|
|
1679
|
+
return {
|
|
1680
|
+
log: {
|
|
1681
|
+
version: "1.2",
|
|
1682
|
+
creator: { name: "Wide CLI Browser", version: "1.0" },
|
|
1683
|
+
pages: [
|
|
1684
|
+
{
|
|
1685
|
+
startedDateTime: this.harStartedAt || new Date().toISOString(),
|
|
1686
|
+
id: "page_1",
|
|
1687
|
+
title: stripBrowserEmojis(this.state.title || this.state.url || "New Tab") || "New Tab",
|
|
1688
|
+
pageTimings: { onContentLoad: -1, onLoad: -1 },
|
|
1689
|
+
},
|
|
1690
|
+
],
|
|
1691
|
+
entries: filtered.slice(-limit),
|
|
1692
|
+
},
|
|
1693
|
+
};
|
|
1694
|
+
}
|
|
295
1695
|
close() {
|
|
296
1696
|
const u = this.state.url;
|
|
1697
|
+
void this.cleanupLiveDrivers();
|
|
1698
|
+
this.consoleBuffer = [];
|
|
1699
|
+
this.cookieJar.clear();
|
|
1700
|
+
this.harEntries = [];
|
|
1701
|
+
this.harStartedAt = "";
|
|
1702
|
+
this.stealth = false;
|
|
297
1703
|
this.state = {
|
|
298
1704
|
url: "about:blank",
|
|
299
1705
|
title: "",
|
|
@@ -302,16 +1708,43 @@ export class HeadlessBrowserController {
|
|
|
302
1708
|
textContent: "",
|
|
303
1709
|
rawHtml: "",
|
|
304
1710
|
formInputs: {},
|
|
305
|
-
history: []
|
|
1711
|
+
history: [],
|
|
1712
|
+
mode: "http",
|
|
1713
|
+
stealth: false,
|
|
306
1714
|
};
|
|
307
1715
|
return `Headless browser session closed. Cleared session for ${u}.`;
|
|
308
1716
|
}
|
|
309
1717
|
}
|
|
310
1718
|
export const defaultBrowser = new HeadlessBrowserController();
|
|
1719
|
+
export const browserOpenTool = {
|
|
1720
|
+
definition: {
|
|
1721
|
+
name: "browser_open",
|
|
1722
|
+
description: "Initialize the real Chromium/Edge browser session and optionally navigate to an initial URL.",
|
|
1723
|
+
parameters: {
|
|
1724
|
+
type: "object",
|
|
1725
|
+
properties: {
|
|
1726
|
+
url: { type: "string", description: "Optional URL to open immediately." },
|
|
1727
|
+
stealth: { type: "boolean", description: "Opt-in stealth evasion mode masking navigator.webdriver." }
|
|
1728
|
+
},
|
|
1729
|
+
required: []
|
|
1730
|
+
}
|
|
1731
|
+
},
|
|
1732
|
+
async execute(params) {
|
|
1733
|
+
try {
|
|
1734
|
+
const result = await defaultBrowser.open(params.url, {
|
|
1735
|
+
stealth: typeof params.stealth === "boolean" ? params.stealth : undefined,
|
|
1736
|
+
});
|
|
1737
|
+
return { success: true, output: result };
|
|
1738
|
+
}
|
|
1739
|
+
catch (err) {
|
|
1740
|
+
return { success: false, output: "", error: `Open error: ${err.message}` };
|
|
1741
|
+
}
|
|
1742
|
+
}
|
|
1743
|
+
};
|
|
311
1744
|
export const browserNavigateTool = {
|
|
312
1745
|
definition: {
|
|
313
1746
|
name: "browser_navigate",
|
|
314
|
-
description: "Navigate the autonomous
|
|
1747
|
+
description: "Navigate the autonomous browser to a target URL. Returns page title, status, and @eN / [N] interactive element refs.",
|
|
315
1748
|
parameters: {
|
|
316
1749
|
type: "object",
|
|
317
1750
|
properties: {
|
|
@@ -320,7 +1753,7 @@ export const browserNavigateTool = {
|
|
|
320
1753
|
required: ["url"]
|
|
321
1754
|
}
|
|
322
1755
|
},
|
|
323
|
-
async execute(params
|
|
1756
|
+
async execute(params) {
|
|
324
1757
|
try {
|
|
325
1758
|
const result = await defaultBrowser.navigate(params.url);
|
|
326
1759
|
return { success: true, output: result };
|
|
@@ -330,19 +1763,39 @@ export const browserNavigateTool = {
|
|
|
330
1763
|
}
|
|
331
1764
|
}
|
|
332
1765
|
};
|
|
1766
|
+
export const browserSnapshotTool = {
|
|
1767
|
+
definition: {
|
|
1768
|
+
name: "browser_snapshot",
|
|
1769
|
+
description: "Capture a fresh interactive DOM snapshot with @eN element refs and visible text from the current page.",
|
|
1770
|
+
parameters: {
|
|
1771
|
+
type: "object",
|
|
1772
|
+
properties: {},
|
|
1773
|
+
required: []
|
|
1774
|
+
}
|
|
1775
|
+
},
|
|
1776
|
+
async execute() {
|
|
1777
|
+
try {
|
|
1778
|
+
const result = await defaultBrowser.snapshot();
|
|
1779
|
+
return { success: true, output: result };
|
|
1780
|
+
}
|
|
1781
|
+
catch (err) {
|
|
1782
|
+
return { success: false, output: "", error: `Snapshot error: ${err.message}` };
|
|
1783
|
+
}
|
|
1784
|
+
}
|
|
1785
|
+
};
|
|
333
1786
|
export const browserClickTool = {
|
|
334
1787
|
definition: {
|
|
335
1788
|
name: "browser_click",
|
|
336
|
-
description: "Click an interactive element (link, button, tab)
|
|
1789
|
+
description: "Click an interactive element (link, button, tab) by its @eN ref, [N] index, CSS selector, or text label.",
|
|
337
1790
|
parameters: {
|
|
338
1791
|
type: "object",
|
|
339
1792
|
properties: {
|
|
340
|
-
target: { type: "string", description: "Element index (
|
|
1793
|
+
target: { type: "string", description: "Element ref ('@e3'), index ('[3]' or '3'), CSS selector, or text label." }
|
|
341
1794
|
},
|
|
342
1795
|
required: ["target"]
|
|
343
1796
|
}
|
|
344
1797
|
},
|
|
345
|
-
async execute(params
|
|
1798
|
+
async execute(params) {
|
|
346
1799
|
try {
|
|
347
1800
|
const result = await defaultBrowser.click(params.target);
|
|
348
1801
|
return { success: true, output: result };
|
|
@@ -355,18 +1808,18 @@ export const browserClickTool = {
|
|
|
355
1808
|
export const browserTypeTool = {
|
|
356
1809
|
definition: {
|
|
357
1810
|
name: "browser_type",
|
|
358
|
-
description: "Fill text into an input field or textarea identified by
|
|
1811
|
+
description: "Fill text into an input field or textarea identified by @eN ref, [N] index, CSS selector, or name/placeholder.",
|
|
359
1812
|
parameters: {
|
|
360
1813
|
type: "object",
|
|
361
1814
|
properties: {
|
|
362
|
-
target: { type: "string", description: "Element index (
|
|
1815
|
+
target: { type: "string", description: "Element ref ('@e2'), index ('[2]'), CSS selector, or input name/placeholder." },
|
|
363
1816
|
text: { type: "string", description: "Text string to fill into the input field." },
|
|
364
|
-
submit: { type: "boolean", description: "Optional boolean flag to
|
|
1817
|
+
submit: { type: "boolean", description: "Optional boolean flag to submit after typing." }
|
|
365
1818
|
},
|
|
366
1819
|
required: ["target", "text"]
|
|
367
1820
|
}
|
|
368
1821
|
},
|
|
369
|
-
async execute(params
|
|
1822
|
+
async execute(params) {
|
|
370
1823
|
try {
|
|
371
1824
|
const result = await defaultBrowser.fill(params.target, params.text, Boolean(params.submit));
|
|
372
1825
|
return { success: true, output: result };
|
|
@@ -376,10 +1829,124 @@ export const browserTypeTool = {
|
|
|
376
1829
|
}
|
|
377
1830
|
}
|
|
378
1831
|
};
|
|
1832
|
+
export const browserPressTool = {
|
|
1833
|
+
definition: {
|
|
1834
|
+
name: "browser_press",
|
|
1835
|
+
description: "Dispatch a keyboard key press (e.g. Enter, Tab, Escape, ArrowDown) in the active browser page.",
|
|
1836
|
+
parameters: {
|
|
1837
|
+
type: "object",
|
|
1838
|
+
properties: {
|
|
1839
|
+
key: { type: "string", description: "Key name (e.g. 'Enter', 'Tab', 'Escape')." }
|
|
1840
|
+
},
|
|
1841
|
+
required: ["key"]
|
|
1842
|
+
}
|
|
1843
|
+
},
|
|
1844
|
+
async execute(params) {
|
|
1845
|
+
try {
|
|
1846
|
+
const result = await defaultBrowser.press(params.key);
|
|
1847
|
+
return { success: true, output: result };
|
|
1848
|
+
}
|
|
1849
|
+
catch (err) {
|
|
1850
|
+
return { success: false, output: "", error: `Key press error: ${err.message}` };
|
|
1851
|
+
}
|
|
1852
|
+
}
|
|
1853
|
+
};
|
|
1854
|
+
export const browserHoverTool = {
|
|
1855
|
+
definition: {
|
|
1856
|
+
name: "browser_hover",
|
|
1857
|
+
description: "Hover the mouse cursor over an element identified by @eN ref, index, or CSS selector.",
|
|
1858
|
+
parameters: {
|
|
1859
|
+
type: "object",
|
|
1860
|
+
properties: {
|
|
1861
|
+
target: { type: "string", description: "Element ref, index, or CSS selector." }
|
|
1862
|
+
},
|
|
1863
|
+
required: ["target"]
|
|
1864
|
+
}
|
|
1865
|
+
},
|
|
1866
|
+
async execute(params) {
|
|
1867
|
+
try {
|
|
1868
|
+
const result = await defaultBrowser.hover(params.target);
|
|
1869
|
+
return { success: true, output: result };
|
|
1870
|
+
}
|
|
1871
|
+
catch (err) {
|
|
1872
|
+
return { success: false, output: "", error: `Hover error: ${err.message}` };
|
|
1873
|
+
}
|
|
1874
|
+
}
|
|
1875
|
+
};
|
|
1876
|
+
export const browserSelectTool = {
|
|
1877
|
+
definition: {
|
|
1878
|
+
name: "browser_select",
|
|
1879
|
+
description: "Select an option value on a <select> dropdown element.",
|
|
1880
|
+
parameters: {
|
|
1881
|
+
type: "object",
|
|
1882
|
+
properties: {
|
|
1883
|
+
target: { type: "string", description: "Select element @eN ref, index, or CSS selector." },
|
|
1884
|
+
value: { type: "string", description: "Option value to select." }
|
|
1885
|
+
},
|
|
1886
|
+
required: ["target", "value"]
|
|
1887
|
+
}
|
|
1888
|
+
},
|
|
1889
|
+
async execute(params) {
|
|
1890
|
+
try {
|
|
1891
|
+
const result = await defaultBrowser.select(params.target, params.value);
|
|
1892
|
+
return { success: true, output: result };
|
|
1893
|
+
}
|
|
1894
|
+
catch (err) {
|
|
1895
|
+
return { success: false, output: "", error: `Select error: ${err.message}` };
|
|
1896
|
+
}
|
|
1897
|
+
}
|
|
1898
|
+
};
|
|
1899
|
+
export const browserScrollTool = {
|
|
1900
|
+
definition: {
|
|
1901
|
+
name: "browser_scroll",
|
|
1902
|
+
description: "Scroll the browser viewport up, down, top, or bottom.",
|
|
1903
|
+
parameters: {
|
|
1904
|
+
type: "object",
|
|
1905
|
+
properties: {
|
|
1906
|
+
direction: { type: "string", description: "Scroll direction.", enum: ["up", "down", "top", "bottom"] },
|
|
1907
|
+
pixels: { type: "number", description: "Pixels to scroll for up/down (default: 500)." }
|
|
1908
|
+
},
|
|
1909
|
+
required: []
|
|
1910
|
+
}
|
|
1911
|
+
},
|
|
1912
|
+
async execute(params) {
|
|
1913
|
+
try {
|
|
1914
|
+
const result = await defaultBrowser.scroll(params.direction || "down", params.pixels || 500);
|
|
1915
|
+
return { success: true, output: result };
|
|
1916
|
+
}
|
|
1917
|
+
catch (err) {
|
|
1918
|
+
return { success: false, output: "", error: `Scroll error: ${err.message}` };
|
|
1919
|
+
}
|
|
1920
|
+
}
|
|
1921
|
+
};
|
|
1922
|
+
export const browserWaitTool = {
|
|
1923
|
+
definition: {
|
|
1924
|
+
name: "browser_wait",
|
|
1925
|
+
description: "Wait for a CSS selector, visible text, or fixed duration in the browser.",
|
|
1926
|
+
parameters: {
|
|
1927
|
+
type: "object",
|
|
1928
|
+
properties: {
|
|
1929
|
+
ms: { type: "number", description: "Timeout or wait duration in milliseconds." },
|
|
1930
|
+
selector: { type: "string", description: "Optional CSS selector to wait for." },
|
|
1931
|
+
text: { type: "string", description: "Optional visible text to wait for." }
|
|
1932
|
+
},
|
|
1933
|
+
required: []
|
|
1934
|
+
}
|
|
1935
|
+
},
|
|
1936
|
+
async execute(params) {
|
|
1937
|
+
try {
|
|
1938
|
+
const result = await defaultBrowser.wait({ ms: params.ms, selector: params.selector, text: params.text });
|
|
1939
|
+
return { success: true, output: result };
|
|
1940
|
+
}
|
|
1941
|
+
catch (err) {
|
|
1942
|
+
return { success: false, output: "", error: `Wait error: ${err.message}` };
|
|
1943
|
+
}
|
|
1944
|
+
}
|
|
1945
|
+
};
|
|
379
1946
|
export const browserExtractTool = {
|
|
380
1947
|
definition: {
|
|
381
1948
|
name: "browser_extract",
|
|
382
|
-
description: "Extract text content or search for specific keywords from the currently loaded page
|
|
1949
|
+
description: "Extract text content or search for specific keywords from the currently loaded page.",
|
|
383
1950
|
parameters: {
|
|
384
1951
|
type: "object",
|
|
385
1952
|
properties: {
|
|
@@ -388,7 +1955,7 @@ export const browserExtractTool = {
|
|
|
388
1955
|
required: []
|
|
389
1956
|
}
|
|
390
1957
|
},
|
|
391
|
-
async execute(params
|
|
1958
|
+
async execute(params) {
|
|
392
1959
|
try {
|
|
393
1960
|
const result = defaultBrowser.extract(params.query || "");
|
|
394
1961
|
return { success: true, output: result };
|
|
@@ -401,15 +1968,16 @@ export const browserExtractTool = {
|
|
|
401
1968
|
export const browserScreenshotTool = {
|
|
402
1969
|
definition: {
|
|
403
1970
|
name: "browser_screenshot",
|
|
404
|
-
description: "Capture a
|
|
1971
|
+
description: "Capture a screenshot PNG (when Chromium/Edge is active) and structured viewport snapshot.",
|
|
405
1972
|
parameters: {
|
|
406
1973
|
type: "object",
|
|
407
1974
|
properties: {},
|
|
408
1975
|
required: []
|
|
409
1976
|
}
|
|
410
1977
|
},
|
|
411
|
-
async execute(
|
|
1978
|
+
async execute(_params, context) {
|
|
412
1979
|
try {
|
|
1980
|
+
await defaultBrowser.captureScreenshotFile(context?.workingDirectory);
|
|
413
1981
|
const result = defaultBrowser.screenshot();
|
|
414
1982
|
return { success: true, output: result };
|
|
415
1983
|
}
|
|
@@ -418,17 +1986,113 @@ export const browserScreenshotTool = {
|
|
|
418
1986
|
}
|
|
419
1987
|
}
|
|
420
1988
|
};
|
|
1989
|
+
export const browserConsoleTool = {
|
|
1990
|
+
definition: {
|
|
1991
|
+
name: "browser_console",
|
|
1992
|
+
description: "Read console logs and runtime errors emitted by the active browser page.",
|
|
1993
|
+
parameters: {
|
|
1994
|
+
type: "object",
|
|
1995
|
+
properties: {
|
|
1996
|
+
clear: { type: "boolean", description: "Clear console buffer after reading." }
|
|
1997
|
+
},
|
|
1998
|
+
required: []
|
|
1999
|
+
}
|
|
2000
|
+
},
|
|
2001
|
+
async execute(params) {
|
|
2002
|
+
try {
|
|
2003
|
+
const entries = defaultBrowser.getConsole(Boolean(params.clear));
|
|
2004
|
+
return { success: true, output: entries.length > 0 ? JSON.stringify(entries, null, 2) : "No console messages." };
|
|
2005
|
+
}
|
|
2006
|
+
catch (err) {
|
|
2007
|
+
return { success: false, output: "", error: `Console error: ${err.message}` };
|
|
2008
|
+
}
|
|
2009
|
+
}
|
|
2010
|
+
};
|
|
2011
|
+
export const browserCookiesTool = {
|
|
2012
|
+
definition: {
|
|
2013
|
+
name: "browser_cookies",
|
|
2014
|
+
description: "Inspect, set, or clear browser cookies.",
|
|
2015
|
+
parameters: {
|
|
2016
|
+
type: "object",
|
|
2017
|
+
properties: {
|
|
2018
|
+
action: { type: "string", description: "Cookie action.", enum: ["get", "set", "clear"] },
|
|
2019
|
+
name: { type: "string", description: "Cookie name (for 'set')." },
|
|
2020
|
+
value: { type: "string", description: "Cookie value (for 'set')." },
|
|
2021
|
+
url: { type: "string", description: "Optional cookie URL (for 'set')." }
|
|
2022
|
+
},
|
|
2023
|
+
required: []
|
|
2024
|
+
}
|
|
2025
|
+
},
|
|
2026
|
+
async execute(params) {
|
|
2027
|
+
try {
|
|
2028
|
+
const action = (params.action || "get");
|
|
2029
|
+
const result = await defaultBrowser.cookies(action, params.name ? { name: params.name, value: String(params.value || ""), url: params.url } : undefined);
|
|
2030
|
+
return { success: true, output: result };
|
|
2031
|
+
}
|
|
2032
|
+
catch (err) {
|
|
2033
|
+
return { success: false, output: "", error: `Cookies error: ${err.message}` };
|
|
2034
|
+
}
|
|
2035
|
+
}
|
|
2036
|
+
};
|
|
2037
|
+
export const browserEvalTool = {
|
|
2038
|
+
definition: {
|
|
2039
|
+
name: "browser_eval",
|
|
2040
|
+
description: "Execute JavaScript in the active browser page context and return the serialized result.",
|
|
2041
|
+
parameters: {
|
|
2042
|
+
type: "object",
|
|
2043
|
+
properties: {
|
|
2044
|
+
script: { type: "string", description: "JavaScript expression or IIFE to run in the page." }
|
|
2045
|
+
},
|
|
2046
|
+
required: ["script"]
|
|
2047
|
+
}
|
|
2048
|
+
},
|
|
2049
|
+
async execute(params) {
|
|
2050
|
+
try {
|
|
2051
|
+
const result = await defaultBrowser.evalJs(params.script);
|
|
2052
|
+
return { success: true, output: result };
|
|
2053
|
+
}
|
|
2054
|
+
catch (err) {
|
|
2055
|
+
return { success: false, output: "", error: `Eval error: ${err.message}` };
|
|
2056
|
+
}
|
|
2057
|
+
}
|
|
2058
|
+
};
|
|
2059
|
+
export const browserExportHarTool = {
|
|
2060
|
+
definition: {
|
|
2061
|
+
name: "browser_export_har",
|
|
2062
|
+
description: "Export captured live browser network traffic in HAR 1.2 JSON format (with sensitive Auth/Cookie headers redacted).",
|
|
2063
|
+
parameters: {
|
|
2064
|
+
type: "object",
|
|
2065
|
+
properties: {
|
|
2066
|
+
urlFilter: { type: "string", description: "Optional substring to filter recorded request URLs." },
|
|
2067
|
+
limit: { type: "number", description: "Maximum number of HAR entries to include (default 100)." }
|
|
2068
|
+
},
|
|
2069
|
+
required: []
|
|
2070
|
+
}
|
|
2071
|
+
},
|
|
2072
|
+
async execute(params) {
|
|
2073
|
+
try {
|
|
2074
|
+
const har = defaultBrowser.exportHar({
|
|
2075
|
+
urlFilter: params.urlFilter,
|
|
2076
|
+
limit: params.limit,
|
|
2077
|
+
});
|
|
2078
|
+
return { success: true, output: JSON.stringify(har, null, 2).slice(0, 18000) };
|
|
2079
|
+
}
|
|
2080
|
+
catch (err) {
|
|
2081
|
+
return { success: false, output: "", error: `HAR export error: ${err.message}` };
|
|
2082
|
+
}
|
|
2083
|
+
}
|
|
2084
|
+
};
|
|
421
2085
|
export const browserCloseTool = {
|
|
422
2086
|
definition: {
|
|
423
2087
|
name: "browser_close",
|
|
424
|
-
description: "Close the active
|
|
2088
|
+
description: "Close the active browser session and release Chromium/Edge resources.",
|
|
425
2089
|
parameters: {
|
|
426
2090
|
type: "object",
|
|
427
2091
|
properties: {},
|
|
428
2092
|
required: []
|
|
429
2093
|
}
|
|
430
2094
|
},
|
|
431
|
-
async execute(
|
|
2095
|
+
async execute() {
|
|
432
2096
|
try {
|
|
433
2097
|
const result = defaultBrowser.close();
|
|
434
2098
|
return { success: true, output: result };
|