@rezkam/browser-tools 0.2.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/README.md +65 -0
- package/bin/browser-tools.mjs +71 -0
- package/package.json +41 -0
- package/scripts/browser-control.mjs +1450 -0
- package/scripts/browser-query-cache.mjs +154 -0
- package/scripts/config.mjs +142 -0
- package/scripts/eval.mjs +83 -0
- package/scripts/extract-article.mjs +69 -0
- package/scripts/nav.mjs +38 -0
- package/scripts/pick.mjs +213 -0
- package/scripts/resource-helper.mjs +143 -0
- package/scripts/scrape-page.mjs +127 -0
- package/scripts/screenshot.mjs +25 -0
- package/scripts/start.mjs +49 -0
- package/scripts/status.mjs +51 -0
- package/scripts/stop.mjs +71 -0
|
@@ -0,0 +1,154 @@
|
|
|
1
|
+
import { createHash } from 'node:crypto';
|
|
2
|
+
import { existsSync, mkdirSync, readFileSync, writeFileSync } from 'node:fs';
|
|
3
|
+
import path from 'node:path';
|
|
4
|
+
|
|
5
|
+
function stable(value) {
|
|
6
|
+
if (value === null || typeof value !== 'object') return JSON.stringify(value);
|
|
7
|
+
if (Array.isArray(value)) return `[${value.map(stable).join(',')}]`;
|
|
8
|
+
const keys = Object.keys(value).sort();
|
|
9
|
+
return `{${keys.map(k => `${JSON.stringify(k)}:${stable(value[k])}`).join(',')}}`;
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
function sha256(text) {
|
|
13
|
+
return createHash('sha256').update(text).digest('hex');
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
function ensureDir(dir) {
|
|
17
|
+
mkdirSync(dir, { recursive: true });
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
function nowStamp() {
|
|
21
|
+
return new Date().toISOString().replace(/[:.]/g, '-');
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
function isFreshEnough(createdAt, ttlSeconds) {
|
|
25
|
+
if (ttlSeconds === null || ttlSeconds === undefined) return true;
|
|
26
|
+
if (!Number.isFinite(ttlSeconds) || ttlSeconds < 0) return false;
|
|
27
|
+
const ts = Date.parse(createdAt || '');
|
|
28
|
+
if (!Number.isFinite(ts)) return false;
|
|
29
|
+
return Date.now() - ts <= ttlSeconds * 1000;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
function parseTtlSeconds(value = process.env.BROWSER_QUERY_TTL_SECONDS) {
|
|
33
|
+
if (value === undefined || value === null) return null;
|
|
34
|
+
const raw = String(value).trim();
|
|
35
|
+
if (!raw) return null;
|
|
36
|
+
const ttlSeconds = Number(raw);
|
|
37
|
+
if (!Number.isFinite(ttlSeconds) || ttlSeconds < 0) {
|
|
38
|
+
throw new Error(`Invalid BROWSER_QUERY_TTL_SECONDS: expected a non-negative number of seconds, got "${value}"`);
|
|
39
|
+
}
|
|
40
|
+
return ttlSeconds;
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
export function getCacheConfig() {
|
|
44
|
+
const cacheDir = process.env.BROWSER_QUERY_CACHE_DIR;
|
|
45
|
+
if (!cacheDir) return null;
|
|
46
|
+
return {
|
|
47
|
+
cacheDir,
|
|
48
|
+
entriesDir: path.join(cacheDir, 'entries'),
|
|
49
|
+
responsesDir: path.join(cacheDir, 'responses'),
|
|
50
|
+
rawDir: path.join(cacheDir, 'raw'),
|
|
51
|
+
invocationsDir: process.env.BROWSER_QUERY_RUN_DIR && process.env.BROWSER_QUERY_STEP_ID
|
|
52
|
+
? path.join(process.env.BROWSER_QUERY_RUN_DIR, 'browser-tool-calls', process.env.BROWSER_QUERY_STEP_ID)
|
|
53
|
+
: null,
|
|
54
|
+
stepId: process.env.BROWSER_QUERY_STEP_ID || null,
|
|
55
|
+
stepLabel: process.env.BROWSER_QUERY_STEP_LABEL || null,
|
|
56
|
+
runDir: process.env.BROWSER_QUERY_RUN_DIR || null,
|
|
57
|
+
ttlSeconds: parseTtlSeconds(),
|
|
58
|
+
};
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
export function buildCacheKey(tool, input) {
|
|
62
|
+
return sha256(stable({ tool, input }));
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
export function readCachedResponse(tool, input) {
|
|
66
|
+
const cfg = getCacheConfig();
|
|
67
|
+
if (!cfg) return null;
|
|
68
|
+
const key = buildCacheKey(tool, input);
|
|
69
|
+
const entryPath = path.join(cfg.entriesDir, `${key}.json`);
|
|
70
|
+
if (!existsSync(entryPath)) return null;
|
|
71
|
+
const entry = JSON.parse(readFileSync(entryPath, 'utf-8'));
|
|
72
|
+
if (!isFreshEnough(entry.created_at, cfg.ttlSeconds)) {
|
|
73
|
+
recordInvocation(tool, key, {
|
|
74
|
+
cache_hit: false,
|
|
75
|
+
cache_stale: true,
|
|
76
|
+
input,
|
|
77
|
+
response_path: entry.response_path || null,
|
|
78
|
+
raw_path: entry.raw_path || null,
|
|
79
|
+
page_url: entry.page_url || null,
|
|
80
|
+
metadata: entry.metadata || null,
|
|
81
|
+
cached_at: entry.created_at || null,
|
|
82
|
+
ttl_seconds: cfg.ttlSeconds,
|
|
83
|
+
});
|
|
84
|
+
return null;
|
|
85
|
+
}
|
|
86
|
+
const responsePath = entry.response_path;
|
|
87
|
+
if (!responsePath || !existsSync(responsePath)) return null;
|
|
88
|
+
const output = readFileSync(responsePath, 'utf-8');
|
|
89
|
+
const rawText = entry.raw_path && existsSync(entry.raw_path) ? readFileSync(entry.raw_path, 'utf-8') : output;
|
|
90
|
+
recordInvocation(tool, key, {
|
|
91
|
+
cache_hit: true,
|
|
92
|
+
input,
|
|
93
|
+
response_path: responsePath,
|
|
94
|
+
raw_path: entry.raw_path || null,
|
|
95
|
+
page_url: entry.page_url || null,
|
|
96
|
+
metadata: entry.metadata || null,
|
|
97
|
+
cached_at: entry.created_at || null,
|
|
98
|
+
ttl_seconds: cfg.ttlSeconds,
|
|
99
|
+
});
|
|
100
|
+
return { key, entryPath, entry, output, rawText };
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
export function writeCachedResponse(tool, input, payload) {
|
|
104
|
+
const cfg = getCacheConfig();
|
|
105
|
+
if (!cfg) return null;
|
|
106
|
+
const key = buildCacheKey(tool, input);
|
|
107
|
+
ensureDir(cfg.entriesDir);
|
|
108
|
+
ensureDir(path.join(cfg.responsesDir, tool));
|
|
109
|
+
ensureDir(path.join(cfg.rawDir, tool));
|
|
110
|
+
const extension = payload.extension || 'txt';
|
|
111
|
+
const responsePath = path.join(cfg.responsesDir, tool, `${key}.${extension}`);
|
|
112
|
+
const rawPath = payload.rawText ? path.join(cfg.rawDir, tool, `${key}.txt`) : null;
|
|
113
|
+
writeFileSync(responsePath, payload.output, 'utf-8');
|
|
114
|
+
if (rawPath) writeFileSync(rawPath, payload.rawText, 'utf-8');
|
|
115
|
+
const entry = {
|
|
116
|
+
key,
|
|
117
|
+
tool,
|
|
118
|
+
created_at: new Date().toISOString(),
|
|
119
|
+
input,
|
|
120
|
+
input_hash: key,
|
|
121
|
+
page_url: payload.pageUrl || null,
|
|
122
|
+
response_path: responsePath,
|
|
123
|
+
raw_path: rawPath,
|
|
124
|
+
metadata: payload.metadata || null,
|
|
125
|
+
};
|
|
126
|
+
const entryPath = path.join(cfg.entriesDir, `${key}.json`);
|
|
127
|
+
writeFileSync(entryPath, JSON.stringify(entry, null, 2), 'utf-8');
|
|
128
|
+
recordInvocation(tool, key, {
|
|
129
|
+
cache_hit: false,
|
|
130
|
+
input,
|
|
131
|
+
response_path: responsePath,
|
|
132
|
+
raw_path: rawPath,
|
|
133
|
+
page_url: payload.pageUrl || null,
|
|
134
|
+
metadata: payload.metadata || null,
|
|
135
|
+
});
|
|
136
|
+
return { key, entryPath, responsePath, rawPath };
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
export function recordInvocation(tool, key, details) {
|
|
140
|
+
const cfg = getCacheConfig();
|
|
141
|
+
if (!cfg?.invocationsDir) return;
|
|
142
|
+
ensureDir(cfg.invocationsDir);
|
|
143
|
+
const invocation = {
|
|
144
|
+
ts: new Date().toISOString(),
|
|
145
|
+
tool,
|
|
146
|
+
key,
|
|
147
|
+
step_id: cfg.stepId,
|
|
148
|
+
step_label: cfg.stepLabel,
|
|
149
|
+
run_dir: cfg.runDir,
|
|
150
|
+
...details,
|
|
151
|
+
};
|
|
152
|
+
const filePath = path.join(cfg.invocationsDir, `${nowStamp()}-${tool}-${key.slice(0, 10)}.json`);
|
|
153
|
+
writeFileSync(filePath, JSON.stringify(invocation, null, 2), 'utf-8');
|
|
154
|
+
}
|
|
@@ -0,0 +1,142 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
/**
|
|
3
|
+
* Manage private Browser Tools agent configuration.
|
|
4
|
+
* Stores user-specific profile labels and task preferences outside the repo.
|
|
5
|
+
*/
|
|
6
|
+
|
|
7
|
+
import {
|
|
8
|
+
activeChromeProfiles,
|
|
9
|
+
browserToolsConfigFile,
|
|
10
|
+
ensureBrowserToolsConfig,
|
|
11
|
+
hasFlag,
|
|
12
|
+
requiredOptionValue,
|
|
13
|
+
readBrowserToolsConfig,
|
|
14
|
+
setTaskProfiles,
|
|
15
|
+
} from './browser-control.mjs';
|
|
16
|
+
|
|
17
|
+
function usage() {
|
|
18
|
+
console.error(`Usage:
|
|
19
|
+
scripts/config.mjs profiles [--refresh] [--json]
|
|
20
|
+
scripts/config.mjs active-profiles [--refresh] [--json]
|
|
21
|
+
scripts/config.mjs task-profile list [--json]
|
|
22
|
+
scripts/config.mjs task-profile get <task> [--json]
|
|
23
|
+
scripts/config.mjs task-profile set <task> --profile <profile-or-alias> [--profile <profile-or-alias>]
|
|
24
|
+
|
|
25
|
+
Private config location:
|
|
26
|
+
${browserToolsConfigFile()}
|
|
27
|
+
|
|
28
|
+
Config roots and runtime overrides:
|
|
29
|
+
AGENT_CONFIG_DIR Override the shared agent config root. Default: ~/.agents
|
|
30
|
+
BROWSER_TOOLS_CONFIG_DIR Override only Browser Tools config.
|
|
31
|
+
BROWSER_TOOLS_CACHE_DIR Override managed browser cache and copied profiles.
|
|
32
|
+
BROWSER_TOOLS_ARTIFACT_DIR Override screenshots and generated artifacts.
|
|
33
|
+
BROWSER_TOOLS_CHROME_SOURCE_DIR Override source Chrome profile directory.
|
|
34
|
+
BROWSER_TOOLS_CHROME_BIN Override Chrome executable path.
|
|
35
|
+
|
|
36
|
+
Notes:
|
|
37
|
+
profiles Creates or reads the cached Chrome profile registry.
|
|
38
|
+
active-profiles Shows profiles Chrome marks as last active in Local State.
|
|
39
|
+
task-profile Remembers which profile or profiles to use for non-primary helper tasks.
|
|
40
|
+
`);
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
function profileValues(args) {
|
|
44
|
+
const values = [];
|
|
45
|
+
for (let i = 0; i < args.length; i++) {
|
|
46
|
+
if (args[i] === '--profile') {
|
|
47
|
+
const value = args[i + 1];
|
|
48
|
+
if (!value || value.startsWith('--')) throw new Error('Missing value after --profile');
|
|
49
|
+
values.push(value);
|
|
50
|
+
i += 1;
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
return values;
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
function printProfiles(config, { onlyActive = false } = {}) {
|
|
57
|
+
const entries = Object.values(config.profiles || {}).filter((profile) => !onlyActive || profile.lastActive);
|
|
58
|
+
console.log(`Config: ${browserToolsConfigFile()}`);
|
|
59
|
+
if (!entries.length) {
|
|
60
|
+
console.log(onlyActive ? 'No last-active Chrome profiles found in Local State.' : 'No Chrome profiles discovered.');
|
|
61
|
+
return;
|
|
62
|
+
}
|
|
63
|
+
for (const profile of entries) {
|
|
64
|
+
const active = profile.lastActive ? ' active' : '';
|
|
65
|
+
const account = profile.account ? ` account=${profile.account}` : '';
|
|
66
|
+
const name = profile.name && profile.name !== profile.folder ? ` name=${profile.name}` : '';
|
|
67
|
+
console.log(`${profile.folder}${active}${name}${account}`);
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
function printTaskProfiles(config, task = null, { json = false } = {}) {
|
|
72
|
+
const taskProfiles = config.taskProfiles || {};
|
|
73
|
+
const data = task ? taskProfiles[task] || null : taskProfiles;
|
|
74
|
+
if (json) {
|
|
75
|
+
console.log(JSON.stringify(data, null, 2));
|
|
76
|
+
return;
|
|
77
|
+
}
|
|
78
|
+
if (task) {
|
|
79
|
+
if (!data) {
|
|
80
|
+
console.log(`No profile preference for task "${task}".`);
|
|
81
|
+
return;
|
|
82
|
+
}
|
|
83
|
+
console.log(`${task}: ${data.profiles.join(', ')} (default: ${data.defaultProfile})`);
|
|
84
|
+
return;
|
|
85
|
+
}
|
|
86
|
+
const entries = Object.entries(taskProfiles);
|
|
87
|
+
if (!entries.length) {
|
|
88
|
+
console.log('No task profile preferences configured.');
|
|
89
|
+
return;
|
|
90
|
+
}
|
|
91
|
+
for (const [name, entry] of entries) {
|
|
92
|
+
console.log(`${name}: ${entry.profiles.join(', ')} (default: ${entry.defaultProfile})`);
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
const args = process.argv.slice(2);
|
|
97
|
+
const command = args[0] || 'profiles';
|
|
98
|
+
|
|
99
|
+
try {
|
|
100
|
+
if (hasFlag(args, '--help') || hasFlag(args, '-h')) {
|
|
101
|
+
usage();
|
|
102
|
+
process.exit(0);
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
const refresh = hasFlag(args, '--refresh');
|
|
106
|
+
const json = hasFlag(args, '--json');
|
|
107
|
+
const configDir = requiredOptionValue(args, '--config-dir', undefined);
|
|
108
|
+
|
|
109
|
+
if (command === 'profiles') {
|
|
110
|
+
const config = ensureBrowserToolsConfig({ configDir, refresh });
|
|
111
|
+
if (json) console.log(JSON.stringify(config, null, 2));
|
|
112
|
+
else printProfiles(config);
|
|
113
|
+
} else if (command === 'active-profiles') {
|
|
114
|
+
const config = ensureBrowserToolsConfig({ configDir, refresh });
|
|
115
|
+
if (json) console.log(JSON.stringify(activeChromeProfiles({ configDir, refresh }), null, 2));
|
|
116
|
+
else printProfiles(config, { onlyActive: true });
|
|
117
|
+
} else if (command === 'task-profile') {
|
|
118
|
+
const action = args[1] || 'list';
|
|
119
|
+
if (action === 'list') {
|
|
120
|
+
printTaskProfiles(ensureBrowserToolsConfig({ configDir, refresh }), null, { json });
|
|
121
|
+
} else if (action === 'get') {
|
|
122
|
+
const task = args[2];
|
|
123
|
+
if (!task) throw new Error('Missing task name');
|
|
124
|
+
printTaskProfiles(ensureBrowserToolsConfig({ configDir, refresh }), task, { json });
|
|
125
|
+
} else if (action === 'set') {
|
|
126
|
+
const task = args[2];
|
|
127
|
+
if (!task) throw new Error('Missing task name');
|
|
128
|
+
const entry = setTaskProfiles(task, profileValues(args.slice(3)), { configDir });
|
|
129
|
+
if (json) console.log(JSON.stringify(entry, null, 2));
|
|
130
|
+
else console.log(`${task}: ${entry.profiles.join(', ')} (default: ${entry.defaultProfile})`);
|
|
131
|
+
} else {
|
|
132
|
+
usage();
|
|
133
|
+
process.exit(1);
|
|
134
|
+
}
|
|
135
|
+
} else {
|
|
136
|
+
usage();
|
|
137
|
+
process.exit(1);
|
|
138
|
+
}
|
|
139
|
+
} catch (e) {
|
|
140
|
+
console.error(`✗ ${e.message}`);
|
|
141
|
+
process.exit(1);
|
|
142
|
+
}
|
package/scripts/eval.mjs
ADDED
|
@@ -0,0 +1,83 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
/**
|
|
3
|
+
* Execute JavaScript in the active tab's page context.
|
|
4
|
+
* Runs in an async context, so you can use await.
|
|
5
|
+
*
|
|
6
|
+
* Usage:
|
|
7
|
+
* scripts/eval.mjs 'document.title'
|
|
8
|
+
* scripts/eval.mjs 'await fetch("/api/data").then(r => r.json())'
|
|
9
|
+
* scripts/eval.mjs --port 9223 'document.title'
|
|
10
|
+
* scripts/eval.mjs --owner-token "$BROWSER_TOOLS_OWNER_TOKEN" 'document.title'
|
|
11
|
+
*/
|
|
12
|
+
|
|
13
|
+
import { realpathSync } from 'node:fs';
|
|
14
|
+
import { fileURLToPath } from 'node:url';
|
|
15
|
+
import { format } from 'node:util';
|
|
16
|
+
import { activePage, parseOwnerToken, parsePort, stripBrowserSessionArgs, withBrowser } from './browser-control.mjs';
|
|
17
|
+
|
|
18
|
+
if (isDirectExecution()) await main();
|
|
19
|
+
|
|
20
|
+
async function main() {
|
|
21
|
+
const args = process.argv.slice(2);
|
|
22
|
+
const port = parsePort(args);
|
|
23
|
+
const ownerToken = parseOwnerToken(args);
|
|
24
|
+
const code = stripBrowserSessionArgs(args).join(' ');
|
|
25
|
+
|
|
26
|
+
if (!code) {
|
|
27
|
+
console.log("Usage: eval.js [--port <n>] '<javascript>'");
|
|
28
|
+
console.log(" ./eval.js 'document.title'");
|
|
29
|
+
console.log(' ./eval.js \'document.querySelectorAll("a").length\'');
|
|
30
|
+
process.exit(1);
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
await withBrowser(port, async (browser) => {
|
|
34
|
+
const page = await activePage(browser);
|
|
35
|
+
const result = await page.evaluate((c) => {
|
|
36
|
+
const AsyncFunction = (async () => {}).constructor;
|
|
37
|
+
return new AsyncFunction(`return (${c})`)();
|
|
38
|
+
}, code);
|
|
39
|
+
|
|
40
|
+
printResult(result);
|
|
41
|
+
}, { ownerToken });
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
function isDirectExecution() {
|
|
45
|
+
if (!process.argv[1]) return false;
|
|
46
|
+
return realpathSync(process.argv[1]) === realpathSync(fileURLToPath(import.meta.url));
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
export function formatResultLines(result) {
|
|
50
|
+
if (Array.isArray(result)) {
|
|
51
|
+
return result.flatMap((item, i) => {
|
|
52
|
+
const lines = formatResultLines(item);
|
|
53
|
+
return i > 0 ? ['', ...lines] : lines;
|
|
54
|
+
});
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
if (typeof result === 'object' && result !== null) {
|
|
58
|
+
return Object.entries(result).map(([k, v]) => `${k}: ${formatNestedValue(v)}`);
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
return [formatPrimitiveValue(result)];
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
function printResult(result) {
|
|
65
|
+
for (const line of formatResultLines(result)) console.log(line);
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
function formatNestedValue(value) {
|
|
69
|
+
if (value !== null && typeof value === 'object') {
|
|
70
|
+
try {
|
|
71
|
+
const json = JSON.stringify(value);
|
|
72
|
+
if (json !== undefined) return json;
|
|
73
|
+
} catch {
|
|
74
|
+
return format('%s', value);
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
return formatPrimitiveValue(value);
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
function formatPrimitiveValue(value) {
|
|
82
|
+
return format('%s', value);
|
|
83
|
+
}
|
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
/**
|
|
3
|
+
* Extract full article text from the current tab.
|
|
4
|
+
* Usage: ./extract-article.js [--port 9222] [--owner-token token] [--chars 6000]
|
|
5
|
+
*/
|
|
6
|
+
import { activePage, parseOwnerToken, parsePort, parsePositiveIntegerOption } from './browser-control.mjs';
|
|
7
|
+
import { runBrowserResource } from './resource-helper.mjs';
|
|
8
|
+
|
|
9
|
+
const args = process.argv.slice(2);
|
|
10
|
+
let maxChars;
|
|
11
|
+
try {
|
|
12
|
+
maxChars = parsePositiveIntegerOption(args, '--chars', 5000);
|
|
13
|
+
} catch (error) {
|
|
14
|
+
console.error(error.message);
|
|
15
|
+
process.exit(1);
|
|
16
|
+
}
|
|
17
|
+
const port = parsePort(args);
|
|
18
|
+
const ownerToken = parseOwnerToken(args);
|
|
19
|
+
|
|
20
|
+
await runBrowserResource({
|
|
21
|
+
port,
|
|
22
|
+
ownerToken,
|
|
23
|
+
getPage: activePage,
|
|
24
|
+
run: async ({ page }) => {
|
|
25
|
+
// Wait for content to settle
|
|
26
|
+
await new Promise(r => setTimeout(r, 1500));
|
|
27
|
+
|
|
28
|
+
const text = await page.evaluate((max) => {
|
|
29
|
+
const url = location.href;
|
|
30
|
+
const title = document.title;
|
|
31
|
+
|
|
32
|
+
// Try known article body selectors first
|
|
33
|
+
const selectors = [
|
|
34
|
+
"article",
|
|
35
|
+
"[class*='article-body']",
|
|
36
|
+
"[class*='articleBody']",
|
|
37
|
+
"[class*='story-body']",
|
|
38
|
+
"[class*='StoryBody']",
|
|
39
|
+
"[class*='body-content']",
|
|
40
|
+
"[class*='article__body']",
|
|
41
|
+
"[class*='post-body']",
|
|
42
|
+
"[data-module='ArticleBody']",
|
|
43
|
+
"[data-testid='article-body']",
|
|
44
|
+
"main",
|
|
45
|
+
];
|
|
46
|
+
|
|
47
|
+
for (const sel of selectors) {
|
|
48
|
+
const el = document.querySelector(sel);
|
|
49
|
+
if (el) {
|
|
50
|
+
const txt = el.innerText.trim();
|
|
51
|
+
if (txt.length > 300) return { url, title, body: txt.slice(0, max) };
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
// Fallback: collect all visible paragraphs
|
|
56
|
+
const paras = Array.from(document.querySelectorAll("p"))
|
|
57
|
+
.map(p => p.innerText.trim())
|
|
58
|
+
.filter(t => t.length > 50)
|
|
59
|
+
.join("\n\n");
|
|
60
|
+
|
|
61
|
+
return { url, title, body: paras.slice(0, max) };
|
|
62
|
+
}, maxChars);
|
|
63
|
+
|
|
64
|
+
console.log(`URL: ${text.url}`);
|
|
65
|
+
console.log(`TITLE: ${text.title}`);
|
|
66
|
+
console.log(`---`);
|
|
67
|
+
console.log(text.body);
|
|
68
|
+
},
|
|
69
|
+
});
|
package/scripts/nav.mjs
ADDED
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
/**
|
|
3
|
+
* Navigate the active tab or open a new one.
|
|
4
|
+
*
|
|
5
|
+
* Usage:
|
|
6
|
+
* scripts/nav.mjs https://example.com
|
|
7
|
+
* scripts/nav.mjs https://example.com --new
|
|
8
|
+
* scripts/nav.mjs https://example.com --port 9223
|
|
9
|
+
* scripts/nav.mjs https://example.com --owner-token "$BROWSER_TOOLS_OWNER_TOKEN"
|
|
10
|
+
*/
|
|
11
|
+
|
|
12
|
+
import { activePage, hasFlag, parseOwnerToken, parsePort, withBrowser } from './browser-control.mjs';
|
|
13
|
+
|
|
14
|
+
const args = process.argv.slice(2);
|
|
15
|
+
const url = args.find((a) => a.startsWith('http') || a.startsWith('file'));
|
|
16
|
+
const newTab = hasFlag(args, '--new');
|
|
17
|
+
const port = parsePort(args);
|
|
18
|
+
const ownerToken = parseOwnerToken(args);
|
|
19
|
+
|
|
20
|
+
if (!url) {
|
|
21
|
+
console.log('Usage: scripts/nav.mjs <url> [--new] [--port <n>]');
|
|
22
|
+
console.log(' scripts/nav.mjs https://example.com');
|
|
23
|
+
console.log(' scripts/nav.mjs https://example.com --new');
|
|
24
|
+
process.exit(1);
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
await withBrowser(port, async (browser) => {
|
|
28
|
+
if (newTab) {
|
|
29
|
+
const page = await browser.newPage({ background: true });
|
|
30
|
+
await page.goto(url, { waitUntil: 'domcontentloaded' });
|
|
31
|
+
console.log(`✓ Opened new tab: ${url}`);
|
|
32
|
+
return;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
const page = await activePage(browser);
|
|
36
|
+
await page.goto(url, { waitUntil: 'domcontentloaded' });
|
|
37
|
+
console.log(`✓ Navigated to: ${url}`);
|
|
38
|
+
}, { ownerToken });
|
package/scripts/pick.mjs
ADDED
|
@@ -0,0 +1,213 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
/**
|
|
3
|
+
* Interactive DOM element picker. Lets you click elements in Chrome
|
|
4
|
+
* and get back their tag, id, class, text, html, and parent chain.
|
|
5
|
+
* Useful for pinpointing selectors without guessing at the DOM structure.
|
|
6
|
+
*
|
|
7
|
+
* Usage:
|
|
8
|
+
* scripts/pick.mjs "Click the price element"
|
|
9
|
+
* scripts/pick.mjs --port 9223 "Select the nav links"
|
|
10
|
+
* scripts/pick.mjs --owner-token "$BROWSER_TOOLS_OWNER_TOKEN" "Select the nav links"
|
|
11
|
+
*
|
|
12
|
+
* Controls in the browser:
|
|
13
|
+
* Click: select single element and finish
|
|
14
|
+
* Cmd/Ctrl+Click: add to multi-selection
|
|
15
|
+
* Enter: finish with multi-selection
|
|
16
|
+
* Esc: cancel
|
|
17
|
+
*/
|
|
18
|
+
|
|
19
|
+
import { realpathSync } from 'node:fs';
|
|
20
|
+
import { fileURLToPath } from 'node:url';
|
|
21
|
+
import { activePage, parseOwnerToken, parsePort, stripBrowserSessionArgs, withBrowser } from './browser-control.mjs';
|
|
22
|
+
|
|
23
|
+
if (isDirectExecution()) await main();
|
|
24
|
+
|
|
25
|
+
async function main() {
|
|
26
|
+
const args = process.argv.slice(2);
|
|
27
|
+
const port = parsePort(args);
|
|
28
|
+
const ownerToken = parseOwnerToken(args);
|
|
29
|
+
const message = stripBrowserSessionArgs(args).join(' ');
|
|
30
|
+
|
|
31
|
+
if (!message) {
|
|
32
|
+
console.log("Usage: pick.js [--port <n>] '<instruction>'");
|
|
33
|
+
console.log(' ./pick.js "Click the submit button"');
|
|
34
|
+
process.exit(1);
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
await withBrowser(port, async (browser) => {
|
|
38
|
+
const page = await activePage(browser);
|
|
39
|
+
|
|
40
|
+
await page.evaluate(installPicker);
|
|
41
|
+
|
|
42
|
+
const result = await page.evaluate((msg) => window.pick(msg), message);
|
|
43
|
+
printResult(result);
|
|
44
|
+
}, { ownerToken });
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
function isDirectExecution() {
|
|
48
|
+
if (!process.argv[1]) return false;
|
|
49
|
+
return realpathSync(process.argv[1]) === realpathSync(fileURLToPath(import.meta.url));
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
export function installPicker() {
|
|
53
|
+
if (window.__piPickerInstalled) return;
|
|
54
|
+
window.__piPickerInstalled = true;
|
|
55
|
+
|
|
56
|
+
const normalizeClassValue = (el) => {
|
|
57
|
+
const value = typeof el.className === 'string'
|
|
58
|
+
? el.className
|
|
59
|
+
: typeof el.getAttribute === 'function'
|
|
60
|
+
? el.getAttribute('class')
|
|
61
|
+
: '';
|
|
62
|
+
return typeof value === 'string' ? value.trim().split(/\s+/).filter(Boolean).join(' ') : '';
|
|
63
|
+
};
|
|
64
|
+
|
|
65
|
+
const selectorClass = (el) => {
|
|
66
|
+
const value = normalizeClassValue(el);
|
|
67
|
+
return value ? `.${value.split(/\s+/).join('.')}` : '';
|
|
68
|
+
};
|
|
69
|
+
|
|
70
|
+
const info = (el) => {
|
|
71
|
+
const parents = [];
|
|
72
|
+
let cur = el.parentElement;
|
|
73
|
+
while (cur && cur !== document.body) {
|
|
74
|
+
const tag = cur.tagName.toLowerCase();
|
|
75
|
+
const id = cur.id ? `#${cur.id}` : '';
|
|
76
|
+
parents.push(tag + id + selectorClass(cur));
|
|
77
|
+
cur = cur.parentElement;
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
const className = normalizeClassValue(el);
|
|
81
|
+
return {
|
|
82
|
+
tag: el.tagName.toLowerCase(),
|
|
83
|
+
id: el.id || null,
|
|
84
|
+
class: className || null,
|
|
85
|
+
text: el.textContent?.trim().slice(0, 200) || null,
|
|
86
|
+
html: el.outerHTML.slice(0, 500),
|
|
87
|
+
parents: parents.join(' > '),
|
|
88
|
+
};
|
|
89
|
+
};
|
|
90
|
+
|
|
91
|
+
const errorMessage = (error) => {
|
|
92
|
+
if (error && typeof error.message === 'string' && error.message) return error.message;
|
|
93
|
+
return String(error);
|
|
94
|
+
};
|
|
95
|
+
|
|
96
|
+
window.pick = (message) =>
|
|
97
|
+
new Promise((resolve, reject) => {
|
|
98
|
+
const selections = [];
|
|
99
|
+
const selectedEls = new Set();
|
|
100
|
+
let settled = false;
|
|
101
|
+
|
|
102
|
+
const overlay = document.createElement('div');
|
|
103
|
+
overlay.style.cssText =
|
|
104
|
+
'position:fixed;top:0;left:0;width:100%;height:100%;z-index:2147483647;pointer-events:none';
|
|
105
|
+
|
|
106
|
+
const highlight = document.createElement('div');
|
|
107
|
+
highlight.style.cssText =
|
|
108
|
+
'position:absolute;border:2px solid #3b82f6;background:rgba(59,130,246,0.1);transition:all 0.1s;pointer-events:none';
|
|
109
|
+
overlay.appendChild(highlight);
|
|
110
|
+
|
|
111
|
+
const banner = document.createElement('div');
|
|
112
|
+
banner.style.cssText =
|
|
113
|
+
'position:fixed;bottom:20px;left:50%;transform:translateX(-50%);background:#1f2937;color:white;' +
|
|
114
|
+
'padding:12px 24px;border-radius:8px;font:14px/1.4 sans-serif;box-shadow:0 4px 12px rgba(0,0,0,0.4);' +
|
|
115
|
+
'pointer-events:auto;z-index:2147483647;white-space:nowrap';
|
|
116
|
+
|
|
117
|
+
const refresh = () => {
|
|
118
|
+
banner.textContent = `${message} | ${selections.length} selected | Cmd+click multi | Enter finish | Esc cancel`;
|
|
119
|
+
};
|
|
120
|
+
refresh();
|
|
121
|
+
document.body.append(banner, overlay);
|
|
122
|
+
|
|
123
|
+
const cleanup = () => {
|
|
124
|
+
document.removeEventListener('mousemove', onMove, true);
|
|
125
|
+
document.removeEventListener('click', onClick, true);
|
|
126
|
+
document.removeEventListener('keydown', onKey, true);
|
|
127
|
+
overlay.remove();
|
|
128
|
+
banner.remove();
|
|
129
|
+
selectedEls.forEach((el) => (el.style.outline = ''));
|
|
130
|
+
};
|
|
131
|
+
|
|
132
|
+
const finish = (result) => {
|
|
133
|
+
if (settled) return;
|
|
134
|
+
settled = true;
|
|
135
|
+
cleanup();
|
|
136
|
+
resolve(result);
|
|
137
|
+
};
|
|
138
|
+
|
|
139
|
+
const fail = (error) => {
|
|
140
|
+
if (settled) return;
|
|
141
|
+
settled = true;
|
|
142
|
+
cleanup();
|
|
143
|
+
reject(new Error(`Picker failed to extract element info: ${errorMessage(error)}`));
|
|
144
|
+
};
|
|
145
|
+
|
|
146
|
+
const onMove = (e) => {
|
|
147
|
+
const el = document.elementFromPoint(e.clientX, e.clientY);
|
|
148
|
+
if (!el || overlay.contains(el) || banner.contains(el)) return;
|
|
149
|
+
const r = el.getBoundingClientRect();
|
|
150
|
+
Object.assign(highlight.style, {
|
|
151
|
+
top: `${r.top}px`,
|
|
152
|
+
left: `${r.left}px`,
|
|
153
|
+
width: `${r.width}px`,
|
|
154
|
+
height: `${r.height}px`,
|
|
155
|
+
});
|
|
156
|
+
};
|
|
157
|
+
|
|
158
|
+
const onClick = (e) => {
|
|
159
|
+
if (banner.contains(e.target)) return;
|
|
160
|
+
e.preventDefault();
|
|
161
|
+
e.stopPropagation();
|
|
162
|
+
const el = document.elementFromPoint(e.clientX, e.clientY);
|
|
163
|
+
if (!el || overlay.contains(el) || banner.contains(el)) return;
|
|
164
|
+
|
|
165
|
+
try {
|
|
166
|
+
if (e.metaKey || e.ctrlKey) {
|
|
167
|
+
if (!selectedEls.has(el)) {
|
|
168
|
+
selectedEls.add(el);
|
|
169
|
+
el.style.outline = '3px solid #10b981';
|
|
170
|
+
selections.push(info(el));
|
|
171
|
+
refresh();
|
|
172
|
+
}
|
|
173
|
+
} else {
|
|
174
|
+
finish(selections.length > 0 ? selections : info(el));
|
|
175
|
+
}
|
|
176
|
+
} catch (error) {
|
|
177
|
+
fail(error);
|
|
178
|
+
}
|
|
179
|
+
};
|
|
180
|
+
|
|
181
|
+
const onKey = (e) => {
|
|
182
|
+
if (e.key === 'Escape') {
|
|
183
|
+
e.preventDefault();
|
|
184
|
+
finish(null);
|
|
185
|
+
}
|
|
186
|
+
if (e.key === 'Enter' && selections.length > 0) {
|
|
187
|
+
e.preventDefault();
|
|
188
|
+
finish(selections);
|
|
189
|
+
}
|
|
190
|
+
};
|
|
191
|
+
|
|
192
|
+
document.addEventListener('mousemove', onMove, true);
|
|
193
|
+
document.addEventListener('click', onClick, true);
|
|
194
|
+
document.addEventListener('keydown', onKey, true);
|
|
195
|
+
});
|
|
196
|
+
}
|
|
197
|
+
|
|
198
|
+
function printResult(result) {
|
|
199
|
+
if (result === null) {
|
|
200
|
+
console.log('(cancelled)');
|
|
201
|
+
return;
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
if (Array.isArray(result)) {
|
|
205
|
+
result.forEach((item, i) => {
|
|
206
|
+
if (i > 0) console.log('');
|
|
207
|
+
for (const [k, v] of Object.entries(item)) console.log(`${k}: ${v}`);
|
|
208
|
+
});
|
|
209
|
+
return;
|
|
210
|
+
}
|
|
211
|
+
|
|
212
|
+
for (const [k, v] of Object.entries(result)) console.log(`${k}: ${v}`);
|
|
213
|
+
}
|