pi-browser-use-lite 0.0.0-stage → 0.1.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/CHANGELOG.md +18 -0
- package/LICENSE +22 -0
- package/README.md +150 -2
- package/browser-extension/background/capture.js +267 -0
- package/browser-extension/background/dispatch.js +231 -0
- package/browser-extension/background/index.js +66 -0
- package/browser-extension/background/input/debugger.js +185 -0
- package/browser-extension/background/input/keyboard.js +230 -0
- package/browser-extension/background/input/pointer.js +223 -0
- package/browser-extension/background/input/upload.js +93 -0
- package/browser-extension/background/ledger.js +53 -0
- package/browser-extension/background/page/evaluate.js +93 -0
- package/browser-extension/background/page/helpers.js +28 -0
- package/browser-extension/background/page/screenshot.js +106 -0
- package/browser-extension/background/page/snapshot.js +363 -0
- package/browser-extension/background/page/waitFor.js +64 -0
- package/browser-extension/background/tabs.js +182 -0
- package/browser-extension/background/target.js +116 -0
- package/browser-extension/background/transport.js +144 -0
- package/browser-extension/manifest.json +19 -0
- package/browser-extension/page/snapshot.js +255 -0
- package/package.json +41 -5
- package/src/bridge/index.ts +660 -0
- package/src/commands/index.ts +112 -0
- package/src/config/index.ts +79 -0
- package/src/config/schema.ts +120 -0
- package/src/config/validate.ts +396 -0
- package/src/format/index.ts +173 -0
- package/src/fs/index.ts +103 -0
- package/src/index.ts +125 -0
- package/src/policy/index.ts +73 -0
- package/src/protocol/index.ts +136 -0
- package/src/tools/errors.ts +66 -0
- package/src/tools/exec/act.ts +351 -0
- package/src/tools/exec/cdp.ts +27 -0
- package/src/tools/exec/common.ts +186 -0
- package/src/tools/exec/index.ts +65 -0
- package/src/tools/exec/see.ts +107 -0
- package/src/tools/exec/tab.ts +75 -0
- package/src/tools/exec/vision.ts +56 -0
- package/src/tools/index.ts +450 -0
- package/src/tools/result.ts +56 -0
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
import { onCommitted } from "./capture.js";
|
|
2
|
+
import { dispatch } from "./dispatch.js";
|
|
3
|
+
import { forgetPrefixes } from "./ledger.js";
|
|
4
|
+
import { WINDOW_KEY_PREFIX } from "./tabs.js";
|
|
5
|
+
import { OWNED_KEY_PREFIX, TARGET_KEY_PREFIX } from "./target.js";
|
|
6
|
+
import { startTransport } from "./transport.js";
|
|
7
|
+
|
|
8
|
+
const POLL_ALARM_NAME = "pi-browser-use:poll";
|
|
9
|
+
const POLL_ALARM_MINUTES = 0.5;
|
|
10
|
+
|
|
11
|
+
/** 武装轮询用的 alarm。重复调用是安全的:同名 alarm 会被替换。 */
|
|
12
|
+
function armPollAlarm() {
|
|
13
|
+
chrome.alarms.create(POLL_ALARM_NAME, {
|
|
14
|
+
periodInMinutes: POLL_ALARM_MINUTES,
|
|
15
|
+
});
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
let polling = false;
|
|
19
|
+
|
|
20
|
+
/** 起轮询。重复调用是安全的:一次只跑一个循环。 */
|
|
21
|
+
function startPolling() {
|
|
22
|
+
if (polling) return;
|
|
23
|
+
polling = true;
|
|
24
|
+
const transport = startTransport({
|
|
25
|
+
version: chrome.runtime.getManifest().version,
|
|
26
|
+
dispatch,
|
|
27
|
+
});
|
|
28
|
+
void transport.done.then(() => {
|
|
29
|
+
polling = false;
|
|
30
|
+
});
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
/** 醒来后把 alarm 续上,并确保轮询在跑。 */
|
|
34
|
+
function onWake() {
|
|
35
|
+
armPollAlarm();
|
|
36
|
+
startPolling();
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
/** 浏览器刚启动:上一次的台账一律作废——标签 id 与窗口 id 都可能被重新分配。 */
|
|
40
|
+
async function onBrowserStart() {
|
|
41
|
+
try {
|
|
42
|
+
await forgetPrefixes([
|
|
43
|
+
TARGET_KEY_PREFIX,
|
|
44
|
+
OWNED_KEY_PREFIX,
|
|
45
|
+
WINDOW_KEY_PREFIX,
|
|
46
|
+
]);
|
|
47
|
+
} catch (error) {
|
|
48
|
+
// 清不掉也要让轮询起来,把原因留在扩展的控制台里。
|
|
49
|
+
console.warn("could not forget the previous automation ledger:", error);
|
|
50
|
+
}
|
|
51
|
+
onWake();
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
/** 只认自己的 alarm:唤醒前的第一件事是续上它,然后让轮询起跑。 */
|
|
55
|
+
function onPollAlarm(alarm) {
|
|
56
|
+
if (alarm.name !== POLL_ALARM_NAME) return;
|
|
57
|
+
onWake();
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
chrome.webNavigation.onCommitted.addListener(onCommitted);
|
|
61
|
+
chrome.runtime.onInstalled.addListener(onWake);
|
|
62
|
+
chrome.runtime.onStartup.addListener(onBrowserStart);
|
|
63
|
+
chrome.alarms.onAlarm.addListener(onPollAlarm);
|
|
64
|
+
armPollAlarm();
|
|
65
|
+
// 每秒叫一次轮询:alarm 的周期最短 0.5 分钟,轮询循环退过一次不该等这么久。
|
|
66
|
+
setInterval(startPolling, 1000);
|
|
@@ -0,0 +1,185 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 调试器通道:按标签缓存 attach,发原始 CDP 命令。
|
|
3
|
+
* 只碰 chrome.debugger;不激活标签,不做 DOM 假事件回退(DEC-0007)。
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
/** 已挂上的标签。attach 每次都要走一次 API,缓存省掉往返。 */
|
|
7
|
+
const attachedTabs = new Set();
|
|
8
|
+
|
|
9
|
+
/** chrome.scripting 因权限拒绝时的文案。Chrome 在不同页面上报的四种。 */
|
|
10
|
+
const SCRIPTING_DENIED =
|
|
11
|
+
/Cannot access contents of url|Cannot access .+ at origin|Extension manifest must request permission|matchAboutBlank must be true/i;
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
* 挂上标签的调试器。已挂过就直接返回。
|
|
15
|
+
* @param {number} tabId 标签 id
|
|
16
|
+
* @returns {Promise<void>}
|
|
17
|
+
*/
|
|
18
|
+
export async function attach(tabId) {
|
|
19
|
+
requireDebugger();
|
|
20
|
+
if (attachedTabs.has(tabId)) return;
|
|
21
|
+
try {
|
|
22
|
+
await chrome.debugger.attach({ tabId }, "1.3");
|
|
23
|
+
} catch (error) {
|
|
24
|
+
// 挂不上的原因(开发者工具占着、别的扩展占着)只有这一处能说清。
|
|
25
|
+
throw new Error(`cdp_unavailable: ${errorMessage(error)}`);
|
|
26
|
+
}
|
|
27
|
+
attachedTabs.add(tabId);
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
/**
|
|
31
|
+
* 发一条原始 CDP 命令。会话被浏览器收回时自愈一次。
|
|
32
|
+
* @param {number} tabId 标签 id
|
|
33
|
+
* @param {string} method CDP 方法名
|
|
34
|
+
* @param {Record<string, unknown>} [params] CDP 参数
|
|
35
|
+
* @returns {Promise<unknown>} CDP 的返回
|
|
36
|
+
*/
|
|
37
|
+
export async function sendCommand(tabId, method, params) {
|
|
38
|
+
await attach(tabId);
|
|
39
|
+
try {
|
|
40
|
+
return await chrome.debugger.sendCommand({ tabId }, method, params);
|
|
41
|
+
} catch (error) {
|
|
42
|
+
if (!isSessionGone(error)) throw error;
|
|
43
|
+
await reconnect(tabId);
|
|
44
|
+
return chrome.debugger.sendCommand({ tabId }, method, params);
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
/**
|
|
49
|
+
* 在页面里求值一次,返回结果值。
|
|
50
|
+
* @param {number} tabId 标签 id
|
|
51
|
+
* @param {string} expression JavaScript 表达式
|
|
52
|
+
* @param {{ userGesture?: boolean, awaitPromise?: boolean }} [options] `userGesture` 给页面一次用户激活,`awaitPromise` 等 Promise
|
|
53
|
+
* @returns {Promise<unknown>} 求值结果
|
|
54
|
+
*/
|
|
55
|
+
export async function evaluate(tabId, expression, options = {}) {
|
|
56
|
+
const response = await sendCommand(tabId, "Runtime.evaluate", {
|
|
57
|
+
expression,
|
|
58
|
+
returnByValue: true,
|
|
59
|
+
userGesture: options.userGesture === true,
|
|
60
|
+
awaitPromise: options.awaitPromise === true,
|
|
61
|
+
});
|
|
62
|
+
if (response?.exceptionDetails)
|
|
63
|
+
throw new Error(
|
|
64
|
+
`page_error: ${exceptionMessage(response.exceptionDetails)}`,
|
|
65
|
+
);
|
|
66
|
+
return response?.result?.value;
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
/**
|
|
70
|
+
* 跑 executeScript;被权限拒绝时改走 CDP 的 Runtime.evaluate。
|
|
71
|
+
* @param {object} options
|
|
72
|
+
* @param {number} options.tabId 标签 id
|
|
73
|
+
* @param {Function} options.func 要注入页面的函数
|
|
74
|
+
* @param {unknown[]} [options.args] 函数的参数
|
|
75
|
+
* @param {string} [options.world] 执行世界
|
|
76
|
+
* @returns {Promise<unknown>} 与 chrome.scripting.executeScript 同形:`[{ result }]`
|
|
77
|
+
*/
|
|
78
|
+
export async function executeScriptWithFallback({
|
|
79
|
+
tabId,
|
|
80
|
+
func,
|
|
81
|
+
args = [],
|
|
82
|
+
world = "MAIN",
|
|
83
|
+
}) {
|
|
84
|
+
try {
|
|
85
|
+
return await chrome.scripting.executeScript({
|
|
86
|
+
target: { tabId },
|
|
87
|
+
func,
|
|
88
|
+
args,
|
|
89
|
+
world,
|
|
90
|
+
});
|
|
91
|
+
} catch (error) {
|
|
92
|
+
if (!isAccessDenied(error)) throw error;
|
|
93
|
+
const response = await sendCommand(tabId, "Runtime.evaluate", {
|
|
94
|
+
expression: `(${func.toString()})(...${jsonArgs(args)})`,
|
|
95
|
+
returnByValue: true,
|
|
96
|
+
awaitPromise: true,
|
|
97
|
+
});
|
|
98
|
+
if (response.exceptionDetails)
|
|
99
|
+
throw new Error(exceptionMessage(response.exceptionDetails));
|
|
100
|
+
return [{ result: response.result?.value }];
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
/**
|
|
105
|
+
* 把注入目录里的经典脚本送进页面。被权限拒绝时读源码改走 CDP。
|
|
106
|
+
* @param {object} options
|
|
107
|
+
* @param {number} options.tabId 标签 id
|
|
108
|
+
* @param {string} options.path 相对扩展根目录的路径
|
|
109
|
+
* @param {string} [options.world] 执行世界
|
|
110
|
+
* @returns {Promise<unknown>} 与 chrome.scripting.executeScript 同形
|
|
111
|
+
*/
|
|
112
|
+
export async function injectScriptFile({ tabId, path, world = "MAIN" }) {
|
|
113
|
+
try {
|
|
114
|
+
return await chrome.scripting.executeScript({
|
|
115
|
+
target: { tabId },
|
|
116
|
+
files: [path],
|
|
117
|
+
world,
|
|
118
|
+
});
|
|
119
|
+
} catch (error) {
|
|
120
|
+
if (!isAccessDenied(error)) throw error;
|
|
121
|
+
await evaluate(tabId, await readScriptSource(path));
|
|
122
|
+
return [{ result: undefined }];
|
|
123
|
+
}
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
/** 读扩展自己的脚本源码;扩展资源不受页面 CSP 管。 */
|
|
127
|
+
async function readScriptSource(path) {
|
|
128
|
+
const response = await fetch(chrome.runtime.getURL(path));
|
|
129
|
+
return await response.text();
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
/** 会话失效的自愈:清缓存,detach 残留,再挂一次。只重试一次由调用方保证。 */
|
|
133
|
+
async function reconnect(tabId) {
|
|
134
|
+
attachedTabs.delete(tabId);
|
|
135
|
+
try {
|
|
136
|
+
await chrome.debugger.detach({ tabId });
|
|
137
|
+
} catch (error) {
|
|
138
|
+
// 残留条目可能已经不在。只有「没挂上」可以吞,其它错误照抛。
|
|
139
|
+
if (!isNotAttached(error)) throw error;
|
|
140
|
+
}
|
|
141
|
+
await attach(tabId);
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
/**
|
|
145
|
+
* 取页面异常的首行原文。Chrome 把 `text` 写成 `Uncaught`,原文在 `exception.description`。
|
|
146
|
+
* @param {{ exception?: { description?: string, value?: unknown }, text?: string }} details 异常详情
|
|
147
|
+
* @returns {string} 首行
|
|
148
|
+
*/
|
|
149
|
+
function exceptionMessage(details) {
|
|
150
|
+
const raw =
|
|
151
|
+
details?.exception?.description ??
|
|
152
|
+
details?.exception?.value ??
|
|
153
|
+
details?.text ??
|
|
154
|
+
"evaluation failed";
|
|
155
|
+
return String(raw).split("\n")[0];
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
function errorMessage(error) {
|
|
159
|
+
return error instanceof Error ? error.message : String(error);
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
function isSessionGone(error) {
|
|
163
|
+
const message = errorMessage(error);
|
|
164
|
+
return message.includes("Detached") || message.includes("not attached");
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
function isNotAttached(error) {
|
|
168
|
+
return errorMessage(error).includes("not attached");
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
function isAccessDenied(error) {
|
|
172
|
+
return SCRIPTING_DENIED.test(errorMessage(error));
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
function jsonArgs(args) {
|
|
176
|
+
try {
|
|
177
|
+
return JSON.stringify(args);
|
|
178
|
+
} catch (error) {
|
|
179
|
+
throw new Error("args must be JSON-serializable", { cause: error });
|
|
180
|
+
}
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
function requireDebugger() {
|
|
184
|
+
if (!chrome.debugger) throw new Error("chrome.debugger is not available");
|
|
185
|
+
}
|
|
@@ -0,0 +1,230 @@
|
|
|
1
|
+
import { evaluate, sendCommand } from "./debugger.js";
|
|
2
|
+
import { assertVisible, click } from "./pointer.js";
|
|
3
|
+
|
|
4
|
+
/** CDP 的 modifiers 位。 */
|
|
5
|
+
const ALT = 1;
|
|
6
|
+
const CTRL = 2;
|
|
7
|
+
const META = 4;
|
|
8
|
+
const SHIFT = 8;
|
|
9
|
+
|
|
10
|
+
const MODIFIERS = {
|
|
11
|
+
alt: ALT,
|
|
12
|
+
option: ALT,
|
|
13
|
+
ctrl: CTRL,
|
|
14
|
+
control: CTRL,
|
|
15
|
+
meta: META,
|
|
16
|
+
cmd: META,
|
|
17
|
+
command: META,
|
|
18
|
+
shift: SHIFT,
|
|
19
|
+
};
|
|
20
|
+
|
|
21
|
+
/** 非可打印键。text 是按下时 Chrome 插入的字符。 */
|
|
22
|
+
const SPECIAL = {
|
|
23
|
+
Enter: { code: "Enter", vk: 13, text: "\r" },
|
|
24
|
+
Tab: { code: "Tab", vk: 9, text: "\t" },
|
|
25
|
+
Backspace: { code: "Backspace", vk: 8, text: "" },
|
|
26
|
+
Delete: { code: "Delete", vk: 46, text: "" },
|
|
27
|
+
Escape: { code: "Escape", vk: 27, text: "" },
|
|
28
|
+
ArrowLeft: { code: "ArrowLeft", vk: 37, text: "" },
|
|
29
|
+
ArrowUp: { code: "ArrowUp", vk: 38, text: "" },
|
|
30
|
+
ArrowRight: { code: "ArrowRight", vk: 39, text: "" },
|
|
31
|
+
ArrowDown: { code: "ArrowDown", vk: 40, text: "" },
|
|
32
|
+
Home: { code: "Home", vk: 36, text: "" },
|
|
33
|
+
End: { code: "End", vk: 35, text: "" },
|
|
34
|
+
PageUp: { code: "PageUp", vk: 33, text: "" },
|
|
35
|
+
PageDown: { code: "PageDown", vk: 34, text: "" },
|
|
36
|
+
};
|
|
37
|
+
|
|
38
|
+
/** 可打印字符到物理键位。Shift 变体与基准键共用 code 与键码。 */
|
|
39
|
+
const PRINTABLE = {
|
|
40
|
+
" ": { code: "Space", vk: 32 },
|
|
41
|
+
"`": { code: "Backquote", vk: 192 },
|
|
42
|
+
"~": { code: "Backquote", vk: 192 },
|
|
43
|
+
"-": { code: "Minus", vk: 189 },
|
|
44
|
+
_: { code: "Minus", vk: 189 },
|
|
45
|
+
"=": { code: "Equal", vk: 187 },
|
|
46
|
+
"+": { code: "Equal", vk: 187 },
|
|
47
|
+
"[": { code: "BracketLeft", vk: 219 },
|
|
48
|
+
"{": { code: "BracketLeft", vk: 219 },
|
|
49
|
+
"]": { code: "BracketRight", vk: 221 },
|
|
50
|
+
"}": { code: "BracketRight", vk: 221 },
|
|
51
|
+
"\\": { code: "Backslash", vk: 220 },
|
|
52
|
+
"|": { code: "Backslash", vk: 220 },
|
|
53
|
+
";": { code: "Semicolon", vk: 186 },
|
|
54
|
+
":": { code: "Semicolon", vk: 186 },
|
|
55
|
+
"'": { code: "Quote", vk: 222 },
|
|
56
|
+
'"': { code: "Quote", vk: 222 },
|
|
57
|
+
",": { code: "Comma", vk: 188 },
|
|
58
|
+
"<": { code: "Comma", vk: 188 },
|
|
59
|
+
".": { code: "Period", vk: 190 },
|
|
60
|
+
">": { code: "Period", vk: 190 },
|
|
61
|
+
"/": { code: "Slash", vk: 191 },
|
|
62
|
+
"?": { code: "Slash", vk: 191 },
|
|
63
|
+
"!": { code: "Digit1", vk: 49 },
|
|
64
|
+
"@": { code: "Digit2", vk: 50 },
|
|
65
|
+
"#": { code: "Digit3", vk: 51 },
|
|
66
|
+
$: { code: "Digit4", vk: 52 },
|
|
67
|
+
"%": { code: "Digit5", vk: 53 },
|
|
68
|
+
"^": { code: "Digit6", vk: 54 },
|
|
69
|
+
"&": { code: "Digit7", vk: 55 },
|
|
70
|
+
"*": { code: "Digit8", vk: 56 },
|
|
71
|
+
"(": { code: "Digit9", vk: 57 },
|
|
72
|
+
")": { code: "Digit0", vk: 48 },
|
|
73
|
+
};
|
|
74
|
+
|
|
75
|
+
/**
|
|
76
|
+
* 按键一次。组合键写成 `Ctrl+A`,字母统一大写。
|
|
77
|
+
* @param {object} params
|
|
78
|
+
* @param {number} params.tabId 标签 id
|
|
79
|
+
* @param {string} params.key 单个键或组合键
|
|
80
|
+
* @returns {Promise<{ key: string, modifiers: number }>}
|
|
81
|
+
*/
|
|
82
|
+
export async function press({ tabId, key } = {}) {
|
|
83
|
+
const chord = parseChord(key);
|
|
84
|
+
const info = keyInfo(chord.key);
|
|
85
|
+
if (!info) throw new Error(`bad_args: unknown key: ${chord.key}`);
|
|
86
|
+
await assertVisible(tabId);
|
|
87
|
+
const shortcut = (chord.modifiers & (ALT | CTRL | META)) !== 0;
|
|
88
|
+
await dispatchKey(tabId, info, {
|
|
89
|
+
type: shortcut ? "rawKeyDown" : "keyDown",
|
|
90
|
+
modifiers: chord.modifiers,
|
|
91
|
+
text: shortcut ? "" : info.text,
|
|
92
|
+
});
|
|
93
|
+
return { key: info.key, modifiers: chord.modifiers };
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
/**
|
|
97
|
+
* 在焦点元素上输入文本。普通输入框逐字符发按键事件,contenteditable 走一次原生插入。
|
|
98
|
+
* @param {object} params
|
|
99
|
+
* @param {number} params.tabId 标签 id
|
|
100
|
+
* @param {string} params.text 要输入的文本
|
|
101
|
+
* @param {boolean} [params.replace] 先清空焦点元素的内容
|
|
102
|
+
* @returns {Promise<{ length: number, mode: string }>}
|
|
103
|
+
*/
|
|
104
|
+
export async function type({ tabId, text = "", replace = false } = {}) {
|
|
105
|
+
if (typeof text !== "string")
|
|
106
|
+
throw new Error("bad_args: text must be a string");
|
|
107
|
+
await assertVisible(tabId);
|
|
108
|
+
if (replace) await selectAllAndDelete(tabId);
|
|
109
|
+
const chars = Array.from(text);
|
|
110
|
+
if (chars.length === 0) return { length: 0, mode: "none" };
|
|
111
|
+
if (await isContentEditable(tabId)) {
|
|
112
|
+
await sendCommand(tabId, "Input.insertText", { text });
|
|
113
|
+
return { length: chars.length, mode: "insertText" };
|
|
114
|
+
}
|
|
115
|
+
for (const char of chars) await typeChar(tabId, char);
|
|
116
|
+
return { length: chars.length, mode: "keys" };
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
/**
|
|
120
|
+
* 逐字段填充:先点击字段聚焦,再整体替换内容。
|
|
121
|
+
* @param {object} params
|
|
122
|
+
* @param {number} params.tabId 标签 id
|
|
123
|
+
* @param {Array<{ selector: string, value: string }>} params.fields 字段列表
|
|
124
|
+
* @returns {Promise<{ filled: string[] }>} 已填写的选择器
|
|
125
|
+
*/
|
|
126
|
+
export async function fill({ tabId, fields } = {}) {
|
|
127
|
+
if (!Array.isArray(fields) || fields.length === 0)
|
|
128
|
+
throw new Error("bad_args: fields must be a non-empty array");
|
|
129
|
+
for (const field of fields) {
|
|
130
|
+
if (typeof field?.selector !== "string" || field.selector === "")
|
|
131
|
+
throw new Error("bad_args: every field needs a selector");
|
|
132
|
+
if (typeof field.value !== "string")
|
|
133
|
+
throw new Error("bad_args: every field needs a string value");
|
|
134
|
+
}
|
|
135
|
+
const filled = [];
|
|
136
|
+
for (const field of fields) {
|
|
137
|
+
await click({ tabId, selector: field.selector });
|
|
138
|
+
await type({ tabId, text: field.value, replace: true });
|
|
139
|
+
filled.push(field.selector);
|
|
140
|
+
}
|
|
141
|
+
return { filled };
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
/** 用 Chrome 的 selectAll 编辑命令选中,再发一次删除。命令与平台无关。 */
|
|
145
|
+
async function selectAllAndDelete(tabId) {
|
|
146
|
+
await dispatchKey(
|
|
147
|
+
tabId,
|
|
148
|
+
{ key: "a", code: "KeyA", vk: 65 },
|
|
149
|
+
{ type: "rawKeyDown", modifiers: CTRL, text: "", commands: ["selectAll"] },
|
|
150
|
+
);
|
|
151
|
+
await dispatchKey(tabId, { key: "Delete", code: "Delete", vk: 46 });
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
/**
|
|
155
|
+
* 发一个键事件。所有键盘事件的载荷只在这里拼。
|
|
156
|
+
* @param {number} tabId 标签 id
|
|
157
|
+
* @param {{ key: string, code: string, vk: number, text?: string }} info 键位信息
|
|
158
|
+
* @param {{ type?: string, modifiers?: number, text?: string, commands?: string[] }} [options] 事件选项
|
|
159
|
+
* @returns {Promise<void>}
|
|
160
|
+
*/
|
|
161
|
+
async function dispatchKey(
|
|
162
|
+
tabId,
|
|
163
|
+
info,
|
|
164
|
+
{ type = "keyDown", modifiers = 0, text = info.text ?? "", commands } = {},
|
|
165
|
+
) {
|
|
166
|
+
const event = {
|
|
167
|
+
type,
|
|
168
|
+
key: info.key,
|
|
169
|
+
code: info.code,
|
|
170
|
+
windowsVirtualKeyCode: info.vk,
|
|
171
|
+
modifiers,
|
|
172
|
+
text,
|
|
173
|
+
unmodifiedText: text,
|
|
174
|
+
};
|
|
175
|
+
if (commands) event.commands = commands;
|
|
176
|
+
await sendCommand(tabId, "Input.dispatchKeyEvent", event);
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
async function isContentEditable(tabId) {
|
|
180
|
+
const editable = await evaluate(
|
|
181
|
+
tabId,
|
|
182
|
+
"document.activeElement?.isContentEditable === true",
|
|
183
|
+
);
|
|
184
|
+
return editable === true;
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
/** 键位表外的字符(中文、emoji)只带 text,靠 Chrome 插入。 */
|
|
188
|
+
async function typeChar(tabId, char) {
|
|
189
|
+
const info = keyInfo(char) ?? { key: char, code: "", vk: 0 };
|
|
190
|
+
await dispatchKey(tabId, info, { text: char });
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
function parseChord(key) {
|
|
194
|
+
if (typeof key !== "string" || key.trim() === "")
|
|
195
|
+
throw new Error("bad_args: key is required");
|
|
196
|
+
const parts = key.split("+");
|
|
197
|
+
const name = parts.pop()?.trim() ?? "";
|
|
198
|
+
if (name === "") throw new Error("bad_args: key is required");
|
|
199
|
+
let modifiers = 0;
|
|
200
|
+
for (const part of parts) {
|
|
201
|
+
const bit = MODIFIERS[part.trim().toLowerCase()];
|
|
202
|
+
if (bit === undefined)
|
|
203
|
+
throw new Error(`bad_args: unknown modifier: ${part.trim()}`);
|
|
204
|
+
modifiers |= bit;
|
|
205
|
+
}
|
|
206
|
+
const normalized = /^[a-z]$/.test(name) ? name.toUpperCase() : name;
|
|
207
|
+
return { key: normalized, modifiers };
|
|
208
|
+
}
|
|
209
|
+
|
|
210
|
+
/** 键位表查询。表外的键名返回 null。 */
|
|
211
|
+
function keyInfo(key) {
|
|
212
|
+
const special = SPECIAL[key];
|
|
213
|
+
if (special)
|
|
214
|
+
return { key, code: special.code, vk: special.vk, text: special.text };
|
|
215
|
+
const printable = PRINTABLE[key];
|
|
216
|
+
if (printable)
|
|
217
|
+
return { key, code: printable.code, vk: printable.vk, text: key };
|
|
218
|
+
if (/^[A-Z]$/.test(key))
|
|
219
|
+
return { key, code: `Key${key}`, vk: key.charCodeAt(0), text: key };
|
|
220
|
+
if (/^[a-z]$/.test(key))
|
|
221
|
+
return {
|
|
222
|
+
key,
|
|
223
|
+
code: `Key${key.toUpperCase()}`,
|
|
224
|
+
vk: key.toUpperCase().charCodeAt(0),
|
|
225
|
+
text: key,
|
|
226
|
+
};
|
|
227
|
+
if (/^[0-9]$/.test(key))
|
|
228
|
+
return { key, code: `Digit${key}`, vk: key.charCodeAt(0), text: key };
|
|
229
|
+
return null;
|
|
230
|
+
}
|
|
@@ -0,0 +1,223 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 指针事件:移动、点击、悬停、拖动、滚轮。全部走 CDP,不做 DOM 假事件回退(DEC-0007)。
|
|
3
|
+
* 目标标签不可见时直接抛错:Chrome 会丢弃不可见页面的可信输入,激活标签由调用方决定。
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
import { evaluate, sendCommand } from "./debugger.js";
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* 点击一个点或元素。事件序为 mouseMoved、mousePressed、mouseReleased。
|
|
10
|
+
* @param {object} params
|
|
11
|
+
* @param {number} params.tabId 标签 id
|
|
12
|
+
* @param {string} [params.selector] CSS 选择器,与坐标二选一
|
|
13
|
+
* @param {number} [params.x] 视口坐标 x,与 y 一起给
|
|
14
|
+
* @param {number} [params.y] 视口坐标 y
|
|
15
|
+
* @returns {Promise<{ x: number, y: number }>} 实际点击的点
|
|
16
|
+
*/
|
|
17
|
+
export async function click({ tabId, ...target } = {}) {
|
|
18
|
+
const kind = classifyTarget(target);
|
|
19
|
+
await assertVisible(tabId);
|
|
20
|
+
const point = await resolvePoint(tabId, kind, target);
|
|
21
|
+
await movePointer(tabId, point);
|
|
22
|
+
await dispatchMouse(tabId, "mousePressed", point, {
|
|
23
|
+
button: "left",
|
|
24
|
+
buttons: 1,
|
|
25
|
+
clickCount: 1,
|
|
26
|
+
});
|
|
27
|
+
await dispatchMouse(tabId, "mouseReleased", point, {
|
|
28
|
+
button: "left",
|
|
29
|
+
buttons: 0,
|
|
30
|
+
clickCount: 1,
|
|
31
|
+
});
|
|
32
|
+
return point;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
/**
|
|
36
|
+
* 悬停:把指针移到点或元素上,只发一次 mouseMoved。
|
|
37
|
+
* @param {object} params
|
|
38
|
+
* @param {number} params.tabId 标签 id
|
|
39
|
+
* @param {string} [params.selector] CSS 选择器,与坐标二选一
|
|
40
|
+
* @param {number} [params.x] 视口坐标 x,与 y 一起给
|
|
41
|
+
* @param {number} [params.y] 视口坐标 y
|
|
42
|
+
* @returns {Promise<{ x: number, y: number }>}
|
|
43
|
+
*/
|
|
44
|
+
export async function hover({ tabId, ...target } = {}) {
|
|
45
|
+
const kind = classifyTarget(target);
|
|
46
|
+
await assertVisible(tabId);
|
|
47
|
+
const point = await resolvePoint(tabId, kind, target);
|
|
48
|
+
await movePointer(tabId, point);
|
|
49
|
+
return point;
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
/**
|
|
53
|
+
* 把指针移到视口坐标上。
|
|
54
|
+
* @param {object} params
|
|
55
|
+
* @param {number} params.tabId 标签 id
|
|
56
|
+
* @param {number} params.x 视口坐标 x
|
|
57
|
+
* @param {number} params.y 视口坐标 y
|
|
58
|
+
* @returns {Promise<{ x: number, y: number }>}
|
|
59
|
+
*/
|
|
60
|
+
export async function moveMouse({ tabId, x, y } = {}) {
|
|
61
|
+
const point = requirePoint({ x, y });
|
|
62
|
+
await assertVisible(tabId);
|
|
63
|
+
await movePointer(tabId, point);
|
|
64
|
+
return point;
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
/**
|
|
68
|
+
* 拖拽。从按下到松开之间发 steps 次鼠标移动,步数来自配置 input.dragSteps。
|
|
69
|
+
* @param {object} params
|
|
70
|
+
* @param {number} params.tabId 标签 id
|
|
71
|
+
* @param {object} params.from 起点:{ selector } 或 { x, y }
|
|
72
|
+
* @param {object} params.to 终点:{ selector } 或 { x, y }
|
|
73
|
+
* @param {number} params.steps 中间移动次数,来自配置 input.dragSteps
|
|
74
|
+
* @returns {Promise<{ from: { x: number, y: number }, to: { x: number, y: number }, steps: number }>}
|
|
75
|
+
*/
|
|
76
|
+
export async function drag({ tabId, from, to, steps } = {}) {
|
|
77
|
+
const fromKind = classifyTarget(from ?? {});
|
|
78
|
+
const toKind = classifyTarget(to ?? {});
|
|
79
|
+
requireSteps("dragSteps", steps);
|
|
80
|
+
await assertVisible(tabId);
|
|
81
|
+
const start = await resolvePoint(tabId, fromKind, from);
|
|
82
|
+
const end = await resolvePoint(tabId, toKind, to);
|
|
83
|
+
await dispatchMouse(tabId, "mousePressed", start, {
|
|
84
|
+
button: "left",
|
|
85
|
+
buttons: 1,
|
|
86
|
+
clickCount: 1,
|
|
87
|
+
});
|
|
88
|
+
for (let step = 1; step <= steps; step++) {
|
|
89
|
+
const ratio = step / steps;
|
|
90
|
+
await dispatchMouse(
|
|
91
|
+
tabId,
|
|
92
|
+
"mouseMoved",
|
|
93
|
+
{
|
|
94
|
+
x: start.x + (end.x - start.x) * ratio,
|
|
95
|
+
y: start.y + (end.y - start.y) * ratio,
|
|
96
|
+
},
|
|
97
|
+
{ button: "left", buttons: 1 },
|
|
98
|
+
);
|
|
99
|
+
}
|
|
100
|
+
await dispatchMouse(tabId, "mouseReleased", end, {
|
|
101
|
+
button: "left",
|
|
102
|
+
buttons: 0,
|
|
103
|
+
clickCount: 1,
|
|
104
|
+
});
|
|
105
|
+
return { from: start, to: end, steps };
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
/**
|
|
109
|
+
* 滚轮。发 steps 次 mouseWheel,位移平分到每次,落点是视口中心。
|
|
110
|
+
* @param {object} params
|
|
111
|
+
* @param {number} params.tabId 标签 id
|
|
112
|
+
* @param {number} [params.dx] 横向位移,正值向右
|
|
113
|
+
* @param {number} [params.dy] 纵向位移,正值向下
|
|
114
|
+
* @param {number} params.steps 事件次数,来自配置 input.scrollSteps
|
|
115
|
+
* @returns {Promise<{ deltaX: number, deltaY: number, steps: number }>}
|
|
116
|
+
*/
|
|
117
|
+
export async function scroll({ tabId, dx = 0, dy = 0, steps } = {}) {
|
|
118
|
+
requireSteps("scrollSteps", steps);
|
|
119
|
+
await assertVisible(tabId);
|
|
120
|
+
const center = await viewportCenter(tabId);
|
|
121
|
+
for (let step = 0; step < steps; step++) {
|
|
122
|
+
await sendCommand(tabId, "Input.dispatchMouseEvent", {
|
|
123
|
+
type: "mouseWheel",
|
|
124
|
+
x: center.x,
|
|
125
|
+
y: center.y,
|
|
126
|
+
deltaX: dx / steps,
|
|
127
|
+
deltaY: dy / steps,
|
|
128
|
+
pointerType: "mouse",
|
|
129
|
+
});
|
|
130
|
+
}
|
|
131
|
+
return { deltaX: dx, deltaY: dy, steps };
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
function isFiniteNumber(value) {
|
|
135
|
+
return typeof value === "number" && Number.isFinite(value);
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
function movePointer(tabId, point) {
|
|
139
|
+
return dispatchMouse(tabId, "mouseMoved", point, {
|
|
140
|
+
button: "none",
|
|
141
|
+
buttons: 0,
|
|
142
|
+
});
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
function dispatchMouse(tabId, type, point, extra) {
|
|
146
|
+
return sendCommand(tabId, "Input.dispatchMouseEvent", {
|
|
147
|
+
type,
|
|
148
|
+
x: point.x,
|
|
149
|
+
y: point.y,
|
|
150
|
+
pointerType: "mouse",
|
|
151
|
+
...extra,
|
|
152
|
+
});
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
/** 目标三选一:ref、selector、x+y。ref 由快照解析,pointer 只收后两种。 */
|
|
156
|
+
function classifyTarget(target) {
|
|
157
|
+
const hasRef = typeof target.ref === "string" && target.ref !== "";
|
|
158
|
+
const hasSelector =
|
|
159
|
+
typeof target.selector === "string" && target.selector !== "";
|
|
160
|
+
const wantsPoint = target.x !== undefined || target.y !== undefined;
|
|
161
|
+
if (wantsPoint) requirePoint(target);
|
|
162
|
+
const given = [hasRef, hasSelector, wantsPoint].filter(Boolean).length;
|
|
163
|
+
if (given !== 1)
|
|
164
|
+
throw new Error(
|
|
165
|
+
`bad_args: pass exactly one of ref, selector, or x+y; got ${given}`,
|
|
166
|
+
);
|
|
167
|
+
if (hasRef)
|
|
168
|
+
throw new Error(
|
|
169
|
+
"bad_args: ref is resolved by the snapshot; pass selector or x+y",
|
|
170
|
+
);
|
|
171
|
+
return hasSelector ? "selector" : "point";
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
function requirePoint({ x, y }) {
|
|
175
|
+
if (!isFiniteNumber(x) || !isFiniteNumber(y))
|
|
176
|
+
throw new Error("bad_args: x and y must be finite numbers");
|
|
177
|
+
return { x, y };
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
function requireSteps(name, steps) {
|
|
181
|
+
if (!Number.isInteger(steps) || steps < 1)
|
|
182
|
+
throw new Error(`bad_args: ${name} must be a positive integer`);
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
async function resolvePoint(tabId, kind, target) {
|
|
186
|
+
if (kind === "point") return { x: target.x, y: target.y };
|
|
187
|
+
return elementCenter(tabId, target.selector);
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
async function elementCenter(tabId, selector) {
|
|
191
|
+
const expression = `(() => {
|
|
192
|
+
const element = document.querySelector(${JSON.stringify(selector)});
|
|
193
|
+
if (!element) return null;
|
|
194
|
+
const rect = element.getBoundingClientRect();
|
|
195
|
+
return { x: rect.left + rect.width / 2, y: rect.top + rect.height / 2 };
|
|
196
|
+
})()`;
|
|
197
|
+
const center = await evaluate(tabId, expression);
|
|
198
|
+
if (!isFiniteNumber(center?.x) || !isFiniteNumber(center?.y))
|
|
199
|
+
throw new Error(`page_error: selector matched no element: ${selector}`);
|
|
200
|
+
return { x: center.x, y: center.y };
|
|
201
|
+
}
|
|
202
|
+
|
|
203
|
+
async function viewportCenter(tabId) {
|
|
204
|
+
const center = await evaluate(
|
|
205
|
+
tabId,
|
|
206
|
+
"({ x: Math.round(innerWidth / 2), y: Math.round(innerHeight / 2) })",
|
|
207
|
+
);
|
|
208
|
+
if (!isFiniteNumber(center?.x) || !isFiniteNumber(center?.y))
|
|
209
|
+
throw new Error("page_error: could not read viewport size");
|
|
210
|
+
return { x: center.x, y: center.y };
|
|
211
|
+
}
|
|
212
|
+
|
|
213
|
+
export async function assertVisible(tabId) {
|
|
214
|
+
const state = await evaluate(tabId, "document.visibilityState");
|
|
215
|
+
if (state === "visible") return;
|
|
216
|
+
if (state === "hidden")
|
|
217
|
+
throw new Error(
|
|
218
|
+
`page_error: tab ${tabId} is hidden; Chrome drops input to hidden pages, so bring it to the front with chrome_tab switch (turn background mode off first)`,
|
|
219
|
+
);
|
|
220
|
+
throw new Error(
|
|
221
|
+
`page_error: could not read document.visibilityState for tab ${tabId}`,
|
|
222
|
+
);
|
|
223
|
+
}
|