feat(extension): auto-close automation window after command completes

- Add 'close-window' action to extension protocol and background.ts
- Add Page.closeWindow() method to send close-window command
- browserSession() now closes automation window in cleanup
- Remove domain pre-navigation + 2s wait from main.ts (CDP handles
  cross-domain cookies natively, no same-origin workaround needed)
- Net effect: commands run faster, no stale windows left behind
This commit is contained in:
jackwener committed 2026-03-19 23:59:31 +08:00
1 parent fde618063f
commit 2f6d28a3e9
7 files changed
+46 -13

No files matched your search

+14
View File
@@ -219,6 +219,7 @@ async function handleCommand(cmd) {
case "tabs": return await handleTabs(cmd);
case "cookies": return await handleCookies(cmd);
case "screenshot": return await handleScreenshot(cmd);
case "close-window": return await handleCloseWindow(cmd);
default: return {
id: cmd.id,
ok: false,
@@ -447,4 +448,17 @@ async function handleScreenshot(cmd) {
};
}
}
async function handleCloseWindow(cmd) {
if (automationWindowId !== null) {
try {
await chrome.windows.remove(automationWindowId);
} catch {}
automationWindowId = null;
}
return {
id: cmd.id,
ok: true,
data: { closed: true }
};
}
//#endregion
+14
View File
@@ -165,6 +165,8 @@ async function handleCommand(cmd: Command): Promise<Result> {
return await handleCookies(cmd);
case 'screenshot':
return await handleScreenshot(cmd);
case 'close-window':
return await handleCloseWindow(cmd);
default:
return { id: cmd.id, ok: false, error: `Unknown action: ${cmd.action}` };
}
@@ -332,3 +334,15 @@ async function handleScreenshot(cmd: Command): Promise<Result> {
return { id: cmd.id, ok: false, error: err instanceof Error ? err.message : String(err) };
}
}
async function handleCloseWindow(cmd: Command): Promise<Result> {
if (automationWindowId !== null) {
try {
await chrome.windows.remove(automationWindowId);
} catch {
// Window may already be closed
}
automationWindowId = null;
}
return { id: cmd.id, ok: true, data: { closed: true } };
}
+1 -1
View File
@@ -5,7 +5,7 @@
* Everything else is just JS code sent via 'exec'.
*/
export type Action = 'exec' | 'navigate' | 'tabs' | 'cookies' | 'screenshot';
export type Action = 'exec' | 'navigate' | 'tabs' | 'cookies' | 'screenshot' | 'close-window';
export interface Command {
/** Unique request ID */