dsh-chrome-use 0.6.8 → 0.7.1
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/CHANGELOG.md +31 -0
- package/PRIVACY.md +4 -4
- package/README.md +35 -5
- package/docs/chrome-store-draft.md +6 -4
- package/docs/standalone-runner.md +143 -0
- package/extension/background.js +529 -142
- package/extension/controller.js +34 -3
- package/extension/leases.js +64 -0
- package/extension/manifest.json +2 -2
- package/extension/pairing-code.js +79 -0
- package/extension/popup-state.js +36 -13
- package/extension/popup.css +8 -4
- package/extension/popup.html +8 -10
- package/extension/popup.js +38 -11
- package/extension/protocol.js +13 -0
- package/package.json +1 -1
- package/src/bridge.js +116 -25
- package/src/client.js +25 -11
- package/src/index.js +9 -2
- package/src/port-selector.js +63 -0
- package/src/settings-client.js +11 -7
- package/src/settings-contract.js +13 -3
- package/src/settings.js +1 -1
- package/src/skill.js +3 -2
package/extension/controller.js
CHANGED
|
@@ -204,6 +204,11 @@ export class BrowserController {
|
|
|
204
204
|
if (method === 'Page.frameNavigated' && !params.frame.parentId || method === 'DOM.documentUpdated') {
|
|
205
205
|
state.refs.clear();
|
|
206
206
|
state.revision++;
|
|
207
|
+
// Requests of the previous document may never report loadingFinished, so
|
|
208
|
+
// drop them instead of making networkIdle wait for an event that is gone.
|
|
209
|
+
state.networkRequests.clear();
|
|
210
|
+
state.networkInFlight = 0;
|
|
211
|
+
state.lastNetworkActivity = Date.now();
|
|
207
212
|
}
|
|
208
213
|
let entry;
|
|
209
214
|
if (method === 'Runtime.consoleAPICalled') entry = { kind: 'console', level: params.type, text: params.args.map(arg => clip(arg.value ?? arg.description)).join(' ').slice(0, 4000) };
|
|
@@ -395,6 +400,9 @@ export class BrowserController {
|
|
|
395
400
|
if (!src) return { error: 'DRAG_SOURCE_GONE: take a fresh snapshot' };
|
|
396
401
|
const dst = ${dstExpr};
|
|
397
402
|
if (${hasDst} && !dst) return { error: 'DRAG_TARGET_NOT_FOUND: scroll the target into view and retry' };
|
|
403
|
+
// Pin the resolved target now. A page that rewrites its own text on
|
|
404
|
+
// dragover would otherwise make a toText lookup fail at drop time.
|
|
405
|
+
if (dst) dst.setAttribute('data-dsh-drag-dst', ${JSON.stringify(marker)});
|
|
398
406
|
const center = (el) => { const r = el.getBoundingClientRect(); return { x: r.left + r.width / 2, y: r.top + r.height / 2 }; };
|
|
399
407
|
src.scrollIntoView({ block: 'center', inline: 'center', behavior: 'instant' });
|
|
400
408
|
const a = center(src);
|
|
@@ -411,7 +419,7 @@ export class BrowserController {
|
|
|
411
419
|
const nativeExpression = `(() => {
|
|
412
420
|
const node = document.querySelector(${JSON.stringify(srcSelector)});
|
|
413
421
|
const src = (node && node.closest('[draggable="true"]')) || node;
|
|
414
|
-
const dst = ${
|
|
422
|
+
const dst = document.querySelector(${JSON.stringify(dstSelector)});
|
|
415
423
|
if (!src) return { error: 'DRAG_SOURCE_GONE' };
|
|
416
424
|
const target = dst || document.elementFromPoint(${plan.b.x}, ${plan.b.y});
|
|
417
425
|
if (!target) return { error: 'DRAG_TARGET_NOT_FOUND' };
|
|
@@ -462,7 +470,7 @@ export class BrowserController {
|
|
|
462
470
|
finally { clearTimeout(timer); if (state.loadResolve === resolveLoad) state.loadResolve = null; }
|
|
463
471
|
}
|
|
464
472
|
|
|
465
|
-
async execute(action, raw, signal = new AbortController().signal) {
|
|
473
|
+
async execute(action, raw, signal = new AbortController().signal, options = {}) {
|
|
466
474
|
const args = validateArgs(`chrome_${action}`, raw);
|
|
467
475
|
signal.throwIfAborted();
|
|
468
476
|
const tabId = args.tabId;
|
|
@@ -489,6 +497,10 @@ export class BrowserController {
|
|
|
489
497
|
const url = pageUrl(args.url);
|
|
490
498
|
const title = args.group.trim();
|
|
491
499
|
const tab = await bounded(this.api.tabs.create({ url: 'about:blank', active: false }), signal, 'tabs.create');
|
|
500
|
+
// Reserve ownership before any other await. If the caller refuses the
|
|
501
|
+
// reservation, this tab may belong to another connection; never detach it.
|
|
502
|
+
const reserved = options.onTabCreated ? options.onTabCreated(tab.id) : true;
|
|
503
|
+
if (reserved === false) throw new Error(`OPEN_FAILED (tab ${tab.id} remains open): LEASE_CONFLICT: this tab is already owned by another Harness`);
|
|
492
504
|
let group;
|
|
493
505
|
let groupError;
|
|
494
506
|
try { group = await this.group(tab.id, title, signal); }
|
|
@@ -738,8 +750,27 @@ export class BrowserController {
|
|
|
738
750
|
} finally {
|
|
739
751
|
await this.command(tabId, 'Runtime.releaseObject', { objectId }, AbortSignal.timeout(1000)).catch(() => {});
|
|
740
752
|
}
|
|
753
|
+
} else if (args.selector) {
|
|
754
|
+
// The schema advertises selector, so resolve it the same way scroll_to
|
|
755
|
+
// does instead of rejecting the call.
|
|
756
|
+
const located = await cdp('Runtime.evaluate', {
|
|
757
|
+
returnByValue: true,
|
|
758
|
+
expression: `(() => {
|
|
759
|
+
const el = document.querySelector(${JSON.stringify(args.selector)});
|
|
760
|
+
if (!el) return { error: 'SELECTOR_NOT_FOUND' };
|
|
761
|
+
if (!el.isConnected) return { error: 'STALE_ELEMENT' };
|
|
762
|
+
el.scrollIntoView({block:'center', inline:'center', behavior:'instant'});
|
|
763
|
+
const r = el.getBoundingClientRect();
|
|
764
|
+
const s = getComputedStyle(el);
|
|
765
|
+
if (!r.width || !r.height || s.visibility !== 'visible' || s.display === 'none' || Number(s.opacity) === 0) return { error: 'ELEMENT_NOT_VISIBLE' };
|
|
766
|
+
return { x: r.x + r.width / 2, y: r.y + r.height / 2 };
|
|
767
|
+
})()`,
|
|
768
|
+
});
|
|
769
|
+
if (located.exceptionDetails) throw new Error(clip(located.exceptionDetails.exception?.description ?? 'HOVER_FAILED'));
|
|
770
|
+
if (!located.result.value || located.result.value.error) throw new Error(located.result.value?.error ?? 'HOVER_TARGET');
|
|
771
|
+
point = located.result.value;
|
|
741
772
|
} else {
|
|
742
|
-
if (args.x === undefined || args.y === undefined) throw new Error('HOVER_TARGET: give ref or x and y');
|
|
773
|
+
if (args.x === undefined || args.y === undefined) throw new Error('HOVER_TARGET: give ref, selector, or x and y');
|
|
743
774
|
point = { x: args.x, y: args.y };
|
|
744
775
|
}
|
|
745
776
|
await cdp('Input.dispatchMouseEvent', { type: 'mouseMoved', x: point.x, y: point.y, buttons: 0 });
|
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
// Cross-harness tab leases. The bridge keeps chat-level claims; this table is
|
|
2
|
+
// the extension's authority over which connection may touch a tab. A lease is
|
|
3
|
+
// owned by the client object (one socket/generation), not by a port number, so
|
|
4
|
+
// a stale connection can never inherit a newer connection's tab.
|
|
5
|
+
export class TabLeases {
|
|
6
|
+
constructor() {
|
|
7
|
+
this.entries = new Map();
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
get(tabId) {
|
|
11
|
+
return this.entries.get(tabId);
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
owner(tabId) {
|
|
15
|
+
return this.entries.get(tabId)?.client;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
// A claim may re-enter for the same connection. A quarantined tab stays
|
|
19
|
+
// blocked until the worker reloads because native cleanup never finished.
|
|
20
|
+
acquire(tabId, client) {
|
|
21
|
+
const current = this.entries.get(tabId);
|
|
22
|
+
if (!current) {
|
|
23
|
+
this.entries.set(tabId, { client, state: 'active' });
|
|
24
|
+
return;
|
|
25
|
+
}
|
|
26
|
+
if (current.state === 'quarantined') throw new Error('TAB_LEASE_QUARANTINED: this tab did not finish native cleanup; reload the extension to clear it');
|
|
27
|
+
if (current.state === 'closing') throw new Error('TAB_LEASE_CLOSING: this tab is being cleaned up; retry after cleanup settles');
|
|
28
|
+
if (current.client === client) return;
|
|
29
|
+
throw new Error('TAB_OWNED_BY_ANOTHER_HARNESS: another Harness has claimed this tab; use another tab or release it there');
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
// Used for a tab that Chrome just created. Returning false tells the caller
|
|
33
|
+
// to fail closed without detaching a tab owned by someone else.
|
|
34
|
+
reserve(tabId, client) {
|
|
35
|
+
if (this.entries.has(tabId)) return false;
|
|
36
|
+
this.entries.set(tabId, { client, state: 'active' });
|
|
37
|
+
return true;
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
release(tabId, client) {
|
|
41
|
+
const current = this.entries.get(tabId);
|
|
42
|
+
if (current && (!client || current.client === client)) this.entries.delete(tabId);
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
// A connection that is closing owns no new commands. Mark it before any
|
|
46
|
+
// await so a command accepted in the same turn cannot slip through.
|
|
47
|
+
beginClose(tabId, client) {
|
|
48
|
+
const current = this.entries.get(tabId);
|
|
49
|
+
if (current && current.client === client && current.state === 'active') current.state = 'closing';
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
quarantine(tabId, client) {
|
|
53
|
+
const current = this.entries.get(tabId);
|
|
54
|
+
if (current && (!client || current.client === client)) current.state = 'quarantined';
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
ownedBy(client) {
|
|
58
|
+
return [...this.entries].filter(([, entry]) => entry.client === client).map(([tabId]) => tabId);
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
clear() {
|
|
62
|
+
this.entries.clear();
|
|
63
|
+
}
|
|
64
|
+
}
|
package/extension/manifest.json
CHANGED
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
"manifest_version": 3,
|
|
3
3
|
"name": "dsh-chrome-use",
|
|
4
4
|
"description": "Let DeepSeek Harness control Chrome tabs, with several agent sessions working in parallel.",
|
|
5
|
-
"version": "0.
|
|
5
|
+
"version": "0.7.1",
|
|
6
6
|
"minimum_chrome_version": "120",
|
|
7
7
|
"permissions": [
|
|
8
8
|
"debugger",
|
|
@@ -27,7 +27,7 @@
|
|
|
27
27
|
}
|
|
28
28
|
},
|
|
29
29
|
"content_security_policy": {
|
|
30
|
-
"extension_pages": "script-src 'self'; object-src 'none'; connect-src ws://127.0.0.1
|
|
30
|
+
"extension_pages": "script-src 'self'; object-src 'none'; connect-src ws://127.0.0.1:*"
|
|
31
31
|
},
|
|
32
32
|
"icons": {
|
|
33
33
|
"16": "icon-16.png",
|
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
// Crockford base32, uppercase, no I/L/O/U. Eight characters carry a 16-bit
|
|
2
|
+
// port and a 24-bit nonce. Keep this module pure so the bridge, extension and
|
|
3
|
+
// tests share exactly one encoder.
|
|
4
|
+
export const PAIRING_ALPHABET = '0123456789ABCDEFGHJKMNPQRSTVWXYZ';
|
|
5
|
+
export const PAIRING_CODE_LENGTH = 8;
|
|
6
|
+
const ALPHABET = [...PAIRING_ALPHABET];
|
|
7
|
+
const INDEX = new Map(ALPHABET.map((char, index) => [char, index]));
|
|
8
|
+
const ENCODED_RE = /^[0-9A-HJKMNP-TV-Z]{8}$/;
|
|
9
|
+
const LEGACY_RE = /^\d{8}$/;
|
|
10
|
+
const MAX_NONCE = 0xffffff;
|
|
11
|
+
|
|
12
|
+
export function normalizePairingCode(value) {
|
|
13
|
+
if (typeof value !== 'string') return null;
|
|
14
|
+
const text = value.trim().toUpperCase();
|
|
15
|
+
return text || null;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
export function isLegacyPairingCode(value) {
|
|
19
|
+
const text = normalizePairingCode(value);
|
|
20
|
+
return text !== null && LEGACY_RE.test(text);
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
export function isEncodedPairingCode(value) {
|
|
24
|
+
const text = normalizePairingCode(value);
|
|
25
|
+
return text !== null && !LEGACY_RE.test(text) && ENCODED_RE.test(text);
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
function assertPort(port) {
|
|
29
|
+
if (!Number.isSafeInteger(port) || port < 1024 || port > 65535) throw new Error(`INVALID_PORT: ${port} must be between 1024 and 65535`);
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
export function encodePairingCode(port, nonce) {
|
|
33
|
+
assertPort(port);
|
|
34
|
+
if (!Number.isSafeInteger(nonce) || nonce < 0 || nonce > MAX_NONCE) throw new Error(`INVALID_NONCE: ${nonce}`);
|
|
35
|
+
let value = (BigInt(port) << 24n) | BigInt(nonce);
|
|
36
|
+
let code = '';
|
|
37
|
+
for (let index = 0; index < PAIRING_CODE_LENGTH; index++) {
|
|
38
|
+
code = ALPHABET[Number(value & 31n)] + code;
|
|
39
|
+
value >>= 5n;
|
|
40
|
+
}
|
|
41
|
+
return code;
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
export function decodePairingCode(input) {
|
|
45
|
+
const code = normalizePairingCode(input);
|
|
46
|
+
if (code === null || LEGACY_RE.test(code) || !ENCODED_RE.test(code)) return null;
|
|
47
|
+
let value = 0n;
|
|
48
|
+
for (const char of code) {
|
|
49
|
+
const digit = INDEX.get(char);
|
|
50
|
+
if (digit === undefined) return null;
|
|
51
|
+
value = (value << 5n) | BigInt(digit);
|
|
52
|
+
}
|
|
53
|
+
const port = Number(value >> 24n);
|
|
54
|
+
const nonce = Number(value & 0xffffffn);
|
|
55
|
+
if (port < 1024 || port > 65535) return null;
|
|
56
|
+
return { port, nonce };
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
// Exact eight digits are the legacy alias. An all-digit encoded candidate is
|
|
60
|
+
// still a valid internal value, but the generator never returns one.
|
|
61
|
+
export function classifyPairingCode(value) {
|
|
62
|
+
const code = normalizePairingCode(value);
|
|
63
|
+
if (code === null) return null;
|
|
64
|
+
if (LEGACY_RE.test(code)) return { kind: 'legacy', code };
|
|
65
|
+
const decoded = decodePairingCode(code);
|
|
66
|
+
if (!decoded) return null;
|
|
67
|
+
return { kind: 'encoded', code, port: decoded.port };
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
export function generatePairingCode(port, nextNonce) {
|
|
71
|
+
assertPort(port);
|
|
72
|
+
for (let attempt = 0; attempt < 64; attempt++) {
|
|
73
|
+
const nonce = nextNonce();
|
|
74
|
+
if (!Number.isSafeInteger(nonce) || nonce < 0 || nonce > MAX_NONCE) throw new Error(`INVALID_NONCE: ${nonce}`);
|
|
75
|
+
const code = encodePairingCode(port, nonce);
|
|
76
|
+
if (!LEGACY_RE.test(code)) return code;
|
|
77
|
+
}
|
|
78
|
+
throw new Error('PAIRING_CODE_GENERATION_FAILED: could not generate a code containing a letter');
|
|
79
|
+
}
|
package/extension/popup-state.js
CHANGED
|
@@ -1,16 +1,39 @@
|
|
|
1
|
-
//
|
|
2
|
-
//
|
|
3
|
-
|
|
4
|
-
|
|
1
|
+
// The popup can hold several harness connections, so the pairing form is never
|
|
2
|
+
// hidden: a user with one connection may want to add a second. Keep the view
|
|
3
|
+
// model pure so the states are testable without a DOM.
|
|
4
|
+
export function popupState({ connections = [], reconnecting = false } = {}) {
|
|
5
|
+
const rows = connections.map(connectionRow);
|
|
6
|
+
const connectedCount = rows.filter(row => row.connected).length;
|
|
7
|
+
const connected = connectedCount > 0;
|
|
8
|
+
const paired = rows.length > 0;
|
|
5
9
|
return {
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
10
|
+
connected,
|
|
11
|
+
paired,
|
|
12
|
+
connectedCount,
|
|
13
|
+
reconnecting: Boolean(reconnecting) && !connected,
|
|
14
|
+
statusText: connected
|
|
15
|
+
? `已连接 ${connectedCount} 个 Harness`
|
|
16
|
+
: reconnecting
|
|
17
|
+
? '已配对,正在等待本机 Harness'
|
|
18
|
+
: paired
|
|
19
|
+
? '已配对,点击连接即可'
|
|
20
|
+
: '尚未配对',
|
|
21
|
+
connections: rows,
|
|
22
|
+
};
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
export function connectionRow({ port, connected = false, version = null, error = '' } = {}) {
|
|
26
|
+
const state = connected ? 'connected' : error ? 'error' : 'saved';
|
|
27
|
+
return {
|
|
28
|
+
port,
|
|
29
|
+
state,
|
|
30
|
+
connected: Boolean(connected),
|
|
31
|
+
text: connected
|
|
32
|
+
? `端口 ${port} · 已连接${version ? ` · ${version}` : ''}`
|
|
33
|
+
: error
|
|
34
|
+
? `端口 ${port} · 未连接:${error}`
|
|
35
|
+
: `端口 ${port} · 已保存,未连接`,
|
|
36
|
+
canConnect: !connected,
|
|
37
|
+
canForget: true,
|
|
15
38
|
};
|
|
16
39
|
}
|
package/extension/popup.css
CHANGED
|
@@ -5,14 +5,17 @@ header { display: flex; align-items: center; gap: 12px; padding: 23px 22px 18px;
|
|
|
5
5
|
.mark { width: 42px; height: 42px; border-radius: 12px; object-fit: cover; }
|
|
6
6
|
h1 { font-size: 20px; line-height: 1.25; margin: 0; font-weight: 650; }
|
|
7
7
|
header p { margin: 3px 0 0; color: #657083; font-size: 12px; }
|
|
8
|
-
.intro { padding: 16px 22px; background: #f3f7fd; border-bottom: 1px solid #dce4ef; }
|
|
9
|
-
.intro p { margin: 0; font-size: 15px; line-height: 1.6; }
|
|
10
|
-
.intro .intro-link { margin-top: 8px; font-weight: 600; }
|
|
11
8
|
main { padding: 18px 22px 8px; }
|
|
12
9
|
#status { margin: 0 0 18px; font-weight: 600; }
|
|
13
10
|
#status.connected { color: #16634a; }
|
|
14
11
|
label { display: block; margin-bottom: 7px; font-size: 12px; }
|
|
15
12
|
.pair-row { display: flex; gap: 8px; }
|
|
13
|
+
.pair-row .field { flex: 1; min-width: 0; }
|
|
14
|
+
.pair-row button { padding: 10px 13px; }
|
|
15
|
+
#code { text-transform: uppercase; }
|
|
16
|
+
#connections { margin: 0 0 14px; padding: 0; list-style: none; }
|
|
17
|
+
#connections li { display: flex; align-items: center; justify-content: space-between; gap: 8px; padding: 5px 0; }
|
|
18
|
+
#connections .link { background: none; border: 0; color: #657083; padding: 2px 4px; font-size: 12px; text-decoration: underline; }
|
|
16
19
|
input { min-width: 0; width: 100%; border: 1px solid #9caec6; padding: 10px; font: inherit; border-radius: 6px; letter-spacing: 2px; }
|
|
17
20
|
button { font: inherit; font-weight: 600; background: #185adb; color: white; border: 1px solid transparent; border-radius: 6px; padding: 10px 15px; cursor: pointer; white-space: nowrap; }
|
|
18
21
|
button:disabled { opacity: .45; cursor: default; }
|
|
@@ -27,7 +30,8 @@ li { padding: 5px 0; overflow-wrap: anywhere; }
|
|
|
27
30
|
.secondary:hover:not(:disabled) { background: #edf3fc; }
|
|
28
31
|
.mode-row { display: flex; align-items: center; gap: 7px; margin: 6px 0 8px; font-size: 13px; font-weight: 600; }
|
|
29
32
|
.mode-row input { width: auto; margin: 0; padding: 0; letter-spacing: 0; }
|
|
30
|
-
#error { color: #a22331; font-size: 12px; overflow-wrap: anywhere; }
|
|
33
|
+
#error { color: #a22331; font-size: 12px; overflow-wrap: anywhere; margin: 8px 0 0; }
|
|
34
|
+
#error:empty { display: none; }
|
|
31
35
|
footer { padding: 12px 22px 18px; color: #657083; font-size: 11px; }
|
|
32
36
|
footer p { margin: 0; }
|
|
33
37
|
footer .star { margin-bottom: 6px; font-size: 12px; }
|
package/extension/popup.html
CHANGED
|
@@ -3,20 +3,19 @@
|
|
|
3
3
|
<head><meta charset="utf-8"><meta name="viewport" content="width=device-width, initial-scale=1"><title>dsh-chrome-use</title><link rel="stylesheet" href="popup.css"></head>
|
|
4
4
|
<body>
|
|
5
5
|
<header><img class="mark" src="icon-48.png" alt=""><div><h1>dsh-chrome-use</h1><p>连接你的 Harness 与浏览器</p></div></header>
|
|
6
|
-
<section class="intro" id="intro" aria-label="使用方法">
|
|
7
|
-
<p class="intro-text" id="intro-text">使用方法:在 Harness 里说“请用 chrome_pair 连接浏览器”,把收到的 8 位配对码填到下面,点击“连接”即可让 AI 操作网页。</p>
|
|
8
|
-
<p class="intro-link"><a href="https://github.com/tao1020/dsh-chrome-use" target="_blank" rel="noreferrer">使用说明与源码(GitHub)</a></p>
|
|
9
|
-
</section>
|
|
10
6
|
<main>
|
|
11
7
|
<p id="status" role="status">正在检查连接…</p>
|
|
8
|
+
<ul id="connections" aria-label="已配对的 Harness"></ul>
|
|
12
9
|
<form id="pair-form">
|
|
13
|
-
<label for="code" id="pair-label">首次连接:输入 Harness 给出的配对码</label>
|
|
14
10
|
<p class="help permission" id="permission-help">连接后,Harness 可以读取和操作此 Chrome 中的所有普通网页,包括已经登录的网站。网页内容、截图和调试信息可能发送给你配置的模型服务。</p>
|
|
15
|
-
<div class="pair-row"
|
|
16
|
-
|
|
11
|
+
<div class="pair-row">
|
|
12
|
+
<input id="code" aria-label="配对码" inputmode="text" autocapitalize="characters" autocomplete="off" spellcheck="false" maxlength="8" placeholder="粘贴 8 位配对码">
|
|
13
|
+
<button type="submit">连接</button>
|
|
14
|
+
</div>
|
|
15
|
+
<p id="error" role="alert"></p>
|
|
17
16
|
</form>
|
|
18
17
|
<section>
|
|
19
|
-
<p class="help">连接后,AI
|
|
18
|
+
<p class="help">连接后,AI 自动选择网页,无需逐页授权。点击“断开全部连接”可以停止全部操作。</p>
|
|
20
19
|
<h2>正在调试的标签页</h2><ul id="tabs"><li>当前没有调试中的标签页</li></ul>
|
|
21
20
|
</section>
|
|
22
21
|
<section aria-label="运行模式">
|
|
@@ -25,8 +24,7 @@
|
|
|
25
24
|
<p class="help" id="foreground-on">开:操作时会把浏览器窗口抬到最前,会抢走你正在用的应用的焦点。</p>
|
|
26
25
|
<p class="help" id="foreground-off">关:只在浏览器窗口内切换标签页,不抢焦点;窗口被最小化或完全遮挡时,操作会返回 PAGE_HIDDEN。改完立即生效,不用重新连接。</p>
|
|
27
26
|
</section>
|
|
28
|
-
<button id="disconnect" class="secondary"
|
|
29
|
-
<p id="error" role="alert"></p>
|
|
27
|
+
<button id="disconnect" class="secondary">断开全部连接</button>
|
|
30
28
|
</main>
|
|
31
29
|
<footer>
|
|
32
30
|
<p class="star"><a href="https://github.com/tao1020/dsh-chrome-use" target="_blank" rel="noreferrer">喜欢这个插件?点个 Star,让更多人发现它 ⭐</a></p>
|
package/extension/popup.js
CHANGED
|
@@ -6,16 +6,35 @@ async function act(action, extra = {}) {
|
|
|
6
6
|
try {
|
|
7
7
|
const result = await chrome.runtime.sendMessage({ action, ...extra });
|
|
8
8
|
if (!result?.ok) throw new Error(result?.error ?? '扩展没有响应,请重新打开。');
|
|
9
|
-
$('status').textContent = result.connected ? '已连接到本机 Harness' : result.reconnecting ? '已配对,正在等待本机 Harness' : result.paired ? '已配对,点击连接即可' : '尚未配对';
|
|
10
|
-
$('status').classList.toggle('connected', result.connected);
|
|
11
9
|
const show = popupState(result);
|
|
12
|
-
$('
|
|
13
|
-
$('
|
|
14
|
-
$('
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
10
|
+
$('status').textContent = show.statusText;
|
|
11
|
+
$('status').classList.toggle('connected', show.connected);
|
|
12
|
+
$('connections').replaceChildren();
|
|
13
|
+
for (const row of show.connections) {
|
|
14
|
+
const li = document.createElement('li');
|
|
15
|
+
const label = document.createElement('span');
|
|
16
|
+
label.textContent = row.text;
|
|
17
|
+
li.append(label);
|
|
18
|
+
const buttons = document.createElement('span');
|
|
19
|
+
if (row.canConnect) {
|
|
20
|
+
const connect = document.createElement('button');
|
|
21
|
+
connect.type = 'button';
|
|
22
|
+
connect.className = 'link';
|
|
23
|
+
connect.textContent = '连接';
|
|
24
|
+
connect.addEventListener('click', () => act('connect', { port: row.port }));
|
|
25
|
+
buttons.append(connect);
|
|
26
|
+
}
|
|
27
|
+
if (row.canForget) {
|
|
28
|
+
const forget = document.createElement('button');
|
|
29
|
+
forget.type = 'button';
|
|
30
|
+
forget.className = 'link';
|
|
31
|
+
forget.textContent = '忘记';
|
|
32
|
+
forget.addEventListener('click', () => act('forget', { port: row.port }));
|
|
33
|
+
buttons.append(forget);
|
|
34
|
+
}
|
|
35
|
+
li.append(buttons);
|
|
36
|
+
$('connections').append(li);
|
|
37
|
+
}
|
|
19
38
|
$('tabs').replaceChildren();
|
|
20
39
|
for (const tab of result.tabs.length ? result.tabs : [{ title: '当前没有调试中的标签页' }]) {
|
|
21
40
|
const li = document.createElement('li');
|
|
@@ -23,7 +42,11 @@ async function act(action, extra = {}) {
|
|
|
23
42
|
$('tabs').append(li);
|
|
24
43
|
}
|
|
25
44
|
if (result.error && !result.connected) $('error').textContent = result.error;
|
|
26
|
-
|
|
45
|
+
return result;
|
|
46
|
+
} catch (error) {
|
|
47
|
+
$('error').textContent = error.message;
|
|
48
|
+
return undefined;
|
|
49
|
+
}
|
|
27
50
|
}
|
|
28
51
|
const foreground = $('foreground');
|
|
29
52
|
chrome.storage.local.get('foregroundMode').then(
|
|
@@ -31,6 +54,10 @@ chrome.storage.local.get('foregroundMode').then(
|
|
|
31
54
|
() => {}, // Keep the default (foreground on) when settings cannot be read.
|
|
32
55
|
);
|
|
33
56
|
foreground.addEventListener('change', () => { chrome.storage.local.set({ foregroundMode: foreground.checked }); });
|
|
34
|
-
$('pair-form').addEventListener('submit', async event => {
|
|
57
|
+
$('pair-form').addEventListener('submit', async event => {
|
|
58
|
+
event.preventDefault();
|
|
59
|
+
const result = await act('connect', { code: $('code').value });
|
|
60
|
+
if (result) $('code').value = '';
|
|
61
|
+
});
|
|
35
62
|
$('disconnect').addEventListener('click', () => act('disconnect'));
|
|
36
63
|
act('status');
|
package/extension/protocol.js
CHANGED
|
@@ -1,9 +1,22 @@
|
|
|
1
1
|
// Shared wire contract; this module has no Node or Chrome side effects.
|
|
2
|
+
// Default bridge port. A second harness can move its bridge with
|
|
3
|
+
// DSH_CHROME_USE_PORT and pair the extension with that port.
|
|
2
4
|
export const PORT = 18791;
|
|
3
5
|
export const MAX_MESSAGE = 8 * 1024 * 1024;
|
|
4
6
|
export const DEADLINE_MS = 15000;
|
|
5
7
|
// Advertised by the extension handshake. Older extensions cannot cancel one command, so the bridge serializes for them.
|
|
6
8
|
export const PARALLEL_TABS = 'parallel-tabs';
|
|
9
|
+
|
|
10
|
+
// The bridge binds loopback only, so any unprivileged port is acceptable. Low
|
|
11
|
+
// ports need root and are rejected instead of failing at listen time.
|
|
12
|
+
export function parsePort(value, fallback = PORT) {
|
|
13
|
+
if (value === undefined || value === null || value === '') return fallback;
|
|
14
|
+
const text = String(value).trim();
|
|
15
|
+
if (!/^\d{2,5}$/.test(text)) throw new Error(`INVALID_PORT: ${value} is not a port number`);
|
|
16
|
+
const port = Number(text);
|
|
17
|
+
if (port < 1024 || port > 65535) throw new Error(`INVALID_PORT: ${value} must be between 1024 and 65535`);
|
|
18
|
+
return port;
|
|
19
|
+
}
|
|
7
20
|
const tabId = { type: 'integer', minimum: 1, description: 'Exact tab ID returned by chrome_tabs or chrome_open.' };
|
|
8
21
|
const group = { type: 'string', maxLength: 40, description: 'Short Chrome tab-group name (1-40 characters). Reuse the same name for every tab that belongs to one task series so those tabs share one group.' };
|
|
9
22
|
const ref = { type: 'string', maxLength: 100, description: 'Element reference from the latest chrome_snapshot. Refresh after navigation.' };
|