@apmantza/greedysearch-pi 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.claude/settings.local.json +12 -0
- package/README.md +59 -0
- package/cdp.mjs +788 -0
- package/extractors/bing-copilot.mjs +169 -0
- package/extractors/consent.mjs +29 -0
- package/extractors/google-ai.mjs +152 -0
- package/extractors/perplexity.mjs +170 -0
- package/extractors/stackoverflow-ai.mjs +169 -0
- package/index.ts +145 -0
- package/launch.mjs +194 -0
- package/package.json +26 -0
- package/search.mjs +297 -0
- package/setup.mjs +138 -0
- package/skills/greedy-search/SKILL.md +38 -0
package/cdp.mjs
ADDED
|
@@ -0,0 +1,788 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
// cdp - lightweight Chrome DevTools Protocol CLI (no Puppeteer)
|
|
3
|
+
// Forked from https://github.com/pasky/chrome-cdp-skill with Windows fixes:
|
|
4
|
+
// - getWsUrl() uses platform-aware DevToolsActivePort path
|
|
5
|
+
// - SOCK_PREFIX / PAGES_CACHE use os.tmpdir() instead of hardcoded /tmp
|
|
6
|
+
//
|
|
7
|
+
// Per-tab persistent daemon: page commands go through a daemon that holds
|
|
8
|
+
// the CDP session open. Chrome's "Allow debugging" modal fires once per
|
|
9
|
+
// daemon (= once per tab). Daemons auto-exit after 20min idle.
|
|
10
|
+
|
|
11
|
+
import { readFileSync, writeFileSync, unlinkSync, existsSync, readdirSync } from 'fs';
|
|
12
|
+
import { homedir, tmpdir, platform } from 'os';
|
|
13
|
+
import { join } from 'path';
|
|
14
|
+
import { spawn } from 'child_process';
|
|
15
|
+
import net from 'net';
|
|
16
|
+
|
|
17
|
+
const TIMEOUT = 15000;
|
|
18
|
+
const NAVIGATION_TIMEOUT = 30000;
|
|
19
|
+
const IDLE_TIMEOUT = 20 * 60 * 1000;
|
|
20
|
+
const DAEMON_CONNECT_RETRIES = 20;
|
|
21
|
+
const DAEMON_CONNECT_DELAY = 300;
|
|
22
|
+
const MIN_TARGET_PREFIX_LEN = 8;
|
|
23
|
+
|
|
24
|
+
const _tmpdir = tmpdir().replace(/\\/g, '/');
|
|
25
|
+
const PAGES_CACHE = `${_tmpdir}/cdp-pages.json`;
|
|
26
|
+
|
|
27
|
+
function sockPath(targetId) {
|
|
28
|
+
// Windows: use named pipes (reliable cross-platform IPC in Node.js)
|
|
29
|
+
if (platform() === 'win32') return `\\\\.\\pipe\\cdp-${targetId}`;
|
|
30
|
+
return `${_tmpdir}/cdp-${targetId}.sock`;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
function getDevToolsActivePortPath() {
|
|
34
|
+
const os = platform();
|
|
35
|
+
if (os === 'win32') return join(homedir(), 'AppData', 'Local', 'Google', 'Chrome', 'User Data', 'DevToolsActivePort');
|
|
36
|
+
if (os === 'darwin') return join(homedir(), 'Library', 'Application Support', 'Google', 'Chrome', 'DevToolsActivePort');
|
|
37
|
+
return join(homedir(), '.config', 'google-chrome', 'DevToolsActivePort');
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
function getWsUrl() {
|
|
41
|
+
const portFile = getDevToolsActivePortPath();
|
|
42
|
+
const lines = readFileSync(portFile, 'utf8').trim().split('\n');
|
|
43
|
+
return `ws://127.0.0.1:${lines[0]}${lines[1]}`;
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
const sleep = (ms) => new Promise(r => setTimeout(r, ms));
|
|
47
|
+
|
|
48
|
+
function listDaemonSockets() {
|
|
49
|
+
// Named pipes on Windows aren't enumerable as filesystem entries
|
|
50
|
+
if (platform() === 'win32') return [];
|
|
51
|
+
const tmp = tmpdir();
|
|
52
|
+
try {
|
|
53
|
+
return readdirSync(tmp)
|
|
54
|
+
.filter(f => f.startsWith('cdp-') && f.endsWith('.sock'))
|
|
55
|
+
.map(f => ({
|
|
56
|
+
targetId: f.slice(4, -5),
|
|
57
|
+
socketPath: join(tmp, f),
|
|
58
|
+
}));
|
|
59
|
+
} catch {
|
|
60
|
+
return [];
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
function resolvePrefix(prefix, candidates, noun = 'target', missingHint = '') {
|
|
65
|
+
const upper = prefix.toUpperCase();
|
|
66
|
+
const matches = candidates.filter(candidate => candidate.toUpperCase().startsWith(upper));
|
|
67
|
+
if (matches.length === 0) {
|
|
68
|
+
const hint = missingHint ? ` ${missingHint}` : '';
|
|
69
|
+
throw new Error(`No ${noun} matching prefix "${prefix}".${hint}`);
|
|
70
|
+
}
|
|
71
|
+
if (matches.length > 1) {
|
|
72
|
+
throw new Error(`Ambiguous prefix "${prefix}" — matches ${matches.length} ${noun}s. Use more characters.`);
|
|
73
|
+
}
|
|
74
|
+
return matches[0];
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
function getDisplayPrefixLength(targetIds) {
|
|
78
|
+
if (targetIds.length === 0) return MIN_TARGET_PREFIX_LEN;
|
|
79
|
+
const maxLen = Math.max(...targetIds.map(id => id.length));
|
|
80
|
+
for (let len = MIN_TARGET_PREFIX_LEN; len <= maxLen; len++) {
|
|
81
|
+
const prefixes = new Set(targetIds.map(id => id.slice(0, len).toUpperCase()));
|
|
82
|
+
if (prefixes.size === targetIds.length) return len;
|
|
83
|
+
}
|
|
84
|
+
return maxLen;
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
// ---------------------------------------------------------------------------
|
|
88
|
+
// CDP WebSocket client
|
|
89
|
+
// ---------------------------------------------------------------------------
|
|
90
|
+
|
|
91
|
+
class CDP {
|
|
92
|
+
#ws; #id = 0; #pending = new Map(); #eventHandlers = new Map(); #closeHandlers = [];
|
|
93
|
+
|
|
94
|
+
async connect(wsUrl) {
|
|
95
|
+
return new Promise((res, rej) => {
|
|
96
|
+
this.#ws = new WebSocket(wsUrl);
|
|
97
|
+
this.#ws.onopen = () => res();
|
|
98
|
+
this.#ws.onerror = (e) => rej(new Error('WebSocket error: ' + (e.message || e.type)));
|
|
99
|
+
this.#ws.onclose = () => this.#closeHandlers.forEach(h => h());
|
|
100
|
+
this.#ws.onmessage = (ev) => {
|
|
101
|
+
const msg = JSON.parse(ev.data);
|
|
102
|
+
if (msg.id && this.#pending.has(msg.id)) {
|
|
103
|
+
const { resolve, reject } = this.#pending.get(msg.id);
|
|
104
|
+
this.#pending.delete(msg.id);
|
|
105
|
+
if (msg.error) reject(new Error(msg.error.message));
|
|
106
|
+
else resolve(msg.result);
|
|
107
|
+
} else if (msg.method && this.#eventHandlers.has(msg.method)) {
|
|
108
|
+
for (const handler of [...this.#eventHandlers.get(msg.method)]) {
|
|
109
|
+
handler(msg.params || {}, msg);
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
};
|
|
113
|
+
});
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
send(method, params = {}, sessionId) {
|
|
117
|
+
const id = ++this.#id;
|
|
118
|
+
return new Promise((resolve, reject) => {
|
|
119
|
+
this.#pending.set(id, { resolve, reject });
|
|
120
|
+
const msg = { id, method, params };
|
|
121
|
+
if (sessionId) msg.sessionId = sessionId;
|
|
122
|
+
this.#ws.send(JSON.stringify(msg));
|
|
123
|
+
setTimeout(() => {
|
|
124
|
+
if (this.#pending.has(id)) {
|
|
125
|
+
this.#pending.delete(id);
|
|
126
|
+
reject(new Error(`Timeout: ${method}`));
|
|
127
|
+
}
|
|
128
|
+
}, TIMEOUT);
|
|
129
|
+
});
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
onEvent(method, handler) {
|
|
133
|
+
if (!this.#eventHandlers.has(method)) this.#eventHandlers.set(method, new Set());
|
|
134
|
+
const handlers = this.#eventHandlers.get(method);
|
|
135
|
+
handlers.add(handler);
|
|
136
|
+
return () => {
|
|
137
|
+
handlers.delete(handler);
|
|
138
|
+
if (handlers.size === 0) this.#eventHandlers.delete(method);
|
|
139
|
+
};
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
waitForEvent(method, timeout = TIMEOUT) {
|
|
143
|
+
let settled = false;
|
|
144
|
+
let off;
|
|
145
|
+
let timer;
|
|
146
|
+
const promise = new Promise((resolve, reject) => {
|
|
147
|
+
off = this.onEvent(method, (params) => {
|
|
148
|
+
if (settled) return;
|
|
149
|
+
settled = true;
|
|
150
|
+
clearTimeout(timer);
|
|
151
|
+
off();
|
|
152
|
+
resolve(params);
|
|
153
|
+
});
|
|
154
|
+
timer = setTimeout(() => {
|
|
155
|
+
if (settled) return;
|
|
156
|
+
settled = true;
|
|
157
|
+
off();
|
|
158
|
+
reject(new Error(`Timeout waiting for event: ${method}`));
|
|
159
|
+
}, timeout);
|
|
160
|
+
});
|
|
161
|
+
return {
|
|
162
|
+
promise,
|
|
163
|
+
cancel() {
|
|
164
|
+
if (settled) return;
|
|
165
|
+
settled = true;
|
|
166
|
+
clearTimeout(timer);
|
|
167
|
+
off?.();
|
|
168
|
+
},
|
|
169
|
+
};
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
onClose(handler) { this.#closeHandlers.push(handler); }
|
|
173
|
+
close() { this.#ws.close(); }
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
// ---------------------------------------------------------------------------
|
|
177
|
+
// Command implementations — return strings, take (cdp, sessionId)
|
|
178
|
+
// ---------------------------------------------------------------------------
|
|
179
|
+
|
|
180
|
+
async function getPages(cdp) {
|
|
181
|
+
const { targetInfos } = await cdp.send('Target.getTargets');
|
|
182
|
+
return targetInfos.filter(t => t.type === 'page' && !t.url.startsWith('chrome://'));
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
function formatPageList(pages) {
|
|
186
|
+
const prefixLen = getDisplayPrefixLength(pages.map(p => p.targetId));
|
|
187
|
+
return pages.map(p => {
|
|
188
|
+
const id = p.targetId.slice(0, prefixLen).padEnd(prefixLen);
|
|
189
|
+
const title = p.title.substring(0, 54).padEnd(54);
|
|
190
|
+
return `${id} ${title} ${p.url}`;
|
|
191
|
+
}).join('\n');
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
function shouldShowAxNode(node, compact = false) {
|
|
195
|
+
const role = node.role?.value || '';
|
|
196
|
+
const name = node.name?.value ?? '';
|
|
197
|
+
const value = node.value?.value;
|
|
198
|
+
if (compact && role === 'InlineTextBox') return false;
|
|
199
|
+
return role !== 'none' && role !== 'generic' && !(name === '' && (value === '' || value == null));
|
|
200
|
+
}
|
|
201
|
+
|
|
202
|
+
function formatAxNode(node, depth) {
|
|
203
|
+
const role = node.role?.value || '';
|
|
204
|
+
const name = node.name?.value ?? '';
|
|
205
|
+
const value = node.value?.value;
|
|
206
|
+
const indent = ' '.repeat(Math.min(depth, 10));
|
|
207
|
+
let line = `${indent}[${role}]`;
|
|
208
|
+
if (name !== '') line += ` ${name}`;
|
|
209
|
+
if (!(value === '' || value == null)) line += ` = ${JSON.stringify(value)}`;
|
|
210
|
+
return line;
|
|
211
|
+
}
|
|
212
|
+
|
|
213
|
+
function orderedAxChildren(node, nodesById, childrenByParent) {
|
|
214
|
+
const children = [];
|
|
215
|
+
const seen = new Set();
|
|
216
|
+
for (const childId of node.childIds || []) {
|
|
217
|
+
const child = nodesById.get(childId);
|
|
218
|
+
if (child && !seen.has(child.nodeId)) {
|
|
219
|
+
seen.add(child.nodeId);
|
|
220
|
+
children.push(child);
|
|
221
|
+
}
|
|
222
|
+
}
|
|
223
|
+
for (const child of childrenByParent.get(node.nodeId) || []) {
|
|
224
|
+
if (!seen.has(child.nodeId)) {
|
|
225
|
+
seen.add(child.nodeId);
|
|
226
|
+
children.push(child);
|
|
227
|
+
}
|
|
228
|
+
}
|
|
229
|
+
return children;
|
|
230
|
+
}
|
|
231
|
+
|
|
232
|
+
async function snapshotStr(cdp, sid, compact = false) {
|
|
233
|
+
const { nodes } = await cdp.send('Accessibility.getFullAXTree', {}, sid);
|
|
234
|
+
const nodesById = new Map(nodes.map(node => [node.nodeId, node]));
|
|
235
|
+
const childrenByParent = new Map();
|
|
236
|
+
for (const node of nodes) {
|
|
237
|
+
if (!node.parentId) continue;
|
|
238
|
+
if (!childrenByParent.has(node.parentId)) childrenByParent.set(node.parentId, []);
|
|
239
|
+
childrenByParent.get(node.parentId).push(node);
|
|
240
|
+
}
|
|
241
|
+
|
|
242
|
+
const lines = [];
|
|
243
|
+
const visited = new Set();
|
|
244
|
+
function visit(node, depth) {
|
|
245
|
+
if (!node || visited.has(node.nodeId)) return;
|
|
246
|
+
visited.add(node.nodeId);
|
|
247
|
+
if (shouldShowAxNode(node, compact)) lines.push(formatAxNode(node, depth));
|
|
248
|
+
for (const child of orderedAxChildren(node, nodesById, childrenByParent)) {
|
|
249
|
+
visit(child, depth + 1);
|
|
250
|
+
}
|
|
251
|
+
}
|
|
252
|
+
|
|
253
|
+
const roots = nodes.filter(node => !node.parentId || !nodesById.has(node.parentId));
|
|
254
|
+
for (const root of roots) visit(root, 0);
|
|
255
|
+
for (const node of nodes) visit(node, 0);
|
|
256
|
+
|
|
257
|
+
return lines.join('\n');
|
|
258
|
+
}
|
|
259
|
+
|
|
260
|
+
async function evalStr(cdp, sid, expression) {
|
|
261
|
+
await cdp.send('Runtime.enable', {}, sid);
|
|
262
|
+
const result = await cdp.send('Runtime.evaluate', {
|
|
263
|
+
expression, returnByValue: true, awaitPromise: true,
|
|
264
|
+
}, sid);
|
|
265
|
+
if (result.exceptionDetails) {
|
|
266
|
+
throw new Error(result.exceptionDetails.text || result.exceptionDetails.exception?.description);
|
|
267
|
+
}
|
|
268
|
+
const val = result.result.value;
|
|
269
|
+
return typeof val === 'object' ? JSON.stringify(val, null, 2) : String(val ?? '');
|
|
270
|
+
}
|
|
271
|
+
|
|
272
|
+
async function shotStr(cdp, sid, filePath) {
|
|
273
|
+
let dpr = 1;
|
|
274
|
+
try {
|
|
275
|
+
const metrics = await cdp.send('Page.getLayoutMetrics', {}, sid);
|
|
276
|
+
dpr = metrics.visualViewport?.clientWidth
|
|
277
|
+
? metrics.cssVisualViewport?.clientWidth
|
|
278
|
+
? Math.round((metrics.visualViewport.clientWidth / metrics.cssVisualViewport.clientWidth) * 100) / 100
|
|
279
|
+
: 1
|
|
280
|
+
: 1;
|
|
281
|
+
const { deviceScaleFactor } = await cdp.send('Emulation.getDeviceMetricsOverride', {}, sid).catch(() => ({}));
|
|
282
|
+
if (deviceScaleFactor) dpr = deviceScaleFactor;
|
|
283
|
+
} catch {}
|
|
284
|
+
if (dpr === 1) {
|
|
285
|
+
try {
|
|
286
|
+
const raw = await evalStr(cdp, sid, 'window.devicePixelRatio');
|
|
287
|
+
const parsed = parseFloat(raw);
|
|
288
|
+
if (parsed > 0) dpr = parsed;
|
|
289
|
+
} catch {}
|
|
290
|
+
}
|
|
291
|
+
|
|
292
|
+
const { data } = await cdp.send('Page.captureScreenshot', { format: 'png' }, sid);
|
|
293
|
+
const out = filePath || join(tmpdir(), 'screenshot.png');
|
|
294
|
+
writeFileSync(out, Buffer.from(data, 'base64'));
|
|
295
|
+
|
|
296
|
+
const lines = [out];
|
|
297
|
+
lines.push(`Screenshot saved. Device pixel ratio (DPR): ${dpr}`);
|
|
298
|
+
lines.push(`Coordinate mapping:`);
|
|
299
|
+
lines.push(` Screenshot pixels → CSS pixels (for CDP Input events): divide by ${dpr}`);
|
|
300
|
+
lines.push(` e.g. screenshot point (${Math.round(100 * dpr)}, ${Math.round(200 * dpr)}) → CSS (100, 200) → use clickxy <target> 100 200`);
|
|
301
|
+
if (dpr !== 1) {
|
|
302
|
+
lines.push(` On this ${dpr}x display: CSS px = screenshot px / ${dpr} ≈ screenshot px × ${Math.round(100/dpr)/100}`);
|
|
303
|
+
}
|
|
304
|
+
return lines.join('\n');
|
|
305
|
+
}
|
|
306
|
+
|
|
307
|
+
async function htmlStr(cdp, sid, selector) {
|
|
308
|
+
const expr = selector
|
|
309
|
+
? `document.querySelector(${JSON.stringify(selector)})?.outerHTML || 'Element not found'`
|
|
310
|
+
: `document.documentElement.outerHTML`;
|
|
311
|
+
return evalStr(cdp, sid, expr);
|
|
312
|
+
}
|
|
313
|
+
|
|
314
|
+
async function waitForDocumentReady(cdp, sid, timeoutMs = NAVIGATION_TIMEOUT) {
|
|
315
|
+
const deadline = Date.now() + timeoutMs;
|
|
316
|
+
let lastState = '';
|
|
317
|
+
let lastError;
|
|
318
|
+
while (Date.now() < deadline) {
|
|
319
|
+
try {
|
|
320
|
+
const state = await evalStr(cdp, sid, 'document.readyState');
|
|
321
|
+
lastState = state;
|
|
322
|
+
if (state === 'complete') return;
|
|
323
|
+
} catch (e) {
|
|
324
|
+
lastError = e;
|
|
325
|
+
}
|
|
326
|
+
await sleep(200);
|
|
327
|
+
}
|
|
328
|
+
|
|
329
|
+
if (lastState) {
|
|
330
|
+
throw new Error(`Timed out waiting for navigation to finish (last readyState: ${lastState})`);
|
|
331
|
+
}
|
|
332
|
+
if (lastError) {
|
|
333
|
+
throw new Error(`Timed out waiting for navigation to finish (${lastError.message})`);
|
|
334
|
+
}
|
|
335
|
+
throw new Error('Timed out waiting for navigation to finish');
|
|
336
|
+
}
|
|
337
|
+
|
|
338
|
+
async function navStr(cdp, sid, url) {
|
|
339
|
+
await cdp.send('Page.enable', {}, sid);
|
|
340
|
+
const loadEvent = cdp.waitForEvent('Page.loadEventFired', NAVIGATION_TIMEOUT);
|
|
341
|
+
const result = await cdp.send('Page.navigate', { url }, sid);
|
|
342
|
+
if (result.errorText) {
|
|
343
|
+
loadEvent.cancel();
|
|
344
|
+
throw new Error(result.errorText);
|
|
345
|
+
}
|
|
346
|
+
if (result.loaderId) {
|
|
347
|
+
await loadEvent.promise;
|
|
348
|
+
} else {
|
|
349
|
+
loadEvent.cancel();
|
|
350
|
+
}
|
|
351
|
+
await waitForDocumentReady(cdp, sid, 5000);
|
|
352
|
+
return `Navigated to ${url}`;
|
|
353
|
+
}
|
|
354
|
+
|
|
355
|
+
async function netStr(cdp, sid) {
|
|
356
|
+
const raw = await evalStr(cdp, sid, `JSON.stringify(performance.getEntriesByType('resource').map(e => ({
|
|
357
|
+
name: e.name.substring(0, 120), type: e.initiatorType,
|
|
358
|
+
duration: Math.round(e.duration), size: e.transferSize
|
|
359
|
+
})))`);
|
|
360
|
+
return JSON.parse(raw).map(e =>
|
|
361
|
+
`${String(e.duration).padStart(5)}ms ${String(e.size || '?').padStart(8)}B ${e.type.padEnd(8)} ${e.name}`
|
|
362
|
+
).join('\n');
|
|
363
|
+
}
|
|
364
|
+
|
|
365
|
+
async function clickStr(cdp, sid, selector) {
|
|
366
|
+
if (!selector) throw new Error('CSS selector required');
|
|
367
|
+
const expr = `
|
|
368
|
+
(function() {
|
|
369
|
+
const el = document.querySelector(${JSON.stringify(selector)});
|
|
370
|
+
if (!el) return { ok: false, error: 'Element not found: ' + ${JSON.stringify(selector)} };
|
|
371
|
+
el.scrollIntoView({ block: 'center' });
|
|
372
|
+
el.click();
|
|
373
|
+
return { ok: true, tag: el.tagName, text: el.textContent.trim().substring(0, 80) };
|
|
374
|
+
})()
|
|
375
|
+
`;
|
|
376
|
+
const result = await evalStr(cdp, sid, expr);
|
|
377
|
+
const r = JSON.parse(result);
|
|
378
|
+
if (!r.ok) throw new Error(r.error);
|
|
379
|
+
return `Clicked <${r.tag}> "${r.text}"`;
|
|
380
|
+
}
|
|
381
|
+
|
|
382
|
+
async function clickXyStr(cdp, sid, x, y) {
|
|
383
|
+
const cx = parseFloat(x);
|
|
384
|
+
const cy = parseFloat(y);
|
|
385
|
+
if (isNaN(cx) || isNaN(cy)) throw new Error('x and y must be numbers (CSS pixels)');
|
|
386
|
+
const base = { x: cx, y: cy, button: 'left', clickCount: 1, modifiers: 0 };
|
|
387
|
+
await cdp.send('Input.dispatchMouseEvent', { ...base, type: 'mouseMoved' }, sid);
|
|
388
|
+
await cdp.send('Input.dispatchMouseEvent', { ...base, type: 'mousePressed' }, sid);
|
|
389
|
+
await sleep(50);
|
|
390
|
+
await cdp.send('Input.dispatchMouseEvent', { ...base, type: 'mouseReleased' }, sid);
|
|
391
|
+
return `Clicked at CSS (${cx}, ${cy})`;
|
|
392
|
+
}
|
|
393
|
+
|
|
394
|
+
async function typeStr(cdp, sid, text) {
|
|
395
|
+
if (text == null || text === '') throw new Error('text required');
|
|
396
|
+
await cdp.send('Input.insertText', { text }, sid);
|
|
397
|
+
return `Typed ${text.length} characters`;
|
|
398
|
+
}
|
|
399
|
+
|
|
400
|
+
async function loadAllStr(cdp, sid, selector, intervalMs = 1500) {
|
|
401
|
+
if (!selector) throw new Error('CSS selector required');
|
|
402
|
+
let clicks = 0;
|
|
403
|
+
const deadline = Date.now() + 5 * 60 * 1000;
|
|
404
|
+
while (Date.now() < deadline) {
|
|
405
|
+
const exists = await evalStr(cdp, sid,
|
|
406
|
+
`!!document.querySelector(${JSON.stringify(selector)})`
|
|
407
|
+
);
|
|
408
|
+
if (exists !== 'true') break;
|
|
409
|
+
const clickExpr = `
|
|
410
|
+
(function() {
|
|
411
|
+
const el = document.querySelector(${JSON.stringify(selector)});
|
|
412
|
+
if (!el) return false;
|
|
413
|
+
el.scrollIntoView({ block: 'center' });
|
|
414
|
+
el.click();
|
|
415
|
+
return true;
|
|
416
|
+
})()
|
|
417
|
+
`;
|
|
418
|
+
const clicked = await evalStr(cdp, sid, clickExpr);
|
|
419
|
+
if (clicked !== 'true') break;
|
|
420
|
+
clicks++;
|
|
421
|
+
await sleep(intervalMs);
|
|
422
|
+
}
|
|
423
|
+
return `Clicked "${selector}" ${clicks} time(s) until it disappeared`;
|
|
424
|
+
}
|
|
425
|
+
|
|
426
|
+
async function evalRawStr(cdp, sid, method, paramsJson) {
|
|
427
|
+
if (!method) throw new Error('CDP method required (e.g. "DOM.getDocument")');
|
|
428
|
+
let params = {};
|
|
429
|
+
if (paramsJson) {
|
|
430
|
+
try { params = JSON.parse(paramsJson); }
|
|
431
|
+
catch { throw new Error(`Invalid JSON params: ${paramsJson}`); }
|
|
432
|
+
}
|
|
433
|
+
const result = await cdp.send(method, params, sid);
|
|
434
|
+
return JSON.stringify(result, null, 2);
|
|
435
|
+
}
|
|
436
|
+
|
|
437
|
+
// ---------------------------------------------------------------------------
|
|
438
|
+
// Per-tab daemon
|
|
439
|
+
// ---------------------------------------------------------------------------
|
|
440
|
+
|
|
441
|
+
async function runDaemon(targetId) {
|
|
442
|
+
const sp = sockPath(targetId);
|
|
443
|
+
|
|
444
|
+
const cdp = new CDP();
|
|
445
|
+
try {
|
|
446
|
+
await cdp.connect(getWsUrl());
|
|
447
|
+
} catch (e) {
|
|
448
|
+
process.stderr.write(`Daemon: cannot connect to Chrome: ${e.message}\n`);
|
|
449
|
+
process.exit(1);
|
|
450
|
+
}
|
|
451
|
+
|
|
452
|
+
let sessionId;
|
|
453
|
+
try {
|
|
454
|
+
const res = await cdp.send('Target.attachToTarget', { targetId, flatten: true });
|
|
455
|
+
sessionId = res.sessionId;
|
|
456
|
+
} catch (e) {
|
|
457
|
+
process.stderr.write(`Daemon: attach failed: ${e.message}\n`);
|
|
458
|
+
cdp.close();
|
|
459
|
+
process.exit(1);
|
|
460
|
+
}
|
|
461
|
+
|
|
462
|
+
let alive = true;
|
|
463
|
+
function shutdown() {
|
|
464
|
+
if (!alive) return;
|
|
465
|
+
alive = false;
|
|
466
|
+
server.close();
|
|
467
|
+
try { unlinkSync(sp); } catch {}
|
|
468
|
+
cdp.close();
|
|
469
|
+
process.exit(0);
|
|
470
|
+
}
|
|
471
|
+
|
|
472
|
+
cdp.onEvent('Target.targetDestroyed', (params) => {
|
|
473
|
+
if (params.targetId === targetId) shutdown();
|
|
474
|
+
});
|
|
475
|
+
cdp.onEvent('Target.detachedFromTarget', (params) => {
|
|
476
|
+
if (params.sessionId === sessionId) shutdown();
|
|
477
|
+
});
|
|
478
|
+
cdp.onClose(() => shutdown());
|
|
479
|
+
process.on('SIGTERM', shutdown);
|
|
480
|
+
process.on('SIGINT', shutdown);
|
|
481
|
+
|
|
482
|
+
let idleTimer = setTimeout(shutdown, IDLE_TIMEOUT);
|
|
483
|
+
function resetIdle() {
|
|
484
|
+
clearTimeout(idleTimer);
|
|
485
|
+
idleTimer = setTimeout(shutdown, IDLE_TIMEOUT);
|
|
486
|
+
}
|
|
487
|
+
|
|
488
|
+
async function handleCommand({ cmd, args }) {
|
|
489
|
+
resetIdle();
|
|
490
|
+
try {
|
|
491
|
+
let result;
|
|
492
|
+
switch (cmd) {
|
|
493
|
+
case 'list': {
|
|
494
|
+
const pages = await getPages(cdp);
|
|
495
|
+
result = formatPageList(pages);
|
|
496
|
+
break;
|
|
497
|
+
}
|
|
498
|
+
case 'list_raw': {
|
|
499
|
+
const pages = await getPages(cdp);
|
|
500
|
+
result = JSON.stringify(pages);
|
|
501
|
+
break;
|
|
502
|
+
}
|
|
503
|
+
case 'snap': case 'snapshot': result = await snapshotStr(cdp, sessionId, true); break;
|
|
504
|
+
case 'eval': result = await evalStr(cdp, sessionId, args[0]); break;
|
|
505
|
+
case 'shot': case 'screenshot': result = await shotStr(cdp, sessionId, args[0]); break;
|
|
506
|
+
case 'html': result = await htmlStr(cdp, sessionId, args[0]); break;
|
|
507
|
+
case 'nav': case 'navigate': result = await navStr(cdp, sessionId, args[0]); break;
|
|
508
|
+
case 'net': case 'network': result = await netStr(cdp, sessionId); break;
|
|
509
|
+
case 'click': result = await clickStr(cdp, sessionId, args[0]); break;
|
|
510
|
+
case 'clickxy': result = await clickXyStr(cdp, sessionId, args[0], args[1]); break;
|
|
511
|
+
case 'type': result = await typeStr(cdp, sessionId, args[0]); break;
|
|
512
|
+
case 'loadall': result = await loadAllStr(cdp, sessionId, args[0], args[1] ? parseInt(args[1]) : 1500); break;
|
|
513
|
+
case 'evalraw': result = await evalRawStr(cdp, sessionId, args[0], args[1]); break;
|
|
514
|
+
case 'stop': return { ok: true, result: '', stopAfter: true };
|
|
515
|
+
default: return { ok: false, error: `Unknown command: ${cmd}` };
|
|
516
|
+
}
|
|
517
|
+
return { ok: true, result: result ?? '' };
|
|
518
|
+
} catch (e) {
|
|
519
|
+
return { ok: false, error: e.message };
|
|
520
|
+
}
|
|
521
|
+
}
|
|
522
|
+
|
|
523
|
+
const server = net.createServer((conn) => {
|
|
524
|
+
let buf = '';
|
|
525
|
+
conn.on('data', (chunk) => {
|
|
526
|
+
buf += chunk.toString();
|
|
527
|
+
const lines = buf.split('\n');
|
|
528
|
+
buf = lines.pop();
|
|
529
|
+
for (const line of lines) {
|
|
530
|
+
if (!line.trim()) continue;
|
|
531
|
+
let req;
|
|
532
|
+
try {
|
|
533
|
+
req = JSON.parse(line);
|
|
534
|
+
} catch {
|
|
535
|
+
conn.write(JSON.stringify({ ok: false, error: 'Invalid JSON request', id: null }) + '\n');
|
|
536
|
+
continue;
|
|
537
|
+
}
|
|
538
|
+
handleCommand(req).then((res) => {
|
|
539
|
+
const payload = JSON.stringify({ ...res, id: req.id }) + '\n';
|
|
540
|
+
if (res.stopAfter) conn.end(payload, shutdown);
|
|
541
|
+
else conn.write(payload);
|
|
542
|
+
});
|
|
543
|
+
}
|
|
544
|
+
});
|
|
545
|
+
});
|
|
546
|
+
|
|
547
|
+
try { unlinkSync(sp); } catch {}
|
|
548
|
+
server.listen(sp);
|
|
549
|
+
}
|
|
550
|
+
|
|
551
|
+
// ---------------------------------------------------------------------------
|
|
552
|
+
// CLI ↔ daemon communication
|
|
553
|
+
// ---------------------------------------------------------------------------
|
|
554
|
+
|
|
555
|
+
function connectToSocket(sp) {
|
|
556
|
+
return new Promise((resolve, reject) => {
|
|
557
|
+
const conn = net.connect(sp);
|
|
558
|
+
conn.on('connect', () => resolve(conn));
|
|
559
|
+
conn.on('error', reject);
|
|
560
|
+
});
|
|
561
|
+
}
|
|
562
|
+
|
|
563
|
+
async function getOrStartTabDaemon(targetId) {
|
|
564
|
+
const sp = sockPath(targetId);
|
|
565
|
+
try { return await connectToSocket(sp); } catch {}
|
|
566
|
+
try { unlinkSync(sp); } catch {}
|
|
567
|
+
|
|
568
|
+
const child = spawn(process.execPath, [process.argv[1], '_daemon', targetId], {
|
|
569
|
+
detached: true,
|
|
570
|
+
stdio: 'ignore',
|
|
571
|
+
});
|
|
572
|
+
child.unref();
|
|
573
|
+
|
|
574
|
+
for (let i = 0; i < DAEMON_CONNECT_RETRIES; i++) {
|
|
575
|
+
await sleep(DAEMON_CONNECT_DELAY);
|
|
576
|
+
try { return await connectToSocket(sp); } catch {}
|
|
577
|
+
}
|
|
578
|
+
throw new Error('Daemon failed to start — did you click Allow in Chrome?');
|
|
579
|
+
}
|
|
580
|
+
|
|
581
|
+
function sendCommand(conn, req) {
|
|
582
|
+
return new Promise((resolve, reject) => {
|
|
583
|
+
let buf = '';
|
|
584
|
+
let settled = false;
|
|
585
|
+
|
|
586
|
+
const cleanup = () => {
|
|
587
|
+
conn.off('data', onData);
|
|
588
|
+
conn.off('error', onError);
|
|
589
|
+
conn.off('end', onEnd);
|
|
590
|
+
conn.off('close', onClose);
|
|
591
|
+
};
|
|
592
|
+
|
|
593
|
+
const onData = (chunk) => {
|
|
594
|
+
buf += chunk.toString();
|
|
595
|
+
const idx = buf.indexOf('\n');
|
|
596
|
+
if (idx === -1) return;
|
|
597
|
+
settled = true;
|
|
598
|
+
cleanup();
|
|
599
|
+
resolve(JSON.parse(buf.slice(0, idx)));
|
|
600
|
+
conn.end();
|
|
601
|
+
};
|
|
602
|
+
|
|
603
|
+
const onError = (error) => {
|
|
604
|
+
if (settled) return;
|
|
605
|
+
settled = true;
|
|
606
|
+
cleanup();
|
|
607
|
+
reject(error);
|
|
608
|
+
};
|
|
609
|
+
|
|
610
|
+
const onEnd = () => {
|
|
611
|
+
if (settled) return;
|
|
612
|
+
settled = true;
|
|
613
|
+
cleanup();
|
|
614
|
+
reject(new Error('Connection closed before response'));
|
|
615
|
+
};
|
|
616
|
+
|
|
617
|
+
const onClose = () => {
|
|
618
|
+
if (settled) return;
|
|
619
|
+
settled = true;
|
|
620
|
+
cleanup();
|
|
621
|
+
reject(new Error('Connection closed before response'));
|
|
622
|
+
};
|
|
623
|
+
|
|
624
|
+
conn.on('data', onData);
|
|
625
|
+
conn.on('error', onError);
|
|
626
|
+
conn.on('end', onEnd);
|
|
627
|
+
conn.on('close', onClose);
|
|
628
|
+
req.id = 1;
|
|
629
|
+
conn.write(JSON.stringify(req) + '\n');
|
|
630
|
+
});
|
|
631
|
+
}
|
|
632
|
+
|
|
633
|
+
function findAnyDaemonSocket() {
|
|
634
|
+
return listDaemonSockets()[0]?.socketPath || null;
|
|
635
|
+
}
|
|
636
|
+
|
|
637
|
+
// ---------------------------------------------------------------------------
|
|
638
|
+
// Stop daemons
|
|
639
|
+
// ---------------------------------------------------------------------------
|
|
640
|
+
|
|
641
|
+
async function stopDaemons(targetPrefix) {
|
|
642
|
+
const daemons = listDaemonSockets();
|
|
643
|
+
|
|
644
|
+
if (targetPrefix) {
|
|
645
|
+
const targetId = resolvePrefix(targetPrefix, daemons.map(d => d.targetId), 'daemon');
|
|
646
|
+
const daemon = daemons.find(d => d.targetId === targetId);
|
|
647
|
+
try {
|
|
648
|
+
const conn = await connectToSocket(daemon.socketPath);
|
|
649
|
+
await sendCommand(conn, { cmd: 'stop' });
|
|
650
|
+
} catch {
|
|
651
|
+
try { unlinkSync(daemon.socketPath); } catch {}
|
|
652
|
+
}
|
|
653
|
+
return;
|
|
654
|
+
}
|
|
655
|
+
|
|
656
|
+
for (const daemon of daemons) {
|
|
657
|
+
try {
|
|
658
|
+
const conn = await connectToSocket(daemon.socketPath);
|
|
659
|
+
await sendCommand(conn, { cmd: 'stop' });
|
|
660
|
+
} catch {
|
|
661
|
+
try { unlinkSync(daemon.socketPath); } catch {}
|
|
662
|
+
}
|
|
663
|
+
}
|
|
664
|
+
}
|
|
665
|
+
|
|
666
|
+
// ---------------------------------------------------------------------------
|
|
667
|
+
// Main
|
|
668
|
+
// ---------------------------------------------------------------------------
|
|
669
|
+
|
|
670
|
+
const USAGE = `cdp - lightweight Chrome DevTools Protocol CLI (no Puppeteer)
|
|
671
|
+
|
|
672
|
+
Usage: cdp <command> [args]
|
|
673
|
+
|
|
674
|
+
list List open pages (shows unique target prefixes)
|
|
675
|
+
snap <target> Accessibility tree snapshot
|
|
676
|
+
eval <target> <expr> Evaluate JS expression
|
|
677
|
+
shot <target> [file] Screenshot; prints coordinate mapping
|
|
678
|
+
html <target> [selector] Get HTML (full page or CSS selector)
|
|
679
|
+
nav <target> <url> Navigate to URL and wait for load completion
|
|
680
|
+
net <target> Network performance entries
|
|
681
|
+
click <target> <selector> Click an element by CSS selector
|
|
682
|
+
clickxy <target> <x> <y> Click at CSS pixel coordinates
|
|
683
|
+
type <target> <text> Type text at current focus
|
|
684
|
+
loadall <target> <selector> [ms] Repeatedly click a "load more" button
|
|
685
|
+
evalraw <target> <method> [json] Send a raw CDP command; returns JSON result
|
|
686
|
+
stop [target] Stop daemon(s)
|
|
687
|
+
`;
|
|
688
|
+
|
|
689
|
+
const NEEDS_TARGET = new Set([
|
|
690
|
+
'snap','snapshot','eval','shot','screenshot','html','nav','navigate',
|
|
691
|
+
'net','network','click','clickxy','type','loadall','evalraw',
|
|
692
|
+
]);
|
|
693
|
+
|
|
694
|
+
async function main() {
|
|
695
|
+
const [cmd, ...args] = process.argv.slice(2);
|
|
696
|
+
|
|
697
|
+
if (cmd === '_daemon') { await runDaemon(args[0]); return; }
|
|
698
|
+
|
|
699
|
+
if (!cmd || cmd === 'help' || cmd === '--help' || cmd === '-h') {
|
|
700
|
+
console.log(USAGE); process.exit(0);
|
|
701
|
+
}
|
|
702
|
+
|
|
703
|
+
if (cmd === 'list' || cmd === 'ls') {
|
|
704
|
+
let pages;
|
|
705
|
+
const existingSock = findAnyDaemonSocket();
|
|
706
|
+
if (existingSock) {
|
|
707
|
+
try {
|
|
708
|
+
const conn = await connectToSocket(existingSock);
|
|
709
|
+
const resp = await sendCommand(conn, { cmd: 'list_raw' });
|
|
710
|
+
if (resp.ok) pages = JSON.parse(resp.result);
|
|
711
|
+
} catch {}
|
|
712
|
+
}
|
|
713
|
+
if (!pages) {
|
|
714
|
+
const cdp = new CDP();
|
|
715
|
+
await cdp.connect(getWsUrl());
|
|
716
|
+
pages = await getPages(cdp);
|
|
717
|
+
cdp.close();
|
|
718
|
+
}
|
|
719
|
+
writeFileSync(PAGES_CACHE, JSON.stringify(pages));
|
|
720
|
+
console.log(formatPageList(pages));
|
|
721
|
+
setTimeout(() => process.exit(0), 100);
|
|
722
|
+
return;
|
|
723
|
+
}
|
|
724
|
+
|
|
725
|
+
if (cmd === 'stop') {
|
|
726
|
+
await stopDaemons(args[0]);
|
|
727
|
+
return;
|
|
728
|
+
}
|
|
729
|
+
|
|
730
|
+
if (!NEEDS_TARGET.has(cmd)) {
|
|
731
|
+
console.error(`Unknown command: ${cmd}\n`);
|
|
732
|
+
console.log(USAGE);
|
|
733
|
+
process.exit(1);
|
|
734
|
+
}
|
|
735
|
+
|
|
736
|
+
const targetPrefix = args[0];
|
|
737
|
+
if (!targetPrefix) {
|
|
738
|
+
console.error('Error: target ID required. Run "cdp list" first.');
|
|
739
|
+
process.exit(1);
|
|
740
|
+
}
|
|
741
|
+
|
|
742
|
+
let targetId;
|
|
743
|
+
const daemonTargetIds = listDaemonSockets().map(d => d.targetId);
|
|
744
|
+
const daemonMatches = daemonTargetIds.filter(id => id.toUpperCase().startsWith(targetPrefix.toUpperCase()));
|
|
745
|
+
|
|
746
|
+
if (daemonMatches.length > 0) {
|
|
747
|
+
targetId = resolvePrefix(targetPrefix, daemonTargetIds, 'daemon');
|
|
748
|
+
} else {
|
|
749
|
+
if (!existsSync(PAGES_CACHE)) {
|
|
750
|
+
console.error('No page list cached. Run "cdp list" first.');
|
|
751
|
+
process.exit(1);
|
|
752
|
+
}
|
|
753
|
+
const pages = JSON.parse(readFileSync(PAGES_CACHE, 'utf8'));
|
|
754
|
+
targetId = resolvePrefix(targetPrefix, pages.map(p => p.targetId), 'target', 'Run "cdp list".');
|
|
755
|
+
}
|
|
756
|
+
|
|
757
|
+
const conn = await getOrStartTabDaemon(targetId);
|
|
758
|
+
const cmdArgs = args.slice(1);
|
|
759
|
+
|
|
760
|
+
if (cmd === 'eval') {
|
|
761
|
+
const expr = cmdArgs.join(' ');
|
|
762
|
+
if (!expr) { console.error('Error: expression required'); process.exit(1); }
|
|
763
|
+
cmdArgs[0] = expr;
|
|
764
|
+
} else if (cmd === 'type') {
|
|
765
|
+
const text = cmdArgs.join(' ');
|
|
766
|
+
if (!text) { console.error('Error: text required'); process.exit(1); }
|
|
767
|
+
cmdArgs[0] = text;
|
|
768
|
+
} else if (cmd === 'evalraw') {
|
|
769
|
+
if (!cmdArgs[0]) { console.error('Error: CDP method required'); process.exit(1); }
|
|
770
|
+
if (cmdArgs.length > 2) cmdArgs[1] = cmdArgs.slice(1).join(' ');
|
|
771
|
+
}
|
|
772
|
+
|
|
773
|
+
if ((cmd === 'nav' || cmd === 'navigate') && !cmdArgs[0]) {
|
|
774
|
+
console.error('Error: URL required');
|
|
775
|
+
process.exit(1);
|
|
776
|
+
}
|
|
777
|
+
|
|
778
|
+
const response = await sendCommand(conn, { cmd, args: cmdArgs });
|
|
779
|
+
|
|
780
|
+
if (response.ok) {
|
|
781
|
+
if (response.result) console.log(response.result);
|
|
782
|
+
} else {
|
|
783
|
+
console.error('Error:', response.error);
|
|
784
|
+
process.exitCode = 1;
|
|
785
|
+
}
|
|
786
|
+
}
|
|
787
|
+
|
|
788
|
+
main().catch(e => { console.error(e.message); process.exit(1); });
|