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,361 @@
|
|
|
1
|
+
const B64_CHUNK = 24576 // 3 的倍数:分段 base64 可以直接拼接
|
|
2
|
+
|
|
3
|
+
function bytesToBase64(bytes) {
|
|
4
|
+
let out = ''
|
|
5
|
+
for (let i = 0; i < bytes.length; i += B64_CHUNK) {
|
|
6
|
+
const part = bytes.subarray(i, Math.min(i + B64_CHUNK, bytes.length))
|
|
7
|
+
let binary = ''
|
|
8
|
+
for (let j = 0; j < part.length; j++) binary += String.fromCharCode(part[j])
|
|
9
|
+
out += btoa(binary)
|
|
10
|
+
}
|
|
11
|
+
return out
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
function base64ToBytes(base64) {
|
|
15
|
+
const binary = atob(String(base64 || ''))
|
|
16
|
+
const out = new Uint8Array(binary.length)
|
|
17
|
+
for (let i = 0; i < binary.length; i++) out[i] = binary.charCodeAt(i)
|
|
18
|
+
return out
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
function mimeOf(format, fallback = 'image/jpeg') {
|
|
22
|
+
const f = String(format || '').toLowerCase().replace('jpg', 'jpeg')
|
|
23
|
+
if (f === 'jpeg' || f === 'png' || f === 'webp') return `image/${f}`
|
|
24
|
+
if (String(fallback || '').startsWith('image/')) return fallback
|
|
25
|
+
return 'image/jpeg'
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
function formatOf(mime) {
|
|
29
|
+
return String(mime || '').split('/')[1] || 'jpeg'
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
function qualityOf(quality) {
|
|
33
|
+
const q = Number(quality)
|
|
34
|
+
if (!Number.isFinite(q)) return undefined
|
|
35
|
+
return Math.max(0, Math.min(1, q > 1 ? q / 100 : q))
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
function jpegDimensions(bytes) {
|
|
39
|
+
if (!bytes || bytes.length < 4 || bytes[0] !== 0xff || bytes[1] !== 0xd8) return null
|
|
40
|
+
let at = 2
|
|
41
|
+
while (at + 9 < bytes.length) {
|
|
42
|
+
while (at < bytes.length && bytes[at] !== 0xff) at++
|
|
43
|
+
while (at < bytes.length && bytes[at] === 0xff) at++
|
|
44
|
+
if (at >= bytes.length) break
|
|
45
|
+
const marker = bytes[at++]
|
|
46
|
+
if (marker === 0xd8 || marker === 0xd9 || (marker >= 0xd0 && marker <= 0xd7) || marker === 0x01) continue
|
|
47
|
+
if (at + 1 >= bytes.length) break
|
|
48
|
+
const size = (bytes[at] << 8) | bytes[at + 1]
|
|
49
|
+
if (size < 2 || at + size > bytes.length) break
|
|
50
|
+
const sof = marker >= 0xc0 && marker <= 0xcf && ![0xc4, 0xc8, 0xcc].includes(marker)
|
|
51
|
+
if (sof && size >= 7) return { width: (bytes[at + 5] << 8) | bytes[at + 6], height: (bytes[at + 3] << 8) | bytes[at + 4] }
|
|
52
|
+
at += size
|
|
53
|
+
}
|
|
54
|
+
return null
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
function pngDimensions(bytes) {
|
|
58
|
+
if (!bytes || bytes.length < 24) return null
|
|
59
|
+
const sig = [0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a]
|
|
60
|
+
if (!sig.every((v, i) => bytes[i] === v)) return null
|
|
61
|
+
return {
|
|
62
|
+
width: ((bytes[16] << 24) | (bytes[17] << 16) | (bytes[18] << 8) | bytes[19]) >>> 0,
|
|
63
|
+
height: ((bytes[20] << 24) | (bytes[21] << 16) | (bytes[22] << 8) | bytes[23]) >>> 0,
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
function imageDimensions(bytes, mime) {
|
|
68
|
+
if (mime === 'image/png') return pngDimensions(bytes)
|
|
69
|
+
return jpegDimensions(bytes)
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
function even(n) {
|
|
73
|
+
return Math.max(2, Math.floor(Number(n) / 2) * 2)
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
function canvasSupported() {
|
|
77
|
+
return typeof createImageBitmap === 'function' && typeof OffscreenCanvas === 'function' && typeof Blob === 'function'
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
async function decodeImage(base64, mime) {
|
|
81
|
+
if (!canvasSupported()) return null
|
|
82
|
+
try {
|
|
83
|
+
return await createImageBitmap(new Blob([base64ToBytes(base64)], { type: mime }))
|
|
84
|
+
} catch {
|
|
85
|
+
return null
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
function canvas2d(width, height, read = false) {
|
|
90
|
+
const canvas = new OffscreenCanvas(width, height)
|
|
91
|
+
const ctx = canvas.getContext('2d', read ? { willReadFrequently: true } : undefined)
|
|
92
|
+
return ctx ? { canvas, ctx } : null
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
async function encodeCanvas(canvas, mime, quality) {
|
|
96
|
+
const blob = await canvas.convertToBlob({ type: mime, quality: qualityOf(quality) })
|
|
97
|
+
return {
|
|
98
|
+
data: bytesToBase64(new Uint8Array(await blob.arrayBuffer())),
|
|
99
|
+
width: canvas.width,
|
|
100
|
+
height: canvas.height,
|
|
101
|
+
mime,
|
|
102
|
+
format: formatOf(mime),
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
function passthrough(base64, mime, format) {
|
|
107
|
+
let width = 0, height = 0
|
|
108
|
+
try {
|
|
109
|
+
const bytes = base64ToBytes(base64)
|
|
110
|
+
const size = imageDimensions(bytes, mime)
|
|
111
|
+
if (size) ({ width, height } = size)
|
|
112
|
+
} catch {}
|
|
113
|
+
return { data: base64, width, height, mime, format: formatOf(format || mime) }
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
async function downscaleBitmap(base64, mime = 'image/jpeg', maxWidth = 1280, format, quality = 75) {
|
|
117
|
+
const sourceMime = mimeOf(formatOf(mime), mime)
|
|
118
|
+
const targetMime = mimeOf(format || formatOf(sourceMime), sourceMime)
|
|
119
|
+
const limit = Math.floor(Number(maxWidth))
|
|
120
|
+
const original = passthrough(base64, sourceMime, formatOf(targetMime))
|
|
121
|
+
if (!Number.isFinite(limit) || limit <= 0 || !canvasSupported()) return original
|
|
122
|
+
const bitmap = await decodeImage(base64, sourceMime)
|
|
123
|
+
if (!bitmap) return original
|
|
124
|
+
try {
|
|
125
|
+
if (bitmap.width <= limit) return { ...original, width: bitmap.width, height: bitmap.height }
|
|
126
|
+
const width = Math.max(1, Math.round(limit))
|
|
127
|
+
const height = Math.max(1, Math.round(bitmap.height * width / bitmap.width))
|
|
128
|
+
const target = canvas2d(width, height)
|
|
129
|
+
if (!target) return { ...original, width: bitmap.width, height: bitmap.height }
|
|
130
|
+
target.ctx.imageSmoothingEnabled = true
|
|
131
|
+
target.ctx.imageSmoothingQuality = 'high'
|
|
132
|
+
target.ctx.drawImage(bitmap, 0, 0, width, height)
|
|
133
|
+
return await encodeCanvas(target.canvas, targetMime, quality)
|
|
134
|
+
} finally {
|
|
135
|
+
bitmap.close?.()
|
|
136
|
+
}
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
function colorForRole(role) {
|
|
140
|
+
const r = String(role || '').toLowerCase()
|
|
141
|
+
if (r === 'link') return '#2563eb'
|
|
142
|
+
if (r === 'button' || r === 'tab' || r.startsWith('menuitem')) return '#ea580c'
|
|
143
|
+
if (['textbox', 'searchbox', 'combobox', 'listbox', 'checkbox', 'radio', 'switch', 'slider', 'spinbutton', 'file', 'date', 'time', 'color'].includes(r)) return '#16a34a'
|
|
144
|
+
return '#dc2626'
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
function rounded(ctx, x, y, width, height, radius) {
|
|
148
|
+
const r = Math.min(radius, width / 2, height / 2)
|
|
149
|
+
ctx.beginPath()
|
|
150
|
+
ctx.moveTo(x + r, y)
|
|
151
|
+
ctx.lineTo(x + width - r, y)
|
|
152
|
+
ctx.quadraticCurveTo(x + width, y, x + width, y + r)
|
|
153
|
+
ctx.lineTo(x + width, y + height - r)
|
|
154
|
+
ctx.quadraticCurveTo(x + width, y + height, x + width - r, y + height)
|
|
155
|
+
ctx.lineTo(x + r, y + height)
|
|
156
|
+
ctx.quadraticCurveTo(x, y + height, x, y + height - r)
|
|
157
|
+
ctx.lineTo(x, y + r)
|
|
158
|
+
ctx.quadraticCurveTo(x, y, x + r, y)
|
|
159
|
+
ctx.closePath()
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
const overlap = (a, b) => a.x < b.x + b.width && a.x + a.width > b.x && a.y < b.y + b.height && a.y + a.height > b.y
|
|
163
|
+
|
|
164
|
+
async function drawMarks(base64, mime = 'image/jpeg', marks = [], viewportWidth = null, format, quality = 82) {
|
|
165
|
+
const sourceMime = mimeOf(formatOf(mime), mime)
|
|
166
|
+
const targetMime = mimeOf(format || formatOf(sourceMime), sourceMime)
|
|
167
|
+
const original = passthrough(base64, sourceMime, formatOf(targetMime))
|
|
168
|
+
if (!Array.isArray(marks) || !marks.length || !canvasSupported()) return original
|
|
169
|
+
const bitmap = await decodeImage(base64, sourceMime)
|
|
170
|
+
if (!bitmap) return original
|
|
171
|
+
try {
|
|
172
|
+
const target = canvas2d(bitmap.width, bitmap.height)
|
|
173
|
+
if (!target) return { ...original, width: bitmap.width, height: bitmap.height }
|
|
174
|
+
const { canvas, ctx } = target
|
|
175
|
+
ctx.drawImage(bitmap, 0, 0)
|
|
176
|
+
const vw = Number(viewportWidth)
|
|
177
|
+
const ratio = vw > 0 ? Math.max(0.25, Math.min(8, bitmap.width / vw)) : 1
|
|
178
|
+
const list = marks
|
|
179
|
+
.filter(m => m && Number.isFinite(Number(m.x)) && Number.isFinite(Number(m.y)) && Number(m.width) > 1 && Number(m.height) > 1)
|
|
180
|
+
.slice(0, 80)
|
|
181
|
+
if (!list.length) return { data: base64, width: bitmap.width, height: bitmap.height, mime: sourceMime, format: formatOf(sourceMime) }
|
|
182
|
+
|
|
183
|
+
const line = Math.max(1.5, 1.5 * ratio)
|
|
184
|
+
for (const mark of list) {
|
|
185
|
+
const color = colorForRole(mark.role)
|
|
186
|
+
const x = Number(mark.x) * ratio
|
|
187
|
+
const y = Number(mark.y) * ratio
|
|
188
|
+
const width = Math.max(2, Number(mark.width) * ratio)
|
|
189
|
+
const height = Math.max(2, Number(mark.height) * ratio)
|
|
190
|
+
ctx.save()
|
|
191
|
+
ctx.globalAlpha = 0.9
|
|
192
|
+
ctx.lineWidth = line
|
|
193
|
+
ctx.strokeStyle = color
|
|
194
|
+
ctx.strokeRect(Math.max(0, x), Math.max(0, y), Math.min(width, canvas.width), Math.min(height, canvas.height))
|
|
195
|
+
ctx.restore()
|
|
196
|
+
}
|
|
197
|
+
|
|
198
|
+
const fontSize = Math.max(10, Math.round(12 * ratio))
|
|
199
|
+
const padX = Math.max(3, 4 * ratio)
|
|
200
|
+
const padY = Math.max(2, 2.5 * ratio)
|
|
201
|
+
const gap = Math.max(2, 2 * ratio)
|
|
202
|
+
ctx.font = `700 ${fontSize}px system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif`
|
|
203
|
+
ctx.textBaseline = 'middle'
|
|
204
|
+
const placed = []
|
|
205
|
+
for (const mark of list) {
|
|
206
|
+
const text = `[${mark.ref}]`
|
|
207
|
+
const metrics = ctx.measureText(text)
|
|
208
|
+
const labelWidth = Math.ceil(metrics.width + padX * 2)
|
|
209
|
+
const labelHeight = Math.ceil(fontSize + padY * 2)
|
|
210
|
+
const ex = Number(mark.x) * ratio
|
|
211
|
+
const ey = Number(mark.y) * ratio
|
|
212
|
+
const box = {
|
|
213
|
+
x: Math.max(1, Math.min(canvas.width - labelWidth - 1, ex)),
|
|
214
|
+
y: 0,
|
|
215
|
+
width: labelWidth,
|
|
216
|
+
height: labelHeight,
|
|
217
|
+
}
|
|
218
|
+
const above = ey - labelHeight - gap
|
|
219
|
+
box.y = above >= 1 ? above : Math.max(1, ey + gap)
|
|
220
|
+
box.y = Math.max(1, Math.min(canvas.height - labelHeight - 1, box.y))
|
|
221
|
+
for (let tries = 0; tries < 10 && placed.some(p => overlap(box, p)); tries++) {
|
|
222
|
+
box.y = Math.max(1, Math.min(canvas.height - labelHeight - 1, box.y + labelHeight + gap))
|
|
223
|
+
}
|
|
224
|
+
placed.push(box)
|
|
225
|
+
const color = colorForRole(mark.role)
|
|
226
|
+
ctx.save()
|
|
227
|
+
ctx.shadowColor = 'rgba(0, 0, 0, 0.55)'
|
|
228
|
+
ctx.shadowBlur = Math.max(1, 1.5 * ratio)
|
|
229
|
+
ctx.shadowOffsetY = Math.max(0.5, ratio)
|
|
230
|
+
rounded(ctx, box.x, box.y, box.width, box.height, Math.max(3, 4 * ratio))
|
|
231
|
+
ctx.fillStyle = color
|
|
232
|
+
ctx.fill()
|
|
233
|
+
ctx.shadowColor = 'transparent'
|
|
234
|
+
ctx.lineWidth = Math.max(1, 1.1 * ratio)
|
|
235
|
+
ctx.strokeStyle = '#ffffff'
|
|
236
|
+
ctx.stroke()
|
|
237
|
+
ctx.fillStyle = '#ffffff'
|
|
238
|
+
ctx.fillText(text, box.x + padX, box.y + box.height / 2 + 0.5)
|
|
239
|
+
ctx.restore()
|
|
240
|
+
}
|
|
241
|
+
return { ...await encodeCanvas(canvas, targetMime, quality), marked: true }
|
|
242
|
+
} finally {
|
|
243
|
+
bitmap.close?.()
|
|
244
|
+
}
|
|
245
|
+
}
|
|
246
|
+
|
|
247
|
+
function frameData(frame) {
|
|
248
|
+
if (typeof frame === 'string') return frame
|
|
249
|
+
return frame && (frame.jpeg || frame.data) || ''
|
|
250
|
+
}
|
|
251
|
+
|
|
252
|
+
function rgbaToRgbBase64(rgba, pixels) {
|
|
253
|
+
const out = new Uint8Array(pixels * 3)
|
|
254
|
+
for (let i = 0, j = 0; i < pixels * 4; i += 4, j += 3) {
|
|
255
|
+
out[j] = rgba[i]
|
|
256
|
+
out[j + 1] = rgba[i + 1]
|
|
257
|
+
out[j + 2] = rgba[i + 2]
|
|
258
|
+
}
|
|
259
|
+
return bytesToBase64(out)
|
|
260
|
+
}
|
|
261
|
+
|
|
262
|
+
async function decodeFramesToRgb(frames, targetWidth = 640) {
|
|
263
|
+
const list = Array.isArray(frames) ? frames : []
|
|
264
|
+
const result = { width: 0, height: 0, frames: [] }
|
|
265
|
+
if (!list.length || !canvasSupported()) return result
|
|
266
|
+
let width = 0, height = 0
|
|
267
|
+
for (let index = 0; index < list.length; index++) {
|
|
268
|
+
const bitmap = await decodeImage(frameData(list[index]), 'image/jpeg')
|
|
269
|
+
if (!bitmap) continue
|
|
270
|
+
try {
|
|
271
|
+
if (!width) {
|
|
272
|
+
width = even(Math.min(Math.max(2, Number(targetWidth) || 640), bitmap.width))
|
|
273
|
+
height = even(bitmap.height * width / bitmap.width)
|
|
274
|
+
result.width = width
|
|
275
|
+
result.height = height
|
|
276
|
+
}
|
|
277
|
+
const target = canvas2d(width, height, true)
|
|
278
|
+
if (!target) continue
|
|
279
|
+
target.ctx.imageSmoothingEnabled = true
|
|
280
|
+
target.ctx.imageSmoothingQuality = 'high'
|
|
281
|
+
target.ctx.drawImage(bitmap, 0, 0, width, height)
|
|
282
|
+
const data = target.ctx.getImageData(0, 0, width, height).data
|
|
283
|
+
result.frames.push({
|
|
284
|
+
index,
|
|
285
|
+
atMs: Number(list[index] && list[index].atMs) || 0,
|
|
286
|
+
width,
|
|
287
|
+
height,
|
|
288
|
+
rgb: rgbaToRgbBase64(data, width * height),
|
|
289
|
+
})
|
|
290
|
+
} finally {
|
|
291
|
+
bitmap.close?.()
|
|
292
|
+
}
|
|
293
|
+
}
|
|
294
|
+
if (!result.frames.length) result.width = result.height = 0
|
|
295
|
+
return result
|
|
296
|
+
}
|
|
297
|
+
|
|
298
|
+
async function buildContactSheet(frames, { cols = 3, rows = 3, cellWidth = 360 } = {}) {
|
|
299
|
+
const list = (Array.isArray(frames) ? frames : []).filter(frameData)
|
|
300
|
+
if (!list.length || !canvasSupported()) return null
|
|
301
|
+
const count = Math.min(Math.max(1, cols * rows), list.length)
|
|
302
|
+
const picked = []
|
|
303
|
+
if (count === list.length) picked.push(...list.map((frame, index) => ({ frame, index })))
|
|
304
|
+
else {
|
|
305
|
+
for (let i = 0; i < count; i++) {
|
|
306
|
+
const index = Math.round(i * (list.length - 1) / Math.max(1, count - 1))
|
|
307
|
+
picked.push({ frame: list[index], index })
|
|
308
|
+
}
|
|
309
|
+
}
|
|
310
|
+
const first = await decodeImage(frameData(picked[0].frame), 'image/jpeg')
|
|
311
|
+
if (!first) return null
|
|
312
|
+
let cw, ch
|
|
313
|
+
try {
|
|
314
|
+
cw = even(cellWidth)
|
|
315
|
+
ch = even(first.height * cw / first.width)
|
|
316
|
+
} finally {
|
|
317
|
+
first.close?.()
|
|
318
|
+
}
|
|
319
|
+
const target = canvas2d(cw * cols, ch * rows)
|
|
320
|
+
if (!target) return null
|
|
321
|
+
const { canvas, ctx } = target
|
|
322
|
+
ctx.fillStyle = '#111827'
|
|
323
|
+
ctx.fillRect(0, 0, canvas.width, canvas.height)
|
|
324
|
+
const base = Number(list[0] && list[0].atMs) || 0
|
|
325
|
+
for (let i = 0; i < picked.length; i++) {
|
|
326
|
+
const bitmap = await decodeImage(frameData(picked[i].frame), 'image/jpeg')
|
|
327
|
+
if (!bitmap) continue
|
|
328
|
+
try {
|
|
329
|
+
const x = (i % cols) * cw
|
|
330
|
+
const y = Math.floor(i / cols) * ch
|
|
331
|
+
ctx.imageSmoothingEnabled = true
|
|
332
|
+
ctx.imageSmoothingQuality = 'high'
|
|
333
|
+
ctx.drawImage(bitmap, x, y, cw, ch)
|
|
334
|
+
const at = Number(picked[i].frame && picked[i].frame.atMs) || 0
|
|
335
|
+
const label = `${((at - base) / 1000).toFixed(1)}s`
|
|
336
|
+
ctx.font = '700 13px system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif'
|
|
337
|
+
ctx.textBaseline = 'middle'
|
|
338
|
+
const w = Math.ceil(ctx.measureText(label).width + 12)
|
|
339
|
+
ctx.fillStyle = 'rgba(0, 0, 0, 0.68)'
|
|
340
|
+
rounded(ctx, x + 5, y + 5, w, 22, 5)
|
|
341
|
+
ctx.fill()
|
|
342
|
+
ctx.fillStyle = '#ffffff'
|
|
343
|
+
ctx.fillText(label, x + 11, y + 16)
|
|
344
|
+
} finally {
|
|
345
|
+
bitmap.close?.()
|
|
346
|
+
}
|
|
347
|
+
}
|
|
348
|
+
return encodeCanvas(canvas, 'image/jpeg', 76)
|
|
349
|
+
}
|
|
350
|
+
|
|
351
|
+
export {
|
|
352
|
+
base64ToBytes,
|
|
353
|
+
bytesToBase64,
|
|
354
|
+
buildContactSheet,
|
|
355
|
+
decodeFramesToRgb,
|
|
356
|
+
downscaleBitmap,
|
|
357
|
+
drawMarks,
|
|
358
|
+
imageDimensions,
|
|
359
|
+
jpegDimensions,
|
|
360
|
+
mimeOf,
|
|
361
|
+
}
|
|
@@ -0,0 +1,111 @@
|
|
|
1
|
+
import { handle } from './dispatch.js'
|
|
2
|
+
import { releaseAllTabs } from './popup-api.js'
|
|
3
|
+
import { CHUNK_CHARS, HOST_NAME, MAX_INBOX_BYTES, RECONNECT_MAX_MS, RECONNECT_MS, S } from './state.js'
|
|
4
|
+
import { log, updateBadge } from './util.js'
|
|
5
|
+
|
|
6
|
+
function sweepInbox() {
|
|
7
|
+
clearTimeout(S.inboxSweep)
|
|
8
|
+
const now = Date.now()
|
|
9
|
+
for (const [id, entry] of S.inbox) if (now - entry.at >= 30000) S.inbox.delete(id)
|
|
10
|
+
if (S.inbox.size) {
|
|
11
|
+
const oldest = Math.min(...[...S.inbox.values()].map(e => e.at))
|
|
12
|
+
S.inboxSweep = setTimeout(sweepInbox, oldest + 30000 - now)
|
|
13
|
+
}
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
function receiveChunk({ id, chunk: { seq, total }, data }) {
|
|
17
|
+
sweepInbox()
|
|
18
|
+
if (!Number.isInteger(total) || total < 1 || total > 65536 ||
|
|
19
|
+
!Number.isInteger(seq) || seq < 0 || seq >= total || typeof data !== 'string') return
|
|
20
|
+
let entry = S.inbox.get(id)
|
|
21
|
+
if (!entry && S.inbox.size >= 64) return
|
|
22
|
+
if (!entry) entry = { parts: new Array(total), received: 0, bytes: 0, at: Date.now() }
|
|
23
|
+
if (entry.parts.length !== total || entry.parts[seq] !== undefined) return
|
|
24
|
+
const bytes = new TextEncoder().encode(data).byteLength
|
|
25
|
+
if ([...S.inbox.values()].reduce((sum, e) => sum + e.bytes, bytes) > MAX_INBOX_BYTES) {
|
|
26
|
+
S.inbox.delete(id)
|
|
27
|
+
return
|
|
28
|
+
}
|
|
29
|
+
entry.parts[seq] = data
|
|
30
|
+
entry.received++
|
|
31
|
+
entry.bytes += bytes
|
|
32
|
+
if (entry.received === total) {
|
|
33
|
+
S.inbox.delete(id)
|
|
34
|
+
try { handle(JSON.parse(entry.parts.join(''))) } catch (e) { log('重组失败:', String(e)) }
|
|
35
|
+
} else S.inbox.set(id, entry)
|
|
36
|
+
sweepInbox()
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
function connect() {
|
|
40
|
+
if (S.port || S.connecting) return
|
|
41
|
+
S.connecting = true
|
|
42
|
+
try {
|
|
43
|
+
S.port = chrome.runtime.connectNative(HOST_NAME)
|
|
44
|
+
} catch (e) {
|
|
45
|
+
S.connecting = false
|
|
46
|
+
log('connectNative 失败:', String(e))
|
|
47
|
+
return
|
|
48
|
+
}
|
|
49
|
+
S.connecting = false
|
|
50
|
+
|
|
51
|
+
const source = S.port
|
|
52
|
+
S.port.onMessage.addListener((msg) => {
|
|
53
|
+
if (S.port !== source) return
|
|
54
|
+
S.disconnectReason = S.disconnectError = null
|
|
55
|
+
S.retryMs = RECONNECT_MS // 宿主回过话才算真连上(connectNative 本身总是「成功」),退避清零
|
|
56
|
+
if (msg && msg.id && msg.chunk) {
|
|
57
|
+
receiveChunk(msg)
|
|
58
|
+
return
|
|
59
|
+
}
|
|
60
|
+
handle(msg)
|
|
61
|
+
})
|
|
62
|
+
|
|
63
|
+
S.port.onDisconnect.addListener(() => {
|
|
64
|
+
const err = chrome.runtime.lastError
|
|
65
|
+
if (S.port !== source) return
|
|
66
|
+
S.disconnectError = err?.message || ''
|
|
67
|
+
S.disconnectReason = /not found|not registered/i.test(S.disconnectError) ? 'hostNotFound'
|
|
68
|
+
: /forbidden|not allowed/i.test(S.disconnectError) ? 'forbidden'
|
|
69
|
+
: /exited|closed|terminated/i.test(S.disconnectError) ? 'hostExited' : 'other'
|
|
70
|
+
S.inbox.clear()
|
|
71
|
+
sweepInbox()
|
|
72
|
+
if (err) log('原生宿主断开:', err.message)
|
|
73
|
+
S.port = null
|
|
74
|
+
S.hostInfo = null
|
|
75
|
+
S.activeHandoff?.resolve({ done: false, aborted: true })
|
|
76
|
+
releaseAllTabs().catch(e => log(String(e)))
|
|
77
|
+
updateBadge()
|
|
78
|
+
setTimeout(connect, S.retryMs)
|
|
79
|
+
S.retryMs = Math.min(S.retryMs * 2, RECONNECT_MAX_MS)
|
|
80
|
+
})
|
|
81
|
+
|
|
82
|
+
log('已连上原生宿主')
|
|
83
|
+
// 主动报到:宿主靠它判断扩展是否在线(否则宿主刚起来时会误判"扩展未连接")
|
|
84
|
+
post({ ...S.helloMetadata, hello: true, version: chrome.runtime.getManifest().version })
|
|
85
|
+
post({ hostPing: true })
|
|
86
|
+
// connectNative 本身总是「成功」,宿主起不来要等 onDisconnect —— 稳住一会儿再把角标的 ! 收掉
|
|
87
|
+
setTimeout(() => { if (S.port) updateBadge() }, 1500)
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
function post(msg) {
|
|
91
|
+
if (!S.port) { log('postMessage 失败: 宿主已断开'); return false }
|
|
92
|
+
try {
|
|
93
|
+
const s = JSON.stringify(msg)
|
|
94
|
+
if (s.length <= CHUNK_CHARS) {
|
|
95
|
+
S.port.postMessage(msg)
|
|
96
|
+
return true
|
|
97
|
+
}
|
|
98
|
+
const total = Math.ceil(s.length / CHUNK_CHARS)
|
|
99
|
+
for (let i = 0; i < total; i++) {
|
|
100
|
+
S.port.postMessage({ id: msg.id, chunk: { seq: i, total }, data: s.slice(i * CHUNK_CHARS, (i + 1) * CHUNK_CHARS) })
|
|
101
|
+
}
|
|
102
|
+
return true
|
|
103
|
+
} catch (e) {
|
|
104
|
+
log('postMessage 失败:', String(e))
|
|
105
|
+
return false
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
export {
|
|
110
|
+
sweepInbox, receiveChunk, connect, post,
|
|
111
|
+
}
|