dsh-chrome-bridge 2.0.2 → 2.1.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 +1 -1
- package/README.md +1 -1
- package/core/ext.js +44 -15
- package/core/hosts.js +3 -4
- package/core/receipt.js +228 -31
- package/core/session.js +11 -14
- package/core/tooldef.js +10 -1
- package/core/transport.js +67 -17
- package/drag.js +118 -36
- package/features/frames.js +5 -2
- package/i18n/en.js +37 -7
- package/i18n/ru.js +37 -7
- package/i18n/zh.js +37 -7
- package/nav.js +103 -32
- package/package.json +1 -1
- package/refs.js +317 -77
- package/tools/batch.js +311 -0
- package/tools/click.js +37 -11
- package/tools/dialog.js +19 -2
- package/tools/drag.js +52 -9
- package/tools/fill.js +71 -22
- package/tools/index.js +3 -1
- package/tools/input.js +97 -36
- package/tools/nav.js +46 -23
- package/tools/read.js +2 -0
- package/tools/screenshot.js +10 -1
- package/tools/tabs.js +16 -2
- package/tools/wait.js +14 -6
- package/vendor/cb +3 -2
- package/vendor/extension/in-page-cursor.js +49 -11
- package/vendor/extension/manifest.json +1 -1
- package/vendor/extension/popup.css +1 -0
- package/vendor/extension/popup.html +6 -3
- package/vendor/extension/popup.js +19 -13
- package/vendor/extension/sw/actions.js +573 -0
- package/vendor/extension/sw/boot.js +7 -1
- package/vendor/extension/sw/cdp.js +219 -33
- package/vendor/extension/sw/conditions.js +176 -0
- package/vendor/extension/sw/confirm.js +39 -7
- package/vendor/extension/sw/cursor.js +534 -49
- package/vendor/extension/sw/dispatch.js +43 -8
- package/vendor/extension/sw/frames.js +643 -49
- package/vendor/extension/sw/imaging.js +35 -17
- package/vendor/extension/sw/lifecycle.js +82 -0
- package/vendor/extension/sw/ops.js +1028 -148
- package/vendor/extension/sw/page-access.js +292 -0
- package/vendor/extension/sw/popup-api.js +42 -10
- package/vendor/extension/sw/record.js +22 -5
- package/vendor/extension/sw/snapshot.js +251 -0
- package/vendor/extension/sw/state.js +24 -5
- package/vendor/extension/sw/tabui.js +10 -2
- package/vendor/in-page-cursor.js +49 -11
- package/vendor/skill/SKILL.md +8 -7
|
@@ -0,0 +1,292 @@
|
|
|
1
|
+
import { areAllFramesAllowed, hasProtectedSites, isUrlAllowed, loadProtectedSites, matchesProtectedSite } from './confirm.js'
|
|
2
|
+
import { evaluate } from './cdp.js'
|
|
3
|
+
import { isSensitiveField } from './frames.js'
|
|
4
|
+
import { S } from './state.js'
|
|
5
|
+
import { isExtUrl, timeLeft } from './util.js'
|
|
6
|
+
|
|
7
|
+
export function originOf(url) {
|
|
8
|
+
try { return new URL(url).origin } catch { return '' }
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
export function isOpaqueOrigin(origin) {
|
|
12
|
+
return !origin || origin === 'null'
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
export function hashUrl(url) {
|
|
16
|
+
const s = String(url || '')
|
|
17
|
+
let h1 = 0x811c9dc5, h2 = 0x9e3779b9
|
|
18
|
+
for (let i = 0; i < s.length; i++) {
|
|
19
|
+
const c = s.charCodeAt(i)
|
|
20
|
+
h1 = Math.imul(h1 ^ c, 0x01000193)
|
|
21
|
+
h2 = Math.imul(h2 ^ c, 0x85ebca6b)
|
|
22
|
+
}
|
|
23
|
+
return (h1 >>> 0).toString(16).padStart(8, '0') + (h2 >>> 0).toString(16).padStart(8, '0')
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
// Metadata is inspected outside the document. Protected titles, paths, dialogs and live regions never enter a receipt.
|
|
27
|
+
export async function pageAccess(id, { autoObserve = false, expectedOrigin, expectedUrl, frames = true } = {}) {
|
|
28
|
+
let tab
|
|
29
|
+
try { tab = await chrome.tabs.get(id) } catch (error) {
|
|
30
|
+
if (autoObserve) return { url: '', title: '', reason: 'protected' }
|
|
31
|
+
throw error
|
|
32
|
+
}
|
|
33
|
+
const pendingUrl = tab.pendingUrl || ''
|
|
34
|
+
const committedUrl = tab.url || ''
|
|
35
|
+
const url = pendingUrl || committedUrl
|
|
36
|
+
const urls = [pendingUrl, committedUrl, S.tabUrl.get(id)].filter(Boolean)
|
|
37
|
+
let protectedSite = urls.some(isExtUrl)
|
|
38
|
+
if (!protectedSite && hasProtectedSites()) {
|
|
39
|
+
protectedSite = !await loadProtectedSites() || !urls.length ||
|
|
40
|
+
urls.some(u => !isUrlAllowed(id, u) || (autoObserve && matchesProtectedSite(u))) ||
|
|
41
|
+
(frames && !await areAllFramesAllowed(id, autoObserve))
|
|
42
|
+
}
|
|
43
|
+
if (protectedSite) return { url: originOf(urls.find(u => isExtUrl(u) || !isUrlAllowed(id, u) || (autoObserve && matchesProtectedSite(u))) || url), committedUrl: '', committedSameOrigin: false, title: '', reason: 'protected' }
|
|
44
|
+
const urlOrigin = originOf(url)
|
|
45
|
+
const committedOrigin = originOf(committedUrl)
|
|
46
|
+
const committedSameOrigin = Boolean(committedUrl) && (!pendingUrl || (!isOpaqueOrigin(committedOrigin) && committedOrigin === urlOrigin) || committedUrl === url)
|
|
47
|
+
const originMismatch = expectedOrigin !== undefined && (isOpaqueOrigin(expectedOrigin)
|
|
48
|
+
? (!expectedUrl || url !== expectedUrl || (Boolean(committedUrl) && committedUrl !== expectedUrl))
|
|
49
|
+
: (isOpaqueOrigin(urlOrigin) || expectedOrigin !== urlOrigin || (Boolean(committedUrl) && committedOrigin !== expectedOrigin)))
|
|
50
|
+
if (originMismatch) {
|
|
51
|
+
return { url: urlOrigin, committedUrl, committedSameOrigin, title: '', reason: 'origin_changed' }
|
|
52
|
+
}
|
|
53
|
+
return {
|
|
54
|
+
url,
|
|
55
|
+
committedUrl,
|
|
56
|
+
committedSameOrigin,
|
|
57
|
+
title: committedSameOrigin ? (tab.title || '') : '',
|
|
58
|
+
...(committedSameOrigin && S.dialogs.has(id) && { reason: 'dialog' }),
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
function liveRegions(masked) {
|
|
63
|
+
const secrets = new Set()
|
|
64
|
+
const collectSecrets = (root) => {
|
|
65
|
+
for (const el of root.querySelectorAll('*')) {
|
|
66
|
+
if (secrets.size >= 50) return
|
|
67
|
+
if (el.shadowRoot) collectSecrets(el.shadowRoot)
|
|
68
|
+
if (el.tagName === 'IFRAME' || el.tagName === 'FRAME') {
|
|
69
|
+
try { if (el.contentDocument?.body) collectSecrets(el.contentDocument) } catch {}
|
|
70
|
+
}
|
|
71
|
+
if (el.tagName !== 'INPUT' && el.tagName !== 'TEXTAREA' && !el.isContentEditable) continue
|
|
72
|
+
const view = el.ownerDocument?.defaultView || window
|
|
73
|
+
if (masked(el, view.getComputedStyle(el), true)) {
|
|
74
|
+
const value = el.isContentEditable ? el.innerText : el.value
|
|
75
|
+
if (value && String(value).length >= 4) secrets.add(String(value).slice(0, 512))
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
collectSecrets(document)
|
|
80
|
+
const selfOrigin = typeof self !== 'undefined' && typeof self.origin === 'string' ? self.origin : location.origin
|
|
81
|
+
const live = []
|
|
82
|
+
const regions = document.querySelectorAll('[role="alert"],[role="status"],[aria-live="polite"],[aria-live="assertive"]')
|
|
83
|
+
if (!regions.length) return { live, secrets: [...secrets], selfOrigin }
|
|
84
|
+
const escRe = s => s.replace(/[.*+?^${}()|[\]\\]/g, '\\$&')
|
|
85
|
+
const enc = typeof TextEncoder === 'function' ? new TextEncoder() : null
|
|
86
|
+
const buildRegex = (secret) => {
|
|
87
|
+
if (!enc || secret.length > 256) return null
|
|
88
|
+
let pat = ''
|
|
89
|
+
for (const ch of secret) {
|
|
90
|
+
if (ch === ' ') { pat += '(?: |%20|\\+)'; continue }
|
|
91
|
+
if (/^[a-zA-Z0-9]$/.test(ch)) { pat += ch; continue }
|
|
92
|
+
let pct = ''
|
|
93
|
+
for (const b of enc.encode(ch)) {
|
|
94
|
+
const h = b.toString(16).padStart(2, '0')
|
|
95
|
+
pct += '%' + h.replace(/[a-f]/g, c => '[' + c + c.toUpperCase() + ']')
|
|
96
|
+
}
|
|
97
|
+
pat += '(?:' + escRe(ch) + '|' + pct + ')'
|
|
98
|
+
}
|
|
99
|
+
return new RegExp(pat, 'g')
|
|
100
|
+
}
|
|
101
|
+
const rules = [...secrets].sort((a, b) => b.length - a.length).map(value => {
|
|
102
|
+
const re = buildRegex(value)
|
|
103
|
+
if (re) return { value, re, extras: [] }
|
|
104
|
+
const extras = new Set()
|
|
105
|
+
try {
|
|
106
|
+
const e1 = encodeURIComponent(value)
|
|
107
|
+
if (e1 && e1 !== value) { extras.add(e1); if (e1.includes('%20')) extras.add(e1.replace(/%20/g, '+')) }
|
|
108
|
+
const e2 = typeof URLSearchParams === 'function' ? new URLSearchParams([['x', value]]).toString().slice(2) : ''
|
|
109
|
+
if (e2 && e2 !== value) { extras.add(e2); if (e2.includes('+')) extras.add(e2.replace(/\+/g, '%20')) }
|
|
110
|
+
} catch {}
|
|
111
|
+
return { value, re: null, extras: [...extras].map(v => new RegExp(escRe(v), 'gi')) }
|
|
112
|
+
})
|
|
113
|
+
for (const el of regions) {
|
|
114
|
+
const st = getComputedStyle(el)
|
|
115
|
+
if (!el.getClientRects().length || st.visibility !== 'visible' || st.opacity === '0') continue
|
|
116
|
+
if ([el, ...el.querySelectorAll('input,textarea,[contenteditable]')].some(node => masked(node, getComputedStyle(node), true))) continue
|
|
117
|
+
let safe = el.innerText || ''
|
|
118
|
+
for (const { value, re, extras } of rules) {
|
|
119
|
+
if (re) safe = safe.replace(re, '••••')
|
|
120
|
+
else {
|
|
121
|
+
safe = safe.replaceAll(value, '••••')
|
|
122
|
+
for (const x of extras) safe = safe.replace(x, '••••')
|
|
123
|
+
}
|
|
124
|
+
}
|
|
125
|
+
const normText = safe.replace(/\s+/g, ' ').trim()
|
|
126
|
+
const text = normText.length > 16384 ? '__DSH_TOO_LONG__' : normText
|
|
127
|
+
if (text && !live.includes(text)) live.push(text)
|
|
128
|
+
if (live.length >= 30) break
|
|
129
|
+
}
|
|
130
|
+
return { live, secrets: [...secrets], selfOrigin }
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
export async function receiptState(id, deadline, guard, _secrets = [], beforeOrigin, beforeUrl) {
|
|
134
|
+
const accessOpts = beforeOrigin !== undefined
|
|
135
|
+
? { expectedOrigin: beforeOrigin, ...(isOpaqueOrigin(beforeOrigin) && beforeUrl ? { expectedUrl: beforeUrl } : {}) }
|
|
136
|
+
: {}
|
|
137
|
+
const page = await pageAccess(id, accessOpts)
|
|
138
|
+
if (page.reason) return { ...page, selfOrigin: '', navGeneration: S.mainNavGeneration.get(Number(id)) || 0, live: [], secrets: [] }
|
|
139
|
+
const expected = originOf(page.url)
|
|
140
|
+
const opaque = isOpaqueOrigin(expected)
|
|
141
|
+
const originCheck = opaque ? `location.href === ${JSON.stringify(page.url)}` : `location.origin === ${JSON.stringify(expected)}`
|
|
142
|
+
// Never serialize cached SW secrets into any page world; SW redacts complete fields before truncation.
|
|
143
|
+
const expression = `${originCheck} ? { docToken: (globalThis.performance?.timeOrigin ?? (window.__dshDocToken ||= Math.random().toString(36).slice(2))), ...(${liveRegions})(${isSensitiveField}) } : null`
|
|
144
|
+
let state
|
|
145
|
+
try { state = await evaluate(id, expression, false, Math.min(1200, timeLeft(deadline)), guard) }
|
|
146
|
+
catch { guard?.() }
|
|
147
|
+
const after = await pageAccess(id, accessOpts)
|
|
148
|
+
if (after.reason === 'protected' || after.reason === 'origin_changed') {
|
|
149
|
+
return { ...after, selfOrigin: '', navGeneration: S.mainNavGeneration.get(Number(id)) || 0, live: [], secrets: [] }
|
|
150
|
+
}
|
|
151
|
+
const valid = !after.reason && page.url === after.url && state && typeof state === 'object'
|
|
152
|
+
const selfOrigin = valid && typeof state.selfOrigin === 'string' ? state.selfOrigin : ''
|
|
153
|
+
const foundSecrets = valid && Array.isArray(state.secrets) ? state.secrets : []
|
|
154
|
+
if (isOpaqueOrigin(selfOrigin) || opaque) {
|
|
155
|
+
S.recentSecrets.delete(Number(id))
|
|
156
|
+
} else if (foundSecrets.length && !after.reason) {
|
|
157
|
+
rememberSecrets(id, selfOrigin, foundSecrets)
|
|
158
|
+
}
|
|
159
|
+
return {
|
|
160
|
+
...after,
|
|
161
|
+
selfOrigin,
|
|
162
|
+
navGeneration: S.mainNavGeneration.get(Number(id)) || 0,
|
|
163
|
+
docToken: valid ? state.docToken : undefined,
|
|
164
|
+
live: valid && Array.isArray(state.live) ? state.live : [],
|
|
165
|
+
secrets: foundSecrets,
|
|
166
|
+
}
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
const SECRET_TTL_MS = 15_000
|
|
170
|
+
|
|
171
|
+
export function rememberSecrets(tabId, origin, secrets) {
|
|
172
|
+
const id = Number(tabId)
|
|
173
|
+
if (isOpaqueOrigin(origin)) {
|
|
174
|
+
S.recentSecrets.delete(id)
|
|
175
|
+
return
|
|
176
|
+
}
|
|
177
|
+
if (!Array.isArray(secrets) || !secrets.length) return
|
|
178
|
+
const now = Date.now()
|
|
179
|
+
const prev = S.recentSecrets.get(id)
|
|
180
|
+
const values = prev && prev.origin === origin && prev.expiresAt > now ? prev.values : new Set()
|
|
181
|
+
for (const v of secrets) {
|
|
182
|
+
if (typeof v === 'string' && v.length >= 4 && values.size < 50) values.add(v.slice(0, 512))
|
|
183
|
+
}
|
|
184
|
+
if (values.size) S.recentSecrets.set(id, { origin, expiresAt: now + SECRET_TTL_MS, values })
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
export function getRecentSecrets(tabId, origin) {
|
|
188
|
+
const id = Number(tabId)
|
|
189
|
+
if (isOpaqueOrigin(origin)) return []
|
|
190
|
+
const entry = S.recentSecrets.get(id)
|
|
191
|
+
if (!entry) return []
|
|
192
|
+
if (entry.expiresAt <= Date.now() || entry.origin !== origin) {
|
|
193
|
+
S.recentSecrets.delete(id)
|
|
194
|
+
return []
|
|
195
|
+
}
|
|
196
|
+
return [...entry.values]
|
|
197
|
+
}
|
|
198
|
+
|
|
199
|
+
export function makeSecretRedactor(secrets, minLen = 4) {
|
|
200
|
+
const ordered = [...new Set((secrets || []).filter(v => typeof v === 'string' && v.length >= minLen))].sort((a, b) => b.length - a.length)
|
|
201
|
+
if (!ordered.length) return text => String(text ?? '')
|
|
202
|
+
const escRe = s => s.replace(/[.*+?^${}()|[\]\\]/g, '\\$&')
|
|
203
|
+
const enc = new TextEncoder()
|
|
204
|
+
const rules = ordered.map(value => {
|
|
205
|
+
if (value.length <= 256) {
|
|
206
|
+
let pat = ''
|
|
207
|
+
for (const ch of value) {
|
|
208
|
+
if (ch === ' ') { pat += '(?: |%20|\\+)'; continue }
|
|
209
|
+
if (/^[a-zA-Z0-9]$/.test(ch)) { pat += ch; continue }
|
|
210
|
+
let pct = ''
|
|
211
|
+
for (const b of enc.encode(ch)) {
|
|
212
|
+
const h = b.toString(16).padStart(2, '0')
|
|
213
|
+
pct += '%' + h.replace(/[a-f]/g, c => '[' + c + c.toUpperCase() + ']')
|
|
214
|
+
}
|
|
215
|
+
pat += '(?:' + escRe(ch) + '|' + pct + ')'
|
|
216
|
+
}
|
|
217
|
+
return { value, re: new RegExp(pat, 'g'), extras: [] }
|
|
218
|
+
}
|
|
219
|
+
const extras = new Set()
|
|
220
|
+
try {
|
|
221
|
+
const e1 = encodeURIComponent(value)
|
|
222
|
+
if (e1 && e1 !== value) { extras.add(e1); if (e1.includes('%20')) extras.add(e1.replace(/%20/g, '+')) }
|
|
223
|
+
const e2 = new URLSearchParams([['x', value]]).toString().slice(2)
|
|
224
|
+
if (e2 && e2 !== value) { extras.add(e2); if (e2.includes('+')) extras.add(e2.replace(/\+/g, '%20')) }
|
|
225
|
+
} catch {}
|
|
226
|
+
return { value, re: null, extras: [...extras].map(v => new RegExp(escRe(v), 'gi')) }
|
|
227
|
+
})
|
|
228
|
+
return text => {
|
|
229
|
+
let out = String(text ?? '')
|
|
230
|
+
for (const { value, re, extras } of rules) {
|
|
231
|
+
if (re) out = out.replace(re, '••••')
|
|
232
|
+
else {
|
|
233
|
+
out = out.replaceAll(value, '••••')
|
|
234
|
+
for (const x of extras) out = out.replace(x, '••••')
|
|
235
|
+
}
|
|
236
|
+
}
|
|
237
|
+
return out
|
|
238
|
+
}
|
|
239
|
+
}
|
|
240
|
+
|
|
241
|
+
export function receiptOf(id, before, after, openedTabs, secrets = [], allowOriginChange = false) {
|
|
242
|
+
const protectedSite = after.reason === 'protected'
|
|
243
|
+
const originReason = after.reason === 'origin_changed'
|
|
244
|
+
const liveUpdate = after.live?.find(line => before.live && !before.live.includes(line)) || null
|
|
245
|
+
const beforeOrigin = originOf(before?.url)
|
|
246
|
+
const afterOrigin = originOf(after?.url)
|
|
247
|
+
const effBeforeOrigin = before?.selfOrigin !== undefined ? before.selfOrigin : beforeOrigin
|
|
248
|
+
const effAfterOrigin = after?.selfOrigin !== undefined ? after.selfOrigin : afterOrigin
|
|
249
|
+
const cacheOpaque = isOpaqueOrigin(afterOrigin) || isOpaqueOrigin(beforeOrigin) || isOpaqueOrigin(effAfterOrigin) || isOpaqueOrigin(effBeforeOrigin)
|
|
250
|
+
const hardOpaque = isOpaqueOrigin(afterOrigin) || isOpaqueOrigin(beforeOrigin) || after?.selfOrigin === 'null' || before?.selfOrigin === 'null'
|
|
251
|
+
const unverifiedOrigin = isOpaqueOrigin(effAfterOrigin) || isOpaqueOrigin(effBeforeOrigin)
|
|
252
|
+
const sameOrigin = !originReason && !cacheOpaque && effBeforeOrigin === effAfterOrigin && beforeOrigin === afterOrigin
|
|
253
|
+
const prior = sameOrigin ? getRecentSecrets(id, effAfterOrigin) : []
|
|
254
|
+
// Never store or reuse cached secrets when the actual security origin is opaque or unverified (e.g. dialog early return / sandboxed HTTPS).
|
|
255
|
+
if (protectedSite || originReason || isOpaqueOrigin(afterOrigin) || isOpaqueOrigin(effAfterOrigin)) {
|
|
256
|
+
S.recentSecrets.delete(Number(id))
|
|
257
|
+
} else if (sameOrigin) {
|
|
258
|
+
const sameOriginSecrets = [...(secrets || []), ...(before?.secrets || []), ...(after?.secrets || []), ...prior]
|
|
259
|
+
if (sameOriginSecrets.length) rememberSecrets(id, effAfterOrigin, sameOriginSecrets)
|
|
260
|
+
} else {
|
|
261
|
+
S.recentSecrets.delete(Number(id))
|
|
262
|
+
if (after?.secrets?.length) rememberSecrets(id, effAfterOrigin, after.secrets)
|
|
263
|
+
}
|
|
264
|
+
const allSecrets = [...(secrets || []), ...(before?.secrets || []), ...(after?.secrets || []), ...prior]
|
|
265
|
+
const redactMeta = makeSecretRedactor(allSecrets, 6)
|
|
266
|
+
const redactText = makeSecretRedactor(allSecrets, 4)
|
|
267
|
+
const committedOrigin = after.committedUrl ? originOf(after.committedUrl) : ''
|
|
268
|
+
const committedMatches = after.committedSameOrigin !== undefined
|
|
269
|
+
? Boolean(after.committedSameOrigin)
|
|
270
|
+
: after.committedUrl !== undefined
|
|
271
|
+
? (Boolean(after.committedUrl) && (!isOpaqueOrigin(committedOrigin) ? committedOrigin === afterOrigin : after.committedUrl === after.url))
|
|
272
|
+
: true
|
|
273
|
+
const navGenChanged = before?.navGeneration != null && after?.navGeneration != null && before.navGeneration !== after.navGeneration
|
|
274
|
+
const sameUrlReload = (before.docToken != null && after.docToken != null && before.docToken !== after.docToken) || navGenChanged
|
|
275
|
+
const originChanged = protectedSite || originReason || (hardOpaque
|
|
276
|
+
? (Boolean(before?.url) && (before.url !== after.url || sameUrlReload))
|
|
277
|
+
: (beforeOrigin !== afterOrigin || (unverifiedOrigin && sameUrlReload) || (before?.selfOrigin && after?.selfOrigin && before.selfOrigin !== after.selfOrigin)))
|
|
278
|
+
const hideDest = protectedSite || originReason || (!allowOriginChange && originChanged)
|
|
279
|
+
const dialog = !hideDest && committedMatches && S.dialogs.get(id)
|
|
280
|
+
const safeLive = hideDest || !committedMatches || !liveUpdate
|
|
281
|
+
? null
|
|
282
|
+
: liveUpdate === '__DSH_TOO_LONG__'
|
|
283
|
+
? (allSecrets.length ? '••••' : null)
|
|
284
|
+
: redactText(liveUpdate).slice(0, 120)
|
|
285
|
+
return { beforeUrl: redactMeta(before.url), beforeTitle: redactMeta(before.title), url: hideDest ? (isOpaqueOrigin(afterOrigin) ? '' : afterOrigin) : redactMeta(after.url), title: hideDest || !committedMatches ? '' : redactMeta(after.title),
|
|
286
|
+
documentToken: after.docToken, urlHash: after.url ? hashUrl(after.url) : '',
|
|
287
|
+
navigated: before.url !== after.url || sameUrlReload, originChanged,
|
|
288
|
+
protectedSite, pendingDialog: dialog ? { type: dialog.type, message: redactText(dialog.message || ''), at: dialog.at } : null,
|
|
289
|
+
liveUpdate: safeLive,
|
|
290
|
+
controlEpoch: S.controlEpoch.get(id) || 0, navGeneration: S.mainNavGeneration.get(id) || 0,
|
|
291
|
+
...(openedTabs?.length && { openedTabs: openedTabs.map(t => ({ ...t, url: redactMeta(t.url), title: redactMeta(t.title || '') })) }) }
|
|
292
|
+
}
|
|
@@ -2,7 +2,7 @@ import {
|
|
|
2
2
|
addProtectedSite, getPendingConfirm, getProtectedSites, matchesProtectedSite, normalizeSitePattern,
|
|
3
3
|
removeProtectedSite, resolveConfirmation, revokeAllGrants, revokeTabGrants,
|
|
4
4
|
} from './confirm.js'
|
|
5
|
-
import { hidePageCursor, markCursor } from './cursor.js'
|
|
5
|
+
import { bumpControlEpoch, hidePageCursor, isAgentInputActive, markCursor } from './cursor.js'
|
|
6
6
|
import { connect, post } from './native.js'
|
|
7
7
|
import { getOps } from './ops.js'
|
|
8
8
|
import { ACTIVITY_MAX, DETACH_IDLE_MS, S } from './state.js'
|
|
@@ -10,10 +10,12 @@ import { CURSOR_FAVICON } from './tabui.js'
|
|
|
10
10
|
import { controlledCount, isExtUrl, log, updateBadge } from './util.js'
|
|
11
11
|
|
|
12
12
|
function restorePrefs() {
|
|
13
|
-
S.prefsReady = chrome.storage.local.get(['paused', 'pauseReason', 'showCursor', 'ownWindow']).then((v) => {
|
|
13
|
+
S.prefsReady = chrome.storage.local.get(['paused', 'pauseReason', 'showCursor', 'cursorPace', 'lastCursorPace', 'ownWindow']).then((v) => {
|
|
14
14
|
S.paused = !!(v && v.paused)
|
|
15
15
|
S.pauseReason = S.paused ? v.pauseReason || null : null
|
|
16
|
-
S.
|
|
16
|
+
S.cursorPace = ['fast', 'smooth', 'off'].includes(v?.cursorPace) ? v.cursorPace : v?.showCursor === false ? 'off' : 'fast'
|
|
17
|
+
S.lastCursorPace = S.cursorPace === 'off' ? (v?.lastCursorPace === 'smooth' ? 'smooth' : 'fast') : S.cursorPace
|
|
18
|
+
S.showCursor = S.cursorPace !== 'off'
|
|
17
19
|
S.ownWindow = !(v && v.ownWindow === false)
|
|
18
20
|
updateBadge()
|
|
19
21
|
}).catch(() => {})
|
|
@@ -118,7 +120,7 @@ async function buildState() {
|
|
|
118
120
|
host: S.hostInfo,
|
|
119
121
|
idleMs: DETACH_IDLE_MS,
|
|
120
122
|
attachedCount: controlledCount(),
|
|
121
|
-
showCursor: S.showCursor,
|
|
123
|
+
showCursor: S.showCursor, cursorPace: S.cursorPace,
|
|
122
124
|
ownWindow: S.ownWindow,
|
|
123
125
|
protectedSites,
|
|
124
126
|
activeTabHost,
|
|
@@ -133,6 +135,8 @@ async function buildState() {
|
|
|
133
135
|
}
|
|
134
136
|
|
|
135
137
|
async function releaseTab(tabId) {
|
|
138
|
+
S.releasing.add(Number(tabId))
|
|
139
|
+
bumpControlEpoch(tabId)
|
|
136
140
|
revokeTabGrants(tabId) // 用户放开 = 解绑:受保护网站的放行跟着作废
|
|
137
141
|
await getOps().detach({ tabId })
|
|
138
142
|
S.ACTIVITY.push({ at: Date.now(), last: Date.now(), key: 'user', call: null, tool: null, hint: '',
|
|
@@ -163,6 +167,8 @@ async function diagnostics(state) {
|
|
|
163
167
|
// ── 用户开关(弹窗和 DSH 设置页共用)────────────────────────────────────────
|
|
164
168
|
|
|
165
169
|
async function releaseAllTabs() {
|
|
170
|
+
S.releaseAllEpoch += 1
|
|
171
|
+
for (const id of new Set([...S.attached, ...S.ensureInflight.keys(), ...S.tabQueues.keys()])) bumpControlEpoch(id)
|
|
166
172
|
revokeAllGrants('detached') // 全部放开 = 全部解绑:受保护网站的放行和正在等的确认一起作废
|
|
167
173
|
await Promise.all([...new Set([...S.attached, ...S.ensureInflight.keys()])].map(releaseTab))
|
|
168
174
|
}
|
|
@@ -172,6 +178,8 @@ async function setPaused(on, where) {
|
|
|
172
178
|
S.paused = !!on
|
|
173
179
|
S.pauseReason = S.paused ? where : null
|
|
174
180
|
if (S.paused) {
|
|
181
|
+
S.releaseAllEpoch += 1
|
|
182
|
+
for (const id of new Set([...S.attached, ...S.ensureInflight.keys(), ...S.tabQueues.keys()])) bumpControlEpoch(id)
|
|
175
183
|
S.activeHandoff?.resolve({ done: false, aborted: true })
|
|
176
184
|
revokeAllGrants('paused') // 受保护网站:放行全作废,正在等的确认按拒绝收掉
|
|
177
185
|
}
|
|
@@ -183,8 +191,25 @@ async function setPaused(on, where) {
|
|
|
183
191
|
}
|
|
184
192
|
|
|
185
193
|
async function setShowCursor(on, where) {
|
|
186
|
-
S.
|
|
187
|
-
|
|
194
|
+
return queueCursorPace(() => on ? S.lastCursorPace : 'off', where)
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
async function setCursorPace(pace, where) {
|
|
198
|
+
if (!['off', 'fast', 'smooth'].includes(pace)) throw new Error('Invalid cursorPace')
|
|
199
|
+
return queueCursorPace(() => pace, where)
|
|
200
|
+
}
|
|
201
|
+
|
|
202
|
+
function queueCursorPace(select, where) {
|
|
203
|
+
const run = () => saveCursorPace(select(), where)
|
|
204
|
+
const job = S.cursorPrefsWrite.then(run, run)
|
|
205
|
+
S.cursorPrefsWrite = job.catch(() => {})
|
|
206
|
+
return job
|
|
207
|
+
}
|
|
208
|
+
|
|
209
|
+
async function saveCursorPace(pace, where) {
|
|
210
|
+
const prefs = { cursorPace: pace, showCursor: pace !== 'off', lastCursorPace: pace === 'off' ? S.lastCursorPace : pace }
|
|
211
|
+
await chrome.storage.local.set(prefs)
|
|
212
|
+
Object.assign(S, prefs)
|
|
188
213
|
log(`${S.showCursor ? '打开' : '关掉'}了页内光标(${where})`)
|
|
189
214
|
if (!S.showCursor) {
|
|
190
215
|
for (const id of [...S.cursorReady]) {
|
|
@@ -207,9 +232,15 @@ function handleRuntimeMessage(msg, sender, sendResponse) {
|
|
|
207
232
|
if (msg.bridgeEvent && sender.tab) {
|
|
208
233
|
const tabId = sender.tab.id
|
|
209
234
|
if (msg.bridgeEvent === 'userInput' && S.attached.has(tabId) && ['pointerdown', 'keydown'].includes(msg.type) &&
|
|
210
|
-
|
|
235
|
+
!isAgentInputActive(tabId, msg, sender)) {
|
|
236
|
+
bumpControlEpoch(tabId)
|
|
211
237
|
S.userTakeover.set(tabId, { at: Date.now(), type: msg.type })
|
|
212
|
-
|
|
238
|
+
S.pageAgentUntil.delete(tabId)
|
|
239
|
+
if (S.cursorReady.delete(tabId)) {
|
|
240
|
+
void hidePageCursor(tabId).then(() => {
|
|
241
|
+
if (!S.cursorReady.has(tabId)) return markCursor(tabId, false)
|
|
242
|
+
})
|
|
243
|
+
}
|
|
213
244
|
}
|
|
214
245
|
if (msg.bridgeEvent === 'handoffDone' && S.activeHandoff?.tabId === tabId && S.activeHandoff.id === msg.id)
|
|
215
246
|
S.activeHandoff.resolve({ done: true, by: 'page' })
|
|
@@ -266,7 +297,8 @@ function handleRuntimeMessage(msg, sender, sendResponse) {
|
|
|
266
297
|
await setPaused(!!msg.on, '弹窗')
|
|
267
298
|
return buildState()
|
|
268
299
|
case 'cursor':
|
|
269
|
-
await
|
|
300
|
+
if (msg.cursorPace !== undefined) await setCursorPace(msg.cursorPace, '弹窗')
|
|
301
|
+
else await setShowCursor(!!msg.on, '弹窗')
|
|
270
302
|
return buildState()
|
|
271
303
|
case 'window':
|
|
272
304
|
await setOwnWindow(!!msg.on, '弹窗')
|
|
@@ -299,5 +331,5 @@ function handleRuntimeMessage(msg, sender, sendResponse) {
|
|
|
299
331
|
|
|
300
332
|
export {
|
|
301
333
|
restorePrefs, buildState, releaseTab, diagnostics, releaseAllTabs, setPaused, setShowCursor,
|
|
302
|
-
setOwnWindow, setProfileLabel, handleRuntimeMessage,
|
|
334
|
+
setOwnWindow, setCursorPace, setProfileLabel, handleRuntimeMessage,
|
|
303
335
|
}
|
|
@@ -70,13 +70,17 @@ async function shotMarked({ tabId, format = 'jpeg', quality = 75, full = false,
|
|
|
70
70
|
const el = document.querySelector(${JSON.stringify(selector)});
|
|
71
71
|
if (!el) return null;
|
|
72
72
|
el.scrollIntoView({ block: 'center', inline: 'center', behavior: 'instant' });
|
|
73
|
-
return new Promise((res) => {
|
|
73
|
+
return new Promise((res, rej) => {
|
|
74
74
|
let done = false;
|
|
75
75
|
const finish = () => {
|
|
76
76
|
if (done) return;
|
|
77
77
|
done = true;
|
|
78
|
-
|
|
79
|
-
|
|
78
|
+
try {
|
|
79
|
+
const r = el.getBoundingClientRect();
|
|
80
|
+
const sx = typeof window !== 'undefined' ? (Number(window.scrollX || window.pageXOffset) || 0) : 0;
|
|
81
|
+
const sy = typeof window !== 'undefined' ? (Number(window.scrollY || window.pageYOffset) || 0) : 0;
|
|
82
|
+
res({ x: r.left + sx, y: r.top + sy, width: r.width, height: r.height });
|
|
83
|
+
} catch (err) { rej(err); }
|
|
80
84
|
};
|
|
81
85
|
requestAnimationFrame(() => requestAnimationFrame(finish));
|
|
82
86
|
setTimeout(finish, 120);
|
|
@@ -106,8 +110,10 @@ async function shotMarked({ tabId, format = 'jpeg', quality = 75, full = false,
|
|
|
106
110
|
const r = await send(tabId, 'Page.captureScreenshot', params, deadline && timeLeft(deadline))
|
|
107
111
|
const mime = type === 'png' ? 'image/png' : 'image/jpeg'
|
|
108
112
|
const original = { data: r.data, width: 0, height: 0, mime, format: type }
|
|
113
|
+
let rawBytes = null
|
|
109
114
|
try {
|
|
110
|
-
|
|
115
|
+
rawBytes = base64ToBytes(r.data)
|
|
116
|
+
const size = imageDimensions(rawBytes, mime)
|
|
111
117
|
if (size) {
|
|
112
118
|
original.width = size.width
|
|
113
119
|
original.height = size.height
|
|
@@ -121,7 +127,18 @@ async function shotMarked({ tabId, format = 'jpeg', quality = 75, full = false,
|
|
|
121
127
|
marked = image.marked === true
|
|
122
128
|
}
|
|
123
129
|
const limit = Math.floor(Number(maxWidth))
|
|
124
|
-
|
|
130
|
+
const needScale = Number.isFinite(limit) && limit > 0 && !(image.width > 0 && image.width <= limit && (image.mime || mime) === mime)
|
|
131
|
+
if (needScale) {
|
|
132
|
+
image = await downscaleBitmap(
|
|
133
|
+
image.data,
|
|
134
|
+
image.mime || mime,
|
|
135
|
+
limit,
|
|
136
|
+
type,
|
|
137
|
+
params.quality || 82,
|
|
138
|
+
image === original ? { width: original.width, height: original.height, bytes: rawBytes }
|
|
139
|
+
: (image.width > 0 && image.height > 0 ? { width: image.width, height: image.height } : null),
|
|
140
|
+
)
|
|
141
|
+
}
|
|
125
142
|
} catch {
|
|
126
143
|
image = original
|
|
127
144
|
marked = false
|