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.
- 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 +37 -16
- package/download.js +895 -69
- package/drag.js +68 -30
- 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 +312 -16
- package/i18n/index.js +17 -0
- package/i18n/ru.js +312 -16
- package/i18n/zh.js +310 -14
- package/image-guard.js +13 -6
- package/index.js +21 -1073
- package/locale/ru.json +7 -0
- package/nav.js +56 -10
- package/package.json +8 -1
- package/panel.js +14 -4
- package/refs.js +293 -240
- package/setup.js +248 -74
- 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,192 @@
|
|
|
1
|
+
import { CHOOSER_SKIP_MS, CHOOSER_WAIT_MS, S } from './state.js'
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* 「允许访问文件网址」开没开:Chrome 默认关;关着时 DOM.setFileInputFiles 只回一句 Not allowed(issue #7)。
|
|
5
|
+
* 用户在扩展详情里切换这个开关,Chrome 会把扩展整个重载,所以这里不用缓存也不会过期。
|
|
6
|
+
*/
|
|
7
|
+
async function fileAccess() {
|
|
8
|
+
try { return await chrome.extension.isAllowedFileSchemeAccess() } catch { return null }
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
function fileAccessError() {
|
|
12
|
+
const url = `chrome://extensions/?id=${chrome.runtime.id}`
|
|
13
|
+
return new Error(`FILE_ACCESS_OFF ${url} 扩展没开「允许访问文件网址」,Chrome 不让它把电脑上的文件塞进网页:在 ${url} 打开「允许访问文件网址」,扩展会自动重载,几秒后再上传`)
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
/**
|
|
17
|
+
* 插件明确要求时(点击 / 按键带 chooser: true),在发鼠标 / 键盘事件之前调:这一下要是让页面打开系统的选文件窗口,
|
|
18
|
+
* 就拦下来记进 choosers。没开文件访问时不拦(拦了也塞不进去,不如让窗口照常弹出来给用户自己选)。
|
|
19
|
+
* 老插件、cb 命令行不带这个标记,一律不拦。返回这次开没开成。
|
|
20
|
+
*/
|
|
21
|
+
async function armChooser(tabId) {
|
|
22
|
+
const id = Number(tabId)
|
|
23
|
+
if ((await fileAccess()) !== true) return false
|
|
24
|
+
const skip = S.chooserSkip.get(id)
|
|
25
|
+
if (skip && Date.now() - skip < CHOOSER_SKIP_MS) return false
|
|
26
|
+
try {
|
|
27
|
+
// 每次都发:主动松手 / 空闲断开 / 宿主重连以后调试会话是新的,旧会话里开的拦截已经没了
|
|
28
|
+
await chrome.debugger.sendCommand({ tabId: id }, 'Page.setInterceptFileChooserDialog', { enabled: true })
|
|
29
|
+
} catch { return false }
|
|
30
|
+
S.chooserArmed.add(id)
|
|
31
|
+
return true
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
/** 这一下点击 / 按键做完就关拦截:之后页面(或用户自己)再打开选文件窗口,照常弹出来 */
|
|
35
|
+
async function disarmChooser(tabId) {
|
|
36
|
+
const id = Number(tabId)
|
|
37
|
+
if (!S.chooserArmed.delete(id)) return
|
|
38
|
+
try { await chrome.debugger.sendCommand({ tabId: id }, 'Page.setInterceptFileChooserDialog', { enabled: false }) } catch {}
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
/** 这个请求(token)拦下的框,回给插件的那一份;等它查完是不是选文件夹、属于哪个文档 */
|
|
42
|
+
async function chooserFor(tabId, token) {
|
|
43
|
+
const id = Number(tabId)
|
|
44
|
+
const c = S.choosers.get(id)
|
|
45
|
+
if (!c || c.owner !== token) return undefined
|
|
46
|
+
await c.ready
|
|
47
|
+
if (S.choosers.get(id) !== c) return undefined
|
|
48
|
+
if (!c.backendNodeId) return { unsupported: true }
|
|
49
|
+
if (c.folder) { dropChooser(id, c); return { folder: true } }
|
|
50
|
+
return { id: c.id, mode: c.mode }
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
/**
|
|
54
|
+
* Page.fileChooserOpened 在松开鼠标的回包之后才到(本机无头 Chrome 实测紧跟着、0 毫秒):拦截开着时等一小会儿,
|
|
55
|
+
* 来了马上走;页面没要选文件就等满 ms。
|
|
56
|
+
*/
|
|
57
|
+
async function waitChooser(tabId, token, ms = CHOOSER_WAIT_MS) {
|
|
58
|
+
const id = Number(tabId)
|
|
59
|
+
const has = () => { const c = S.choosers.get(id); return !!c && c.owner === token }
|
|
60
|
+
if (has() || !S.chooserArmed.has(id)) return
|
|
61
|
+
await new Promise((resolve) => {
|
|
62
|
+
const tm = setTimeout(done, ms)
|
|
63
|
+
function done() { clearTimeout(tm); if (S.chooserWaiters.get(id) === done) S.chooserWaiters.delete(id); resolve() }
|
|
64
|
+
S.chooserWaiters.set(id, done)
|
|
65
|
+
})
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
/** 某个框所在文档的 loaderId(找不到这个框就用主框架的):换过页就对不上 */
|
|
69
|
+
async function docOf(tabId, frameId, sendFn) {
|
|
70
|
+
try {
|
|
71
|
+
const { frameTree } = await sendFn(tabId, 'Page.getFrameTree', {})
|
|
72
|
+
let main = null
|
|
73
|
+
const walk = (t) => {
|
|
74
|
+
if (!t || !t.frame) return null
|
|
75
|
+
if (!t.frame.parentId) main = main || t.frame
|
|
76
|
+
if (t.frame.id === frameId) return t.frame
|
|
77
|
+
for (const ch of t.childFrames || []) { const f = walk(ch); if (f) return f }
|
|
78
|
+
return null
|
|
79
|
+
}
|
|
80
|
+
const f = walk(frameTree) || main
|
|
81
|
+
return f ? `${f.id}:${f.loaderId}` : null
|
|
82
|
+
} catch { return null }
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
/** DOM.describeNode 回来的节点有没有某个属性 */
|
|
86
|
+
function hasAttr(node, name) {
|
|
87
|
+
const a = (node && node.attributes) || []
|
|
88
|
+
for (let i = 0; i < a.length; i += 2) if (String(a[i]).toLowerCase() === name) return true
|
|
89
|
+
return false
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
/** DOM.describeNode 回来的节点某个属性的值(没有回 null) */
|
|
93
|
+
function attrValue(node, name) {
|
|
94
|
+
const a = (node && node.attributes) || []
|
|
95
|
+
for (let i = 0; i < a.length; i += 2) if (String(a[i]).toLowerCase() === name) return String(a[i + 1])
|
|
96
|
+
return null
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
/** 节点是不是「选文件夹」的框(webkitdirectory:塞普通文件进去,Chrome 会当目录去枚举,什么也传不上) */
|
|
100
|
+
function isFolderNode(node) { return hasAttr(node, 'webkitdirectory') }
|
|
101
|
+
|
|
102
|
+
/** 放开对拦下那个框的引用(见 inspectChooser 的 hold) */
|
|
103
|
+
function releaseHold(tabId, c) {
|
|
104
|
+
if (!c || !c.hold) return
|
|
105
|
+
try { chrome.debugger.sendCommand({ tabId: Number(tabId) }, 'Runtime.releaseObjectGroup', { objectGroup: c.hold }).catch(() => {}) } catch {}
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
/** 删掉这个标签页拦下的框(给了 c 就只在还是这一条时删),顺手放开引用 */
|
|
109
|
+
function dropChooser(tabId, c) {
|
|
110
|
+
const id = Number(tabId)
|
|
111
|
+
const cur = S.choosers.get(id)
|
|
112
|
+
if (!cur || (c && cur !== c)) return
|
|
113
|
+
S.choosers.delete(id)
|
|
114
|
+
releaseHold(id, cur)
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
/**
|
|
118
|
+
* 文件框现在的样子:还在不在页面上、里面已有哪些文件、拦下以后有没有人自己点开 / 选过(watch 时装上记数)。
|
|
119
|
+
* 记数挂在节点上的 key 下:拦下那一下的点击早过去了,之后再有 click / change 就是别人(用户、页面)动过它。查不到回 null
|
|
120
|
+
*/
|
|
121
|
+
async function inputState(tabId, backendNodeId, sendFn, key, watch = false) {
|
|
122
|
+
const objectGroup = `dsh-chooser-${crypto.randomUUID()}`
|
|
123
|
+
try {
|
|
124
|
+
const { object } = await sendFn(tabId, 'DOM.resolveNode', { backendNodeId, objectGroup })
|
|
125
|
+
const { result } = await sendFn(tabId, 'Runtime.callFunctionOn', {
|
|
126
|
+
objectId: object.objectId, returnByValue: true, arguments: [{ value: key }, { value: watch }],
|
|
127
|
+
functionDeclaration: `function (k, watch) {
|
|
128
|
+
const el = this
|
|
129
|
+
if (watch) { el[k] = 0; for (const t of ['click', 'change']) el.addEventListener(t, () => { el[k]++ }, true) }
|
|
130
|
+
return { on: el.isConnected, touched: typeof el[k] === 'number' ? el[k] : -1,
|
|
131
|
+
files: [...(el.files || [])].map((f) => f.name + '/' + f.size + '/' + f.lastModified).join('|') }
|
|
132
|
+
}`,
|
|
133
|
+
})
|
|
134
|
+
return (result && result.value) || null
|
|
135
|
+
} catch { return null } finally {
|
|
136
|
+
Promise.resolve().then(() => sendFn(tabId, 'Runtime.releaseObjectGroup', { objectGroup })).catch(() => {})
|
|
137
|
+
}
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
/** 拦下框以后马上查:是不是「选文件夹」,属于哪个文档,框里原来有哪些文件 */
|
|
141
|
+
async function inspectChooser(tabId, c) {
|
|
142
|
+
const raw = (_t, method, params) => chrome.debugger.sendCommand({ tabId }, method, params)
|
|
143
|
+
if (c.backendNodeId) {
|
|
144
|
+
// 先抓住这个框:很多网站在点击函数里现造一个 input、不插进页面也不留引用,拦下以后没人拿着它,
|
|
145
|
+
// 模型想文件的工夫浏览器一回收就没了(实测强制回收后上传报「没在等」)。用完 / 换掉 / 换页 / 断开时放开
|
|
146
|
+
c.hold = `dsh-hold-${c.id}`
|
|
147
|
+
try { await raw(tabId, 'DOM.resolveNode', { backendNodeId: c.backendNodeId, objectGroup: c.hold }) } catch {}
|
|
148
|
+
try {
|
|
149
|
+
const { node } = await raw(tabId, 'DOM.describeNode', { backendNodeId: c.backendNodeId })
|
|
150
|
+
c.folder = isFolderNode(node)
|
|
151
|
+
} catch {}
|
|
152
|
+
c.state = await inputState(tabId, c.backendNodeId, raw, c.key, true)
|
|
153
|
+
}
|
|
154
|
+
c.doc = await docOf(tabId, c.frameId, raw)
|
|
155
|
+
if (S.choosers.get(tabId) !== c) releaseHold(tabId, c) // 查的工夫已经被新框顶掉 / 换页了
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
/**
|
|
159
|
+
* 插件要求时,把一下点击 / 按键包在拦截里:排队 → 开 → 做 → 等一小会儿看页面要不要选文件 → 关。
|
|
160
|
+
* 回 { ...做的结果, fileChooser }(没要选文件就没有这个字段)。
|
|
161
|
+
*/
|
|
162
|
+
async function withChooser(tabId, want, fn) {
|
|
163
|
+
if (!want) return fn()
|
|
164
|
+
const id = Number(tabId)
|
|
165
|
+
const prev = S.chooserLocks.get(id) || Promise.resolve()
|
|
166
|
+
let release
|
|
167
|
+
const mine = new Promise((r) => { release = r })
|
|
168
|
+
const chain = prev.then(() => mine)
|
|
169
|
+
S.chooserLocks.set(id, chain)
|
|
170
|
+
await prev
|
|
171
|
+
const token = crypto.randomUUID()
|
|
172
|
+
let r
|
|
173
|
+
try {
|
|
174
|
+
S.chooserOwner.set(id, token)
|
|
175
|
+
await armChooser(id)
|
|
176
|
+
r = await fn()
|
|
177
|
+
await waitChooser(id, token)
|
|
178
|
+
} finally {
|
|
179
|
+
await disarmChooser(id) // 关拦截的回包到了,之前拦下的 Page.fileChooserOpened 也都到了
|
|
180
|
+
if (S.chooserOwner.get(id) === token) S.chooserOwner.delete(id)
|
|
181
|
+
release()
|
|
182
|
+
if (S.chooserLocks.get(id) === chain) S.chooserLocks.delete(id)
|
|
183
|
+
}
|
|
184
|
+
const fileChooser = await chooserFor(id, token)
|
|
185
|
+
return fileChooser ? { ...r, fileChooser } : r
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
export {
|
|
189
|
+
fileAccess, fileAccessError, armChooser, disarmChooser, chooserFor, waitChooser, docOf,
|
|
190
|
+
hasAttr, attrValue, isFolderNode, releaseHold, dropChooser, inputState, inspectChooser,
|
|
191
|
+
withChooser,
|
|
192
|
+
}
|