dsh-chrome-bridge 1.19.0 → 1.20.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/i18n/zh.js CHANGED
@@ -164,4 +164,148 @@ export default {
164
164
  'hint.copiedWin': '粘进文件框顶上的地址栏、回车',
165
165
  'hint.notes': '\n(%notes)',
166
166
  'hint.notesSep': ';',
167
+ 'err.navFirstPage': '已经是第一页了,没法后退',
168
+ 'err.navLastPage': '已经是最后一页了,没法前进',
169
+ 'res.wait.whatText': '文字「%text」',
170
+ 'res.wait.whatUrl': '网址包含「%url」',
171
+ 'res.wait.found': '等到了',
172
+ 'res.wait.goneState': '已经没有',
173
+ 'res.wait.ok': '%state %what,用了 %ms',
174
+ 'res.wait.still': '还在',
175
+ 'res.wait.notYet': '还没出现',
176
+ 'err.waitTimeout': '等了 %ms,%what %state',
177
+ 'err.panelTooLarge': '请求太大',
178
+ 'err.panelNotJson': '不是 JSON',
179
+ 'res.devtools.consoleEmpty': '(控制台没有输出)',
180
+ 'res.devtools.consoleNoMatch': '(没有匹配的条目)',
181
+ 'res.devtools.uncaught': '[未捕获] ',
182
+ 'res.devtools.consoleHead': '最近 %shown 条(共 %total 条)',
183
+ 'res.devtools.consoleCount': '%n 条',
184
+ 'res.devtools.netEmptyFiltered': '(没有匹配的请求)',
185
+ 'res.devtools.netEmpty': '(还没记到请求——只记调试器接上之后的;传 reload=true 刷新一次就能抓到页面加载的全部请求)',
186
+ 'res.devtools.netHead': '最近 %shown 个请求(共 %total 个)',
187
+ 'res.devtools.netCount': '%n 个请求',
188
+ 'res.devtools.netHintResponse': ';看返回内容:action=response,request 传 # 后面的编号',
189
+ 'res.devtools.netHintOldExt': ';扩展版本旧,看不了返回内容——重载一下扩展',
190
+ 'err.devtools.requestNeeded': 'action=response 需要 request(action=network 列出的 # 编号)',
191
+ 'err.devtools.noBody': '拿不到这个请求的返回内容(太早了被清掉、请求还没完成,或调试器断开过)——action=network reload=true 重抓一次',
192
+ 'res.devtools.binary': '(二进制内容,约 %size——不是文字)',
193
+ 'res.devtools.truncated': '\n…(截断,全文 %n 字)',
194
+ 'res.devtools.empty': '(空)',
195
+ 'res.devtools.cleared': '已清空控制台和网络记录',
196
+ 'err.dlTooManyNames': '%dir 里同名文件太多了:%name',
197
+ 'err.dlHttp': '下载失败:服务器回 HTTP %status',
198
+ 'err.dlPageGone': '页面跳走了,下载中断',
199
+ 'err.dlRedirects': '下载失败:重定向太多次',
200
+ 'err.dlNeedsTarget': '要给出 url、ref 或 selector 之一',
201
+ 'err.dlNoAnswer': '页面没回应(可能刚跳转,重试一次)',
202
+ 'err.dlRefGone': '编号 %ref 已失效——重新 browser_read what=snapshot',
203
+ 'err.noElement': '没找到元素: %sel',
204
+ 'err.dlNoHref': '这个 %tag 元素没有链接地址(href / src)。是按钮的话,先 browser_click 它,看会不会打开下载链接',
205
+ 'err.dlBadUrl': '网址不对:%raw',
206
+ 'err.dlBadProto': '不支持这种网址:%proto',
207
+ 'err.dlBlobDead': '页面里读不出这个文件(blob 链接可能已经失效:重新点一次生成它的按钮再试)',
208
+ 'res.drag.label': '拖拽 %from → %to',
209
+ 'res.drag.quotedText': '「%text」',
210
+ 'err.dragNoAnswer': '页面没回话(可能刚跳转,重试一次)',
211
+ 'err.dragNotFound': '没找到%end %desc',
212
+ 'err.dragRefStale': '(编号可能已失效,重新 snapshot)',
213
+ 'err.dragSame': '起点 %from 和终点 %to 找到的是同一个元素——换个更具体的写法(ref 或 selector)',
214
+ 'err.dragOffscreen': '终点 %to 不在当前屏幕里——先滚动让起点和终点都露出来',
215
+ 'err.dragNoTarget': '终点那里没有元素',
216
+ 'res.drag.doneHtml5': '已拖拽 %from → %to(HTML5 拖放%verdict)',
217
+ 'res.drag.accepted': ',目标接受了',
218
+ 'res.drag.notAccepted': ',但目标没表示接受——看看结果对不对',
219
+ 'res.drag.doneCoords': '已拖拽 %from → %to:(%x1, %y1) → (%x2, %y2)',
220
+ 'err.refGone': '编号 %ref 已失效(页面变了或跳转过)——重新 browser_read what=snapshot 拿新编号',
221
+ 'err.snapshotFailed': '页面没返回快照(可能刚跳转,重试一次)',
222
+ 'res.snap.head': '可操作元素 %count 个([N] 是编号:browser_click / browser_input 传 ref=N)',
223
+ 'res.snap.nothing': '(没找到可操作的元素)',
224
+ 'res.snap.more': '\n…还有 %more 个元素没列出——传 selector 只看页面的一部分',
225
+ 'res.snap.truncated': '\n…(截断,全文 %n 字——传 selector 缩小范围,或调大 limit)',
226
+ 'err.refNotInt': '编号要是正整数,收到的是 %ref',
227
+ 'err.refFocus': '编号 %n 聚焦失败(元素可能不可聚焦)',
228
+ 'err.selectNotSelect': '不是 <select> 元素',
229
+ 'err.selectNoOption': '没有这个选项',
230
+ 'err.selectFailed': '选择失败:%why%options',
231
+ 'err.selectOptions': '(可选值:%options)',
232
+ 'res.hint.quoted': '「%text」',
233
+ 'res.hint.typing': '打字 %n 字',
234
+ 'res.hint.key': '按键 %key',
235
+ 'res.hint.element': '元素 %el',
236
+ 'res.hint.waitGone': '等消失 ',
237
+ 'res.hint.url': '网址 %url',
238
+ 'res.tabs.agentWindow': '(DSH 干活窗口)',
239
+ 'res.tabs.empty': '(没有标签页)',
240
+ 'res.tabs.footnote': '(* = 用户正在看的页)',
241
+ 'res.tabs.noMatch': '(没有匹配的标签页)',
242
+ 'res.tabs.agentHint': '(在单独的 DSH 干活窗口里,后台打开,没打扰用户正在看的页)',
243
+ 'res.tabs.background': '(后台标签页)',
244
+ 'res.tabs.opened': '已打开标签页 %id%where:%url',
245
+ 'res.tabs.closed': '已关闭标签页 %id',
246
+ 'res.tabs.activated': '已切到标签页 %id',
247
+ 'res.read.iframeCross': '\n\n[iframe] %src(跨站框架,读不到里面的字:browser_screenshot 看)',
248
+ 'res.read.noText': '(页面没有可见文字)',
249
+ 'res.read.noAttr': '(没有这个属性)',
250
+ 'res.read.viewport': ' | 视口 %w×%h,已滚动 %scrollYpx',
251
+ 'res.read.dialog': '\n⚠️ 有 %type 弹窗挡着:「%message」——先调 browser_dialog 放行',
252
+ 'res.click.atRef': '已点击 [%ref] %tag「%text」@ (%x, %y)',
253
+ 'res.click.atRefCount': '已点击 [%ref] %tag「%text」@ (%x, %y) ×%count',
254
+ 'res.click.byTag': '已点击 %tag「%text」@ (%x, %y)',
255
+ 'res.click.atCoords': '已点击坐标 (%x, %y)',
256
+ 'res.input.typedAfterClick': '已输入 %n 个字符(先点了 %target)',
257
+ 'res.input.typed': '已输入 %n 个字符',
258
+ 'res.input.typedAfterFocus': '(先聚焦了 %target)',
259
+ 'res.input.pressedKey': '已按键 %key',
260
+ 'res.input.selected': '已选择「%label」(%value)',
261
+ 'res.input.uploaded': '已把 %n 个文件塞进 %target',
262
+ 'res.input.uploadedChooser': '已把 %n 个文件交给页面刚才打开的选文件框',
263
+ 'res.input.focused': '已聚焦 %target',
264
+ 'res.input.hovered': '已悬停 @ (%x, %y)',
265
+ 'res.input.hoveredEl': ' %tag「%text」',
266
+ 'res.input.scrolled': '已滚动 %dy,当前 scrollY=%scrollY / 页高 %pageHeight',
267
+ 'res.nav.late': '(页面还没加载完,需要的话 browser_wait 等一下)',
268
+ 'res.nav.subtitle': ' — %title',
269
+ 'res.nav.go': '已到 %url%title%late',
270
+ 'res.nav.history': '%verb %url%title%late',
271
+ 'res.nav.back': '已后退到',
272
+ 'res.nav.forward': '已前进到',
273
+ 'res.nav.reload': '已刷新',
274
+ 'res.shot.note': '截图(%mode,%size)— %where',
275
+ 'res.shot.saved': '\n(附件库不可用,已存到 %file,可以用 read_image 看)',
276
+ 'res.wait.selector': '等到了 %sel @ (%x, %y)「%text」,用了 %ms',
277
+ 'res.dialog.accepted': '已放行弹窗',
278
+ 'res.dialog.dismissed': '已取消弹窗',
279
+ 'res.dl.warnHtml': '\n⚠️ 下到的是网页(text/html),多半是登录页或「下载即将开始」的中转页,不是文件本身:打开看看,或者换成真正的文件链接',
280
+ 'res.dl.done': '已下载:%file(%size%type)%warn',
281
+ 'res.dl.typePart': ',%type',
282
+ 'setup.noVendor': '插件包里没有自带宿主:请重新用 release tarball 安装本插件(README 的 Install 一节)。',
283
+ 'setup.unsupportedPlatform': '浏览器桥暂不支持 %platform(只支持 macOS、Windows、Linux)。',
284
+ 'setup.regHead': '宿主或 native messaging 注册没写成功(多半是权限问题):',
285
+ 'setup.regHostDir': '宿主目录 %dir%mark',
286
+ 'setup.regNm': '注册%kind %file%mark',
287
+ 'setup.nmKindWin': '(注册表 + 文件)',
288
+ 'setup.nmKindFile': '文件',
289
+ 'setup.regTail': '重启一次 DSH 会再试一遍;还不行就在 GitHub 提 issue。',
290
+ 'setup.launchFailed': 'Chrome 拉起宿主失败:%err\n重启 DSH 之后,完全退出 Chrome 再打开一次。',
291
+ 'setup.storeHead': '扩展已经从 Chrome 应用商店装好了(DSH Bridge %version),但宿主没连上:',
292
+ 'setup.storeStep1': '1. 确认 Chrome 开着',
293
+ 'setup.storeStep2': '2. chrome://extensions 里 DSH Bridge 是启用状态',
294
+ 'setup.storeStep3': '3. 还不行就完全退出 Chrome 再打开一次',
295
+ 'setup.storeTail': '不用再「加载已解压」;宿主和 native 注册已经自动装好了。',
296
+ 'setup.manualHead': '浏览器桥还差最后一步(一次性),两种装法选一个:',
297
+ 'setup.manualStore': '· 最省事:从 Chrome 应用商店装 DSH Bridge(不用开发者模式):',
298
+ 'setup.storeUrl': ' %url',
299
+ 'setup.manualUnpacked': '· 或者加载已解压的扩展:',
300
+ 'setup.manualStep1': ' 1. Chrome 打开 chrome://extensions',
301
+ 'setup.manualStep2': ' 2. 右上角打开「开发者模式」',
302
+ 'setup.manualStep3': ' 3. 点「加载已解压的扩展程序」,%paste,选这个文件夹:',
303
+ 'setup.manualDir': ' %dir',
304
+ 'setup.manualTail': '装好后回到这里重试;宿主和 native 注册已经自动装好了,不用再跑任何脚本。',
305
+ 'setup.pasteDarwin': '在文件框里按 ⌘⇧G、粘贴、回车',
306
+ 'setup.pasteWin': '把路径粘进文件框顶上的地址栏、回车',
307
+ 'setup.pasteLinux': '在文件框里按 Ctrl+L、粘贴、回车',
308
+ 'res.read.truncated': '\n…(截断,全文 %n 字)',
309
+ 'err.setupProcList': '列进程没有跑完',
310
+ 'err.chromeNotFound': '找不到 Chrome',
167
311
  }
package/index.js CHANGED
@@ -24,7 +24,7 @@ import { historyNav, waitUntil } from './nav.js'
24
24
  import { PANEL_ROUTE, panelHandler } from './panel.js'
25
25
  import { drag } from './drag.js'
26
26
  import { download, downloadsDir, humanSize } from './download.js'
27
- import { createT, DEFAULT_LOCALE, normalizeLocale } from './i18n/index.js'
27
+ import { createT, setLocale, DEFAULT_LOCALE, normalizeLocale } from './i18n/index.js'
28
28
 
29
29
  /**
30
30
  * 这里刻意不 import 任何外部包——不碰 `@deepseek-ai/dsh-tools`。
@@ -117,8 +117,8 @@ function probeExec(exec) {
117
117
  }
118
118
 
119
119
  const short = (v, n = 40) => {
120
- const t = String(v ?? '').replace(/\s+/g, ' ').trim()
121
- return t.length > n ? t.slice(0, n - 1) + '…' : t
120
+ const s = String(v ?? '').replace(/\s+/g, ' ').trim()
121
+ return s.length > n ? s.slice(0, n - 1) + '…' : s
122
122
  }
123
123
 
124
124
  /** 一眼看得懂的一句话:「[3]」「「加载完成」」「back」「https://…」 */
@@ -129,7 +129,7 @@ function hintOf(name, a = {}) {
129
129
  const sel = a[p ? `${p}Selector` : 'selector']
130
130
  if (sel) return short(sel, 30)
131
131
  const text = a[p ? `${p}Text` : 'text']
132
- if (text) return `「${short(text, 20)}」`
132
+ if (text) return t('res.hint.quoted', { text: short(text, 20) })
133
133
  const x = a[p ? `${p}X` : 'x']
134
134
  const y = a[p ? `${p}Y` : 'y']
135
135
  return x !== undefined && y !== undefined ? `(${x}, ${y})` : ''
@@ -140,14 +140,20 @@ function hintOf(name, a = {}) {
140
140
  case 'browser_click': return el()
141
141
  case 'browser_input':
142
142
  // 只记字数:这句会显示在扩展弹窗的「最近动作」里,打的可能是密码
143
- return a.action === 'type' ? `打字 ${String(a.text ?? '').length} 字${a.ref != null || a.selector ? ` → ${el()}` : ''}`
144
- : a.action === 'key' ? `按键 ${a.key || ''}`
143
+ return a.action === 'type'
144
+ ? t('res.hint.typing', { n: String(a.text ?? '').length }) + (a.ref != null || a.selector ? ` → ${el()}` : '')
145
+ : a.action === 'key' ? t('res.hint.key', { key: a.key || '' })
145
146
  : `${a.action} ${el()}`.trim()
146
147
  case 'browser_nav': return (a.action && a.action !== 'go') ? a.action : short(a.url, 50)
147
- case 'browser_screenshot': return a.mode === 'element' ? `元素 ${el()}` : a.mode || ''
148
+ case 'browser_screenshot': return a.mode === 'element' ? t('res.hint.element', { el: el() }) : a.mode || ''
148
149
  case 'browser_eval': return short(a.expression, 40)
149
150
  case 'browser_wait':
150
- return `${a.gone ? '等消失 ' : ''}${a.text ? `「${short(a.text, 20)}」` : a.url ? `网址 ${short(a.url, 30)}` : short(a.selector, 30)}`
151
+ return (
152
+ (a.gone ? t('res.hint.waitGone') : '') +
153
+ (a.text
154
+ ? t('res.hint.quoted', { text: short(a.text, 20) })
155
+ : a.url ? t('res.hint.url', { url: short(a.url, 30) }) : short(a.selector, 30))
156
+ )
151
157
  case 'browser_dialog': return a.action || ''
152
158
  case 'browser_drag': return `${el('from')} → ${el('to')}`
153
159
  case 'browser_download': return a.ref !== undefined && a.ref !== null ? `[${a.ref}]` : short(a.selector || a.url || '', 50)
@@ -280,9 +286,9 @@ function forgetTab(id) {
280
286
  * 再挑不在 DSH 干活窗口里的。老扩展不给 focused / agentWindow,就退回第一个 active(以前的做法)。
281
287
  */
282
288
  function userTab(tabs) {
283
- return tabs.find((t) => t.active && t.focused) ||
284
- tabs.find((t) => t.active && !t.agentWindow) ||
285
- tabs.find((t) => t.active)
289
+ return tabs.find((tb) => tb.active && tb.focused) ||
290
+ tabs.find((tb) => tb.active && !tb.agentWindow) ||
291
+ tabs.find((tb) => tb.active)
286
292
  }
287
293
 
288
294
  async function resolveTab(tab) {
@@ -300,7 +306,7 @@ async function resolveTab(tab) {
300
306
  }
301
307
  const id = currentTab.get(sessionKey())
302
308
  if (id !== undefined) {
303
- if ((await call('tabs')).some((t) => t.id === id)) return id
309
+ if ((await call('tabs')).some((tb) => tb.id === id)) return id
304
310
  currentTab.delete(sessionKey())
305
311
  }
306
312
  throw new Error(t('err.noTab'))
@@ -396,7 +402,7 @@ const withCursor = (extra = {}) => {
396
402
  return script ? { ...extra, script } : extra
397
403
  }
398
404
 
399
- const clip = (s, n = 6000) => (s.length > n ? s.slice(0, n) + `\n…(截断,全文 ${s.length} 字)` : s)
405
+ const clip = (s, n = 6000) => (s.length > n ? s.slice(0, n) + t('res.read.truncated', { n: s.length }) : s)
400
406
 
401
407
  /** DSH 的会话投影服务(读会话现在的工作目录用);apply 里 inject 到了才有 */
402
408
  let projections = null
@@ -501,7 +507,7 @@ const shotRefs = new Map()
501
507
  export function apply(ctx, config) {
502
508
  // 工具描述是写进模型上下文的那一层,注册那一刻就得是最终语言:
503
509
  // config.locale 给 en / ru,缺省或认不出就是 zh(见 i18n/index.js)。
504
- t = createT(config && config.locale)
510
+ t = setLocale(config && config.locale)
505
511
  trace(`apply 被调用(locale: ${normalizeLocale(config && config.locale)})`)
506
512
 
507
513
  // 一次性安装:宿主 + native 注册自动落地,用户只剩把扩展装进 Chrome(加载已解压,或商店装)
@@ -572,36 +578,38 @@ export function apply(ctx, config) {
572
578
  case 'list': {
573
579
  const tabs = await call('tabs')
574
580
  const mine = userTab(tabs)
575
- const lines = tabs.map((t) =>
576
- `${t === mine ? '*' : ' '} ${String(t.id).padStart(5)} ${(t.title || '').slice(0, 50)} — ${t.url.slice(0, 80)}${t.agentWindow ? '(DSH 干活窗口)' : ''}`)
577
- const text = lines.join('\n') || '(没有标签页)'
578
- return { text: lines.length ? `${text}\n(* = 用户正在看的页)` : text }
581
+ const lines = tabs.map((tb) =>
582
+ `${tb === mine ? '*' : ' '} ${String(tb.id).padStart(5)} ${(tb.title || '').slice(0, 50)} — ${tb.url.slice(0, 80)}${tb.agentWindow ? t('res.tabs.agentWindow') : ''}`)
583
+ const text = lines.join('\n') || t('res.tabs.empty')
584
+ return { text: lines.length ? `${text}\n${t('res.tabs.footnote')}` : text }
579
585
  }
580
586
  case 'find': {
581
587
  const q = String(args.query || '').toLowerCase()
582
- const tabs = (await call('tabs')).filter((t) =>
583
- (t.title || '').toLowerCase().includes(q) || (t.url || '').toLowerCase().includes(q))
588
+ const tabs = (await call('tabs')).filter((tb) =>
589
+ (tb.title || '').toLowerCase().includes(q) || (tb.url || '').toLowerCase().includes(q))
584
590
  return {
585
- text: tabs.map((t) => `${String(t.id).padStart(5)} ${t.title} — ${t.url}`).join('\n') || '(没有匹配的标签页)',
591
+ text:
592
+ tabs.map((tb) => `${String(tb.id).padStart(5)} ${tb.title} — ${tb.url}`).join('\n') ||
593
+ t('res.tabs.noMatch'),
586
594
  }
587
595
  }
588
596
  case 'open': {
589
597
  if (!args.url) throw new Error(t('err.openNeedsUrl'))
590
598
  const r = await call('open', { url: args.url })
591
599
  rememberTab(r.tabId)
592
- const where = r.window === 'agent' ? '(在单独的 DSH 干活窗口里,后台打开,没打扰用户正在看的页)' : '(后台标签页)'
593
- return { text: `已打开标签页 ${r.tabId}${where}:${r.url}` }
600
+ const where = r.window === 'agent' ? t('res.tabs.agentHint') : t('res.tabs.background')
601
+ return { text: t('res.tabs.opened', { id: r.tabId, where, url: r.url }) }
594
602
  }
595
603
  case 'close': {
596
604
  const tabId = await resolveTab(args.tab)
597
605
  await call('close', { tabId })
598
606
  forgetTab(tabId)
599
- return { text: `已关闭标签页 ${tabId}` }
607
+ return { text: t('res.tabs.closed', { id: tabId }) }
600
608
  }
601
609
  case 'activate': {
602
610
  const tabId = await resolveTab(args.tab)
603
611
  await call('activate', { tabId })
604
- return { text: `已切到标签页 ${tabId}` }
612
+ return { text: t('res.tabs.activated', { id: tabId }) }
605
613
  }
606
614
  default:
607
615
  throw new Error(t('err.unknownAction', { action: args.action }))
@@ -636,11 +644,11 @@ export function apply(ctx, config) {
636
644
  if (!args.selector) {
637
645
  for (const f of await frameTexts(call, tabId).catch(() => [])) {
638
646
  text += f.cross
639
- ? `\n\n[iframe] ${f.src}(跨站框架,读不到里面的字:browser_screenshot 看)`
647
+ ? t('res.read.iframeCross', { src: f.src })
640
648
  : f.text ? `\n\n[iframe] ${f.src}\n${f.text}` : ''
641
649
  }
642
650
  }
643
- return { text: clip(text.trim(), limit) || '(页面没有可见文字)' }
651
+ return { text: clip(text.trim(), limit) || t('res.read.noText') }
644
652
  }
645
653
  case 'html': {
646
654
  const r = await call('html', { tabId, selector: args.selector || 'body', limit })
@@ -649,13 +657,22 @@ export function apply(ctx, config) {
649
657
  case 'attr': {
650
658
  if (!args.selector || !args.name) throw new Error(t('err.attrNeeds'))
651
659
  const r = await call('attr', { tabId, selector: args.selector, name: args.name })
652
- return { text: r.value === null ? '(没有这个属性)' : String(r.value) }
660
+ return { text: r.value === null ? t('res.read.noAttr') : String(r.value) }
653
661
  }
654
662
  case 'info': {
655
663
  const r = await call('info', { tabId })
656
- const v = r.viewport ? ` | 视口 ${r.viewport.w}×${r.viewport.h},已滚动 ${r.viewport.scrollY}px` : ''
664
+ const v = r.viewport
665
+ ? t('res.read.viewport', {
666
+ w: r.viewport.w,
667
+ h: r.viewport.h,
668
+ scrollYpx: `${r.viewport.scrollY}px`,
669
+ })
670
+ : ''
657
671
  const d = r.pendingDialog
658
- ? `\n⚠️ 有 ${r.pendingDialog.type} 弹窗挡着:「${r.pendingDialog.message}」——先调 browser_dialog 放行`
672
+ ? t('res.read.dialog', {
673
+ type: r.pendingDialog.type,
674
+ message: r.pendingDialog.message,
675
+ })
659
676
  : ''
660
677
  return { text: `${r.title}\n${r.url}${v}${d}` }
661
678
  }
@@ -688,23 +705,48 @@ export function apply(ctx, config) {
688
705
  const el = await locate(call, tabId, args.ref, { wantPoint: count > 1 })
689
706
  if (el.selector && count === 1) {
690
707
  const r = await call('clickEl', { tabId, selector: el.selector, ...withCursor(), chooser: true })
691
- return { text: `已点击 [${args.ref}] ${r.target.tag}「${r.target.text}」@ (${r.x}, ${r.y})${chooserNote(r, tabId)}` }
708
+ return {
709
+ text: t('res.click.atRef', {
710
+ ref: args.ref,
711
+ tag: r.target.tag,
712
+ text: r.target.text,
713
+ x: r.x,
714
+ y: r.y,
715
+ }) + chooserNote(r, tabId),
716
+ }
692
717
  }
693
718
  // shadow DOM / iframe 里的元素(选择器够不着),或者要双击:按坐标点
694
719
  const r = await call('click', { tabId, x: el.x, y: el.y, clickCount: count, ...withCursor(), chooser: true })
695
- return { text: `已点击 [${args.ref}] ${el.tag}「${el.text}」@ (${r.x}, ${r.y})${count > 1 ? ` ×${count}` : ''}${chooserNote(r, tabId)}` }
720
+ return {
721
+ text:
722
+ (count > 1
723
+ ? t('res.click.atRefCount', {
724
+ ref: args.ref,
725
+ tag: el.tag,
726
+ text: el.text,
727
+ x: r.x,
728
+ y: r.y,
729
+ count,
730
+ })
731
+ : t('res.click.atRef', { ref: args.ref, tag: el.tag, text: el.text, x: r.x, y: r.y })) +
732
+ chooserNote(r, tabId),
733
+ }
696
734
  }
697
735
  if (args.selector) {
698
736
  const r = await call('clickEl', { tabId, selector: args.selector, ...withCursor(), chooser: true })
699
- return { text: `已点击 ${r.target.tag}「${r.target.text}」@ (${r.x}, ${r.y})${chooserNote(r, tabId)}` }
737
+ return {
738
+ text: t('res.click.byTag', { tag: r.target.tag, text: r.target.text, x: r.x, y: r.y }) + chooserNote(r, tabId),
739
+ }
700
740
  }
701
741
  if (args.text) {
702
742
  const r = await call('clickText', { tabId, text: args.text, exact: false, ...withCursor(), chooser: true })
703
- return { text: `已点击 ${r.target.tag}「${r.target.text}」@ (${r.x}, ${r.y})${chooserNote(r, tabId)}` }
743
+ return {
744
+ text: t('res.click.byTag', { tag: r.target.tag, text: r.target.text, x: r.x, y: r.y }) + chooserNote(r, tabId),
745
+ }
704
746
  }
705
747
  if (args.x !== undefined && args.y !== undefined) {
706
748
  const r = await call('click', { tabId, x: Number(args.x), y: Number(args.y), clickCount: count, ...withCursor(), chooser: true })
707
- return { text: `已点击坐标 (${r.x}, ${r.y})${chooserNote(r, tabId)}` }
749
+ return { text: t('res.click.atCoords', { x: r.x, y: r.y }) + chooserNote(r, tabId) }
708
750
  }
709
751
  throw new Error(t('err.clickNeeds'))
710
752
  },
@@ -751,25 +793,29 @@ export function apply(ctx, config) {
751
793
  if (el && el.shadow) {
752
794
  await call('click', { tabId, x: el.x, y: el.y, ...withCursor() })
753
795
  const r = await call('type', { tabId, text: args.text })
754
- return { text: `已输入 ${r.typed} 个字符(先点了 ${target})` }
796
+ return { text: t('res.input.typedAfterClick', { n: r.typed, target }) }
755
797
  }
756
798
  const r = await call('type', { tabId, text: args.text, selector: selector || undefined, ...withCursor() })
757
- return { text: `已输入 ${r.typed} 个字符${selector ? `(先聚焦了 ${target})` : ''}` }
799
+ return {
800
+ text:
801
+ t('res.input.typed', { n: r.typed }) +
802
+ (selector ? t('res.input.typedAfterFocus', { target }) : ''),
803
+ }
758
804
  }
759
805
  case 'key': {
760
806
  if (!args.key) throw new Error(t('err.keyNeedsKey'))
761
807
  const r = await call('key', { tabId, key: args.key, chooser: true })
762
- return { text: `已按键 ${args.key}${chooserNote(r, tabId)}` }
808
+ return { text: t('res.input.pressedKey', { key: args.key }) + chooserNote(r, tabId) }
763
809
  }
764
810
  case 'select': {
765
811
  if (!selector && !el) throw new Error(t('err.selectNeedsTarget'))
766
812
  if (args.value === undefined) throw new Error(t('err.selectNeedsValue'))
767
813
  if (el && el.shadow) {
768
814
  const r = await selectInPage(call, tabId, args.ref, args.value)
769
- return { text: `已选择「${r.label}」(${r.value})` }
815
+ return { text: t('res.input.selected', { label: r.label, value: r.value }) }
770
816
  }
771
817
  const r = await call('select', { tabId, selector, value: args.value })
772
- return { text: `已选择「${r.label}」(${r.value})` }
818
+ return { text: t('res.input.selected', { label: r.label, value: r.value }) }
773
819
  }
774
820
  case 'upload': {
775
821
  if (!args.files?.length) throw new Error(t('err.uploadNeedsFiles'))
@@ -784,17 +830,17 @@ export function apply(ctx, config) {
784
830
  if (!r || !r.chooser) throw new Error(t('err.uploadNeedsTarget'))
785
831
  // 等回包时这个会话可能又点开了新框(存了新 id):只删这次用掉的
786
832
  if (chooserIds.get(key) === used) chooserIds.delete(key)
787
- return { text: `已把 ${r.files.length} 个文件交给页面刚才打开的选文件框` }
833
+ return { text: t('res.input.uploadedChooser', { n: r.files.length }) }
788
834
  }
789
835
  let r
790
836
  try { r = await call('upload', { tabId, selector, files }) } catch (e) { throw uploadError(e) }
791
- return { text: `已把 ${r.files.length} 个文件塞进 ${target}` }
837
+ return { text: t('res.input.uploaded', { n: r.files.length, target }) }
792
838
  }
793
839
  case 'focus': {
794
840
  if (!selector && !el) throw new Error(t('err.focusNeedsTarget'))
795
841
  if (el && el.shadow) await focusShadow(call, tabId, args.ref)
796
842
  else await call('focus', { tabId, selector })
797
- return { text: `已聚焦 ${target}` }
843
+ return { text: t('res.input.focused', { target }) }
798
844
  }
799
845
  case 'hover': {
800
846
  const byPoint = el && el.shadow
@@ -808,11 +854,21 @@ export function apply(ctx, config) {
808
854
  selector: byPoint ? undefined : selector || undefined,
809
855
  ...withCursor(),
810
856
  })
811
- return { text: `已悬停 @ (${r.x}, ${r.y})${r.target ? ` ${r.target.tag}「${r.target.text}」` : ''}` }
857
+ return {
858
+ text:
859
+ t('res.input.hovered', { x: r.x, y: r.y }) +
860
+ (r.target ? t('res.input.hoveredEl', { tag: r.target.tag, text: r.target.text }) : ''),
861
+ }
812
862
  }
813
863
  case 'scroll': {
814
864
  const r = await call('scroll', { tabId, deltaY: Number(args.dy ?? 600), deltaX: Number(args.dx || 0) })
815
- return { text: `已滚动 ${r.deltaY}px,当前 scrollY=${r.scrollY} / 页高 ${r.pageHeight}` }
865
+ return {
866
+ text: t('res.input.scrolled', {
867
+ dy: `${r.deltaY}px`,
868
+ scrollY: r.scrollY,
869
+ pageHeight: r.pageHeight,
870
+ }),
871
+ }
816
872
  }
817
873
  default:
818
874
  throw new Error(t('err.unknownAction', { action: args.action }))
@@ -840,14 +896,31 @@ export function apply(ctx, config) {
840
896
  if (action === 'go') {
841
897
  if (!args.url) throw new Error(t('err.navNeedsUrl'))
842
898
  const r = await call('nav', { tabId, url: args.url, wait: true }, 60000)
843
- const late = r.loaded === false ? '(页面还没加载完,需要的话 browser_wait 等一下)' : ''
844
- return { text: `已到 ${r.url}${r.title ? ` — ${r.title}` : ''}${late}` }
899
+ const late = r.loaded === false ? t('res.nav.late') : ''
900
+ return {
901
+ text: t('res.nav.go', {
902
+ url: r.url,
903
+ title: r.title ? t('res.nav.subtitle', { title: r.title }) : '',
904
+ late,
905
+ }),
906
+ }
845
907
  }
846
908
  const r = await historyNav(call, tabId, action)
847
- const verb = { back: '已后退到', forward: '已前进到', reload: '已刷新' }[action]
909
+ const verb = {
910
+ back: t('res.nav.back'),
911
+ forward: t('res.nav.forward'),
912
+ reload: t('res.nav.reload'),
913
+ }[action]
848
914
  if (!verb) throw new Error(t('err.unknownAction', { action }))
849
- const late = r.done ? '' : '(页面还没加载完,需要的话 browser_wait 等一下)'
850
- return { text: `${verb} ${r.url}${r.title ? ` — ${r.title}` : ''}${late}` }
915
+ const late = r.done ? '' : t('res.nav.late')
916
+ return {
917
+ text: t('res.nav.history', {
918
+ verb,
919
+ url: r.url,
920
+ title: r.title ? t('res.nav.subtitle', { title: r.title }) : '',
921
+ late,
922
+ }),
923
+ }
851
924
  },
852
925
  }),
853
926
 
@@ -910,7 +983,7 @@ export function apply(ctx, config) {
910
983
 
911
984
  const buf = Buffer.from(r.data, 'base64')
912
985
  const where = `${r.title || ''} ${r.url || ''}`.trim()
913
- const note = `截图(${args.mode},${Math.round(buf.length / 1024)}KB)— ${where}`
986
+ const note = t('res.shot.note', { mode: args.mode, size: `${Math.round(buf.length / 1024)}KB`, where })
914
987
 
915
988
  const attachments = ctx.get('attachments')
916
989
  if (attachments) {
@@ -932,7 +1005,7 @@ export function apply(ctx, config) {
932
1005
  }
933
1006
  const file = path.join(os.tmpdir(), `dsh-shot-${Date.now()}.${String(args.format || '').toLowerCase().startsWith('j') ? 'jpg' : 'png'}`)
934
1007
  fs.writeFileSync(file, buf)
935
- return { text: `${note}\n(附件库不可用,已存到 ${file},可以用 read_image 看)`, token: '' }
1008
+ return { text: note + t('res.shot.saved', { file }), token: '' }
936
1009
  },
937
1010
  }),
938
1011
 
@@ -976,7 +1049,15 @@ export function apply(ctx, config) {
976
1049
  if (given !== 1) throw new Error(t('err.waitNeedsOne'))
977
1050
  if (has(args.selector) && !args.gone) {
978
1051
  const r = await call('waitFor', { tabId, selector: args.selector, timeoutMs, visible: true }, timeoutMs + 15000)
979
- return { text: `等到了 ${args.selector} @ (${r.x}, ${r.y})「${r.text}」,用了 ${r.waitedMs}ms` }
1052
+ return {
1053
+ text: t('res.wait.selector', {
1054
+ sel: args.selector,
1055
+ x: r.x,
1056
+ y: r.y,
1057
+ text: r.text,
1058
+ ms: `${r.waitedMs}ms`,
1059
+ }),
1060
+ }
980
1061
  }
981
1062
  const text = await waitUntil(call, tabId, {
982
1063
  selector: has(args.selector) ? args.selector : undefined,
@@ -1003,7 +1084,7 @@ export function apply(ctx, config) {
1003
1084
  async execute(args) {
1004
1085
  const tabId = await resolveTab(args.tab)
1005
1086
  const r = await call('dialog', { tabId, action: args.action, text: args.text || undefined })
1006
- return { text: r.action === 'accept' ? '已放行弹窗' : '已取消弹窗' }
1087
+ return { text: r.action === 'accept' ? t('res.dialog.accepted') : t('res.dialog.dismissed') }
1007
1088
  },
1008
1089
  }),
1009
1090
 
@@ -1089,10 +1170,15 @@ export function apply(ctx, config) {
1089
1170
  dir,
1090
1171
  })
1091
1172
  const type = (r.type || '').split(';')[0].trim()
1092
- const warn = /^text\/html/i.test(type)
1093
- ? '\n⚠️ 下到的是网页(text/html),多半是登录页或「下载即将开始」的中转页,不是文件本身:打开看看,或者换成真正的文件链接'
1094
- : ''
1095
- return { text: `已下载:${r.file}(${humanSize(r.size)}${type ? `,${type}` : ''})${warn}` }
1173
+ const warn = /^text\/html/i.test(type) ? t('res.dl.warnHtml') : ''
1174
+ return {
1175
+ text: t('res.dl.done', {
1176
+ file: r.file,
1177
+ size: humanSize(r.size),
1178
+ type: type ? t('res.dl.typePart', { type }) : '',
1179
+ warn,
1180
+ }),
1181
+ }
1096
1182
  },
1097
1183
  }),
1098
1184
 
@@ -1147,7 +1233,7 @@ export function apply(ctx, config) {
1147
1233
  throw e
1148
1234
  }
1149
1235
  }
1150
- trace(`已注册 ${tools.length} 个工具: ${tools.map((t) => t.name).join(', ')}`)
1236
+ trace(`已注册 ${tools.length} 个工具: ${tools.map((x) => x.name).join(', ')}`)
1151
1237
 
1152
1238
  // 上游报「图片太多」时卸掉最老的几张再重试(见 image-guard.js)
1153
1239
  installImageGuard(ctx, trace)
package/nav.js CHANGED
@@ -6,6 +6,8 @@
6
6
  * 或者网址变了(单页应用只改历史记录、不换文档)并且 readyState=complete,就算到了。
7
7
  */
8
8
 
9
+ import { tt } from './i18n/index.js'
10
+
9
11
  const sleep = (ms) => new Promise((r) => setTimeout(r, ms))
10
12
 
11
13
  export async function historyNav(call, tabId, action, timeoutMs = 20000) {
@@ -19,7 +21,7 @@ export async function historyNav(call, tabId, action, timeoutMs = 20000) {
19
21
  const h = (await call('cdp', { tabId, method: 'Page.getNavigationHistory', params: {} })).result
20
22
  const i = h.currentIndex + (action === 'back' ? -1 : 1)
21
23
  if (i < 0 || i >= h.entries.length) {
22
- throw new Error(action === 'back' ? '已经是第一页了,没法后退' : '已经是最后一页了,没法前进')
24
+ throw new Error(action === 'back' ? tt('err.navFirstPage') : tt('err.navLastPage'))
23
25
  }
24
26
  await call('cdp', { tabId, method: 'Page.navigateToHistoryEntry', params: { entryId: h.entries[i].id } })
25
27
  }
@@ -51,10 +53,10 @@ export async function waitUntil(call, tabId, { selector, text, url, gone = false
51
53
  let cond, what
52
54
  if (text !== undefined) {
53
55
  cond = `(document.body ? document.body.innerText : '').includes(${JSON.stringify(text)})`
54
- what = `文字「${text}」`
56
+ what = tt('res.wait.whatText', { text })
55
57
  } else if (url !== undefined) {
56
58
  cond = `location.href.includes(${JSON.stringify(url)})`
57
- what = `网址包含「${url}」`
59
+ what = tt('res.wait.whatUrl', { url })
58
60
  } else {
59
61
  cond = VISIBLE_JS(selector)
60
62
  what = selector
@@ -64,12 +66,18 @@ export async function waitUntil(call, tabId, { selector, text, url, gone = false
64
66
  while (Date.now() - start < timeoutMs) {
65
67
  try {
66
68
  if ((await call('eval', { tabId, expression, userGesture: false })).value) {
67
- return `${gone ? '已经没有' : '等到了'} ${what},用了 ${Date.now() - start}ms`
69
+ return tt('res.wait.ok', {
70
+ state: gone ? tt('res.wait.goneState') : tt('res.wait.found'),
71
+ what,
72
+ ms: `${Date.now() - start}ms`,
73
+ })
68
74
  }
69
75
  } catch {
70
76
  // 页面正在跳转,执行上下文没了 —— 下一轮再看
71
77
  }
72
78
  await sleep(300)
73
79
  }
74
- throw new Error(`等了 ${timeoutMs}ms,${what} ${gone ? '还在' : '还没出现'}`)
80
+ throw new Error(
81
+ tt('err.waitTimeout', { ms: `${timeoutMs}ms`, what, state: gone ? tt('res.wait.still') : tt('res.wait.notYet') }),
82
+ )
75
83
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "dsh-chrome-bridge",
3
- "version": "1.19.0",
3
+ "version": "1.20.0",
4
4
  "description": "DSH plugin: native browser_* tools that drive the Chrome you are already using, over native messaging + CDP. No WebSocket, no port, no Connect button. The native host and the MV3 extension ship inside this package and are installed automatically on first load; the one step left is adding the DSH Bridge extension to Chrome, from the Chrome Web Store or as the bundled unpacked copy. Until then browser_* tools fail loudly with those exact steps.",
5
5
  "type": "module",
6
6
  "license": "MIT",