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
@@ -0,0 +1,224 @@
1
+ import { ensure, onTabAttached, onTabDetached, shouldKeepAlive } from './cdp.js'
2
+ import { PAUSED_MSG, S } from './state.js'
3
+ import { log, within } from './util.js'
4
+
5
+ const CONFIG_TIMEOUT = 2000
6
+ const PAUSE_TIMEOUT = 5000
7
+ const RECENT_LIMIT = 2048
8
+ const tabs = new Map()
9
+ let debuggerEvents
10
+
11
+ // Match Chrome's glob semantics, including escaped wildcards and '?' matching zero or one character.
12
+ function urlMatcher(pattern) {
13
+ const segments = []
14
+ let literal = '', gap = 0
15
+ for (const token of pattern.match(/\\[\s\S]|[\s\S]/gu) || []) {
16
+ if (token === '*' || token === '?') {
17
+ if (literal) { segments.push({ literal, gap }); literal = ''; gap = 0 }
18
+ gap = token === '*' || gap === Infinity ? Infinity : gap + 1
19
+ } else if (token !== '\\') literal += token.startsWith('\\') ? token.slice(1) : token
20
+ }
21
+ return url => {
22
+ let offset = 0
23
+ for (const part of segments) {
24
+ const next = part.gap === 0
25
+ ? (url.startsWith(part.literal, offset) ? offset : -1)
26
+ : url.indexOf(part.literal, offset)
27
+ if (next < 0 || next - offset > part.gap) return false
28
+ offset = next + part.literal.length
29
+ }
30
+ const end = url.length - literal.length
31
+ return end >= offset && end - offset <= gap && url.endsWith(literal)
32
+ }
33
+ }
34
+
35
+ function reconcile(id, tab) {
36
+ const operation = tab.queue.catch(() => {}).then(async () => {
37
+ if (tabs.get(id) !== tab || !S.attached.has(id)) return
38
+ const patterns = [...new Map([...tab.consumers.values()].flatMap(c => c.patterns)
39
+ .map(p => [JSON.stringify(p), p])).values()]
40
+ const signature = patterns.length ? JSON.stringify(patterns) : ''
41
+ if (signature && S.paused) throw new Error(PAUSED_MSG)
42
+ if (signature === tab.applied) return
43
+ const generation = tab.generation
44
+ // Keep the listener armed even if Chrome applied the command and its reply timed out.
45
+ const enabled = tab.applied
46
+ tab.applied = null
47
+ if (signature || enabled !== '') {
48
+ if (!signature) await Promise.all([...tab.pending.values()].map(request => request.done))
49
+ if (generation !== tab.generation) return
50
+ await within(chrome.debugger.sendCommand({ tabId: id },
51
+ signature ? 'Fetch.enable' : 'Fetch.disable', signature ? { patterns } : {}), CONFIG_TIMEOUT, 'Fetch configuration timed out')
52
+ }
53
+ if (generation === tab.generation) tab.applied = signature
54
+ })
55
+ tab.queue = operation
56
+ return operation.finally(() => {
57
+ if (tab.queue === operation && !tab.consumers.size && tab.applied === '' && tabs.get(id) === tab) {
58
+ tabs.delete(id)
59
+ }
60
+ })
61
+ }
62
+
63
+ async function dispatch(src, event, tab, stage, request) {
64
+ const { requestId } = event
65
+ const { signal } = request.controller
66
+ const resume = stage === 'Response' ? 'Fetch.continueResponse' : 'Fetch.continueRequest'
67
+ const command = (method, params = {}) =>
68
+ chrome.debugger.sendCommand(src, method, { ...params, requestId })
69
+ let completed = false
70
+ let terminal
71
+ try {
72
+ for (const consumer of tab.consumers.values()) {
73
+ if (S.paused) break
74
+ if (tab.consumers.get(consumer.id) !== consumer) continue
75
+ if (!consumer.matches.some(match => match(event, stage))) continue
76
+ const run = consumer.onPaused(event, stage, src)
77
+ if (!run) continue
78
+ request.owner = consumer
79
+ const cancel = new Promise((_, reject) => signal.addEventListener('abort', () => reject(signal.reason), { once: true }))
80
+ const task = Promise.resolve().then(() => run({
81
+ signal,
82
+ getResponseBody() {
83
+ signal.throwIfAborted()
84
+ if (S.paused) throw new Error(PAUSED_MSG)
85
+ return command('Fetch.getResponseBody')
86
+ },
87
+ finish(method, params) {
88
+ signal.throwIfAborted()
89
+ if (S.paused) throw new Error(PAUSED_MSG)
90
+ terminal ??= Promise.resolve(command(method, params)).then(() => { completed = true })
91
+ return terminal
92
+ },
93
+ }))
94
+ await within(Promise.race([task, cancel]), consumer.timeoutMs ?? PAUSE_TIMEOUT, 'Fetch consumer timed out')
95
+ if (terminal) await within(terminal, CONFIG_TIMEOUT, 'Fetch completion timed out')
96
+ break
97
+ }
98
+ } catch {
99
+ // Failed claims also release the pause.
100
+ } finally {
101
+ request.controller.abort()
102
+ if (!completed && request.generation === tab.generation) {
103
+ try { await within(command(resume), CONFIG_TIMEOUT, 'Fetch continuation timed out') }
104
+ catch (error) {
105
+ // Older Chrome accepts continueRequest at the response stage.
106
+ if (stage === 'Response' && request.generation === tab.generation &&
107
+ /-32601|method not found|wasn't found/i.test(String(error))) {
108
+ await within(command('Fetch.continueRequest'), CONFIG_TIMEOUT, 'Fetch continuation timed out').catch(() => {})
109
+ }
110
+ }
111
+ }
112
+ }
113
+ }
114
+
115
+ async function recoverPause(src, requestId, stage) {
116
+ const resume = method => within(chrome.debugger.sendCommand(src, method, { requestId }),
117
+ CONFIG_TIMEOUT, 'Fetch continuation timed out')
118
+ try {
119
+ if (stage === 'Response') await resume('Fetch.continueResponse').catch(() => resume('Fetch.continueRequest'))
120
+ else await resume('Fetch.continueRequest')
121
+ } finally {
122
+ // A new consumer may have enabled Fetch while the continuation was in flight.
123
+ if (!tabs.has(src.tabId)) {
124
+ await within(chrome.debugger.sendCommand(src, 'Fetch.disable', {}), CONFIG_TIMEOUT,
125
+ 'Fetch configuration timed out').catch(() => {})
126
+ }
127
+ }
128
+ }
129
+
130
+ function handleEvent(src, method, event) {
131
+ if (method !== 'Fetch.requestPaused') return
132
+ if (!event.requestId) return
133
+ const stage = event.responseStatusCode != null || event.responseErrorReason != null ? 'Response' : 'Request'
134
+ const tab = tabs.get(src.tabId)
135
+ if (!tab) {
136
+ void recoverPause(src, event.requestId, stage).catch(() => {})
137
+ return
138
+ }
139
+ const key = JSON.stringify([src.sessionId, event.requestId, stage])
140
+ if (tab.pending.has(key) || tab.recent.has(key)) return
141
+ const request = { controller: new AbortController(), generation: tab.generation }
142
+ tab.pending.set(key, request)
143
+ request.done = Promise.resolve().then(() => {
144
+ if (request.generation === tab.generation) return dispatch(src, event, tab, stage, request)
145
+ }).finally(() => {
146
+ if (tab.pending.get(key) !== request) return
147
+ tab.pending.delete(key)
148
+ tab.recent.add(key)
149
+ if (tab.recent.size > RECENT_LIMIT) tab.recent.delete(tab.recent.values().next().value)
150
+ })
151
+ }
152
+
153
+ function detached(id, reason) {
154
+ const tab = tabs.get(id)
155
+ if (!tab) return
156
+ tab.generation++
157
+ tab.applied = ''
158
+ for (const { controller } of tab.pending.values()) controller.abort()
159
+ tab.pending.clear()
160
+ tab.recent.clear()
161
+ if (reason === 'target_closed') tab.consumers.clear()
162
+ if (!tab.consumers.size) {
163
+ tabs.delete(id)
164
+ }
165
+ }
166
+
167
+ export function initFetchHub() {
168
+ const event = chrome.debugger.onEvent
169
+ if (debuggerEvents === event) return
170
+ debuggerEvents = event
171
+ // Register at worker startup even when no in-memory consumers survived.
172
+ event.addListener(handleEvent)
173
+ onTabAttached(id => {
174
+ const tab = tabs.get(id)
175
+ if (tab?.consumers.size) void reconcile(id, tab).catch(() => log('Fetch configuration failed after reattach'))
176
+ })
177
+ onTabDetached(detached)
178
+ shouldKeepAlive(id => [...(tabs.get(id)?.consumers.values() || [])].some(c => c.persistent))
179
+ }
180
+
181
+ /**
182
+ * onPaused(event, stage, src) returns a handler to claim the pause, or undefined to pass.
183
+ * Only the winning handler receives commands. The unregister function's ready
184
+ * promise resolves after Chrome has installed the merged patterns.
185
+ */
186
+ export function registerFetchConsumer(tabId, { id, priority = 0, patterns, onPaused, persistent = false, timeoutMs }) {
187
+ initFetchHub()
188
+ const tabKey = Number(tabId)
189
+ let tab = tabs.get(tabKey)
190
+ if (!tab) {
191
+ tab = { consumers: new Map(), pending: new Map(), recent: new Set(), generation: 0, applied: '', queue: Promise.resolve() }
192
+ tabs.set(tabKey, tab)
193
+ }
194
+ if (tab.consumers.has(id)) throw new Error('Duplicate Fetch consumer')
195
+ const normalized = patterns.map(({ urlPattern = '*', requestStage = 'Request', resourceType }) =>
196
+ ({ urlPattern, requestStage, ...(resourceType && { resourceType }) }))
197
+ const matches = normalized.map(p => {
198
+ const matchesUrl = urlMatcher(p.urlPattern)
199
+ return (event, stage) => p.requestStage === stage &&
200
+ (!p.resourceType || p.resourceType === event.resourceType) &&
201
+ matchesUrl(event.request?.url || '')
202
+ })
203
+ const consumer = { id, priority, patterns: normalized, matches, onPaused, persistent, timeoutMs }
204
+ tab.consumers.set(id, consumer)
205
+ tab.consumers = new Map([...tab.consumers].sort((a, b) => b[1].priority - a[1].priority))
206
+
207
+ let released
208
+ function unregister() {
209
+ if (released) return released
210
+ if (tab.consumers.get(id) !== consumer) return Promise.resolve()
211
+ tab.consumers.delete(id)
212
+ for (const request of tab.pending.values()) {
213
+ if (request.owner === consumer) request.controller.abort()
214
+ }
215
+ released = reconcile(tabKey, tab)
216
+ return released
217
+ }
218
+
219
+ unregister.ready = ensure(tabKey).then(() => reconcile(tabKey, tab)).catch(error => {
220
+ void unregister().catch(() => {})
221
+ throw error
222
+ })
223
+ return unregister
224
+ }