dsh-codex-style-computer-use 2.3.4 → 3.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.md +60 -5
- package/README.zh.md +50 -5
- package/client.js +1556 -32
- package/lib/browser/APACHE-2.0.txt +202 -0
- package/lib/browser/MIT-dsh-doyourweb.txt +21 -0
- package/lib/browser/NOTICE.md +53 -0
- package/lib/browser/README.md +139 -0
- package/lib/browser/access.js +540 -0
- package/lib/browser/approval.js +82 -0
- package/lib/browser/bridge.js +1048 -0
- package/lib/browser/engine.js +669 -0
- package/lib/browser/extension/background.js +474 -0
- package/lib/browser/extension/config.js +6 -0
- package/lib/browser/extension/manifest.json +25 -0
- package/lib/browser/extension/popup.css +89 -0
- package/lib/browser/extension/popup.html +35 -0
- package/lib/browser/extension/popup.js +99 -0
- package/lib/browser/host-protocol.js +132 -0
- package/lib/browser/host.js +1089 -0
- package/lib/browser/model.js +111 -0
- package/lib/browser/pairing.js +126 -0
- package/lib/browser/policy.js +131 -0
- package/lib/browser/protocol.js +171 -0
- package/lib/browser/schema.js +82 -0
- package/lib/browser/setup-page.js +145 -0
- package/lib/core/bin/dsh-computer-use-worker.exe +0 -0
- package/lib/core/bin/dsh-computer-use-worker.exe.json +2 -2
- package/lib/core/index.js +58 -1
- package/lib/core/worker.cs +32 -1
- package/lib/host-debug.js +3 -2
- package/lib/index.js +286 -4
- package/lib/references-apps.js +586 -0
- package/lib/references-files.js +77 -0
- package/lib/references.js +233 -0
- package/package.json +7 -2
- package/scripts/check-behavior.mjs +60 -0
- package/scripts/verify-browser-integration.mjs +807 -0
|
@@ -0,0 +1,474 @@
|
|
|
1
|
+
// lib/browser/extension/background.js — the companion extension's service worker.
|
|
2
|
+
//
|
|
3
|
+
// Derived from dsh-doyourweb 0.2.6 `extension/background.js` (MIT, see
|
|
4
|
+
// ../../NOTICE.md). Kept: the pairing handshake, the per-tab chrome.debugger
|
|
5
|
+
// attach/detach discipline, tab inventory publishing, the disconnect/reconnect
|
|
6
|
+
// loop, and the worker-restart reconciliation that refuses to detach a debugger
|
|
7
|
+
// this extension does not own. Dropped: annotations, app management, favicons,
|
|
8
|
+
// activity overlays, WebMCP and network capture.
|
|
9
|
+
//
|
|
10
|
+
// WHY THE URL POLICY IS DUPLICATED HERE. A Chrome extension cannot import a
|
|
11
|
+
// module from outside its own folder, and this plugin takes no build step, so
|
|
12
|
+
// the admission rules are re-stated below. `build/test-browser-bridge.mjs` runs
|
|
13
|
+
// BOTH implementations over the same hostile/ordinary URL table so they cannot
|
|
14
|
+
// drift apart without a failing test.
|
|
15
|
+
|
|
16
|
+
import { config } from './config.js'
|
|
17
|
+
|
|
18
|
+
/** Compatibility hint only; a mismatching revision never rejects a connection. */
|
|
19
|
+
const bridgeProtocolRevision = 1
|
|
20
|
+
|
|
21
|
+
let socket
|
|
22
|
+
let profile
|
|
23
|
+
let browserSession
|
|
24
|
+
let timer
|
|
25
|
+
let retryTimer
|
|
26
|
+
let ready
|
|
27
|
+
let cleanup = Promise.resolve()
|
|
28
|
+
let retry = 1000
|
|
29
|
+
let stopped = false
|
|
30
|
+
let inventoryTimer
|
|
31
|
+
let availableTabs = 0
|
|
32
|
+
let inventoryGeneration = 0
|
|
33
|
+
let inventoryRead = Promise.resolve()
|
|
34
|
+
let token = config.token || ''
|
|
35
|
+
let port = config.port || 43188
|
|
36
|
+
let pairing
|
|
37
|
+
let hostCompatibility
|
|
38
|
+
const tabRevisions = new Map()
|
|
39
|
+
const attached = new Set()
|
|
40
|
+
let attachmentWrites = Promise.resolve()
|
|
41
|
+
const popupRequests = new Map()
|
|
42
|
+
const browser = navigator.userAgent.includes('Edg/') ? 'Edge' : 'Chrome'
|
|
43
|
+
|
|
44
|
+
function send (message, connection = socket) {
|
|
45
|
+
if (connection?.readyState === WebSocket.OPEN) connection.send(JSON.stringify(message))
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
// ---- the URL policy (mirrors lib/browser/protocol.js) ------------------------------------
|
|
49
|
+
//
|
|
50
|
+
// Restated, not imported: an extension cannot load a module from outside its own
|
|
51
|
+
// folder. `build/test-browser-bridge.mjs` drives both copies over the same table
|
|
52
|
+
// of hostile and ordinary URLs, so a drift between them fails a test.
|
|
53
|
+
|
|
54
|
+
/** A local document that is actually a page. */
|
|
55
|
+
const LOCAL_DOCUMENT = /\.(?:html?|xhtml)$/i
|
|
56
|
+
|
|
57
|
+
function isLoopbackHost (host) {
|
|
58
|
+
const name = String(host ?? '').replace(/^\[|\]$/g, '').replace(/\.$/, '').toLowerCase()
|
|
59
|
+
return name === 'localhost' || name.endsWith('.localhost') || /^127\.\d+\.\d+\.\d+$/.test(name) ||
|
|
60
|
+
name === '0.0.0.0' || name === '::1' || name === '::'
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
function isWebStore (url) {
|
|
64
|
+
const host = url.hostname.replace(/\.$/, '').toLowerCase()
|
|
65
|
+
if (host === 'chromewebstore.google.com') return true
|
|
66
|
+
if (host === 'chrome.google.com' && /^\/webstore(?:\/|$)/i.test(url.pathname)) return true
|
|
67
|
+
if (host === 'microsoftedge.microsoft.com' && /^\/addons(?:\/|$)/i.test(url.pathname)) return true
|
|
68
|
+
return false
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
/** Is this a local HTML document on THIS machine (no UNC, no directory)? */
|
|
72
|
+
function isLocalHtml (url) {
|
|
73
|
+
if (url.hostname !== '') return false
|
|
74
|
+
let pathname
|
|
75
|
+
try { pathname = decodeURIComponent(url.pathname) } catch { return false }
|
|
76
|
+
if (!pathname || pathname.endsWith('/') || pathname.endsWith('\\')) return false
|
|
77
|
+
if (pathname.includes('\0')) return false
|
|
78
|
+
// A bare drive root (`file:///D:`) is a directory, not a document.
|
|
79
|
+
if (/^\/?[a-z]:$/i.test(pathname)) return false
|
|
80
|
+
return LOCAL_DOCUMENT.test(pathname)
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
/**
|
|
84
|
+
* The normalised URL this bridge may report or open, or null.
|
|
85
|
+
*
|
|
86
|
+
* `file` is granted per call: an attachment or openTab never implies a local file
|
|
87
|
+
* page, and listing still requires the user's own browser file-access grant.
|
|
88
|
+
*/
|
|
89
|
+
function sessionWebUrl (value, { port, file = false } = {}) {
|
|
90
|
+
let url
|
|
91
|
+
try { url = new URL(String(value ?? '')) } catch { return null }
|
|
92
|
+
if (url.username || url.password) return null
|
|
93
|
+
if (url.protocol === 'file:') return file && isLocalHtml(url) ? url : null
|
|
94
|
+
if (url.protocol !== 'http:' && url.protocol !== 'https:') return null
|
|
95
|
+
if (isWebStore(url)) return null
|
|
96
|
+
if (url.hostname.replace(/\.$/, '').toLowerCase() === 'passwords.google.com') return null
|
|
97
|
+
// The port this bridge is bound to serves the pairing page; it is never a
|
|
98
|
+
// target. The host also guards its own web-server ports (protectedPorts), which
|
|
99
|
+
// it re-checks on every command, so this side only needs its own port.
|
|
100
|
+
const target = Number(url.port || (url.protocol === 'https:' ? 443 : 80))
|
|
101
|
+
if (isLoopbackHost(url.hostname) && target === Number(port)) return null
|
|
102
|
+
return url
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
// ---- inventory ---------------------------------------------------------------------------
|
|
106
|
+
|
|
107
|
+
/**
|
|
108
|
+
* Publish the tabs this bridge may control.
|
|
109
|
+
*
|
|
110
|
+
* A tab is offered when it is not incognito, is a regular http(s) page, or is a
|
|
111
|
+
* local .html document AND the user granted file access in the extension. The
|
|
112
|
+
* bridge re-filters everything it receives, so this keeps the list small rather
|
|
113
|
+
* than being the gate.
|
|
114
|
+
*/
|
|
115
|
+
function supported (tab) {
|
|
116
|
+
if (tab.incognito) return false
|
|
117
|
+
const url = sessionWebUrl(tab.url, { port, file: fileAccess })
|
|
118
|
+
return url !== null
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
let fileAccess = false
|
|
122
|
+
|
|
123
|
+
async function inventory ({ icons = true } = {}) {
|
|
124
|
+
void icons
|
|
125
|
+
const connection = socket
|
|
126
|
+
if (!token || connection?.readyState !== WebSocket.OPEN) return
|
|
127
|
+
const published = inventoryRead.then(async () => {
|
|
128
|
+
if (socket !== connection) return
|
|
129
|
+
const generation = ++inventoryGeneration
|
|
130
|
+
fileAccess = await chrome.extension.isAllowedFileSchemeAccess()
|
|
131
|
+
const [allTabs, allWindows] = await Promise.all([
|
|
132
|
+
chrome.tabs.query({}),
|
|
133
|
+
chrome.windows.getAll({ windowTypes: ['normal'] }),
|
|
134
|
+
])
|
|
135
|
+
if (socket !== connection) return
|
|
136
|
+
const windows = allWindows.filter(window => !window.incognito).map(window => ({ id: window.id }))
|
|
137
|
+
// A tab must belong to a window the host may offer, or the host could be
|
|
138
|
+
// handed an "open here" target it can never use.
|
|
139
|
+
const known = new Set(windows.map(window => window.id))
|
|
140
|
+
const tabs = allTabs.filter(tab => supported(tab) && known.has(tab.windowId))
|
|
141
|
+
availableTabs = tabs.length
|
|
142
|
+
const rows = tabs.map(tab => ({
|
|
143
|
+
id: tab.id,
|
|
144
|
+
title: tab.title,
|
|
145
|
+
url: tab.url,
|
|
146
|
+
windowId: tab.windowId,
|
|
147
|
+
active: tab.active,
|
|
148
|
+
lastAccessed: tab.lastAccessed,
|
|
149
|
+
incognito: false,
|
|
150
|
+
discarded: tab.discarded === true,
|
|
151
|
+
frozen: tab.frozen === true,
|
|
152
|
+
status: tab.status,
|
|
153
|
+
revision: tabRevisions.get(tab.id) || 0,
|
|
154
|
+
}))
|
|
155
|
+
send({ method: 'dsh.inventory', params: { profile, browserSession, browser,
|
|
156
|
+
protocolRevision: bridgeProtocolRevision, extensionVersion: chrome.runtime.getManifest().version,
|
|
157
|
+
windows, tabs: rows } }, connection)
|
|
158
|
+
return { generation }
|
|
159
|
+
})
|
|
160
|
+
inventoryRead = published.catch(() => {})
|
|
161
|
+
await published
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
function scheduleInventory () {
|
|
165
|
+
clearTimeout(inventoryTimer)
|
|
166
|
+
inventoryTimer = setTimeout(() => void inventory().catch(console.warn), 150)
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
// ---- commands ----------------------------------------------------------------------------
|
|
170
|
+
|
|
171
|
+
async function command (message, connection) {
|
|
172
|
+
const args = message.params || []
|
|
173
|
+
const target = args[0]
|
|
174
|
+
switch (message.method) {
|
|
175
|
+
case 'chrome.debugger.attach': {
|
|
176
|
+
const tab = await chrome.tabs.get(target.tabId)
|
|
177
|
+
fileAccess = await chrome.extension.isAllowedFileSchemeAccess()
|
|
178
|
+
if (!supported(tab)) {
|
|
179
|
+
throw new Error('Use a regular web page, or a local file page with "Allow access to file URLs" enabled')
|
|
180
|
+
}
|
|
181
|
+
if (tab.discarded || tab.frozen || tab.status === 'unloaded') {
|
|
182
|
+
throw new Error('This tab is asleep. Open it to wake it, then reference it again.')
|
|
183
|
+
}
|
|
184
|
+
if (!attached.has(target.tabId)) {
|
|
185
|
+
await chrome.debugger.attach(target, '1.3')
|
|
186
|
+
if (socket !== connection || stopped) {
|
|
187
|
+
await chrome.debugger.detach(target).catch(() => {})
|
|
188
|
+
throw new Error('Browser bridge disconnected during attachment')
|
|
189
|
+
}
|
|
190
|
+
attached.add(target.tabId)
|
|
191
|
+
await rememberAttachments()
|
|
192
|
+
await chrome.action.setBadgeText({ tabId: target.tabId, text: 'DSH' })
|
|
193
|
+
await chrome.action.setBadgeBackgroundColor({ tabId: target.tabId, color: '#237841' })
|
|
194
|
+
}
|
|
195
|
+
return {}
|
|
196
|
+
}
|
|
197
|
+
case 'chrome.debugger.detach':
|
|
198
|
+
if (attached.delete(target.tabId)) {
|
|
199
|
+
await rememberAttachments()
|
|
200
|
+
await chrome.debugger.detach(target).catch(() => {})
|
|
201
|
+
await chrome.action.setBadgeText({ tabId: target.tabId, text: '' }).catch(() => {})
|
|
202
|
+
}
|
|
203
|
+
return {}
|
|
204
|
+
case 'chrome.debugger.sendCommand':
|
|
205
|
+
// The host can only ever reach a tab this extension attached itself.
|
|
206
|
+
if (!attached.has(target.tabId)) throw new Error('Tab is not attached')
|
|
207
|
+
return chrome.debugger.sendCommand(target, args[1], args[2])
|
|
208
|
+
case 'dsh.openTab': {
|
|
209
|
+
// Opening a tab never grants local-file access: an ordinary web URL only.
|
|
210
|
+
const url = sessionWebUrl(args[1], { port, file: false })
|
|
211
|
+
if (!url) throw new Error('A regular HTTP/HTTPS URL without credentials is required')
|
|
212
|
+
const window = await chrome.windows.get(target)
|
|
213
|
+
if (window.incognito || window.type !== 'normal') throw new Error('Select a normal browser window')
|
|
214
|
+
if (socket !== connection || stopped) throw new Error('Browser extension disconnected')
|
|
215
|
+
const tab = await chrome.tabs.create({ windowId: window.id, url: url.href, active: false })
|
|
216
|
+
// Chrome can report only pendingUrl on creation; publish the tab once a
|
|
217
|
+
// supported URL has actually been committed by the browser.
|
|
218
|
+
if (!supported(tab)) {
|
|
219
|
+
await new Promise((resolve, reject) => {
|
|
220
|
+
const finish = callback => value => {
|
|
221
|
+
clearTimeout(deadline)
|
|
222
|
+
chrome.tabs.onUpdated.removeListener(updated)
|
|
223
|
+
chrome.tabs.onRemoved.removeListener(removed)
|
|
224
|
+
callback(value)
|
|
225
|
+
}
|
|
226
|
+
const complete = finish(resolve)
|
|
227
|
+
const fail = finish(reject)
|
|
228
|
+
const committed = current => current.url && current.url !== 'about:blank'
|
|
229
|
+
const updated = (id, _changes, current) => { if (id === tab.id && committed(current)) complete() }
|
|
230
|
+
const removed = id => { if (id === tab.id) fail(new Error('The new browser tab was closed')) }
|
|
231
|
+
const deadline = setTimeout(() => fail(new Error('The new tab did not become ready; it may still be open')), 10000)
|
|
232
|
+
chrome.tabs.onUpdated.addListener(updated)
|
|
233
|
+
chrome.tabs.onRemoved.addListener(removed)
|
|
234
|
+
void chrome.tabs.get(tab.id).then(current => { if (committed(current)) complete() }, fail)
|
|
235
|
+
})
|
|
236
|
+
}
|
|
237
|
+
await inventory()
|
|
238
|
+
return { tabId: tab.id }
|
|
239
|
+
}
|
|
240
|
+
case 'dsh.tabState': {
|
|
241
|
+
// A cheap freshness check for the host's "is this still the page?" probe.
|
|
242
|
+
const tab = await chrome.tabs.get(target).catch(() => undefined)
|
|
243
|
+
if (!tab || tab.incognito || tab.pendingUrl) return {}
|
|
244
|
+
return { title: tab.title, url: tab.url, windowId: tab.windowId, revision: tabRevisions.get(target) || 0 }
|
|
245
|
+
}
|
|
246
|
+
case 'dsh.focus': {
|
|
247
|
+
const tab = await chrome.tabs.get(target)
|
|
248
|
+
if (!supported(tab)) throw new Error('Tab is unsupported')
|
|
249
|
+
await chrome.tabs.update(tab.id, { active: true })
|
|
250
|
+
await chrome.windows.update(tab.windowId, { focused: true })
|
|
251
|
+
return {}
|
|
252
|
+
}
|
|
253
|
+
default: throw new Error('Unsupported browser bridge command')
|
|
254
|
+
}
|
|
255
|
+
}
|
|
256
|
+
|
|
257
|
+
// ---- connection --------------------------------------------------------------------------
|
|
258
|
+
|
|
259
|
+
function rememberAttachments () {
|
|
260
|
+
const ownedTabs = [...attached]
|
|
261
|
+
attachmentWrites = attachmentWrites.then(() => chrome.storage.session.set({ ownedTabs }))
|
|
262
|
+
return attachmentWrites
|
|
263
|
+
}
|
|
264
|
+
|
|
265
|
+
function disconnect () {
|
|
266
|
+
const connection = socket
|
|
267
|
+
socket = undefined
|
|
268
|
+
pairing = undefined
|
|
269
|
+
hostCompatibility = undefined
|
|
270
|
+
clearInterval(timer)
|
|
271
|
+
clearTimeout(retryTimer)
|
|
272
|
+
clearTimeout(inventoryTimer)
|
|
273
|
+
inventoryRead = Promise.resolve()
|
|
274
|
+
for (const finish of popupRequests.values()) finish({ error: 'DSH 连接已断开' })
|
|
275
|
+
connection?.close()
|
|
276
|
+
const tabIds = [...attached]
|
|
277
|
+
attached.clear()
|
|
278
|
+
cleanup = cleanup.then(async () => {
|
|
279
|
+
await rememberAttachments()
|
|
280
|
+
await Promise.all(tabIds.map(async tabId => {
|
|
281
|
+
await chrome.debugger.detach({ tabId }).catch(() => {})
|
|
282
|
+
await chrome.action.setBadgeText({ tabId, text: '' }).catch(() => {})
|
|
283
|
+
}))
|
|
284
|
+
})
|
|
285
|
+
void updateStatus()
|
|
286
|
+
return cleanup
|
|
287
|
+
}
|
|
288
|
+
|
|
289
|
+
async function updateStatus () {
|
|
290
|
+
const connected = !!token && socket?.readyState === WebSocket.OPEN && !pairing
|
|
291
|
+
const title = stopped ? '已暂停'
|
|
292
|
+
: pairing ? `配对中 · ${pairing.code}`
|
|
293
|
+
: !token ? '未配对' : connected ? '已连接' : '等待 DSH'
|
|
294
|
+
await chrome.action.setTitle({ title: `DSH Browser - ${title}` })
|
|
295
|
+
// A badge holds about four characters, so a six-digit code is shown in the
|
|
296
|
+
// popup and the tooltip; truncating it would defeat the comparison.
|
|
297
|
+
await chrome.action.setBadgeText({ text: pairing ? 'PAIR' : connected || stopped ? '' : 'OFF' })
|
|
298
|
+
await chrome.action.setBadgeBackgroundColor({ color: pairing ? '#b45309' : '#6b7280' })
|
|
299
|
+
}
|
|
300
|
+
|
|
301
|
+
async function connect () {
|
|
302
|
+
await ready
|
|
303
|
+
await cleanup
|
|
304
|
+
if (stopped || (socket && [WebSocket.OPEN, WebSocket.CONNECTING].includes(socket.readyState))) return
|
|
305
|
+
clearTimeout(retryTimer)
|
|
306
|
+
// No token means "please pair me"; the bridge answers with a code to compare.
|
|
307
|
+
const protocols = token ? ['dsh-browser-cu', token] : ['dsh-browser-cu']
|
|
308
|
+
const connection = new WebSocket(`ws://127.0.0.1:${port}/extension`, protocols)
|
|
309
|
+
socket = connection
|
|
310
|
+
connection.onopen = () => {
|
|
311
|
+
if (socket !== connection) return connection.close()
|
|
312
|
+
retry = 1000
|
|
313
|
+
pairing = undefined
|
|
314
|
+
void updateStatus()
|
|
315
|
+
if (token) void inventory().catch(console.warn)
|
|
316
|
+
timer = setInterval(() => send({ method: 'dsh.ping' }, connection), 20000)
|
|
317
|
+
if (token) send({ method: 'dsh.ping' }, connection)
|
|
318
|
+
}
|
|
319
|
+
connection.onmessage = async event => {
|
|
320
|
+
if (socket !== connection) return
|
|
321
|
+
let message
|
|
322
|
+
try {
|
|
323
|
+
message = JSON.parse(event.data)
|
|
324
|
+
if (message.method === 'dsh.pong') {
|
|
325
|
+
const version = typeof message.params?.version === 'string' ? message.params.version.slice(0, 40) : 'unknown'
|
|
326
|
+
hostCompatibility = {
|
|
327
|
+
hostVersion: version,
|
|
328
|
+
compatibilityWarning: message.params?.protocolRevision !== bridgeProtocolRevision
|
|
329
|
+
? '主机与扩展的协议修订不同或主机较旧,请同步更新。'
|
|
330
|
+
: version !== chrome.runtime.getManifest().version ? '主机与扩展版本不同,请同步更新。' : '',
|
|
331
|
+
}
|
|
332
|
+
return
|
|
333
|
+
}
|
|
334
|
+
if (message.method === 'dsh.pairing') {
|
|
335
|
+
pairing = message.params
|
|
336
|
+
void updateStatus()
|
|
337
|
+
return
|
|
338
|
+
}
|
|
339
|
+
if (message.method === 'dsh.paired') {
|
|
340
|
+
token = message.params.token
|
|
341
|
+
port = message.params.port || port
|
|
342
|
+
retry = 1000
|
|
343
|
+
await chrome.storage.local.set({ token, port })
|
|
344
|
+
connection.close()
|
|
345
|
+
return
|
|
346
|
+
}
|
|
347
|
+
if (typeof message.id === 'string' && !message.method) {
|
|
348
|
+
const finish = popupRequests.get(message.id)
|
|
349
|
+
if (finish) finish(message.error ? { error: message.error } : message.result)
|
|
350
|
+
return
|
|
351
|
+
}
|
|
352
|
+
if (!Number.isSafeInteger(message.id)) return
|
|
353
|
+
const result = await command(message, connection)
|
|
354
|
+
send({ id: message.id, result: result ?? {} }, connection)
|
|
355
|
+
} catch (error) { if (message?.id) send({ id: message.id, error: error.message }, connection) }
|
|
356
|
+
}
|
|
357
|
+
connection.onerror = () => {}
|
|
358
|
+
connection.onclose = async () => {
|
|
359
|
+
if (socket !== connection) return
|
|
360
|
+
await disconnect()
|
|
361
|
+
if (!stopped && !socket) {
|
|
362
|
+
retryTimer = setTimeout(() => void connect().catch(console.warn), retry)
|
|
363
|
+
retry = Math.min(30000, retry * 2)
|
|
364
|
+
}
|
|
365
|
+
}
|
|
366
|
+
}
|
|
367
|
+
|
|
368
|
+
// ---- browser events ----------------------------------------------------------------------
|
|
369
|
+
|
|
370
|
+
chrome.debugger.onEvent.addListener((source, method, params) => {
|
|
371
|
+
if (attached.has(source.tabId)) send({ method: 'chrome.debugger.onEvent', params: [source, method, params] })
|
|
372
|
+
})
|
|
373
|
+
chrome.debugger.onDetach.addListener((source, reason) => {
|
|
374
|
+
const controlled = attached.delete(source.tabId)
|
|
375
|
+
if (controlled) void rememberAttachments().catch(console.warn)
|
|
376
|
+
void chrome.action.setBadgeText({ tabId: source.tabId, text: '' }).catch(() => {})
|
|
377
|
+
if (controlled) send({ method: 'chrome.debugger.onDetach', params: [source, reason] })
|
|
378
|
+
})
|
|
379
|
+
chrome.tabs.onUpdated.addListener((tabId, change = {}) => {
|
|
380
|
+
if (change.status === 'loading' || change.url) tabRevisions.set(tabId, (tabRevisions.get(tabId) || 0) + 1)
|
|
381
|
+
scheduleInventory()
|
|
382
|
+
})
|
|
383
|
+
chrome.tabs.onCreated.addListener(scheduleInventory)
|
|
384
|
+
chrome.tabs.onRemoved.addListener(tabId => { tabRevisions.delete(tabId); scheduleInventory() })
|
|
385
|
+
chrome.tabs.onActivated.addListener(scheduleInventory)
|
|
386
|
+
chrome.tabs.onAttached.addListener(scheduleInventory)
|
|
387
|
+
chrome.tabs.onDetached.addListener(scheduleInventory)
|
|
388
|
+
chrome.windows.onCreated.addListener(scheduleInventory)
|
|
389
|
+
chrome.windows.onRemoved.addListener(scheduleInventory)
|
|
390
|
+
const reconnect = () => { void connect().catch(console.warn) }
|
|
391
|
+
chrome.runtime.onStartup.addListener(reconnect)
|
|
392
|
+
chrome.runtime.onInstalled.addListener(reconnect)
|
|
393
|
+
chrome.alarms.onAlarm.addListener(alarm => { if (alarm.name === 'reconnect') reconnect() })
|
|
394
|
+
|
|
395
|
+
chrome.runtime.onMessage.addListener((message, sender, respond) => {
|
|
396
|
+
if (sender.id !== chrome.runtime.id || (sender.url && sender.url !== chrome.runtime.getURL('popup.html'))) return
|
|
397
|
+
if (!['status', 'toggle', 'unpair', 'port', 'setup'].includes(message.type)) return
|
|
398
|
+
void (async () => {
|
|
399
|
+
await ready
|
|
400
|
+
const connected = !!token && socket?.readyState === WebSocket.OPEN && !pairing
|
|
401
|
+
if (message.type === 'setup') {
|
|
402
|
+
await chrome.tabs.create({ url: `http://127.0.0.1:${port}/setup` })
|
|
403
|
+
respond({ ok: true })
|
|
404
|
+
return
|
|
405
|
+
}
|
|
406
|
+
if (message.type === 'status') {
|
|
407
|
+
respond({ connected, availableTabs, controlledTabs: attached.size, stopped, paired: !!token,
|
|
408
|
+
extensionVersion: chrome.runtime.getManifest().version,
|
|
409
|
+
...(connected ? hostCompatibility : {}),
|
|
410
|
+
pairing: pairing === undefined ? undefined : { code: pairing.code, expiresAt: pairing.expiresAt }, port })
|
|
411
|
+
return
|
|
412
|
+
}
|
|
413
|
+
if (message.type === 'unpair') {
|
|
414
|
+
token = ''
|
|
415
|
+
pairing = undefined
|
|
416
|
+
await chrome.storage.local.remove(['token'])
|
|
417
|
+
await disconnect()
|
|
418
|
+
await connect()
|
|
419
|
+
await updateStatus()
|
|
420
|
+
respond({ ok: true })
|
|
421
|
+
return
|
|
422
|
+
}
|
|
423
|
+
if (message.type === 'port') {
|
|
424
|
+
const next = Number(message.port)
|
|
425
|
+
if (!Number.isSafeInteger(next) || next < 1 || next > 65535) throw new Error('端口必须是 1-65535 之间的整数')
|
|
426
|
+
port = next
|
|
427
|
+
await chrome.storage.local.set({ port })
|
|
428
|
+
await disconnect()
|
|
429
|
+
await connect()
|
|
430
|
+
respond({ ok: true })
|
|
431
|
+
return
|
|
432
|
+
}
|
|
433
|
+
stopped = message.stopped === true
|
|
434
|
+
await chrome.storage.local.set({ stopped })
|
|
435
|
+
if (stopped) await disconnect()
|
|
436
|
+
else await connect()
|
|
437
|
+
await updateStatus()
|
|
438
|
+
respond({ ok: true })
|
|
439
|
+
})().catch(error => respond({ error: error.message }))
|
|
440
|
+
return true
|
|
441
|
+
})
|
|
442
|
+
|
|
443
|
+
// ---- startup -----------------------------------------------------------------------------
|
|
444
|
+
|
|
445
|
+
// MV3 service workers register listeners synchronously and cannot use top-level
|
|
446
|
+
// await, so initialisation is a promise every entry point awaits.
|
|
447
|
+
ready = (async () => {
|
|
448
|
+
const stored = await chrome.storage.local.get(['profile', 'stopped', 'token', 'port'])
|
|
449
|
+
profile = stored.profile || crypto.randomUUID()
|
|
450
|
+
// Session storage survives worker suspension but never a full browser restart.
|
|
451
|
+
const session = await chrome.storage.session.get(['browserSession', 'ownedTabs'])
|
|
452
|
+
browserSession = session.browserSession || crypto.randomUUID()
|
|
453
|
+
await chrome.storage.session.set({ browserSession })
|
|
454
|
+
// A worker restart loses its relay and event consumers. Release only sessions
|
|
455
|
+
// this extension recorded AND still owns; `attached` alone would include
|
|
456
|
+
// DevTools and other extensions. sendCommand fails when this extension has no
|
|
457
|
+
// session, which is how ownership is proven rather than assumed.
|
|
458
|
+
const targets = await chrome.debugger.getTargets()
|
|
459
|
+
const previouslyOwned = new Set(Array.isArray(session.ownedTabs) ? session.ownedTabs.filter(Number.isSafeInteger) : [])
|
|
460
|
+
for (const target of targets) {
|
|
461
|
+
if (!target.attached || !previouslyOwned.has(target.tabId)) continue
|
|
462
|
+
const ours = await chrome.debugger.sendCommand({ tabId: target.tabId }, 'Target.getTargetInfo').then(() => true, () => false)
|
|
463
|
+
if (ours) await chrome.debugger.detach({ tabId: target.tabId })
|
|
464
|
+
await chrome.action.setBadgeText({ tabId: target.tabId, text: '' })
|
|
465
|
+
}
|
|
466
|
+
await rememberAttachments()
|
|
467
|
+
stopped = stored.stopped === true
|
|
468
|
+
token = stored.token || config.token || ''
|
|
469
|
+
port = stored.port || config.port || 43188
|
|
470
|
+
await chrome.storage.local.set({ profile })
|
|
471
|
+
if (!await chrome.alarms.get('reconnect')) await chrome.alarms.create('reconnect', { periodInMinutes: 0.5 })
|
|
472
|
+
await updateStatus()
|
|
473
|
+
})()
|
|
474
|
+
reconnect()
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
// lib/browser/extension/config.js — defaults for a freshly loaded extension.
|
|
2
|
+
//
|
|
3
|
+
// The pairing token is NEVER stored here: pairing writes it into
|
|
4
|
+
// chrome.storage.local, and this file stays a template so the folder in the
|
|
5
|
+
// plugin package can be loaded as-is.
|
|
6
|
+
export const config = { port: 43188, token: '' }
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
{
|
|
2
|
+
"manifest_version": 3,
|
|
3
|
+
"name": "DSH Computer Use Browser",
|
|
4
|
+
"version": "3.1.0",
|
|
5
|
+
"key": "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEA66PnaLnW5rXpi81/TDzCJOtQ1gEbDZJnYIovY4s2tZn6BYvzRTI7Q1W6vyWObl6i3xyilCuSabyMBJPV4lXYlbwTjf9mazT0jC9pd29Y0Qy4techPumFtMHtRvezCYmUj17NhviSckWbIMzRPuUFiWs3yMatQ6Lzw8jOHYBK7cr6wKgWXDYwg6fYZ9kc6PyOiT9KN1tkkshjaPmI+OBVdq/YLuQ78/k6MKhzDrd+lf05qgVxzTbjq7/FQGX5BeI6LLpVvCcLriwnPHn2RrwGcF7bg8+RNGenQjmPucBQ3wlS1MlJJyeUESrhU5NP6MFpMN1XmPG+AK7EUH0iyFfQWwIDAQAB",
|
|
6
|
+
"minimum_chrome_version": "125",
|
|
7
|
+
"description": "Connect existing Chrome and Edge tabs to DeepSeek Harness locally. Only tabs you reference are controlled.",
|
|
8
|
+
"permissions": [
|
|
9
|
+
"debugger",
|
|
10
|
+
"tabs",
|
|
11
|
+
"storage",
|
|
12
|
+
"alarms"
|
|
13
|
+
],
|
|
14
|
+
"host_permissions": [
|
|
15
|
+
"file:///*"
|
|
16
|
+
],
|
|
17
|
+
"background": {
|
|
18
|
+
"service_worker": "background.js",
|
|
19
|
+
"type": "module"
|
|
20
|
+
},
|
|
21
|
+
"action": {
|
|
22
|
+
"default_title": "DSH Browser",
|
|
23
|
+
"default_popup": "popup.html"
|
|
24
|
+
}
|
|
25
|
+
}
|
|
@@ -0,0 +1,89 @@
|
|
|
1
|
+
:root {
|
|
2
|
+
color-scheme: light dark;
|
|
3
|
+
--ink: #242424;
|
|
4
|
+
--muted: #595959;
|
|
5
|
+
--ground: #fff;
|
|
6
|
+
--soft: #f3f3f3;
|
|
7
|
+
--line: #ddd;
|
|
8
|
+
--control: #b8b8b8;
|
|
9
|
+
--focus: #0066cc;
|
|
10
|
+
--error: #a22330;
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
* { box-sizing: border-box; }
|
|
14
|
+
|
|
15
|
+
body {
|
|
16
|
+
width: 340px;
|
|
17
|
+
max-width: 100%;
|
|
18
|
+
margin: 0;
|
|
19
|
+
padding: 16px;
|
|
20
|
+
font: 13px/1.55 "Segoe UI", "Microsoft YaHei UI", "Microsoft YaHei", "Noto Sans CJK SC", sans-serif;
|
|
21
|
+
color: var(--ink);
|
|
22
|
+
background: var(--ground);
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
header {
|
|
26
|
+
display: flex;
|
|
27
|
+
align-items: center;
|
|
28
|
+
justify-content: space-between;
|
|
29
|
+
flex-wrap: wrap;
|
|
30
|
+
gap: 6px 12px;
|
|
31
|
+
margin-bottom: 10px;
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
h1, p { margin: 0; }
|
|
35
|
+
h1 { font-size: 16px; font-weight: 650; }
|
|
36
|
+
.connection-toggle { display: inline-flex; align-items: center; gap: 5px; white-space: nowrap; }
|
|
37
|
+
.connection-toggle input { margin: 0; accent-color: var(--ink); }
|
|
38
|
+
output, .hint { color: var(--muted); font-size: 12px; }
|
|
39
|
+
output { flex-basis: 100%; overflow-wrap: anywhere; }
|
|
40
|
+
output[data-state="error"] { color: var(--error); }
|
|
41
|
+
|
|
42
|
+
button, input { font: inherit; color: var(--ink); }
|
|
43
|
+
button {
|
|
44
|
+
min-width: 0;
|
|
45
|
+
min-height: 34px;
|
|
46
|
+
padding: 5px 10px;
|
|
47
|
+
border: 1px solid var(--control);
|
|
48
|
+
border-radius: 5px;
|
|
49
|
+
background: var(--ground);
|
|
50
|
+
cursor: pointer;
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
button:hover:not(:disabled) { background: var(--soft); }
|
|
54
|
+
button:disabled { opacity: .55; cursor: default; }
|
|
55
|
+
:is(button, input, summary):focus-visible { outline: 2px solid var(--focus); outline-offset: 2px; }
|
|
56
|
+
|
|
57
|
+
details { min-width: 0; }
|
|
58
|
+
summary { width: fit-content; max-width: 100%; cursor: pointer; color: var(--muted); }
|
|
59
|
+
summary:hover { color: var(--ink); }
|
|
60
|
+
input:not([type="checkbox"]) {
|
|
61
|
+
min-width: 0;
|
|
62
|
+
min-height: 34px;
|
|
63
|
+
padding: 5px 8px;
|
|
64
|
+
border: 1px solid var(--control);
|
|
65
|
+
border-radius: 5px;
|
|
66
|
+
background: var(--ground);
|
|
67
|
+
}
|
|
68
|
+
#port { width: 80px; }
|
|
69
|
+
.connection-settings { margin-top: 10px; padding-top: 10px; border-top: 1px solid var(--line); }
|
|
70
|
+
.connection-settings .row, #file-access, #setup { margin-top: 8px; }
|
|
71
|
+
#setup { width: 100%; }
|
|
72
|
+
|
|
73
|
+
.row { display: flex; flex-wrap: wrap; align-items: center; gap: 6px; }
|
|
74
|
+
#pairing { margin: 10px 0 0; padding: 10px; background: var(--soft); border-radius: 5px; }
|
|
75
|
+
#code { display: block; margin-top: 4px; font: 22px/1.4 ui-monospace, monospace; letter-spacing: .12em; }
|
|
76
|
+
[hidden] { display: none !important; }
|
|
77
|
+
|
|
78
|
+
@media (prefers-color-scheme: dark) {
|
|
79
|
+
:root {
|
|
80
|
+
--ink: #eee;
|
|
81
|
+
--muted: #bcbcbc;
|
|
82
|
+
--ground: #202020;
|
|
83
|
+
--soft: #2b2b2b;
|
|
84
|
+
--line: #424242;
|
|
85
|
+
--control: #747474;
|
|
86
|
+
--focus: #82b5ff;
|
|
87
|
+
--error: #ffa4ad;
|
|
88
|
+
}
|
|
89
|
+
}
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
<!doctype html>
|
|
2
|
+
<html lang="zh-CN">
|
|
3
|
+
<head>
|
|
4
|
+
<meta charset="utf-8">
|
|
5
|
+
<meta name="viewport" content="width=device-width, initial-scale=1">
|
|
6
|
+
<title>DSH Browser</title>
|
|
7
|
+
<link rel="stylesheet" href="popup.css">
|
|
8
|
+
<script type="module" src="popup.js"></script>
|
|
9
|
+
</head>
|
|
10
|
+
<body>
|
|
11
|
+
<header>
|
|
12
|
+
<h1>DSH 浏览器连接</h1>
|
|
13
|
+
<label class="connection-toggle"><input id="enabled" type="checkbox">连接 DSH</label>
|
|
14
|
+
<output id="status" aria-live="polite">正在检查连接…</output>
|
|
15
|
+
<p id="compatibility" class="hint" role="status" hidden></p>
|
|
16
|
+
</header>
|
|
17
|
+
<p class="hint">只连接本机。DSH 只能操作你在对话里引用的标签页;登录状态留在浏览器里。</p>
|
|
18
|
+
<div id="pairing" hidden>
|
|
19
|
+
<p class="hint">在 DSH 的浏览器连接设置里核对下面这个验证码,然后点「允许」。</p>
|
|
20
|
+
<code id="code"></code>
|
|
21
|
+
</div>
|
|
22
|
+
<button id="setup">打开连接设置</button>
|
|
23
|
+
<details class="connection-settings">
|
|
24
|
+
<summary>连接设置</summary>
|
|
25
|
+
<div class="row">
|
|
26
|
+
<label for="port">DSH 端口</label>
|
|
27
|
+
<input id="port" type="number" min="1" max="65535">
|
|
28
|
+
<button id="save-port">保存</button>
|
|
29
|
+
</div>
|
|
30
|
+
<div class="row"><button id="unpair" hidden>解除配对</button></div>
|
|
31
|
+
<p class="hint" id="file-access"></p>
|
|
32
|
+
<p class="hint" id="version"></p>
|
|
33
|
+
</details>
|
|
34
|
+
</body>
|
|
35
|
+
</html>
|