-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcdp-shell.js
More file actions
104 lines (90 loc) · 3.03 KB
/
Copy pathcdp-shell.js
File metadata and controls
104 lines (90 loc) · 3.03 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
#!/usr/bin/env node
'use strict';
const readline = require('readline');
const BASE = 'http://127.0.0.1:1232';
let currentTabId = null;
const tabs = new Map();
async function get(path) {
const r = await fetch(`${BASE}${path}`);
return r.json();
}
async function post(path, body) {
const r = await fetch(`${BASE}${path}`, {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify(body),
});
return r.json();
}
async function queryTabs() {
const r = await get('/tabs');
if (r.error) { console.error('Error:', r.error); return; }
tabs.clear();
r.result.forEach(t => tabs.set(t.tabId, t));
console.log('\nOpen Tabs:');
r.result.forEach(t => console.log(` ${t.tabId}: ${(t.title || t.url || '').slice(0, 60)}`));
console.log('');
}
async function evaluate(code) {
if (!currentTabId) return console.log('No tab selected. Use "use <id>" first.');
const r = await post('/command', {
tabId: currentTabId,
method: 'Runtime.evaluate',
params: { expression: code, returnByValue: true },
});
if (r.error) { console.error('Error:', r.error); return; }
console.log(r.result?.result?.value ?? JSON.stringify(r.result));
}
async function navigate(url) {
if (!currentTabId) return console.log('No tab selected. Use "use <id>" first.');
const r = await post('/command', {
tabId: currentTabId,
method: 'Page.navigate',
params: { url },
});
if (r.error) { console.error('Error:', r.error); return; }
console.log('Navigated.');
}
function help() {
console.log('Commands:');
console.log(' tabs List open tabs');
console.log(' use <id> Select active tab');
console.log(' eval <js> Run JavaScript in selected tab');
console.log(' navigate <url> Navigate selected tab to URL');
console.log(' help Show this help');
}
async function main() {
try {
await get('/health');
} catch {
console.error(`Cannot reach CDP Bridge at ${BASE}`);
console.error('Start the host with: node host/host.js');
process.exit(1);
}
console.log(`Connected to CDP Bridge (${BASE})`);
console.log('Type "help" for commands, "tabs" to list open tabs\n');
await queryTabs();
const rl = readline.createInterface({ input: process.stdin, output: process.stdout, prompt: '> ' });
rl.prompt();
rl.on('line', async (line) => {
const [cmd, ...args] = line.trim().split(/\s+/);
if (!cmd || cmd === 'help') {
help();
} else if (cmd === 'tabs') {
await queryTabs();
} else if (cmd === 'use') {
const id = parseInt(args[0]);
if (tabs.has(id)) { currentTabId = id; console.log(`Using tab ${id}`); }
else console.log('Tab not found. Run "tabs" first.');
} else if (cmd === 'eval') {
await evaluate(args.join(' '));
} else if (cmd === 'navigate') {
await navigate(args.join(' '));
} else {
console.log('Unknown command. Type "help".');
}
rl.prompt();
});
rl.on('close', () => process.exit(0));
}
main().catch(err => { console.error(err.message); process.exit(1); });