dsh-chrome-bridge 1.20.0 → 2.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/README.en.md +6 -2
- package/README.md +6 -2
- package/client.js +110 -29
- package/core/ext.js +119 -0
- package/core/hosts.js +277 -0
- package/core/locale.js +8 -0
- package/core/receipt.js +56 -0
- package/core/session.js +157 -0
- package/core/shots.js +56 -0
- package/core/tooldef.js +142 -0
- package/core/transport.js +212 -0
- package/core/upload.js +59 -0
- package/devtools.js +18 -8
- package/download.js +879 -60
- package/drag.js +38 -20
- package/features/frames.js +58 -0
- package/features/gif.js +203 -0
- package/features/index.js +1 -0
- package/features/mock-har.js +85 -0
- package/features/record.js +225 -0
- package/i18n/en.js +168 -16
- package/i18n/ru.js +168 -16
- package/i18n/zh.js +168 -16
- package/image-guard.js +13 -6
- package/index.js +17 -1155
- package/locale/ru.json +7 -0
- package/nav.js +43 -5
- package/package.json +8 -1
- package/panel.js +10 -1
- package/refs.js +275 -230
- package/setup.js +214 -46
- package/tools/click.js +58 -0
- package/tools/devtools.js +61 -0
- package/tools/dialog.js +22 -0
- package/tools/download.js +50 -0
- package/tools/drag.js +61 -0
- package/tools/eval.js +22 -0
- package/tools/fill.js +77 -0
- package/tools/handoff.js +40 -0
- package/tools/index.js +38 -0
- package/tools/input.js +147 -0
- package/tools/nav.js +57 -0
- package/tools/read.js +87 -0
- package/tools/record.js +120 -0
- package/tools/screenshot.js +223 -0
- package/tools/tabs.js +131 -0
- package/tools/wait.js +56 -0
- package/vendor/cb +616 -35
- package/vendor/extension/_locales/en/messages.json +3 -0
- package/vendor/extension/_locales/ru/messages.json +3 -0
- package/vendor/extension/_locales/zh_CN/messages.json +3 -0
- package/vendor/extension/_locales/zh_TW/messages.json +3 -0
- package/vendor/extension/background.js +2 -1969
- package/vendor/extension/confirm.css +148 -0
- package/vendor/extension/confirm.html +49 -0
- package/vendor/extension/confirm.js +188 -0
- package/vendor/extension/in-page-cursor.js +599 -0
- package/vendor/extension/manifest.json +11 -2
- package/vendor/extension/popup.css +99 -5
- package/vendor/extension/popup.html +28 -2
- package/vendor/extension/popup.js +430 -45
- package/vendor/extension/sw/boot.js +111 -0
- package/vendor/extension/sw/cdp.js +411 -0
- package/vendor/extension/sw/chooser.js +192 -0
- package/vendor/extension/sw/confirm.js +608 -0
- package/vendor/extension/sw/cursor.js +241 -0
- package/vendor/extension/sw/dispatch.js +129 -0
- package/vendor/extension/sw/download.js +279 -0
- package/vendor/extension/sw/fetch-hub.js +224 -0
- package/vendor/extension/sw/frames.js +651 -0
- package/vendor/extension/sw/imaging.js +361 -0
- package/vendor/extension/sw/native.js +111 -0
- package/vendor/extension/sw/net-tools.js +525 -0
- package/vendor/extension/sw/ops.js +829 -0
- package/vendor/extension/sw/popup-api.js +303 -0
- package/vendor/extension/sw/profile.js +166 -0
- package/vendor/extension/sw/record.js +494 -0
- package/vendor/extension/sw/state.js +161 -0
- package/vendor/extension/sw/tabui.js +219 -0
- package/vendor/extension/sw/util.js +166 -0
- package/vendor/extension/sw/welcome.js +26 -0
- package/vendor/extension/sw/windows.js +94 -0
- package/vendor/extension/welcome.css +189 -0
- package/vendor/extension/welcome.html +70 -0
- package/vendor/extension/welcome.js +157 -0
- package/vendor/host/host.js +476 -71
- package/vendor/in-page-cursor.js +309 -37
- package/vendor/skill/SKILL.md +34 -16
|
@@ -0,0 +1,223 @@
|
|
|
1
|
+
import { t } from '../core/locale.js'
|
|
2
|
+
import { call } from '../core/transport.js'
|
|
3
|
+
import { defineTool, present } from '../core/tooldef.js'
|
|
4
|
+
import { shotRefs, saveShotResult } from '../core/shots.js'
|
|
5
|
+
import { elementBox, locate, snapshot } from '../refs.js'
|
|
6
|
+
|
|
7
|
+
const unknownOp = (error, op) => new RegExp('(?:未知操作|unknown (?:operation|op))\\s*[::]\\s*' + op + '\\b', 'i')
|
|
8
|
+
.test(String(error && error.message || error))
|
|
9
|
+
|
|
10
|
+
function markRows(text) {
|
|
11
|
+
const rows = []
|
|
12
|
+
const byRef = new Map()
|
|
13
|
+
for (const line of String(text || '').split('\n')) {
|
|
14
|
+
const hit = /^\[(\d+)\]\s+(\S+)(.*)$/.exec(line)
|
|
15
|
+
if (!hit) continue
|
|
16
|
+
const ref = Number(hit[1])
|
|
17
|
+
const row = { ref, role: hit[2], line }
|
|
18
|
+
rows.push(row)
|
|
19
|
+
byRef.set(ref, row)
|
|
20
|
+
}
|
|
21
|
+
return { rows, byRef }
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
const MARK_BOXES_JS = (refs) => `(() => {
|
|
25
|
+
const refs = ${JSON.stringify(refs)};
|
|
26
|
+
const state = window.__dshSnap || {};
|
|
27
|
+
const out = [];
|
|
28
|
+
for (const n of refs) {
|
|
29
|
+
const el = state.refs && state.refs.get(n) && state.refs.get(n).deref();
|
|
30
|
+
if (!el || !el.isConnected || !el.ownerDocument.defaultView || state.ids && state.ids.get(el) !== n) continue;
|
|
31
|
+
const r = el.getBoundingClientRect();
|
|
32
|
+
let left = r.left, top = r.top;
|
|
33
|
+
for (let w = el.ownerDocument.defaultView; w && w !== window && w.frameElement; w = w.parent) {
|
|
34
|
+
const f = w.frameElement;
|
|
35
|
+
const fr = f.getBoundingClientRect();
|
|
36
|
+
const fs = f.ownerDocument.defaultView.getComputedStyle(f);
|
|
37
|
+
left += fr.left + f.clientLeft + (parseFloat(fs.paddingLeft) || 0);
|
|
38
|
+
top += fr.top + f.clientTop + (parseFloat(fs.paddingTop) || 0);
|
|
39
|
+
}
|
|
40
|
+
const x1 = Math.max(0, left), y1 = Math.max(0, top);
|
|
41
|
+
const x2 = Math.min(window.innerWidth, left + r.width), y2 = Math.min(window.innerHeight, top + r.height);
|
|
42
|
+
if (x2 - x1 >= 2 && y2 - y1 >= 2) out.push({ ref: n, x: x1, y: y1, width: x2 - x1, height: y2 - y1 });
|
|
43
|
+
}
|
|
44
|
+
return { marks: out, viewportWidth: window.innerWidth };
|
|
45
|
+
})()`
|
|
46
|
+
|
|
47
|
+
async function collectMarks(call, tabId) {
|
|
48
|
+
const text = await snapshot(call, tabId, { scope: 'viewport', limit: 160, maxItems: 160 })
|
|
49
|
+
const { rows, byRef } = markRows(text)
|
|
50
|
+
const refs = [...new Set(rows.map(row => row.ref))].filter(n => n < 10000).slice(0, 80)
|
|
51
|
+
if (!refs.length) return { marks: [], viewportWidth: 0, legend: '', more: 0, count: 0 }
|
|
52
|
+
const r = await call('eval', { tabId, expression: MARK_BOXES_JS(refs), userGesture: false }, 15000)
|
|
53
|
+
const marks = (r.value && Array.isArray(r.value.marks) ? r.value.marks : []).map(mark => ({
|
|
54
|
+
...mark,
|
|
55
|
+
role: byRef.get(Number(mark.ref))?.role || 'clickable',
|
|
56
|
+
}))
|
|
57
|
+
const lines = rows.slice(0, 40).map(row => row.line)
|
|
58
|
+
return {
|
|
59
|
+
marks,
|
|
60
|
+
viewportWidth: Number(r.value && r.value.viewportWidth) || 0,
|
|
61
|
+
legend: lines.join(' · '),
|
|
62
|
+
more: Math.max(0, rows.length - lines.length),
|
|
63
|
+
count: rows.length,
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
async function devicePixelRatio(call, tabId) {
|
|
68
|
+
try {
|
|
69
|
+
const r = await call('eval', { tabId, expression: 'window.devicePixelRatio || 1', userGesture: false }, 5000)
|
|
70
|
+
return Math.max(0.25, Math.min(4, Number(r.value) || 1))
|
|
71
|
+
} catch {
|
|
72
|
+
return 1
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
async function captureViewportLegacy(call, tabId, encoding, maxWidth) {
|
|
77
|
+
if (!(maxWidth > 0)) return call('shot', { tabId, ...encoding, full: false }, 90000)
|
|
78
|
+
try {
|
|
79
|
+
const metrics = (await call('cdp', { tabId, method: 'Page.getLayoutMetrics', params: {} }, 15000)).result
|
|
80
|
+
const view = metrics.cssVisualViewport || metrics.visualViewport
|
|
81
|
+
const width = Number(view && view.clientWidth)
|
|
82
|
+
const height = Number(view && view.clientHeight)
|
|
83
|
+
if (!width || !height) throw new Error('viewport')
|
|
84
|
+
const dpr = await devicePixelRatio(call, tabId)
|
|
85
|
+
const scale = Math.min(1, maxWidth / (width * dpr))
|
|
86
|
+
const shot = (await call('cdp', {
|
|
87
|
+
tabId,
|
|
88
|
+
method: 'Page.captureScreenshot',
|
|
89
|
+
params: { ...encoding, clip: { x: Number(view.pageX) || 0, y: Number(view.pageY) || 0, width, height, scale } },
|
|
90
|
+
}, 90000)).result
|
|
91
|
+
const tabInfo = (await call('tabs')).find(x => x.id === tabId)
|
|
92
|
+
return { data: shot.data, format: encoding.format, url: tabInfo && tabInfo.url, title: tabInfo && tabInfo.title }
|
|
93
|
+
} catch {
|
|
94
|
+
return call('shot', { tabId, ...encoding, full: false }, 90000)
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
async function captureElementLegacy(call, tabId, box, encoding, maxWidth) {
|
|
99
|
+
const dpr = maxWidth > 0 ? await devicePixelRatio(call, tabId) : 1
|
|
100
|
+
const scale = maxWidth > 0 ? Math.min(1, maxWidth / (Math.max(1, box.width) * dpr)) : 1
|
|
101
|
+
const capture = () => call('cdp', {
|
|
102
|
+
tabId, method: 'Page.captureScreenshot', params: { ...encoding, clip: { ...box, scale } },
|
|
103
|
+
}, 90000)
|
|
104
|
+
const first = capture()
|
|
105
|
+
first.catch(() => {})
|
|
106
|
+
let timer
|
|
107
|
+
const late = new Promise((resolve) => { timer = setTimeout(resolve, 15000, null) })
|
|
108
|
+
const shot = (await Promise.race([first, late]).finally(() => clearTimeout(timer))) || (await capture())
|
|
109
|
+
const tabInfo = (await call('tabs')).find((x) => x.id === tabId)
|
|
110
|
+
return { data: shot.result.data, format: encoding.format, url: tabInfo && tabInfo.url, title: tabInfo && tabInfo.title }
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
async function tryShotMarked(call, args) {
|
|
114
|
+
try {
|
|
115
|
+
const r = await call('shotMarked', args, 90000)
|
|
116
|
+
// 畸形回包(老扩展/未知 op 被宿主吞成空对象)按不支持处理,走回退
|
|
117
|
+
if (!r || typeof r.data !== 'string' || !r.data) return null
|
|
118
|
+
return r
|
|
119
|
+
} catch (error) {
|
|
120
|
+
if (unknownOp(error, 'shotMarked')) return null
|
|
121
|
+
throw error
|
|
122
|
+
}
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
export function buildScreenshot({ resolveTab, getAttachments, TAB, REF }) {
|
|
126
|
+
return defineTool({
|
|
127
|
+
name: 'browser_screenshot',
|
|
128
|
+
description: t('tool.screenshot.desc'),
|
|
129
|
+
parameters: {
|
|
130
|
+
mode: { type: 'string', required: true, enum: ['viewport', 'full', 'element'], description: t('tool.screenshot.mode') },
|
|
131
|
+
tab: { type: 'string', description: TAB },
|
|
132
|
+
ref: { type: 'integer', description: t('tool.screenshot.ref', { ref: REF }) },
|
|
133
|
+
selector: { type: 'string', description: t('tool.screenshot.selector') },
|
|
134
|
+
format: { type: 'string', enum: ['jpeg', 'jpg', 'png'], description: t('tool.screenshot.format') },
|
|
135
|
+
quality: { type: 'integer', minimum: 1, maximum: 100, description: t('tool.screenshot.quality') },
|
|
136
|
+
marks: { type: 'boolean', description: t('tool.screenshot.marks') },
|
|
137
|
+
maxWidth: { type: 'integer', minimum: 0, maximum: 4096, description: t('tool.screenshot.maxWidth') },
|
|
138
|
+
},
|
|
139
|
+
output: {
|
|
140
|
+
schema: {
|
|
141
|
+
type: 'object',
|
|
142
|
+
additionalProperties: false,
|
|
143
|
+
properties: {
|
|
144
|
+
text: { type: 'string' },
|
|
145
|
+
token: { type: 'string' },
|
|
146
|
+
},
|
|
147
|
+
required: ['text', 'token'],
|
|
148
|
+
},
|
|
149
|
+
render: (_args, value) => {
|
|
150
|
+
const blocks = [{ type: 'text', text: value.text }]
|
|
151
|
+
const ref = shotRefs.get(value.token)
|
|
152
|
+
if (ref) {
|
|
153
|
+
shotRefs.delete(value.token)
|
|
154
|
+
blocks.push({ type: 'image', attachment: ref })
|
|
155
|
+
}
|
|
156
|
+
return blocks
|
|
157
|
+
},
|
|
158
|
+
},
|
|
159
|
+
presentCall: present('screenshot', 'browser_screenshot'),
|
|
160
|
+
async execute(args) {
|
|
161
|
+
const tabId = await resolveTab(args.tab)
|
|
162
|
+
const format = String(args.format || '').toLowerCase() === 'png' ? 'png' : 'jpeg'
|
|
163
|
+
const isJpeg = format === 'jpeg'
|
|
164
|
+
const quality = args.quality === undefined ? 75 : Number(args.quality)
|
|
165
|
+
if (isJpeg && (!Number.isInteger(quality) || quality < 1 || quality > 100)) throw new Error(t('err.shotQuality'))
|
|
166
|
+
const maxWidth = args.maxWidth === undefined ? 1280 : Number(args.maxWidth)
|
|
167
|
+
if (!Number.isInteger(maxWidth) || maxWidth < 0 || maxWidth > 4096) throw new Error(t('err.shotWidth'))
|
|
168
|
+
const encoding = isJpeg ? { format, quality } : { format }
|
|
169
|
+
const wantMarks = args.marks === true
|
|
170
|
+
const markData = wantMarks ? await collectMarks(call, tabId) : null
|
|
171
|
+
let r
|
|
172
|
+
let usedMarked = false
|
|
173
|
+
let legacy = false
|
|
174
|
+
|
|
175
|
+
if (args.mode === 'element') {
|
|
176
|
+
const hasRef = args.ref !== undefined && args.ref !== null
|
|
177
|
+
if (!hasRef && !args.selector) throw new Error(t('err.shotNeedsTarget'))
|
|
178
|
+
let selector = args.selector
|
|
179
|
+
if (!selector && hasRef) {
|
|
180
|
+
const target = await locate(call, tabId, args.ref, { wantPoint: false })
|
|
181
|
+
selector = target.selector || null
|
|
182
|
+
}
|
|
183
|
+
if (selector) {
|
|
184
|
+
r = await tryShotMarked(call, {
|
|
185
|
+
tabId, ...encoding, selector, maxWidth,
|
|
186
|
+
})
|
|
187
|
+
legacy = !r
|
|
188
|
+
}
|
|
189
|
+
if (!r) {
|
|
190
|
+
const box = await elementBox(call, tabId, { ref: hasRef ? args.ref : undefined, selector: args.selector })
|
|
191
|
+
if (!box || box.width < 1 || box.height < 1) throw new Error(t('err.shotZeroSize'))
|
|
192
|
+
r = await captureElementLegacy(call, tabId, box, encoding, maxWidth)
|
|
193
|
+
}
|
|
194
|
+
} else {
|
|
195
|
+
r = await tryShotMarked(call, {
|
|
196
|
+
tabId,
|
|
197
|
+
...encoding,
|
|
198
|
+
full: args.mode === 'full',
|
|
199
|
+
maxWidth,
|
|
200
|
+
marks: wantMarks && markData && markData.marks.length ? markData.marks : null,
|
|
201
|
+
viewportWidth: markData && markData.viewportWidth || null,
|
|
202
|
+
})
|
|
203
|
+
legacy = !r
|
|
204
|
+
if (!r) {
|
|
205
|
+
if (args.mode === 'full') r = await call('shot', { tabId, ...encoding, full: true }, 90000)
|
|
206
|
+
else r = await captureViewportLegacy(call, tabId, encoding, maxWidth)
|
|
207
|
+
}
|
|
208
|
+
usedMarked = !!(r && r.marked)
|
|
209
|
+
}
|
|
210
|
+
|
|
211
|
+
const saved = await saveShotResult(r, { mode: args.mode, isJpeg, getAttachments })
|
|
212
|
+
if (wantMarks) {
|
|
213
|
+
const legend = markData.legend || t('res.shot.marksEmpty')
|
|
214
|
+
const more = markData.more > 0 ? t('res.shot.marksMore', { count: markData.more }) : ''
|
|
215
|
+
if (usedMarked) saved.text += t('res.shot.marksLegend', { legend, more })
|
|
216
|
+
else if (legacy) saved.text += t('res.shot.marksLegacy', { legend, more })
|
|
217
|
+
else if (args.mode === 'element') saved.text += t('res.shot.marksElement', { legend, more })
|
|
218
|
+
else saved.text += t('res.shot.marksUnavailable', { legend, more })
|
|
219
|
+
}
|
|
220
|
+
return saved
|
|
221
|
+
},
|
|
222
|
+
})
|
|
223
|
+
}
|
package/tools/tabs.js
ADDED
|
@@ -0,0 +1,131 @@
|
|
|
1
|
+
import { t } from '../core/locale.js'
|
|
2
|
+
import { call } from '../core/transport.js'
|
|
3
|
+
import { formatTabId, isMultiHost, resolveHost, scanHosts } from '../core/hosts.js'
|
|
4
|
+
import { defineTool, present, textOutput } from '../core/tooldef.js'
|
|
5
|
+
import { bindCallHost, forgetTab, getSessionHost, getSessionTab, rememberHost, rememberTab } from '../core/session.js'
|
|
6
|
+
|
|
7
|
+
function pickTargetHost(profile) {
|
|
8
|
+
const target = profile
|
|
9
|
+
? resolveHost({ profile })
|
|
10
|
+
: resolveHost({ sessionHost: getSessionHost() })
|
|
11
|
+
if (target.id) {
|
|
12
|
+
bindCallHost(target.id)
|
|
13
|
+
if (profile) rememberHost(target.id)
|
|
14
|
+
}
|
|
15
|
+
return target
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
function formatTabCol(tabId, hostId, multi) {
|
|
19
|
+
const ref = formatTabId(tabId, hostId, multi)
|
|
20
|
+
return multi ? ref : ref.padStart(5)
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
export function buildTabs({ resolveTab, userTab }) {
|
|
24
|
+
return defineTool({
|
|
25
|
+
name: 'browser_tabs',
|
|
26
|
+
description: t('tool.tabs.desc'),
|
|
27
|
+
parameters: {
|
|
28
|
+
action: {
|
|
29
|
+
type: 'string', required: true, enum: ['list', 'find', 'open', 'close', 'activate', 'profiles'],
|
|
30
|
+
description: t('tool.tabs.action'),
|
|
31
|
+
},
|
|
32
|
+
query: { type: 'string', description: t('tool.tabs.query') },
|
|
33
|
+
url: { type: 'string', description: t('tool.tabs.url') },
|
|
34
|
+
tab: { type: 'string', description: t('tool.tabs.tab') },
|
|
35
|
+
profile: { type: 'string', description: t('tool.tabs.profile') },
|
|
36
|
+
},
|
|
37
|
+
output: textOutput,
|
|
38
|
+
presentCall: present('tabs', 'browser_tabs'),
|
|
39
|
+
async execute(args) {
|
|
40
|
+
switch (args.action) {
|
|
41
|
+
case 'profiles': {
|
|
42
|
+
const { hosts } = scanHosts()
|
|
43
|
+
if (!hosts.length) return { text: t('res.tabs.profilesEmpty') }
|
|
44
|
+
const lines = hosts.map((p) => {
|
|
45
|
+
const mark = p.default ? '*' : ' '
|
|
46
|
+
const name = [p.browser || 'Chrome', p.label].filter(Boolean).join(' / ')
|
|
47
|
+
const ver = p.extVersion ? ` v${p.extVersion}` : ''
|
|
48
|
+
const tags = []
|
|
49
|
+
if (p.default) tags.push(t('res.tabs.profileDefault'))
|
|
50
|
+
if (p.focused) tags.push(t('res.tabs.profileFocused'))
|
|
51
|
+
if (!p.extConnected) tags.push(t('res.tabs.profileDisconnected'))
|
|
52
|
+
const tagStr = tags.length ? ` [${tags.join(', ')}]` : ''
|
|
53
|
+
const dupStr = p.duplicate
|
|
54
|
+
? ` (${t('res.tabs.profileDuplicate', { dropped: p.duplicate.droppedVersion || p.duplicate.droppedId })})`
|
|
55
|
+
: ''
|
|
56
|
+
return `${mark} ${p.id} ${name}${ver}${tagStr}${dupStr}`
|
|
57
|
+
})
|
|
58
|
+
return { text: `${lines.join('\n')}\n${t('res.tabs.profilesFootnote')}` }
|
|
59
|
+
}
|
|
60
|
+
case 'list': {
|
|
61
|
+
const target = pickTargetHost(args.profile)
|
|
62
|
+
const multi = isMultiHost()
|
|
63
|
+
const tabs = await call('tabs', {}, 60000, false, target.id ? { host: target.id } : {})
|
|
64
|
+
const mine = userTab(tabs)
|
|
65
|
+
const lines = tabs.map((tb) =>
|
|
66
|
+
`${tb === mine ? '*' : ' '} ${formatTabCol(tb.id, target.id, multi)} ${(tb.title || '').slice(0, 50)} — ${tb.url.slice(0, 80)}${tb.agentWindow ? t('res.tabs.agentWindow') : ''}`)
|
|
67
|
+
const text = lines.join('\n') || t('res.tabs.empty')
|
|
68
|
+
return { text: lines.length ? `${text}\n${t('res.tabs.footnote')}` : text }
|
|
69
|
+
}
|
|
70
|
+
case 'find': {
|
|
71
|
+
const target = pickTargetHost(args.profile)
|
|
72
|
+
const multi = isMultiHost()
|
|
73
|
+
const q = String(args.query || '').toLowerCase()
|
|
74
|
+
const tabs = (await call('tabs', {}, 60000, false, target.id ? { host: target.id } : {})).filter((tb) =>
|
|
75
|
+
(tb.title || '').toLowerCase().includes(q) || (tb.url || '').toLowerCase().includes(q))
|
|
76
|
+
return {
|
|
77
|
+
text:
|
|
78
|
+
tabs.map((tb) => `${formatTabCol(tb.id, target.id, multi)} ${tb.title} — ${tb.url}`).join('\n') ||
|
|
79
|
+
t('res.tabs.noMatch'),
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
case 'open': {
|
|
83
|
+
if (!args.url) throw new Error(t('err.openNeedsUrl'))
|
|
84
|
+
const target = pickTargetHost(args.profile)
|
|
85
|
+
const r = await call('open', { url: args.url }, 65000, false, target.id ? { host: target.id } : {})
|
|
86
|
+
rememberTab(r.tabId, target.id)
|
|
87
|
+
const where = r.window === 'agent' ? t('res.tabs.agentHint') : t('res.tabs.background')
|
|
88
|
+
const displayId = formatTabId(r.tabId, target.id, isMultiHost())
|
|
89
|
+
return { text: t('res.tabs.opened', { id: displayId, where, url: r.url }) }
|
|
90
|
+
}
|
|
91
|
+
case 'close': {
|
|
92
|
+
const tabSpec = args.profile && args.tab && !String(args.tab).includes(':')
|
|
93
|
+
? `${args.profile}:${args.tab}`
|
|
94
|
+
: args.tab
|
|
95
|
+
if (args.profile && !args.tab) {
|
|
96
|
+
const target = resolveHost({ profile: args.profile })
|
|
97
|
+
const cur = getSessionTab()
|
|
98
|
+
if (cur && cur.host && target.id && cur.host !== target.id) {
|
|
99
|
+
throw new Error(t('err.tabWrongProfile', { current: cur.host, target: target.id }))
|
|
100
|
+
}
|
|
101
|
+
pickTargetHost(args.profile)
|
|
102
|
+
}
|
|
103
|
+
const tabId = await resolveTab(tabSpec)
|
|
104
|
+
const hostId = getSessionHost()
|
|
105
|
+
await call('close', { tabId })
|
|
106
|
+
forgetTab(tabId, hostId)
|
|
107
|
+
return { text: t('res.tabs.closed', { id: formatTabId(tabId, hostId, isMultiHost()) }) }
|
|
108
|
+
}
|
|
109
|
+
case 'activate': {
|
|
110
|
+
const tabSpec = args.profile && args.tab && !String(args.tab).includes(':')
|
|
111
|
+
? `${args.profile}:${args.tab}`
|
|
112
|
+
: args.tab
|
|
113
|
+
if (args.profile && !args.tab) {
|
|
114
|
+
const target = resolveHost({ profile: args.profile })
|
|
115
|
+
const cur = getSessionTab()
|
|
116
|
+
if (cur && cur.host && target.id && cur.host !== target.id) {
|
|
117
|
+
throw new Error(t('err.tabWrongProfile', { current: cur.host, target: target.id }))
|
|
118
|
+
}
|
|
119
|
+
pickTargetHost(args.profile)
|
|
120
|
+
}
|
|
121
|
+
const tabId = await resolveTab(tabSpec)
|
|
122
|
+
const hostId = getSessionHost()
|
|
123
|
+
await call('activate', { tabId })
|
|
124
|
+
return { text: t('res.tabs.activated', { id: formatTabId(tabId, hostId, isMultiHost()) }) }
|
|
125
|
+
}
|
|
126
|
+
default:
|
|
127
|
+
throw new Error(t('err.unknownAction', { action: args.action }))
|
|
128
|
+
}
|
|
129
|
+
},
|
|
130
|
+
})
|
|
131
|
+
}
|
package/tools/wait.js
ADDED
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
import { t } from '../core/locale.js'
|
|
2
|
+
import { call } from '../core/transport.js'
|
|
3
|
+
import { defineTool, present, textOutput, WAIT_MAX_MS } from '../core/tooldef.js'
|
|
4
|
+
import { lastActionAt } from '../core/session.js'
|
|
5
|
+
import { waitForExtension, waitUntil } from '../nav.js'
|
|
6
|
+
|
|
7
|
+
export function buildWait({ resolveTab, TAB }) {
|
|
8
|
+
return defineTool({
|
|
9
|
+
name: 'browser_wait',
|
|
10
|
+
description: t('tool.wait.desc'),
|
|
11
|
+
parameters: {
|
|
12
|
+
selector: { type: 'string', description: t('tool.wait.selector') },
|
|
13
|
+
text: { type: 'string', description: t('tool.wait.text') },
|
|
14
|
+
url: { type: 'string', description: t('tool.wait.url') },
|
|
15
|
+
gone: { type: 'boolean', description: t('tool.wait.gone') },
|
|
16
|
+
network: { type: 'string', enum: ['idle'], description: t('tool.wait.network') },
|
|
17
|
+
responseUrl: { type: 'string', description: t('tool.wait.responseUrl') },
|
|
18
|
+
newTab: { type: 'boolean', description: t('tool.wait.newTab') },
|
|
19
|
+
tab: { type: 'string', description: TAB },
|
|
20
|
+
timeoutMs: { type: 'integer', description: t('tool.wait.timeoutMs', { max: WAIT_MAX_MS }) },
|
|
21
|
+
},
|
|
22
|
+
output: textOutput,
|
|
23
|
+
presentCall: present('wait', 'browser_wait'),
|
|
24
|
+
async execute(args) {
|
|
25
|
+
const tabId = await resolveTab(args.tab)
|
|
26
|
+
const timeoutMs = Math.min(Number(args.timeoutMs || 15000), WAIT_MAX_MS)
|
|
27
|
+
const has = (v) => v !== undefined && v !== null && v !== ''
|
|
28
|
+
const extensionWait = args.network === 'idle' || has(args.responseUrl) || args.newTab === true
|
|
29
|
+
const given = [args.selector, args.text, !args.newTab ? args.url : undefined, args.network, args.responseUrl].filter(has).length +
|
|
30
|
+
Number(args.newTab === true)
|
|
31
|
+
if (given !== 1 || (extensionWait && args.gone)) throw new Error(t('err.waitNeedsOne'))
|
|
32
|
+
if (extensionWait) return { text: await waitForExtension(call, tabId, { ...args, timeoutMs, since: lastActionAt.get(tabId) }) }
|
|
33
|
+
if (has(args.selector) && !args.gone) {
|
|
34
|
+
// 客户端超时 = 确认上限 60 秒 + 执行时限 + 传输余量
|
|
35
|
+
const r = await call('waitFor', { tabId, selector: args.selector, timeoutMs, visible: true }, timeoutMs + 65_000)
|
|
36
|
+
return {
|
|
37
|
+
text: t('res.wait.selector', {
|
|
38
|
+
sel: args.selector,
|
|
39
|
+
x: r.x,
|
|
40
|
+
y: r.y,
|
|
41
|
+
text: r.text,
|
|
42
|
+
ms: `${r.waitedMs}ms`,
|
|
43
|
+
}),
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
const text = await waitUntil(call, tabId, {
|
|
47
|
+
selector: has(args.selector) ? args.selector : undefined,
|
|
48
|
+
text: has(args.text) ? args.text : undefined,
|
|
49
|
+
url: has(args.url) ? args.url : undefined,
|
|
50
|
+
gone: !!args.gone,
|
|
51
|
+
timeoutMs,
|
|
52
|
+
})
|
|
53
|
+
return { text }
|
|
54
|
+
},
|
|
55
|
+
})
|
|
56
|
+
}
|