dsh-chrome-bridge 1.19.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.
Files changed (89) hide show
  1. package/README.en.md +6 -2
  2. package/README.md +6 -2
  3. package/client.js +110 -29
  4. package/core/ext.js +119 -0
  5. package/core/hosts.js +277 -0
  6. package/core/locale.js +8 -0
  7. package/core/receipt.js +56 -0
  8. package/core/session.js +157 -0
  9. package/core/shots.js +56 -0
  10. package/core/tooldef.js +142 -0
  11. package/core/transport.js +212 -0
  12. package/core/upload.js +59 -0
  13. package/devtools.js +37 -16
  14. package/download.js +895 -69
  15. package/drag.js +68 -30
  16. package/features/frames.js +58 -0
  17. package/features/gif.js +203 -0
  18. package/features/index.js +1 -0
  19. package/features/mock-har.js +85 -0
  20. package/features/record.js +225 -0
  21. package/i18n/en.js +312 -16
  22. package/i18n/index.js +17 -0
  23. package/i18n/ru.js +312 -16
  24. package/i18n/zh.js +310 -14
  25. package/image-guard.js +13 -6
  26. package/index.js +21 -1073
  27. package/locale/ru.json +7 -0
  28. package/nav.js +56 -10
  29. package/package.json +8 -1
  30. package/panel.js +14 -4
  31. package/refs.js +293 -240
  32. package/setup.js +248 -74
  33. package/tools/click.js +58 -0
  34. package/tools/devtools.js +61 -0
  35. package/tools/dialog.js +22 -0
  36. package/tools/download.js +50 -0
  37. package/tools/drag.js +61 -0
  38. package/tools/eval.js +22 -0
  39. package/tools/fill.js +77 -0
  40. package/tools/handoff.js +40 -0
  41. package/tools/index.js +38 -0
  42. package/tools/input.js +147 -0
  43. package/tools/nav.js +57 -0
  44. package/tools/read.js +87 -0
  45. package/tools/record.js +120 -0
  46. package/tools/screenshot.js +223 -0
  47. package/tools/tabs.js +131 -0
  48. package/tools/wait.js +56 -0
  49. package/vendor/cb +616 -35
  50. package/vendor/extension/_locales/en/messages.json +3 -0
  51. package/vendor/extension/_locales/ru/messages.json +3 -0
  52. package/vendor/extension/_locales/zh_CN/messages.json +3 -0
  53. package/vendor/extension/_locales/zh_TW/messages.json +3 -0
  54. package/vendor/extension/background.js +2 -1969
  55. package/vendor/extension/confirm.css +148 -0
  56. package/vendor/extension/confirm.html +49 -0
  57. package/vendor/extension/confirm.js +188 -0
  58. package/vendor/extension/in-page-cursor.js +599 -0
  59. package/vendor/extension/manifest.json +11 -2
  60. package/vendor/extension/popup.css +99 -5
  61. package/vendor/extension/popup.html +28 -2
  62. package/vendor/extension/popup.js +430 -45
  63. package/vendor/extension/sw/boot.js +111 -0
  64. package/vendor/extension/sw/cdp.js +411 -0
  65. package/vendor/extension/sw/chooser.js +192 -0
  66. package/vendor/extension/sw/confirm.js +608 -0
  67. package/vendor/extension/sw/cursor.js +241 -0
  68. package/vendor/extension/sw/dispatch.js +129 -0
  69. package/vendor/extension/sw/download.js +279 -0
  70. package/vendor/extension/sw/fetch-hub.js +224 -0
  71. package/vendor/extension/sw/frames.js +651 -0
  72. package/vendor/extension/sw/imaging.js +361 -0
  73. package/vendor/extension/sw/native.js +111 -0
  74. package/vendor/extension/sw/net-tools.js +525 -0
  75. package/vendor/extension/sw/ops.js +829 -0
  76. package/vendor/extension/sw/popup-api.js +303 -0
  77. package/vendor/extension/sw/profile.js +166 -0
  78. package/vendor/extension/sw/record.js +494 -0
  79. package/vendor/extension/sw/state.js +161 -0
  80. package/vendor/extension/sw/tabui.js +219 -0
  81. package/vendor/extension/sw/util.js +166 -0
  82. package/vendor/extension/sw/welcome.js +26 -0
  83. package/vendor/extension/sw/windows.js +94 -0
  84. package/vendor/extension/welcome.css +189 -0
  85. package/vendor/extension/welcome.html +70 -0
  86. package/vendor/extension/welcome.js +157 -0
  87. package/vendor/host/host.js +476 -71
  88. package/vendor/in-page-cursor.js +309 -37
  89. package/vendor/skill/SKILL.md +34 -16
package/locale/ru.json ADDED
@@ -0,0 +1,7 @@
1
+ {
2
+ "meta": {
3
+ "title": "Управление Chrome",
4
+ "description": "Позволяет DSH работать в вашем обычном Chrome: настоящие клики и ввод, видимый курсор на странице, все ваши входы в аккаунты.",
5
+ "detail": "README.en.md"
6
+ }
7
+ }
package/nav.js CHANGED
@@ -6,8 +6,17 @@
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
 
13
+ /** 轮询里遇到这些错要立刻往上抛:用户在操作 / 拒绝了 / 确认来晚了 / 扩展没回,接着重试只会反复弹窗、白等 */
14
+ const FATAL_CODES = new Set(['USER_ACTIVE', 'USER_DENIED_SENSITIVE', 'CONFIRM_LATE', 'EXTENSION_TIMEOUT'])
15
+ const isFatal = (e) => FATAL_CODES.has(e && e.code) ||
16
+ /^(USER_ACTIVE|USER_DENIED_SENSITIVE|CONFIRM_LATE)\b/.test(String((e && e.message) || ''))
17
+ /** 等扩展那边的操作:外层超时 = 确认上限 60 秒 + 执行时限 + 传输余量,别在用户看确认窗或执行中被提前掐断 */
18
+ const extensionWaitMs = (ms) => ms + 65_000
19
+
11
20
  export async function historyNav(call, tabId, action, timeoutMs = 20000) {
12
21
  // 只读的探测不带 userGesture(不白送页面一次「用户激活」)
13
22
  const ev = async (expression) => (await call('eval', { tabId, expression, userGesture: false })).value
@@ -19,7 +28,7 @@ export async function historyNav(call, tabId, action, timeoutMs = 20000) {
19
28
  const h = (await call('cdp', { tabId, method: 'Page.getNavigationHistory', params: {} })).result
20
29
  const i = h.currentIndex + (action === 'back' ? -1 : 1)
21
30
  if (i < 0 || i >= h.entries.length) {
22
- throw new Error(action === 'back' ? '已经是第一页了,没法后退' : '已经是最后一页了,没法前进')
31
+ throw new Error(action === 'back' ? tt('err.navFirstPage') : tt('err.navLastPage'))
23
32
  }
24
33
  await call('cdp', { tabId, method: 'Page.navigateToHistoryEntry', params: { entryId: h.entries[i].id } })
25
34
  }
@@ -30,7 +39,8 @@ export async function historyNav(call, tabId, action, timeoutMs = 20000) {
30
39
  await sleep(250)
31
40
  try {
32
41
  last = await ev('({ mark: !!window.__dshNavMark, ready: document.readyState, url: location.href, title: document.title })')
33
- } catch {
42
+ } catch (e) {
43
+ if (isFatal(e)) throw e
34
44
  continue // 跳转途中执行上下文没了,下一轮再看
35
45
  }
36
46
  if (last && last.ready === 'complete' && (!last.mark || last.url !== before)) return { ...last, done: true }
@@ -51,25 +61,61 @@ export async function waitUntil(call, tabId, { selector, text, url, gone = false
51
61
  let cond, what
52
62
  if (text !== undefined) {
53
63
  cond = `(document.body ? document.body.innerText : '').includes(${JSON.stringify(text)})`
54
- what = `文字「${text}」`
64
+ what = tt('res.wait.whatText', { text })
55
65
  } else if (url !== undefined) {
56
66
  cond = `location.href.includes(${JSON.stringify(url)})`
57
- what = `网址包含「${url}」`
67
+ what = tt('res.wait.whatUrl', { url })
58
68
  } else {
59
69
  cond = VISIBLE_JS(selector)
60
70
  what = selector
61
71
  }
62
72
  const expression = gone ? `!(${cond})` : `!!(${cond})`
63
- const start = Date.now()
64
- while (Date.now() - start < timeoutMs) {
73
+ let start = null
74
+ while (start === null || Date.now() - start < timeoutMs) {
65
75
  try {
66
- if ((await call('eval', { tabId, expression, userGesture: false })).value) {
67
- return `${gone ? '已经没有' : '等到了'} ${what},用了 ${Date.now() - start}ms`
76
+ const matched = (await call('eval', { tabId, expression, userGesture: false })).value
77
+ if (start === null) start = Date.now()
78
+ if (matched) {
79
+ return tt('res.wait.ok', {
80
+ state: gone ? tt('res.wait.goneState') : tt('res.wait.found'),
81
+ what,
82
+ ms: `${Date.now() - start}ms`,
83
+ })
68
84
  }
69
- } catch {
85
+ } catch (e) {
86
+ if (isFatal(e)) throw e
87
+ if (start === null) start = Date.now()
70
88
  // 页面正在跳转,执行上下文没了 —— 下一轮再看
71
89
  }
72
90
  await sleep(300)
73
91
  }
74
- throw new Error(`等了 ${timeoutMs}ms,${what} ${gone ? '还在' : '还没出现'}`)
92
+ throw new Error(
93
+ tt('err.waitTimeout', { ms: `${timeoutMs}ms`, what, state: gone ? tt('res.wait.still') : tt('res.wait.notYet') }),
94
+ )
95
+ }
96
+
97
+ /** 新扩展操作在旧版上直接返回“未知操作”。保留其他错误。 */
98
+ export async function requireExtensionOp(call, op, args, timeoutMs, upgrade = () => tt('err.extension116')) {
99
+ try {
100
+ return await call(op, args, timeoutMs)
101
+ } catch (error) {
102
+ if (/(?:未知操作|unknown (?:operation|op))\s*[::]?\s/i.test(error.message)) {
103
+ throw new Error(upgrade(), { cause: error })
104
+ }
105
+ throw error
106
+ }
107
+ }
108
+
109
+ export async function waitForExtension(call, tabId, { responseUrl, newTab, url, timeoutMs, since }) {
110
+ if (newTab) {
111
+ const r = await requireExtensionOp(call, 'waitNewTab', {
112
+ openerTabId: tabId, urlFilter: url || undefined, timeoutMs, since,
113
+ }, extensionWaitMs(timeoutMs))
114
+ return tt('res.wait.newTab', { tab: r.tabId, url: r.url })
115
+ }
116
+ const match = responseUrl ? { urlFilter: responseUrl, since } : { idleMs: 500 }
117
+ const r = await requireExtensionOp(call, 'networkIdle', { tabId, ...match, timeoutMs }, extensionWaitMs(timeoutMs))
118
+ return r.matched
119
+ ? tt('res.wait.response', { method: r.method, status: r.status, url: r.url })
120
+ : tt('res.wait.networkIdle', { ms: r.waitedMs })
75
121
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "dsh-chrome-bridge",
3
- "version": "1.19.0",
3
+ "version": "2.0.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",
@@ -30,8 +30,15 @@
30
30
  "./icon.svg": "./icon.svg",
31
31
  "./locale/*.json": "./locale/*.json"
32
32
  },
33
+ "scripts": {
34
+ "test": "node ../scratch/run-unit-tests.mjs",
35
+ "test:unit": "node ../scratch/run-unit-tests.mjs"
36
+ },
33
37
  "files": [
34
38
  "index.js",
39
+ "core",
40
+ "tools",
41
+ "features",
35
42
  "setup.js",
36
43
  "image-guard.js",
37
44
  "refs.js",
package/panel.js CHANGED
@@ -12,6 +12,7 @@
12
12
 
13
13
  import fs from 'node:fs'
14
14
  import { installState, assistInstall, paths, STORE_EXT_IDS } from './setup.js'
15
+ import { tt } from './i18n/index.js'
15
16
 
16
17
  export const PANEL_ROUTE = '/dsh-chrome-bridge/panel'
17
18
 
@@ -45,13 +46,22 @@ export async function panelState(call, sock) {
45
46
  }
46
47
  const ext = { version: ver.version, id: ver.id, store: STORE_EXT_IDS.includes(ver.id) }
47
48
  try {
48
- return { ...base, connected: true, ext, ...(await call('panel', {}, 3000)) }
49
+ const st = (await call('panel', {}, 3000)) || {}
50
+ // 协作等待(handoff)和暂停原因(pauseReason)原样透传给设置卡;老扩展没有就给 null,设置卡静默不显示
51
+ return { ...base, connected: true, ext, ...st, handoff: handoffOf(st.handoff), pauseReason: typeof st.pauseReason === 'string' ? st.pauseReason : null }
49
52
  } catch (e) {
50
53
  // 老扩展(商店里还没更新的那版)没有 panel:能看状态,开关点不了
51
54
  return { ...base, connected: true, ext, outdated: true }
52
55
  }
53
56
  }
54
57
 
58
+ /** 只留设置卡用得上的几样,别把扩展那边多出来的字段一股脑塞给网页 */
59
+ function handoffOf(ho) {
60
+ if (!ho || typeof ho !== 'object') return null
61
+ const str = (v) => (v == null ? '' : String(v).slice(0, 500))
62
+ return { id: ho.id ?? null, tabId: ho.tabId ?? null, reason: str(ho.reason), title: str(ho.title), url: str(ho.url), at: Number(ho.at) || 0 }
63
+ }
64
+
55
65
  /** 跨站请求一律拒:没带 Origin(同源 GET、curl)放行;带了就必须是 DSH 自己。 */
56
66
  function allowed(req, origin) {
57
67
  if (!LOOPBACK.has(req.socket.remoteAddress)) return false
@@ -76,10 +86,10 @@ function readJson(req, limit = 4096) {
76
86
  const chunks = []
77
87
  req.on('data', (c) => {
78
88
  size += c.length
79
- if (size > limit) { reject(new Error('请求太大')); req.destroy() } else chunks.push(c)
89
+ if (size > limit) { reject(new Error(tt('err.panelTooLarge'))); req.destroy() } else chunks.push(c)
80
90
  })
81
91
  req.on('end', () => {
82
- try { resolve(JSON.parse(Buffer.concat(chunks).toString('utf8') || '{}')) } catch { reject(new Error('不是 JSON')) }
92
+ try { resolve(JSON.parse(Buffer.concat(chunks).toString('utf8') || '{}')) } catch { reject(new Error(tt('err.panelNotJson'))) }
83
93
  })
84
94
  req.on('error', reject)
85
95
  })
@@ -126,7 +136,7 @@ export function panelHandler({ call, sock, origin, trace = () => {} }) {
126
136
  return send(200, { ...(await panelState(call, sock)), assist: { opened: !!r.opened, clipboard: !!r.clipboard, store: !!r.store } })
127
137
  }
128
138
  default:
129
- return send(400, { error: `不认识的 action: ${body.action}` })
139
+ return send(400, { error: tt('err.unknownAction', { action: body.action }) })
130
140
  }
131
141
  } catch (e) {
132
142
  return send(200, { ...(await panelState(call, sock)), actionError: String((e && e.message) || e) })