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
package/refs.js
CHANGED
|
@@ -10,14 +10,18 @@
|
|
|
10
10
|
* · 同一个元素再快照沿用旧编号(模型手里的旧编号继续有效);页面跳转后全部失效
|
|
11
11
|
*/
|
|
12
12
|
|
|
13
|
-
import { scanPage } from './vendor/extension/sw/frames.js'
|
|
13
|
+
import { isSensitiveField, scanPage } from './vendor/extension/sw/frames.js'
|
|
14
14
|
import { frameField, isFrameRef, locateFrame, mergeFrameSnapshot, mergeFrameTexts } from './features/frames.js'
|
|
15
|
+
import { call as bridgeCall, resolveEndpoint } from './core/transport.js'
|
|
16
|
+
import { hasCap } from './core/ext.js'
|
|
15
17
|
import { tt } from './i18n/index.js'
|
|
16
18
|
|
|
17
19
|
const ATTR = 'data-dsh-ref'
|
|
18
20
|
|
|
19
|
-
const SNAPSHOT_JS = (selector, scope, query) =>
|
|
20
|
-
|
|
21
|
+
const SNAPSHOT_JS = (selector, scope, query, mode, expectedOrigin) => `(() => {
|
|
22
|
+
if (${Boolean(expectedOrigin)} && location.origin !== ${JSON.stringify(expectedOrigin)}) return { skipped: true };
|
|
23
|
+
return (${scanPage})(${JSON.stringify(selector)}, ${JSON.stringify(scope)}, ${JSON.stringify(query)}, null, ${JSON.stringify({ mode })});
|
|
24
|
+
})()`
|
|
21
25
|
|
|
22
26
|
/** 在页面里跑:编号 → 元素。普通元素给选择器;shadow 里的给坐标(顺带滚进视口)。 */
|
|
23
27
|
const LOCATE_JS = (ref, wantPoint, expectedDocId) => `(async () => {
|
|
@@ -28,7 +32,7 @@ const LOCATE_JS = (ref, wantPoint, expectedDocId) => `(async () => {
|
|
|
28
32
|
if (!el || !el.isConnected || !el.ownerDocument.defaultView) return { gone: true };
|
|
29
33
|
const shadow = el.getRootNode() !== document || document.querySelector(sel) !== el;
|
|
30
34
|
// 敏感输入不进入工具结果或光标名牌。
|
|
31
|
-
const masked = (${
|
|
35
|
+
const masked = (${isSensitiveField})(el, el.ownerDocument.defaultView.getComputedStyle(el));
|
|
32
36
|
const out = { shadow, selector: shadow ? null : sel, tag: el.tagName.toLowerCase(),
|
|
33
37
|
text: (masked ? '••••' : el.innerText || el.value || el.getAttribute('aria-label') || '').replace(/\\s+/g, ' ').trim().slice(0, 60) };
|
|
34
38
|
if (!${wantPoint ? 'true' : 'false'} && !shadow) return out;
|
|
@@ -74,9 +78,19 @@ export const refElementJs = (ref, expectedDocId) => `(() => {
|
|
|
74
78
|
})()`
|
|
75
79
|
|
|
76
80
|
const tabDocs = new Map()
|
|
77
|
-
|
|
81
|
+
const tabKey = (tabId) => {
|
|
82
|
+
try {
|
|
83
|
+
const host = resolveEndpoint()
|
|
84
|
+
return JSON.stringify([host.id || host.socket, tabId])
|
|
85
|
+
} catch {
|
|
86
|
+
return JSON.stringify([null, tabId])
|
|
87
|
+
}
|
|
88
|
+
}
|
|
78
89
|
|
|
79
|
-
const
|
|
90
|
+
export const expectedDocId = (tabId) => tabDocs.get(tabKey(tabId))
|
|
91
|
+
export const tabRefElementJs = (tabId, ref) => refElementJs(ref, expectedDocId(tabId))
|
|
92
|
+
|
|
93
|
+
const goneError = (ref) => Object.assign(new Error(tt('err.refGone', { ref })), { code: 'REF_GONE' })
|
|
80
94
|
|
|
81
95
|
/**
|
|
82
96
|
* 拍快照:返回给模型看的文本。
|
|
@@ -85,71 +99,171 @@ const goneError = (ref) => new Error(tt('err.refGone', { ref }))
|
|
|
85
99
|
const snapshotHistory = new Map()
|
|
86
100
|
const SNAPSHOT_CHARS = 8000
|
|
87
101
|
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
102
|
+
function rememberSnapshot(key, id, documentId, view) {
|
|
103
|
+
tabDocs.delete(id)
|
|
104
|
+
tabDocs.set(id, documentId)
|
|
105
|
+
if (tabDocs.size > 256) tabDocs.delete(tabDocs.keys().next().value)
|
|
106
|
+
|
|
107
|
+
snapshotHistory.delete(key)
|
|
108
|
+
snapshotHistory.set(key, { id, documentId, ...view })
|
|
109
|
+
let cachedRows = [...snapshotHistory.values()].reduce((sum, entry) => sum + entry.lines.size + (entry.diffPage?.rows.length || 0), 0)
|
|
110
|
+
for (const [viewKey, entry] of snapshotHistory) {
|
|
111
|
+
if (snapshotHistory.size === 1 || snapshotHistory.size <= 16 && cachedRows <= 60_000) break
|
|
112
|
+
cachedRows -= entry.lines.size + (entry.diffPage?.rows.length || 0)
|
|
113
|
+
snapshotHistory.delete(viewKey)
|
|
114
|
+
}
|
|
115
|
+
}
|
|
95
116
|
|
|
96
|
-
|
|
117
|
+
const refFromLine = (line) => Number(/^\[(\d+)\]/.exec(line)?.[1]) || undefined
|
|
118
|
+
const diffLines = (before, after) => [
|
|
119
|
+
...[...after].flatMap(([ref, line]) => !before.has(ref) ? [`[+] ${line}`] : before.get(ref) !== line ? [`[~] ${line}`] : []),
|
|
120
|
+
...[...before].flatMap(([ref, line]) => after.has(ref) ? [] : [`[-] ${line}`]),
|
|
121
|
+
]
|
|
97
122
|
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
123
|
+
export function observationBaseline(tabId, url) {
|
|
124
|
+
const id = tabKey(tabId)
|
|
125
|
+
const documentId = tabDocs.get(id)
|
|
126
|
+
return [...snapshotHistory.values()].reverse().find((entry) =>
|
|
127
|
+
entry.id === id && documentId && entry.documentId === documentId && (!url || entry.url === url) &&
|
|
128
|
+
entry.mode === 'interactive' && !entry.selector && !entry.query)
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
export async function snapshot(call, tabId, {
|
|
132
|
+
selector, scope = 'all', mode = 'interactive', diff = false, query = '', cursor = 0,
|
|
133
|
+
limit = 300, maxItems, maxChars = SNAPSHOT_CHARS, details = false, autoObserve = false, expectedOrigin, controlEpoch, persist = true,
|
|
134
|
+
} = {}) {
|
|
135
|
+
const id = tabKey(tabId)
|
|
136
|
+
query = query.trim().toLowerCase()
|
|
137
|
+
const key = JSON.stringify([id, selector || '', scope, query, mode])
|
|
138
|
+
const previous = snapshotHistory.get(key)
|
|
139
|
+
const readLegacy = async () => {
|
|
140
|
+
if (autoObserve && !Number.isInteger(controlEpoch)) return { skipped: true }
|
|
141
|
+
return (await call('eval', {
|
|
142
|
+
tabId, expression: SNAPSHOT_JS(selector, scope, query, mode, expectedOrigin), userGesture: false,
|
|
143
|
+
...(Number.isInteger(controlEpoch) ? { controlEpoch } : {}),
|
|
144
|
+
}, autoObserve ? 2500 : undefined, autoObserve)).value
|
|
145
|
+
}
|
|
146
|
+
const modern = call === bridgeCall && await hasCap('snapshotV2')
|
|
147
|
+
const v = modern
|
|
148
|
+
? await call('snapshotScan', {
|
|
149
|
+
tabId, selector, scope, query, mode, diff, cursor, limit: maxItems ?? limit, maxChars,
|
|
150
|
+
prevSnapshotId: diff ? previous?.snapshotId : undefined,
|
|
151
|
+
prevRevision: diff ? previous?.revision : undefined,
|
|
152
|
+
...(autoObserve ? { autoObserve, expectedOrigin } : {}),
|
|
153
|
+
...(Number.isInteger(controlEpoch) ? { controlEpoch } : {}),
|
|
154
|
+
}, autoObserve ? 4000 : undefined, autoObserve)
|
|
155
|
+
: await readLegacy()
|
|
156
|
+
if (v?.skipped) return details ? { skipped: true } : ''
|
|
157
|
+
const validate = (result) => {
|
|
158
|
+
if (!result) throw new Error(tt('err.snapshotFailed'))
|
|
159
|
+
if (result.error === 'scope') throw new Error(tt('err.noElement', { sel: selector }))
|
|
160
|
+
}
|
|
161
|
+
validate(v)
|
|
162
|
+
|
|
163
|
+
if (modern) {
|
|
164
|
+
v.rows = (v.rows || v.lines || []).map((row) => typeof row === 'string' ? { ref: refFromLine(row), line: row } : row)
|
|
165
|
+
}
|
|
101
166
|
|
|
102
167
|
const old = previous?.url === v.url && previous.documentId === v.documentId ? previous : null
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
168
|
+
if (modern) {
|
|
169
|
+
const rows = v.rows.map(({ line }) => line).filter(Boolean)
|
|
170
|
+
const incremental = Boolean(diff && old && v.diffMode !== 'full' && v.diffMode !== 'reset')
|
|
171
|
+
const refs = incremental || cursor ? new Map(old?.lines) : new Map()
|
|
172
|
+
for (const line of rows) {
|
|
173
|
+
const marker = /^\[([+~-])\] (.*)$/.exec(line)
|
|
174
|
+
const row = marker ? marker[2] : line
|
|
175
|
+
const ref = refFromLine(row)
|
|
176
|
+
if (ref === undefined) continue
|
|
177
|
+
if (marker?.[1] === '-') refs.delete(ref)
|
|
178
|
+
else refs.set(ref, row)
|
|
179
|
+
}
|
|
180
|
+
if (persist) {
|
|
181
|
+
rememberSnapshot(key, id, v.documentId, {
|
|
182
|
+
url: v.url, mode, scope, selector, query, lines: refs, snapshotId: v.snapshotId, revision: v.revision,
|
|
183
|
+
partial: Boolean(cursor || v.more),
|
|
184
|
+
})
|
|
119
185
|
}
|
|
186
|
+
const tooLong = rows.join('\n').length > maxChars
|
|
187
|
+
const page = fitRows(rows, maxChars, maxItems ?? limit)
|
|
188
|
+
const response = {
|
|
189
|
+
incremental, unchanged: Boolean(v.unchanged || incremental && !rows.length),
|
|
190
|
+
more: Boolean(v.more || tooLong), rows: page, documentId: v.documentId,
|
|
191
|
+
}
|
|
192
|
+
response.text = formatSnapshot(v, page, diff, incremental, v.totalRows ?? rows.length, v.nextCursor, v.more)
|
|
193
|
+
return details ? response : response.text
|
|
120
194
|
}
|
|
121
195
|
|
|
122
|
-
const
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
const
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
196
|
+
const legacyFrames = !autoObserve && v.rows.some(({ crossPath }) => crossPath)
|
|
197
|
+
if (!autoObserve) await mergeFrameSnapshot(call, tabId, v, scope, query, mode)
|
|
198
|
+
const legacyOld = old?.partial ? null : old
|
|
199
|
+
const refs = new Map(v.rows.filter(({ ref }) => ref !== undefined).map(({ ref, line }) => [ref, line]))
|
|
200
|
+
let rows = v.rows.map(({ line }) => line).filter(Boolean)
|
|
201
|
+
if (diff && legacyOld) {
|
|
202
|
+
const baseline = autoObserve
|
|
203
|
+
? new Map([...legacyOld.lines].filter(([ref]) => !isFrameRef(ref)))
|
|
204
|
+
: legacyOld.lines
|
|
205
|
+
rows = cursor > 0 && legacyOld.diffPage ? legacyOld.diffPage.rows : diffLines(baseline, refs)
|
|
130
206
|
}
|
|
207
|
+
const page = fitRows(rows.slice(cursor), maxChars, maxItems ?? limit)
|
|
131
208
|
const next = cursor + page.length
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
209
|
+
const more = next < rows.length
|
|
210
|
+
if (autoObserve && legacyOld?.frameView) {
|
|
211
|
+
for (const [ref, line] of legacyOld.lines) if (isFrameRef(ref)) refs.set(ref, line)
|
|
212
|
+
}
|
|
213
|
+
if (persist) {
|
|
214
|
+
rememberSnapshot(key, id, v.documentId, {
|
|
215
|
+
url: v.url, mode, scope, selector, query, lines: cursor > 0 && legacyOld ? legacyOld.lines : refs,
|
|
216
|
+
frameView: legacyFrames || autoObserve && legacyOld?.frameView, legacyFrames,
|
|
217
|
+
diffPage: diff && more ? { rows } : null,
|
|
218
|
+
})
|
|
219
|
+
}
|
|
220
|
+
const text = formatSnapshot(v, page, diff, Boolean(legacyOld), rows.length, next, more)
|
|
221
|
+
return details ? { text, rows: page, incremental: Boolean(diff && legacyOld), unchanged: Boolean(diff && legacyOld && !rows.length), more, documentId: v.documentId } : text
|
|
222
|
+
}
|
|
223
|
+
|
|
224
|
+
function fitRows(rows, maxChars, limit) {
|
|
225
|
+
const page = []
|
|
226
|
+
let chars = 0
|
|
227
|
+
for (const row of rows) {
|
|
228
|
+
const add = row.length + Number(page.length > 0)
|
|
229
|
+
if (page.length >= limit || (page.length > 0 && chars + add > maxChars)) break
|
|
230
|
+
page.push(row)
|
|
231
|
+
chars += add
|
|
144
232
|
}
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
233
|
+
return page
|
|
234
|
+
}
|
|
235
|
+
|
|
236
|
+
function formatSnapshot(v, rows, diff, incremental, total, next, more) {
|
|
237
|
+
const mode = diff ? incremental ? tt('res.snap.diff', { count: total }) : tt('res.snap.full') : ''
|
|
238
|
+
const head = [v.title, v.url, tt('res.snap.head', { count: v.count }), mode].filter(Boolean).join('\n')
|
|
239
|
+
let text = `${head}\n\n${rows.join('\n') || (incremental && diff ? tt('res.snap.noChanges') : tt('res.snap.nothing'))}`
|
|
240
|
+
if (more && Number.isInteger(next)) text += tt('res.snap.more', { more: Math.max(0, total - next), cursor: next })
|
|
148
241
|
if (v.outside) text += tt('res.snap.outside', { count: v.outside })
|
|
149
242
|
if (v.capped) text += tt('res.snap.scanCap')
|
|
150
243
|
return text
|
|
151
244
|
}
|
|
152
245
|
|
|
246
|
+
export async function staleRefHint(call, tabId, ref) {
|
|
247
|
+
const id = tabKey(tabId)
|
|
248
|
+
const old = [...snapshotHistory.values()].reverse().find((entry) => entry.id === id && entry.lines.has(ref))
|
|
249
|
+
if (!old) return ''
|
|
250
|
+
if (!await hasCap('snapshotV2')) {
|
|
251
|
+
const panel = await call('panel', {}, 1500, true)
|
|
252
|
+
if (panel?.protectedCount !== 0) return ''
|
|
253
|
+
const tabs = await call('tabs', {}, 1500, true)
|
|
254
|
+
const current = tabs.find(({ id }) => id === tabId)
|
|
255
|
+
if (!current || new URL(current.url).origin !== new URL(old.url).origin) return ''
|
|
256
|
+
}
|
|
257
|
+
let expectedOrigin
|
|
258
|
+
try { expectedOrigin = new URL(old.url).origin } catch { return '' }
|
|
259
|
+
const label = (line) => line.replace(/^\[\d+\]\s+/, '').match(/^([^"]+"[^"]+")/)?.[1]?.toLowerCase()
|
|
260
|
+
const fingerprint = label(old.lines.get(ref))
|
|
261
|
+
if (!fingerprint) return ''
|
|
262
|
+
const view = await snapshot(call, tabId, { scope: 'viewport', details: true, autoObserve: true, expectedOrigin, persist: false })
|
|
263
|
+
const matches = (view.rows || []).filter((line) => label(line) === fingerprint && refFromLine(line) !== ref)
|
|
264
|
+
return matches.length === 1 ? tt('res.action.refHint', { candidate: refFromLine(matches[0]) }) : ''
|
|
265
|
+
}
|
|
266
|
+
|
|
153
267
|
/**
|
|
154
268
|
* 编号 → 怎么操作它。
|
|
155
269
|
* 普通元素:{ selector } —— 调用方照常走选择器那条路(wantPoint=true 时另给坐标)。
|
|
@@ -159,7 +273,7 @@ export async function locate(call, tabId, ref, { wantPoint = false } = {}) {
|
|
|
159
273
|
const n = Number(ref)
|
|
160
274
|
if (!Number.isInteger(n) || n < 1) throw new Error(tt('err.refNotInt', { ref }))
|
|
161
275
|
if (isFrameRef(n)) return locateFrame(call, tabId, n)
|
|
162
|
-
const r = await call('eval', { tabId, expression: LOCATE_JS(n, wantPoint,
|
|
276
|
+
const r = await call('eval', { tabId, expression: LOCATE_JS(n, wantPoint, expectedDocId(tabId)), userGesture: false })
|
|
163
277
|
const v = r.value
|
|
164
278
|
if (!v || v.gone) throw goneError(n)
|
|
165
279
|
return v
|
|
@@ -207,9 +321,12 @@ export async function focusShadow(call, tabId, ref) {
|
|
|
207
321
|
export async function selectInPage(call, tabId, ref, value) {
|
|
208
322
|
if (isFrameRef(ref)) return frameField(call, tabId, 'frameSelect', { ref, value: String(value) })
|
|
209
323
|
const n = Number(ref)
|
|
210
|
-
const r = await call('eval', {
|
|
211
|
-
|
|
212
|
-
|
|
324
|
+
const r = await call('eval', { tabId, expression: selectFieldJs(tabId, n, value), userGesture: false, requireControl: true })
|
|
325
|
+
const v = r.value
|
|
326
|
+
return selectResult(n, v)
|
|
327
|
+
}
|
|
328
|
+
|
|
329
|
+
const selectFieldJs = (tabId, n, value) => `(() => {
|
|
213
330
|
const el = ${tabRefElementJs(tabId, n)};
|
|
214
331
|
if (!el) return null;
|
|
215
332
|
if (el.tagName !== 'SELECT') return { ok: false, why: ${JSON.stringify(tt('err.selectNotSelect'))} };
|
|
@@ -225,10 +342,9 @@ export async function selectInPage(call, tabId, ref, value) {
|
|
|
225
342
|
el.dispatchEvent(new Event('input', { bubbles: true }));
|
|
226
343
|
el.dispatchEvent(new Event('change', { bubbles: true }));
|
|
227
344
|
return { ok: true, value: hit.value, label: (hit.textContent || '').trim() };
|
|
228
|
-
})()
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
const v = r.value
|
|
345
|
+
})()`
|
|
346
|
+
|
|
347
|
+
function selectResult(n, v) {
|
|
232
348
|
if (v === null) throw goneError(n)
|
|
233
349
|
if (!v.ok) {
|
|
234
350
|
throw new Error(
|
|
@@ -246,18 +362,96 @@ export async function frameTexts(call, tabId) {
|
|
|
246
362
|
const r = await call('eval', {
|
|
247
363
|
tabId,
|
|
248
364
|
expression: `(() => {
|
|
365
|
+
const secrets = new Set();
|
|
366
|
+
const collectSecrets = (root) => {
|
|
367
|
+
for (const el of root.querySelectorAll('*')) {
|
|
368
|
+
if (secrets.size >= 50) return;
|
|
369
|
+
if (el.shadowRoot) collectSecrets(el.shadowRoot);
|
|
370
|
+
if (el.tagName === 'IFRAME' || el.tagName === 'FRAME') {
|
|
371
|
+
try { if (el.contentDocument?.body) collectSecrets(el.contentDocument); } catch {}
|
|
372
|
+
}
|
|
373
|
+
if (el.tagName !== 'INPUT' && el.tagName !== 'TEXTAREA' && !el.isContentEditable) continue;
|
|
374
|
+
const view = el.ownerDocument?.defaultView || window;
|
|
375
|
+
if ((${isSensitiveField})(el, view.getComputedStyle(el), true)) {
|
|
376
|
+
for (const v of [el.value, el.innerText, el.tagName === 'TEXTAREA' && el.defaultValue]) {
|
|
377
|
+
const s = v ? String(v) : '';
|
|
378
|
+
if (s.length >= 4) secrets.add(s.slice(0, 512));
|
|
379
|
+
}
|
|
380
|
+
}
|
|
381
|
+
}
|
|
382
|
+
};
|
|
383
|
+
collectSecrets(document);
|
|
384
|
+
const escRe = (s) => s.replace(/[.*+?^{}()|[\\]\\\\$]/g, '\\\\$&');
|
|
385
|
+
const enc = typeof TextEncoder === 'function' ? new TextEncoder() : null;
|
|
386
|
+
const makeRules = (minLen) => [...secrets].filter((s) => s.length >= minLen).sort((a, b) => b.length - a.length).map((value) => {
|
|
387
|
+
if (enc && value.length <= 256) {
|
|
388
|
+
let pat = '';
|
|
389
|
+
for (const ch of value) {
|
|
390
|
+
if (ch === ' ') { pat += '(?: |%20|\\\\+)'; continue; }
|
|
391
|
+
if (/^[a-zA-Z0-9]$/.test(ch)) { pat += ch; continue; }
|
|
392
|
+
let pct = '';
|
|
393
|
+
for (const b of enc.encode(ch)) {
|
|
394
|
+
const h = b.toString(16).padStart(2, '0');
|
|
395
|
+
pct += '%' + h.replace(/[a-f]/g, (c) => '[' + c + c.toUpperCase() + ']');
|
|
396
|
+
}
|
|
397
|
+
pat += '(?:' + escRe(ch) + '|' + pct + ')';
|
|
398
|
+
}
|
|
399
|
+
return { value, re: new RegExp(pat, 'g'), extras: [] };
|
|
400
|
+
}
|
|
401
|
+
const extras = new Set();
|
|
402
|
+
try {
|
|
403
|
+
const e1 = encodeURIComponent(value);
|
|
404
|
+
if (e1 && e1 !== value) { extras.add(e1); if (e1.includes('%20')) extras.add(e1.replace(/%20/g, '+')); }
|
|
405
|
+
const e2 = typeof URLSearchParams === 'function' ? new URLSearchParams([['x', value]]).toString().slice(2) : '';
|
|
406
|
+
if (e2 && e2 !== value) { extras.add(e2); if (e2.includes('+')) extras.add(e2.replace(/\\+/g, '%20')); }
|
|
407
|
+
} catch {}
|
|
408
|
+
return { value, re: null, extras: [...extras].map((v) => new RegExp(escRe(v), 'gi')) };
|
|
409
|
+
});
|
|
410
|
+
const applyRules = (text, rules) => {
|
|
411
|
+
let out = String(text || '');
|
|
412
|
+
for (const { value, re, extras } of rules) {
|
|
413
|
+
if (re) out = out.replace(re, '••••');
|
|
414
|
+
else {
|
|
415
|
+
out = out.replaceAll(value, '••••');
|
|
416
|
+
for (const x of extras) out = out.replace(x, '••••');
|
|
417
|
+
}
|
|
418
|
+
}
|
|
419
|
+
return out;
|
|
420
|
+
};
|
|
421
|
+
const metaRules = makeRules(6);
|
|
422
|
+
const textRules = makeRules(4);
|
|
423
|
+
const safeSrc = (f, doc, cross) => {
|
|
424
|
+
const rawSrc = f.getAttribute('src') || '';
|
|
425
|
+
if (cross && rawSrc) {
|
|
426
|
+
try {
|
|
427
|
+
const u = new URL(rawSrc, doc.baseURI || location.href);
|
|
428
|
+
if (u.origin && u.origin !== 'null') return u.origin.slice(0, 120);
|
|
429
|
+
} catch {}
|
|
430
|
+
return '';
|
|
431
|
+
}
|
|
432
|
+
if (rawSrc) {
|
|
433
|
+
try {
|
|
434
|
+
const u = new URL(rawSrc, doc.baseURI || location.href);
|
|
435
|
+
if (u.origin && u.origin !== 'null') return (u.origin + applyRules(u.pathname + u.search + u.hash, textRules)).slice(0, 120);
|
|
436
|
+
} catch {}
|
|
437
|
+
}
|
|
438
|
+
const raw = rawSrc || f.getAttribute('title') || f.getAttribute('name') || '';
|
|
439
|
+
return applyRules(raw, metaRules).slice(0, 120);
|
|
440
|
+
};
|
|
249
441
|
const out = [];
|
|
250
442
|
const walk = (doc, depth, path) => {
|
|
251
443
|
for (const f of doc.querySelectorAll('iframe, frame')) {
|
|
252
444
|
const b = f.getBoundingClientRect();
|
|
253
445
|
if (b.width < 2 || b.height < 2) continue;
|
|
254
|
-
const src = (f.getAttribute('src') || f.getAttribute('title') || f.getAttribute('name') || '').slice(0, 120);
|
|
255
446
|
const index = Array.prototype.indexOf.call(doc.defaultView.frames || [], f.contentWindow);
|
|
256
447
|
const childPath = [...path, index];
|
|
257
448
|
let d = null;
|
|
258
449
|
try { d = f.contentDocument; } catch {}
|
|
259
|
-
if (!d?.body || d.defaultView?.origin !== doc.defaultView.origin) {
|
|
260
|
-
|
|
450
|
+
if (!d?.body || d.defaultView?.origin !== doc.defaultView.origin) {
|
|
451
|
+
out.push({ src: safeSrc(f, doc, true), cross: true, path: childPath });
|
|
452
|
+
continue;
|
|
453
|
+
}
|
|
454
|
+
out.push({ src: safeSrc(f, doc, false), text: applyRules((d.body.innerText || '').trim(), textRules) });
|
|
261
455
|
if (depth < 4) walk(d, depth + 1, childPath);
|
|
262
456
|
}
|
|
263
457
|
};
|
|
@@ -277,14 +471,9 @@ function missingTarget({ ref, selector }) {
|
|
|
277
471
|
return ref != null ? goneError(ref) : new Error(tt('err.noElement', { sel: selector || tt('res.input.active') }))
|
|
278
472
|
}
|
|
279
473
|
|
|
280
|
-
|
|
474
|
+
const changeFieldJs = (tabId, args) => {
|
|
281
475
|
const { action, checked, value } = args
|
|
282
|
-
|
|
283
|
-
if (isFrameRef(args.ref)) return frameField(call, tabId, 'frameSetField', args)
|
|
284
|
-
const { value: result } = await call('eval', {
|
|
285
|
-
tabId,
|
|
286
|
-
userGesture: false,
|
|
287
|
-
expression: `(async () => {
|
|
476
|
+
return `(async () => {
|
|
288
477
|
const el = ${elementFor(tabId, args)};
|
|
289
478
|
if (!el || !el.isConnected) return null;
|
|
290
479
|
if (el.matches(':disabled') || el.getAttribute('aria-disabled') === 'true') return 'disabled';
|
|
@@ -319,17 +508,67 @@ export async function setField(call, tabId, args) {
|
|
|
319
508
|
if (!el.isConnected) return null;
|
|
320
509
|
if (el.value !== value) return 'rejected';
|
|
321
510
|
return 'ok';
|
|
322
|
-
})()
|
|
511
|
+
})()`
|
|
512
|
+
}
|
|
513
|
+
|
|
514
|
+
export async function setField(call, tabId, args) {
|
|
515
|
+
const { action, value } = args
|
|
516
|
+
if (action === 'select') return selectInPage(call, tabId, args.ref, value)
|
|
517
|
+
if (isFrameRef(args.ref)) return frameField(call, tabId, 'frameSetField', args)
|
|
518
|
+
const { value: result } = await call('eval', {
|
|
519
|
+
tabId, userGesture: false, requireControl: true, expression: changeFieldJs(tabId, args),
|
|
323
520
|
})
|
|
521
|
+
checkFieldResult(args, result)
|
|
522
|
+
}
|
|
523
|
+
|
|
524
|
+
function checkFieldResult(args, result) {
|
|
324
525
|
if (result == null) throw missingTarget(args)
|
|
325
526
|
if (result === 'disabled') throw new Error(tt('err.fieldDisabled'))
|
|
326
527
|
if (result === 'wrong') throw new Error(tt('err.fieldWrong'))
|
|
327
528
|
if (result !== 'ok') throw new Error(tt('err.fieldRejected'))
|
|
328
529
|
}
|
|
329
530
|
|
|
531
|
+
export async function setFields(call, tabId, fields) {
|
|
532
|
+
const ops = fields.map((field) => field.action === 'select'
|
|
533
|
+
? `() => ${selectFieldJs(tabId, field.ref, field.value)}`
|
|
534
|
+
: `() => ${changeFieldJs(tabId, field)}`)
|
|
535
|
+
const { value } = await call('eval', {
|
|
536
|
+
tabId, userGesture: false, requireControl: true,
|
|
537
|
+
expression: `(async () => {
|
|
538
|
+
const ops = [${ops.join(',')}];
|
|
539
|
+
for (let i = 0; i < ops.length; i++) {
|
|
540
|
+
try {
|
|
541
|
+
const status = await ops[i]();
|
|
542
|
+
if (status !== 'ok' && !status?.ok) {
|
|
543
|
+
return { completed: i, status: typeof status === 'object' && status !== null ? { why: status.why } : status };
|
|
544
|
+
}
|
|
545
|
+
} catch {
|
|
546
|
+
return { completed: i, status: 'rejected' };
|
|
547
|
+
}
|
|
548
|
+
}
|
|
549
|
+
return { completed: ops.length };
|
|
550
|
+
})()`,
|
|
551
|
+
})
|
|
552
|
+
if (!value || !Number.isInteger(value.completed)) throw new Error(tt('err.fieldRejected'))
|
|
553
|
+
if (value.completed === fields.length) return value
|
|
554
|
+
const field = fields[value.completed]
|
|
555
|
+
try {
|
|
556
|
+
if (field.action === 'select') {
|
|
557
|
+
if (value.status === 'rejected') throw new Error(tt('err.fieldRejected'))
|
|
558
|
+
selectResult(field.ref, value.status)
|
|
559
|
+
} else checkFieldResult(field, value.status)
|
|
560
|
+
} catch (error) {
|
|
561
|
+
return { ...value, error }
|
|
562
|
+
}
|
|
563
|
+
return { ...value, error: new Error(tt('err.fieldRejected')) }
|
|
564
|
+
}
|
|
565
|
+
|
|
330
566
|
export async function scrollElement(call, tabId, args) {
|
|
331
567
|
const dx = Number.isFinite(Number(args.dx)) ? Number(args.dx) : 0
|
|
332
568
|
const dy = Number.isFinite(Number(args.dy)) ? Number(args.dy) : 600
|
|
569
|
+
if (args.ref != null && isFrameRef(args.ref)) {
|
|
570
|
+
return frameField(call, tabId, 'frameScroll', { ref: args.ref, dx, dy })
|
|
571
|
+
}
|
|
333
572
|
const r = await call('eval', {
|
|
334
573
|
tabId,
|
|
335
574
|
userGesture: false,
|
|
@@ -346,10 +585,11 @@ export async function scrollElement(call, tabId, args) {
|
|
|
346
585
|
if (!st || (st.overflowY === undefined && st.overflowX === undefined)) return n === el;
|
|
347
586
|
return (canY && /auto|scroll|overlay/.test(st.overflowY)) || (canX && /auto|scroll|overlay/.test(st.overflowX));
|
|
348
587
|
};
|
|
588
|
+
const parentOf = (n) => n.parentElement || (n.getRootNode && n.getRootNode().host) || null;
|
|
349
589
|
let target = el;
|
|
350
590
|
if (!scrollable(target)) {
|
|
351
|
-
let cur = el
|
|
352
|
-
while (cur && !scrollable(cur)) cur = cur
|
|
591
|
+
let cur = parentOf(el);
|
|
592
|
+
while (cur && !scrollable(cur)) cur = parentOf(cur);
|
|
353
593
|
target = cur || el.ownerDocument.scrollingElement || el.ownerDocument.documentElement || el;
|
|
354
594
|
}
|
|
355
595
|
target.scrollBy({ left: ${dx}, top: ${dy}, behavior: 'instant' });
|