dsh-chrome-use 0.13.0 → 0.14.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/CHANGELOG.md CHANGED
@@ -3,6 +3,18 @@
3
3
  版本号遵循语义化版本。格式参考 [Keep a Changelog](https://keepachangelog.com/zh-CN/1.1.0/)。
4
4
  扩展版本和 Harness 插件版本始终一起发布,数字保持一致。
5
5
 
6
+ ## [0.14.0] - 2026-11-18
7
+
8
+ ### 新增
9
+
10
+ - chrome_close 默认直接关闭标签页,不显示网页的 beforeunload 提示(例如“离开此网站?系统可能不会保存您所做的更改。”)。关闭时优先用 Target.closeTarget,不触发页面的离开确认;目标不可用时回退到 tabs.remove 并自动确认。传 force=false 可以让单个调用保留手动确认;返回值增加 forcedBeforeUnload,表示这次是否启用了强制关闭。
11
+ - chrome_cleanup 和 WebSocket 断开时的自动清理也使用同一规则;如果标签页已经 release,会短暂 attach 一次以便接收提示,再关闭。
12
+ - 扩展弹窗增加“关闭时不再确认”,默认勾选。勾选时 Agent 直接关闭,取消勾选后保留页面提示,由用户选择离开或取消;修改立即生效,无需重新连接。该设置只影响 Agent 主动关闭,不影响用户自己点关闭。
13
+
14
+ ### 文档
15
+
16
+ - 运行期 skill 指南补充关闭时的默认行为、force=false 和扩展弹窗开关。
17
+
6
18
  ## [0.13.0] - 2026-11-18
7
19
 
8
20
  ### 新增
@@ -233,11 +233,24 @@ async function saveToken(client, port, token, name) {
233
233
  });
234
234
  }
235
235
 
236
+ // Default on: an agent-initiated close should not leave the user stuck behind
237
+ // a native beforeunload prompt. The popup can turn this off for the whole
238
+ // extension; a per-call force=false still overrides the setting.
239
+ async function forceCloseBeforeUnload() {
240
+ try {
241
+ const stored = await chrome.storage.local.get('forceCloseBeforeUnload');
242
+ return stored.forceCloseBeforeUnload !== false;
243
+ } catch {
244
+ return true;
245
+ }
246
+ }
247
+
236
248
  async function drainClient(client, { closeOpened = false } = {}) {
237
249
  if (client.drain) return client.drain;
238
250
  client.drain = (async () => {
239
251
  // Block commands as soon as cleanup starts, before the first await.
240
252
  for (const tabId of leases.ownedBy(client)) leases.beginClose(tabId, client);
253
+ const forceClose = closeOpened ? await forceCloseBeforeUnload() : false;
241
254
  const tasks = [...client.tasks.values()];
242
255
  for (const task of tasks) task.cancellation.abort();
243
256
  await Promise.allSettled(tasks.map(task => task.done));
@@ -251,21 +264,9 @@ async function drainClient(client, { closeOpened = false } = {}) {
251
264
  if (leases.owner(tabId) !== client) continue;
252
265
  if (leases.get(tabId)?.state === 'quarantined') continue;
253
266
  const wasOpened = closeOpened && client.opened.has(tabId);
254
- try {
255
- await controller.detachTab(tabId);
256
- } catch (error) {
257
- if (leases.owner(tabId) === client) {
258
- leases.quarantine(tabId, client);
259
- client.lastError = String(error.message).slice(0, 2000);
260
- lastError = client.lastError;
261
- portErrors.set(client.port, client.lastError);
262
- }
263
- continue;
264
- }
265
- // Detach releases the lease through the controller callback, so remove
266
- // the tab only after native cleanup; a failed remove leaves it open.
267
267
  if (wasOpened) {
268
- try { await chrome.tabs.remove(tabId); }
268
+ // Close before detaching so the beforeunload event is still visible.
269
+ try { await controller.closeTab(tabId, undefined, { force: forceClose, attachIfNeeded: true }); }
269
270
  catch (error) {
270
271
  const message = String(error?.message ?? error).slice(0, 2000);
271
272
  if (!/No tab with id|Invalid tab ID/i.test(message)) {
@@ -275,6 +276,19 @@ async function drainClient(client, { closeOpened = false } = {}) {
275
276
  }
276
277
  }
277
278
  client.opened.delete(tabId);
279
+ if (leases.owner(tabId) === client) leases.release(tabId, client);
280
+ continue;
281
+ }
282
+ try {
283
+ await controller.detachTab(tabId);
284
+ } catch (error) {
285
+ if (leases.owner(tabId) === client) {
286
+ leases.quarantine(tabId, client);
287
+ client.lastError = String(error.message).slice(0, 2000);
288
+ lastError = client.lastError;
289
+ portErrors.set(client.port, client.lastError);
290
+ }
291
+ continue;
278
292
  }
279
293
  // A task or a late callback may have released/reassigned the lease while
280
294
  // native detach was running; do not clean up someone else's tab.
@@ -289,10 +303,9 @@ async function drainClient(client, { closeOpened = false } = {}) {
289
303
  for (const tabId of [...client.opened]) {
290
304
  const owner = leases.owner(tabId);
291
305
  if (owner && owner !== client) { client.opened.delete(tabId); continue; }
292
- // A detach failure is not fatal here: the tab is already gone from our
293
- // control, and remove() below is what actually closes it.
294
- try { await controller.detachTab(tabId); } catch {}
295
- try { await chrome.tabs.remove(tabId); }
306
+ // A detached tab has no dialog listener, so closeTab briefly reattaches
307
+ // for the forced close; a failure still falls back to plain removal.
308
+ try { await controller.closeTab(tabId, undefined, { force: forceClose, attachIfNeeded: true }); }
296
309
  catch (error) {
297
310
  const message = String(error?.message ?? error).slice(0, 2000);
298
311
  if (!/No tab with id|Invalid tab ID/i.test(message)) {
@@ -587,6 +600,7 @@ async function cleanupOpenedTabs(client, args, signal) {
587
600
  }
588
601
  const closed = [];
589
602
  const failed = [];
603
+ const forceClose = await forceCloseBeforeUnload();
590
604
  for (const tabId of targets) {
591
605
  signal.throwIfAborted();
592
606
  const owner = leases.owner(tabId);
@@ -597,10 +611,10 @@ async function cleanupOpenedTabs(client, args, signal) {
597
611
  }
598
612
  const brief = await tabBrief(tabId);
599
613
  try {
600
- // Detach first so emulation and debugger state are restored before the
601
- // tab disappears. A detach failure must not stop the tab from closing.
602
- await controller.detachTab(tabId).catch(() => {});
603
- await chrome.tabs.remove(tabId);
614
+ // Close while the debugger session is still attached so a beforeunload
615
+ // prompt can be accepted. closeTab removes the tab and drops local state;
616
+ // the lease is released explicitly below.
617
+ await controller.closeTab(tabId, signal, { force: forceClose, attachIfNeeded: true });
604
618
  leases.release(tabId, client);
605
619
  client.opened.delete(tabId);
606
620
  closed.push(brief);
@@ -699,7 +713,10 @@ async function handleCommand(client, message) {
699
713
  const options = action === 'open'
700
714
  ? { onTabCreated: tabId => { openedTabId = tabId; const reserved = leases.reserve(tabId, client); if (reserved) client.opened.add(tabId); return reserved; } }
701
715
  : undefined;
702
- response = { result: action === 'cleanup' ? await cleanupOpenedTabs(client, args, cancellation.signal) : await controller.execute(action, args, cancellation.signal, options) };
716
+ const commandArgs = action === 'close' && args.force === undefined
717
+ ? { ...args, force: await forceCloseBeforeUnload() }
718
+ : args;
719
+ response = { result: action === 'cleanup' ? await cleanupOpenedTabs(client, args, cancellation.signal) : await controller.execute(action, commandArgs, cancellation.signal, options) };
703
720
  if (action === 'close' && tabId !== undefined) client.opened.delete(tabId);
704
721
  } catch (error) {
705
722
  response = failResult(action, message, error);
@@ -272,7 +272,15 @@ export class BrowserController {
272
272
  }
273
273
  if (method === 'Network.loadingFinished' || method === 'Network.loadingFailed') { state.networkRequests.delete(params.requestId); state.networkInFlight = state.networkRequests.size; }
274
274
  if (method === 'Page.loadEventFired' && state.loadResolve) { const resolve = state.loadResolve; state.loadResolve = null; resolve(true); }
275
- if (method === 'Page.javascriptDialogOpening') state.dialog = { type: params.type, message: clip(params.message, 2000), defaultPrompt: clip(params.defaultPrompt, 1000) };
275
+ if (method === 'Page.javascriptDialogOpening') {
276
+ state.dialog = { type: params.type, message: clip(params.message, 2000), defaultPrompt: clip(params.defaultPrompt, 1000) };
277
+ // chrome_close defaults to force=true: a beforeunload guard must not leave
278
+ // the tab wedged behind a native prompt. Only this exact dialog type is
279
+ // auto-accepted; alert/confirm/prompt still belong to the caller.
280
+ if (params.type === 'beforeunload' && state.acceptBeforeUnload) {
281
+ this.api.debugger.sendCommand({ tabId }, 'Page.handleJavaScriptDialog', { accept: true }).catch(() => {});
282
+ }
283
+ }
276
284
  if (method === 'Page.javascriptDialogClosed') state.dialog = null;
277
285
  if (method === 'Page.fileChooserOpened') {
278
286
  clearTimeout(state.chooserTimer);
@@ -324,7 +332,7 @@ export class BrowserController {
324
332
  this.trackCleanup(tabId, attachment.then(() => this.api.debugger.detach({ tabId }), () => {}));
325
333
  throw attachFailure(error, tabId, currentUrl, 'attach');
326
334
  }
327
- this.attached.set(tabId, { refs: new Map(), revision: 0, logs: [], logSeq: 0, requests: [], dialog: null, chooser: null, chooserTimer: null, loadResolve: null, lastNetworkActivity: Date.now(), networkInFlight: 0, networkRequests: new Map(), mainFrameId: null, actions: [], actionSeq: 0, challenge: null, emulation: null });
335
+ this.attached.set(tabId, { refs: new Map(), revision: 0, logs: [], logSeq: 0, requests: [], dialog: null, chooser: null, chooserTimer: null, loadResolve: null, lastNetworkActivity: Date.now(), networkInFlight: 0, networkRequests: new Map(), mainFrameId: null, actions: [], actionSeq: 0, challenge: null, emulation: null, acceptBeforeUnload: false });
328
336
  let stage = 'attach';
329
337
  try {
330
338
  for (const domain of ['Page', 'DOM', 'Accessibility', 'Runtime', 'Network']) {
@@ -704,6 +712,48 @@ export class BrowserController {
704
712
  }
705
713
  }
706
714
 
715
+ async closeTab(tabId, signal, { force = true, attachIfNeeded = false } = {}) {
716
+ if (force && attachIfNeeded && !this.attached.has(tabId)) {
717
+ // A released tab has no debugger session, so a beforeunload event would
718
+ // be invisible. Attach briefly just for the close; failure falls back to
719
+ // a plain remove because an unattachable page cannot show that guard.
720
+ await this.attach(tabId, signal).catch(() => {});
721
+ }
722
+ const state = this.attached.get(tabId);
723
+ if (state) {
724
+ state.acceptBeforeUnload = force;
725
+ // The dialog may already be open before this call; accept it directly
726
+ // instead of waiting for another Page.javascriptDialogOpening event.
727
+ if (force && state.dialog?.type === 'beforeunload') {
728
+ await this.api.debugger.sendCommand({ tabId }, 'Page.handleJavaScriptDialog', { accept: true }).catch(() => {});
729
+ }
730
+ }
731
+ let closedByTarget = false;
732
+ if (force && state) {
733
+ // Target.closeTarget closes the target without running beforeunload, so
734
+ // the page never draws the dialog. Fall back to tabs.remove for a target
735
+ // that cannot be found or closed.
736
+ try {
737
+ const info = await this.command(tabId, 'Target.getTargetInfo', {}, signal);
738
+ const targetId = info?.targetInfo?.targetId;
739
+ if (targetId) {
740
+ await this.command(tabId, 'Target.closeTarget', { targetId }, signal);
741
+ closedByTarget = true;
742
+ }
743
+ } catch {
744
+ closedByTarget = false;
745
+ }
746
+ }
747
+ try {
748
+ if (!closedByTarget) await bounded(this.api.tabs.remove(tabId), signal, 'tabs.remove');
749
+ } finally {
750
+ // Removing the tab also drops the debugger session. Clearing state here
751
+ // keeps a failed remove from leaving a claim pointing at a closed tab.
752
+ this.attached.delete(tabId);
753
+ }
754
+ return { tabId, closed: true, forcedBeforeUnload: Boolean(force && state) };
755
+ }
756
+
707
757
  async perform(action, raw, signal = new AbortController().signal, options = {}) {
708
758
  const args = validateArgs(`chrome_${action}`, raw);
709
759
  signal.throwIfAborted();
@@ -753,13 +803,7 @@ export class BrowserController {
753
803
  await this.attach(tabId, signal);
754
804
  return { tabId, attached: true };
755
805
  }
756
- if (action === 'close') {
757
- // Removing the tab also drops the debugger session; clean local state first
758
- // so a failed remove cannot leave a claim pointing at a closed tab.
759
- this.attached.delete(tabId);
760
- await bounded(this.api.tabs.remove(tabId), signal, 'tabs.remove');
761
- return { tabId, closed: true };
762
- }
806
+ if (action === 'close') return this.closeTab(tabId, signal, { force: args.force !== false, attachIfNeeded: true });
763
807
  if (action === 'downloads') {
764
808
  if (typeof this.api.downloads?.search !== 'function') throw new Error('DOWNLOADS_UNSUPPORTED: this Chrome exposes no downloads API; update the extension and grant the downloads permission');
765
809
  const limit = args.limit ?? 10;
@@ -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.13.0",
5
+ "version": "0.14.0",
6
6
  "minimum_chrome_version": "120",
7
7
  "permissions": [
8
8
  "debugger",
@@ -28,6 +28,12 @@
28
28
  <p class="help" id="foreground-on">开:操作时会把浏览器窗口抬到最前,会抢走你正在用的应用的焦点。</p>
29
29
  <p class="help" id="foreground-off">关:只在浏览器窗口内切换标签页,不抢焦点;窗口被最小化或完全遮挡时,操作会返回 PAGE_HIDDEN。改完立即生效,不用重新连接。</p>
30
30
  </section>
31
+ <section aria-label="关闭行为">
32
+ <h2>关闭行为</h2>
33
+ <label class="mode-row"><input type="checkbox" id="force-close" checked> <span>关闭时不再确认</span></label>
34
+ <p class="help">开:Agent 关闭标签页时直接关掉,不再弹出网页的“离开此网站?”提示,页面未保存的修改会丢弃。</p>
35
+ <p class="help">关:保留这个提示,由你选择离开或取消。改完立即生效。</p>
36
+ </section>
31
37
  <button id="disconnect" class="secondary">断开全部连接</button>
32
38
  </main>
33
39
  <footer>
@@ -83,6 +83,12 @@ chrome.storage.local.get('foregroundMode').then(
83
83
  () => {}, // Keep the default (foreground on) when settings cannot be read.
84
84
  );
85
85
  foreground.addEventListener('change', () => { chrome.storage.local.set({ foregroundMode: foreground.checked }); });
86
+ const forceClose = $('force-close');
87
+ chrome.storage.local.get('forceCloseBeforeUnload').then(
88
+ ({ forceCloseBeforeUnload }) => { forceClose.checked = forceCloseBeforeUnload !== false; },
89
+ () => {}, // Keep the default (forced close on) when settings cannot be read.
90
+ );
91
+ forceClose.addEventListener('change', () => { chrome.storage.local.set({ forceCloseBeforeUnload: forceClose.checked }); });
86
92
  $('pair-form').addEventListener('submit', async event => {
87
93
  event.preventDefault();
88
94
  const result = await act('connect', { code: $('code').value });
@@ -38,7 +38,7 @@ export const TOOLS = [
38
38
  tool('open', 'Open an HTTP(S) URL in a new browser tab owned by this chat and put it in a Chrome tab group named by `group`. Reuse one short group name for tabs that belong to the same task series so those tabs stay in a single group; use a new name for an unrelated task.', { url: text(4096), group }, ['url', 'group']),
39
39
  tool('claim', 'Claim a normal Chrome tab for this chat. If another chat owns it, wait up to 10 seconds in FIFO order for release; never steal ownership. Release promptly when finished, or use a separate tab.', { tabId }, ['tabId']),
40
40
  tool('release', 'Stop debugging this chat\'s tab and release ownership. The tab stays open.', { tabId }, ['tabId']),
41
- tool('close', 'Close a tab owned by this chat, ending its debugger session and removing it. Use it to clean up tabs opened for a finished task; the user\'s own tabs need a claim first.', { tabId }, ['tabId']),
41
+ tool('close', 'Close a tab owned by this chat, ending its debugger session and removing it. By default it closes without waiting for a page beforeunload guard, so agent cleanup never stalls on a manual click; the extension popup can turn that off, and force=false leaves the confirmation to the user. Use it to clean up tabs opened for a finished task; the user\'s own tabs need a claim first.', { tabId, force: { type: 'boolean', description: 'Accept a page beforeunload confirmation and discard unsaved changes. Default comes from the extension\'s 关闭时不再确认 setting (on); false always leaves the dialog for the user.' } }, ['tabId']),
42
42
  tool('cleanup', 'Close the tabs this chat opened with chrome_open, so a finished task does not leave a pile of tabs. It never closes tabs you only claimed and never closes tabs owned by another chat. Use dryRun=true to preview and keep=[tabIds] to spare a few. The extension also runs this cleanup when the chat disconnects; call it explicitly when the task is done but the connection stays open.', { dryRun: { type: 'boolean', description: 'List what would close without closing anything. Default false.' }, keep: { type: 'array', items: { type: 'integer', minimum: 1 }, maxItems: 100, description: 'Tab IDs opened by this chat that should stay open.' } }),
43
43
  tool('keep_awake', 'Keep this Mac awake while a long browser task runs, so an idle display or idle lock does not stop Chrome. macOS only. Starts caffeinate with a bounded lifetime; call again to extend, or off=true to stop early. It cannot unlock a screen that is already locked, and it does not override a manual lock, a closed lid, or a corporate power policy. Call it before the user steps away, and stop it when the task is done.', { minutes: { type: 'integer', minimum: 1, maximum: 480, description: 'How long to stay awake, in minutes (1-480); default 30. Calling again replaces the previous timer.' }, off: { type: 'boolean', description: 'Stop the current keep-awake assertion instead of starting or extending one.' } }),
44
44
  tool('navigate', 'Navigate a claimed tab to an HTTP(S) URL. Take a fresh snapshot afterward.', { tabId, url: text(4096) }, ['tabId', 'url']),
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "dsh-chrome-use",
3
- "version": "0.13.0",
3
+ "version": "0.14.0",
4
4
  "description": "Control Google Chrome tabs from DeepSeek Harness.",
5
5
  "type": "module",
6
6
  "main": "./src/index.js",
package/src/client.js CHANGED
@@ -68,7 +68,7 @@ async function checkRelease(current, fetcher = fetch) {
68
68
  } finally { clearTimeout(timer); }
69
69
  }
70
70
 
71
- const packageVersion = "0.13.0";
71
+ const packageVersion = "0.14.0";
72
72
  // Wrapped by scripts/build-client.mjs for the Harness browser module loader.
73
73
  const React = require('react');
74
74
  const { createElement: h, useState, useEffect, useRef } = React;
package/src/skill.js CHANGED
@@ -42,7 +42,7 @@ export const chromeUseSkill = {
42
42
  11. 滚动到某个元素用 chrome_scroll_to(ref 或 selector);截图用 chrome_screenshot,默认视口,fullPage=true 截整页,给 ref 或 selector 截单个元素,inline=true 会把图片一并返回给模型;导出 PDF 用 chrome_pdf。文件保存在本机;图片能否被理解取决于模型能力,inline 被拒绝时不要反复重试,改回不带 inline 的截图,拿本地路径。
43
43
  12. 点击下载链接后用 chrome_downloads(wait=true)确认是否保存成功;它只读取 Chrome 的下载记录,不修改文件。
44
44
  13. 预计任务会持续几分钟、用户可能要离开时,先调用 chrome_keep_awake(默认 30 分钟,最多 480 分钟),让 Mac 不因空闲息屏或空闲锁屏而中断;再调用会续期,off=true 提前停止。它只适用于 macOS,不能解锁已经锁定的屏幕,也不能覆盖手动锁屏、合盖或公司电源策略。任务结束或不再需要时传 off=true。
45
- 14. 结束时用 chrome_release 释放页面;本会话用 chrome_open 打开的页面用 chrome_cleanup 批量关闭,避免留下几十个标签页。dryRun=true 先看会关哪些,keep=[tabId] 保留指定的页面。它只关本会话自己打开的页面,不会关用户自己开的页面、只是 claim 的页面或其他会话的页面;WebSocket 断开时扩展也会自动做同样的清理。单个页面也可以直接用 chrome_close。
45
+ 14. 结束时用 chrome_release 释放页面;本会话用 chrome_open 打开的页面用 chrome_cleanup 批量关闭,避免留下几十个标签页。dryRun=true 先看会关哪些,keep=[tabId] 保留指定的页面。它只关本会话自己打开的页面,不会关用户自己开的页面、只是 claim 的页面或其他会话的页面;WebSocket 断开时扩展也会自动做同样的清理。单个页面也可以直接用 chrome_close。Agent 关闭默认会自动确认网页的 beforeunload(如“离开此网站?”),不再等你手点;需要保留这个提示时传 force=false,或在扩展弹窗取消“关闭时不再确认”。
46
46
 
47
47
  ## 出错时
48
48