diff options
Diffstat (limited to 'extensions/chrome-profile-bridge/browser-extension')
| -rw-r--r-- | extensions/chrome-profile-bridge/browser-extension/service_worker.js | 427 |
1 files changed, 316 insertions, 111 deletions
diff --git a/extensions/chrome-profile-bridge/browser-extension/service_worker.js b/extensions/chrome-profile-bridge/browser-extension/service_worker.js index 1f178e4..85be0a2 100644 --- a/extensions/chrome-profile-bridge/browser-extension/service_worker.js +++ b/extensions/chrome-profile-bridge/browser-extension/service_worker.js @@ -226,35 +226,34 @@ function isPiChromeOwnedTarget(tabId, sessionKey) { return false; } -// Create a fresh automation target for `sessionKey`. If this session already has a tab group, -// create the tab inside that group's window so one Pi session keeps one Chrome tab group (Chrome -// groups cannot span windows). If no group exists yet, prefer an isolated window; fall back to a -// tab. When the tab is created in a pre-existing group window, leave windowId unset so cleanup only -// closes our tab, never that whole window. +// The window agents' tabs go in: the user's last focused normal window, so every session's tabs +// sit together in one window (as tab groups) instead of a window each. An inactive tab still takes +// trusted input and renders screenshots (measured 2026-11-14), so sharing a window costs nothing. +// Null when no normal window is open; the caller then creates one. +async function agentWindowId() { + if (typeof chrome.windows?.getLastFocused !== "function") return null; + const win = await chrome.windows.getLastFocused({ windowTypes: ["normal"] }).catch(() => null); + if (win && typeof win.id === "number" && !win.incognito) return win.id; + const all = typeof chrome.windows.getAll === "function" ? await chrome.windows.getAll({ windowTypes: ["normal"] }).catch(() => []) : []; + const other = all.find((w) => typeof w.id === "number" && !w.incognito); + return other ? other.id : null; +} + +// Create a fresh automation target for `sessionKey`: an inactive tab in the session's group's +// window if it has a group (Chrome groups cannot span windows), else in the user's window. A new +// window only when the browser has none. When the tab goes in a pre-existing window, windowId stays unset. async function createAutomationTarget(sessionKey, groupTitle) { const existingGroup = groupTitle ? await findGroupRecordByTitle(groupTitle) : null; - if (existingGroup && typeof existingGroup.windowId === "number") { - const tab = await chrome.tabs.create({ url: "about:blank", active: false, windowId: existingGroup.windowId }); - automationTargets.set(sessionKey, { windowId: undefined, tabId: typeof tab.id === "number" ? tab.id : undefined }); + const windowId = existingGroup && typeof existingGroup.windowId === "number" ? existingGroup.windowId : await agentWindowId(); + if (windowId === null && typeof chrome.windows?.create === "function") { + const win = await chrome.windows.create({ url: "about:blank", focused: false }); + const created = win && Array.isArray(win.tabs) ? win.tabs[0] : undefined; + if (!created || typeof created.id !== "number") throw new Error("Chrome created a window without a tab"); + automationTargets.set(sessionKey, { windowId: typeof win.id === "number" ? win.id : undefined, tabId: created.id }); await persistAutomationTargets(); - return tab; + return created; } - if (chrome.windows && typeof chrome.windows.create === "function") { - try { - const win = await chrome.windows.create({ url: "about:blank", focused: false }); - const created = win && Array.isArray(win.tabs) ? win.tabs[0] : undefined; - if (created && typeof created.id === "number") { - automationTargets.set(sessionKey, { windowId: typeof win.id === "number" ? win.id : undefined, tabId: created.id }); - await persistAutomationTargets(); - return created; - } - } catch { - // Window creation can fail (policy, headless, etc.); fall back to a dedicated tab below. - } - } - // Tab fallback: the tab lives in a pre-existing (user/shared) window we did NOT create, so we - // must leave windowId unset — cleanup then closes only our tab, never the user's window. - const tab = await chrome.tabs.create({ url: "about:blank", active: false }); + const tab = await chrome.tabs.create({ url: "about:blank", active: false, ...(windowId !== null ? { windowId } : {}) }); automationTargets.set(sessionKey, { windowId: undefined, tabId: typeof tab.id === "number" ? tab.id : undefined }); await persistAutomationTargets(); return tab; @@ -430,6 +429,7 @@ async function attachDebugger(tabId) { if (err) err = await retryPageTargetIfExtensionBlocked(err, "attach-page-target-retry3"); if (err) { recordAttachEvent({ kind: "attach-retry2-failed", tabId, message: String(err.message || err), tabUrl: tabSnapshot?.url }); + if (FOREIGN_EXTENSION_BLOCK.test(String(err.message || err))) throw await overlayBlockedError(tabId); const meta = await describeInputTarget(tabId); throw new Error(`Chrome debugger attach failed for tab ${tabId}: ${String(err.message || err)}${targetMetaSuffix(meta)}`); } @@ -439,6 +439,13 @@ async function attachDebugger(tabId) { // Seed pointer in a plausible "just left the address bar" location. const entry = { detachAt: Date.now() + INPUT_IDLE_DETACH_MS, pointer: { x: 120 + Math.random() * 200, y: 80 + Math.random() * 120 }, debuggee: attachedDebuggee || { tabId } }; attachedTabs.set(tabId, entry); + // Page events are what tell us a page opened a dialog; without them nobody answers it. + // Agents' tabs are usually inactive, so also let the page believe it has focus: menus and pickers + // that close on blur stay open. Ends when the debugger detaches. + await Promise.all([ + cdpRaw(tabId, "Page.enable", {}).catch(() => undefined), + cdpRaw(tabId, "Emulation.setFocusEmulationEnabled", { enabled: true }).catch(() => undefined), + ]); return entry; } @@ -476,10 +483,12 @@ async function inputDebug(params) { }; } -// Chrome drops trusted CDP input to hidden pages: mouse presses and keys are silently ignored and -// mouse moves can hang waiting for a frame. A page is hidden when its tab is inactive, its window is -// minimized, or (macOS) its window became a native window-tab behind a full-screen window. Check -// before sending input so the caller gets an actionable error instead of a false success. +// A page is hidden when its tab is inactive, its window is minimized, or (macOS) its window is +// covered or on another Space. Measured 2026-11-14 in Brave: an inactive tab still takes trusted +// CDP clicks, key presses, text and mouse moves, and screenshots render it live; only mouseWheel +// hangs waiting for a frame that never comes, so chrome.scroll scrolls hidden pages by script. +// Clicks, text, key presses and screenshots measured the same in Chrome 157 on 2026-11-18. +// A minimized window is untested, so input there still fails up front with an actionable error. async function pageVisibilityState(tabId) { try { const evaluated = await cdp(tabId, "Runtime.evaluate", { expression: "document.visibilityState", returnByValue: true }); @@ -497,8 +506,20 @@ function hiddenTabReason(tab, win) { return "its window is covered by another window or is on another Space (Chrome treats fully covered windows as hidden)"; } +// The tab and window when trusted input to the page may be dropped (hidden in a minimized window), +// else null. +async function inputRefusingWindow(tab) { + if ((await pageVisibilityState(tab.id)) !== "hidden") return null; + const fresh = await chrome.tabs.get(tab.id).catch(() => tab); + const win = typeof chrome.windows?.get === "function" ? await chrome.windows.get(fresh?.windowId ?? tab.windowId).catch(() => null) : null; + return win?.state === "minimized" ? { fresh, win } : null; +} + async function assertTabVisibleForInput(tab, params, label) { - let state = await pageVisibilityState(tab.id); + const refusing = await inputRefusingWindow(tab); + if (!refusing) return; + const { fresh, win } = refusing; + let state = "hidden"; if (state !== "hidden") return; if (foregroundRequested(params)) { // bringToFront just ran; give Chrome a moment to report the page visible. @@ -508,11 +529,9 @@ async function assertTabVisibleForInput(tab, params, label) { } if (state !== "hidden") return; } - const fresh = await chrome.tabs.get(tab.id).catch(() => tab); - const win = typeof chrome.windows?.get === "function" ? await chrome.windows.get(fresh?.windowId ?? tab.windowId).catch(() => null) : null; const error = new Error( - `${label}: tab ${tab.id} is hidden because ${hiddenTabReason(fresh, win)}. Chrome ignores trusted input to hidden pages, so the events would be dropped. ` + - `Pass background:false for this call (or run /chrome background off) to bring the tab forward. chrome_click and chrome_fill can instead use their DOM fallback (untrusted page events).`, + `${label}: tab ${tab.id} is hidden because ${hiddenTabReason(fresh, win)}. Input to a minimized window may be dropped. ` + + `Pass background:false for this call to bring the tab forward. chrome_click and chrome_fill can instead use their DOM fallback (untrusted page events).`, ); error.hiddenTab = true; error.windowState = win?.state; @@ -610,27 +629,36 @@ async function executeScriptWithFallback(options, label) { if (!isScriptingAccessDenied(error) || !chrome.debugger) throw error; const tabId = options?.target?.tabId; recordAttachEvent({ kind: "scripting-denied-cdp-fallback", tabId, files: options?.files || null, message: String(error?.message || error).slice(0, 300) }); - if (Array.isArray(options?.files) && options.files.length) { - for (const file of options.files) { - if (!packagedFileSources.has(file)) { - const response = await fetch(chrome.runtime.getURL(file)); - if (!response.ok) throw new Error(`Could not read ${file} for CDP injection (HTTP ${response.status})`); - packagedFileSources.set(file, await response.text()); - } - const injected = await cdpEval(tabId, packagedFileSources.get(file)); - if (injected.exceptionDetails) { - throw new Error(`${label || file}: ${cdpExceptionText(injected.exceptionDetails) || "CDP injection failed"}`); - } + return executeScriptViaCdp(options, label); + } +} + +// The CDP half of executeScriptWithFallback, also used directly when scripting silently runs +// nothing: a document served with CSP `sandbox` (no allow-scripts), such as +// raw.githubusercontent.com, makes chrome.scripting.executeScript resolve with a null result +// instead of throwing, while Runtime.evaluate still works there. +async function executeScriptViaCdp(options, label) { + const tabId = options?.target?.tabId; + if (Array.isArray(options?.files) && options.files.length) { + for (const file of options.files) { + if (!packagedFileSources.has(file)) { + const response = await fetch(chrome.runtime.getURL(file)); + if (!response.ok) throw new Error(`Could not read ${file} for CDP injection (HTTP ${response.status})`); + packagedFileSources.set(file, await response.text()); + } + const injected = await cdpEval(tabId, packagedFileSources.get(file)); + if (injected.exceptionDetails) { + throw new Error(`${label || file}: ${cdpExceptionText(injected.exceptionDetails) || "CDP injection failed"}`); } - return [{ result: undefined }]; - } - const expression = `(${options.func.toString()})(...${JSON.stringify(Array.isArray(options.args) ? options.args : [])})`; - const result = await cdpEval(tabId, expression); - if (result.exceptionDetails) { - throw new Error(`${label || "page script"}: ${cdpExceptionText(result.exceptionDetails) || "evaluation failed"}`); } - return [{ result: result.result?.value }]; + return [{ result: undefined }]; + } + const expression = `(${options.func.toString()})(...${JSON.stringify(Array.isArray(options.args) ? options.args : [])})`; + const result = await cdpEval(tabId, expression); + if (result.exceptionDetails) { + throw new Error(`${label || "page script"}: ${cdpExceptionText(result.exceptionDetails) || "evaluation failed"}`); } + return [{ result: result.result?.value }]; } // Wraps cdpRaw with one auto-recover on detached/closed sessions: @@ -656,6 +684,12 @@ async function findForeignExtensionTargets() { } } +// Other extensions' targets that belong to this tab: by the target's tabId, or by the tabId that +// password managers put in the frame's own URL (the frame target can come without a tabId). +function foreignTargetsForTab(all, tabId) { + return all.filter((t) => t.tabId === tabId || new RegExp(`[?&]tabId=${tabId}(?:[&#]|$)`).test(String(t.url || ""))); +} + function extractForeignExtId(targets) { for (const t of targets) { if (t.extensionId && t.extensionId !== chrome.runtime.id) return t.extensionId; @@ -665,14 +699,90 @@ function extractForeignExtId(targets) { return null; } -async function dismissOverlayViaEscape(tabId) { - // Esc routes through key dispatcher (target-by-focus), not by mouse coordinates, so it - // works even when a foreign chrome-extension popup is intercepting pointer events. +const FOREIGN_EXTENSION_BLOCK = /Cannot access a chrome-extension:\/\/ URL of different extension/i; + +// Another extension's frame on the page (a password manager's inline prompt, an autofill menu) +// makes Chrome refuse the debugger on that tab. Nothing is closed automatically: the user may be +// using the prompt (signing in, approving something for the agent). The error gives the agent the +// facts and lets it decide, with overlay.clear (chrome_clear_overlay) to close it when it is only +// in the way. +async function overlayBlockedError(tabId) { + const targets = foreignTargetsForTab(await findForeignExtensionTargets(), tabId); + const url = String(targets[0]?.url || "").replace(/[?#].*$/, ""); + const id = extractForeignExtId(targets) || "unknown"; + return new Error( + `Another extension (${id}${url ? `, ${url}` : ""}) has a prompt open on this tab, probably a password manager such as 1Password, and Chrome will not let the debugger work on the tab while it is there. ` + + `If it is only in your way, call chrome_clear_overlay for this tab. If you are waiting for the user to sign in or approve something with it, leave it alone and tell the user. Navigating the tab away also frees it.`, + ); +} + +// Injected into the page by overlay.clear, in the extension's isolated world: only there can +// chrome.dom.openOrClosedShadowRoot read a closed shadow root, and password managers put their +// frame in one (1Password: a <com-1password-uso> element with the frame inside; there is no iframe +// in the page's own DOM). Removes every element whose shadow root holds another extension's +// iframe, plus loose foreign iframes, then blurs the focused field and sends Escape. +function clearForeignOverlays(ownId) { + const removed = []; + const isForeignFrame = (el) => el.tagName === "IFRAME" && /^chrome-extension:\/\//.test(el.src) && !el.src.startsWith(`chrome-extension://${ownId}/`); + const rootOf = (el) => { + try { + return (typeof chrome !== "undefined" && chrome.dom && chrome.dom.openOrClosedShadowRoot ? chrome.dom.openOrClosedShadowRoot(el) : null) || el.shadowRoot; + } catch { + return el.shadowRoot; + } + }; + // Returns the foreign frame URLs found inside `root`, looking through nested shadow roots. + const framesIn = (root) => { + const found = []; + for (const el of Array.from(root.querySelectorAll("*"))) { + if (isForeignFrame(el)) found.push(el.src.replace(/[?#].*$/, "")); + const inner = rootOf(el); + if (inner) found.push(...framesIn(inner)); + } + return found; + }; + for (const el of Array.from(document.querySelectorAll("*"))) { + if (!el.isConnected) continue; + if (isForeignFrame(el)) { + removed.push(el.src.replace(/[?#].*$/, "")); + el.remove(); + continue; + } + const root = rootOf(el); + const frames = root ? framesIn(root) : []; + if (frames.length > 0) { + removed.push(`<${el.tagName.toLowerCase()}> holding ${frames.join(", ")}`); + el.remove(); + } + } + const active = document.activeElement; + if (active && active !== document.body && typeof active.blur === "function") active.blur(); + document.dispatchEvent(new KeyboardEvent("keydown", { key: "Escape", code: "Escape", bubbles: true })); + const custom = Array.from(document.querySelectorAll("*")).filter((el) => el.tagName.includes("-")).map((el) => el.tagName.toLowerCase()); + return { removed, seen: { iframes: document.querySelectorAll("iframe").length, customElements: [...new Set(custom)].slice(0, 10), active: document.activeElement?.tagName ?? null } }; +} + +async function clearOverlay(params) { + const tab = await getTabByParams(params, { createOwnedTarget: false }); + let injected = { removed: [] }; try { - await cdpRaw(tabId, "Input.dispatchKeyEvent", { type: "keyDown", key: "Escape", code: "Escape", windowsVirtualKeyCode: 27 }); - await cdpRaw(tabId, "Input.dispatchKeyEvent", { type: "keyUp", key: "Escape", code: "Escape", windowsVirtualKeyCode: 27 }); - await sleep(120); - } catch {} + const [first] = await executeScriptTimed({ target: { tabId: tab.id, frameIds: [0] }, func: clearForeignOverlays, args: [chrome.runtime.id] }, "chrome_clear_overlay"); + injected = first?.result ?? injected; + } catch (error) { + throw new Error(`Could not run the clean-up on tab ${tab.id}: ${error?.message || error}`); + } + // The prompt takes a moment to go: try the debugger for up to about a second. + let debuggerAttachable = false; + for (let attempt = 0; attempt < 6 && !debuggerAttachable; attempt++) { + await sleep(attempt === 0 ? 150 : 200); + try { + await attachDebugger(tab.id); + debuggerAttachable = true; + } catch {} + } + const allForeign = await findForeignExtensionTargets(); + const remaining = foreignTargetsForTab(allForeign, tab.id).map((t) => String(t.url || "").replace(/[?#].*$/, "")); + return { tab: { id: tab.id, url: tab.url, title: tab.title }, removedFrames: injected.removed, remainingForeignTargets: remaining, foreignTargetsAnywhere: allForeign.map((t) => ({ type: t.type, tabId: t.tabId, url: String(t.url || "").replace(/[?#].*$/, "") })).slice(0, 20), injectedReport: injected, debuggerAttachable, cleared: debuggerAttachable && remaining.length === 0 }; } async function cdp(tabId, method, params, opts) { @@ -681,28 +791,8 @@ async function cdp(tabId, method, params, opts) { } catch (error) { const msg = String(error?.message || error); const isStale = /Debugger is not attached|Detached while|Target closed|No tab with id/i.test(msg); - const isForeignExtBlock = /Cannot access a chrome-extension:\/\/ URL of different extension/i.test(msg); - if (isForeignExtBlock && /Input\./.test(method)) { - // Foreign chrome-extension popup (autofill, password manager) is hijacking input. - // Try once: dismiss via Esc, then retry. - const before = await findForeignExtensionTargets(); - recordAttachEvent({ kind: "foreign-ext-detected", tabId, method, foreignExtId: extractForeignExtId(before), targetCount: before.length }); - await dismissOverlayViaEscape(tabId); - try { - return await cdpRaw(tabId, method, params, opts); - } catch (retryErr) { - const retryMsg = String(retryErr?.message || retryErr); - if (/Cannot access a chrome-extension:\/\/ URL of different extension/i.test(retryMsg)) { - const after = await findForeignExtensionTargets(); - const id = extractForeignExtId(after) || extractForeignExtId(before) || "unknown"; - throw new Error( - `Another Chrome extension (${id}) has an input overlay on this page (e.g. a password manager / autofill popup). \n` + - `pi-chrome tried to dismiss it with Escape but it reappeared. Disable that extension on this page, close its popup, or focus the field via Tab instead of clicking.`, - ); - } - throw retryErr; - } - } + const isForeignExtBlock = FOREIGN_EXTENSION_BLOCK.test(msg); + if (isForeignExtBlock) throw await overlayBlockedError(tabId); if (!isStale) throw error; attachedTabs.delete(tabId); await attachDebugger(tabId).catch(() => undefined); @@ -1293,6 +1383,22 @@ async function chromeInputFill(params) { } } +// Runs in the page: scroll the innermost scrollable element under (x, y), else the document. +function scrollHiddenPage(x, y, dx, dy) { + const canScroll = (el) => { + const style = getComputedStyle(el); + const yOk = dy !== 0 && /(auto|scroll|overlay)/.test(style.overflowY) && el.scrollHeight > el.clientHeight; + const xOk = dx !== 0 && /(auto|scroll|overlay)/.test(style.overflowX) && el.scrollWidth > el.clientWidth; + return yOk || xOk; + }; + let el = document.elementFromPoint(x, y); + while (el && el !== document.body && el !== document.documentElement && !canScroll(el)) el = el.parentElement; + const target = el && el !== document.body && el !== document.documentElement ? el : (document.scrollingElement || document.documentElement); + const before = { x: target.scrollLeft, y: target.scrollTop }; + target.scrollBy({ left: dx, top: dy, behavior: "instant" }); + return { scrolled: target === document.scrollingElement ? "document" : (target.tagName || "").toLowerCase(), before, after: { x: target.scrollLeft, y: target.scrollTop } }; +} + async function chromeInputScroll(params) { const tab = await getTabByParams(params); await bringToFront(tab, params); @@ -1302,6 +1408,14 @@ async function chromeInputScroll(params) { const x = resolved.rect ? resolved.rect.left + Math.min(resolved.rect.width, 800) / 2 : resolved.x; const y = resolved.rect ? resolved.rect.top + Math.min(resolved.rect.height, 600) / 2 : resolved.y; const totalY = params.deltaY || 0, totalX = params.deltaX || 0; + // Chrome never answers a mouseWheel sent to a hidden page, so scroll it by script instead. + if ((await pageVisibilityState(tab.id)) === "hidden") { + const evaluated = await cdp(tab.id, "Runtime.evaluate", { + expression: `(${scrollHiddenPage.toString()})(${JSON.stringify(x)}, ${JSON.stringify(y)}, ${JSON.stringify(totalX)}, ${JSON.stringify(totalY)})`, + returnByValue: true, + }); + return { input: "script", reason: "tab is hidden (not the window's active tab), where Chrome drops scroll-wheel events", deltaX: totalX, deltaY: totalY, ...(evaluated?.result?.value || {}) }; + } // Profile mimics a trackpad flick: short ramp-up (~15% of events), then geometric decay // with a ~12% drop per event. Gives momentum tail tests something to find, and the small // tail deltas (a handful of <20px events) put IntersectionObserver thresholds in range. @@ -1402,7 +1516,35 @@ function fileChooserTabIdForSource(source) { return undefined; } +// Dialogs a page opens (alert, confirm, prompt, and the "Leave site?" prompt of a beforeunload +// handler) block the tab until someone answers, and the agent cannot see them. We answer them: +// "stay"/cancel, unless page.navigate was told leaveIfAsked and it is a beforeunload prompt. +// page.navigate waits on dialogWaiters to report a dismissed one instead of timing out. +const dialogPolicy = new Map(); // tabId -> { leave: boolean }, only while page.navigate runs +const dialogWaiters = new Map(); // tabId -> resolve(dialog), only while page.navigate runs + +function describeDialog(dialog) { + if (dialog.type === "beforeunload") return 'a "Leave site? Changes you made may not be saved" prompt (the page has a beforeunload handler)'; + return `a JavaScript ${dialog.type} dialog saying "${dialog.message}"`; +} + +async function handleDialogOpening(tabId, params) { + const leave = params?.type === "beforeunload" && dialogPolicy.get(tabId)?.leave === true; + const dialog = { type: params?.type, message: params?.message ?? "", url: params?.url, accepted: leave }; + try { + await cdp(tabId, "Page.handleJavaScriptDialog", { accept: leave }); + } catch (error) { + console.warn(`[pi-chrome] could not answer a ${dialog.type} dialog on tab ${tabId}: ${error?.message || error}`); + } + if (!leave) dialogWaiters.get(tabId)?.(dialog); +} + function handleDebuggerEvent(source, method, params) { + if (method === "Page.javascriptDialogOpening") { + const dialogTabId = fileChooserTabIdForSource(source); + if (dialogTabId !== undefined) void handleDialogOpening(dialogTabId, params); + return; + } if (method !== "Page.fileChooserOpened") return; const tabId = fileChooserTabIdForSource(source); const entry = tabId === undefined ? undefined : pendingFileChoosers.get(tabId); @@ -1524,11 +1666,12 @@ async function chromeInputUpload(params) { } // The target opens the native picker when clicked. Intercept the chooser so no OS dialog appears, -// then attach the files to the input Chrome reports. The click is trusted Chrome input when the page -// is visible; a hidden page drops trusted input, so there we call element.click() with a CDP user -// gesture instead (an untrusted event, like chrome_click's DOM fallback) unless domFallback:false. +// then attach the files to the input Chrome reports. The click is trusted Chrome input, also on an +// inactive tab; in a minimized window, where trusted input may be dropped, we call element.click() +// with a CDP user gesture instead (an untrusted event, like chrome_click's DOM fallback) unless +// domFallback:false. async function uploadViaFileChooser(tab, params, paths, objectId, info) { - const hidden = (await pageVisibilityState(tab.id)) === "hidden"; + const hidden = Boolean(await inputRefusingWindow(tab)); if (hidden && params.domFallback === false) await assertTabVisibleForInput(tab, params, "chrome.upload"); await cdp(tab.id, "Page.enable", {}); await cdp(tab.id, "Page.setInterceptFileChooserDialog", { enabled: true }); @@ -1554,7 +1697,7 @@ async function uploadViaFileChooser(tab, params, paths, objectId, info) { input: "chrome", mode: "file-chooser", trigger, - reason: trigger === "activation-click" ? "target tab is hidden; Chrome drops trusted input to hidden pages" : undefined, + reason: trigger === "activation-click" ? "target tab is hidden in a minimized window, where trusted input may be dropped" : undefined, inFrame: info.inFrame || undefined, uploaded: paths.map((path) => ({ path })), }; @@ -1753,17 +1896,25 @@ async function groupTab(tab, title, color) { if (!tab || typeof tab.id !== "number") throw new Error("No tab to group"); const groupTitle = cleanGroupTitle(title); let groupId = tab.groupId; + let createdGroup = false; if (typeof groupId !== "number" || groupId < 0) { // Some browsers omit windowId on freshly created tabs; read it back instead of guessing. const windowId = typeof tab.windowId === "number" ? tab.windowId : (await chrome.tabs.get(tab.id).catch(() => null))?.windowId; const existing = await findGroupByTitle(windowId, groupTitle); // Without createProperties.windowId Chrome creates the group in the *current* (user's) window // and moves the tab there, pulling Pi's automation tab out of its own window as a hidden tab. + createdGroup = existing === null; groupId = existing !== null ? await chrome.tabs.group({ groupId: existing, tabIds: [tab.id] }) : await chrome.tabs.group(typeof windowId === "number" ? { tabIds: [tab.id], createProperties: { windowId } } : { tabIds: [tab.id] }); } - await chrome.tabGroups.update(groupId, { title: groupTitle, color: cleanGroupColor(color), collapsed: false }); + // A new group starts collapsed to one label in the user's window, unless its tab is the one + // showing (collapsing would switch the window to another tab). Leave an existing group as the + // user set it. + const fresh = await chrome.tabs.get(tab.id).catch(() => tab); + const update = { title: groupTitle, color: cleanGroupColor(color) }; + if (createdGroup) update.collapsed = fresh?.active !== true; + await chrome.tabGroups.update(groupId, update); const grouped = await chrome.tabs.get(tab.id); return { tab: await formatTab(grouped), group: await groupRecord(groupId) }; } @@ -1795,10 +1946,21 @@ async function dispatch(action, params) { const existingGroup = await findGroupRecordByTitle(groupTitle); const createParams = { url: params.url || "about:blank", active: foregroundRequested(params) }; if (existingGroup && typeof existingGroup.windowId === "number") createParams.windowId = existingGroup.windowId; + // No group of this session yet: use the window of its automation tab if it has one, else the + // user's window. A window of its own only when the browser has no window open. + let ownWindow = false; + if (createParams.windowId === undefined) { + const home = await resolveOwnedAutomationTarget(sessionKeyOf(params)).catch(() => null); + const windowId = home && typeof home.windowId === "number" ? home.windowId : await agentWindowId(); + if (windowId !== null) createParams.windowId = windowId; + else ownWindow = typeof chrome.windows?.create === "function"; + } // Register the creation as in flight until the tab is tracked, so early capture (which only // runs in tracked tabs) waits for it instead of skipping the new tab's first page load. const creation = (async () => { - const created = await chrome.tabs.create(createParams); + const created = ownWindow + ? (await chrome.windows.create({ url: createParams.url, focused: createParams.active })).tabs[0] + : await chrome.tabs.create(createParams); await trackSessionTab(sessionKeyOf(params), created.id, true); return created; })(); @@ -1831,7 +1993,7 @@ async function dispatch(action, params) { } case "tab.activate": { if (!foregroundRequested(params)) { - throw new Error("Tab activation is blocked by background mode. Ask the user to run /chrome background off to allow foreground work."); + throw new Error("Tab activation is blocked: the call asked for background, or the user turned background mode on (/chrome background on)."); } // Management actions never auto-create an automation target (createOwnedTarget:false): with // no explicit target they act on an owned target if one exists, else error — they must never @@ -1922,17 +2084,49 @@ async function dispatch(action, params) { // Register a one-shot document_start content script. We register, navigate, wait, then unregister. await registerInitScript(tab.id, params.initScript); } - const wait = params.waitUntilLoad !== false ? waitForTabComplete(tab.id, params.timeoutMs || 15000) : Promise.resolve(undefined); - const updated = await chrome.tabs.update(tab.id, { url: params.url }); + // Attach first: the debugger is how we hear about, and answer, a "Leave site?" prompt. + // Best effort: another extension's frame on the page (a password manager's inline prompt) + // makes Chrome refuse the attach, and navigating away is often how a stuck tab is freed. + // Without the debugger we cannot answer a Leave-site prompt, but the navigation still runs. try { - await wait; + await attachDebugger(tab.id); + await cdp(tab.id, "Page.enable", {}).catch(() => undefined); + } catch (error) { + console.warn(`[pi-chrome] navigating tab ${tab.id} without the debugger: ${error?.message || error}`); + } + dialogPolicy.set(tab.id, { leave: params.leaveIfAsked === true }); + const dialogDismissed = new Promise((resolve) => dialogWaiters.set(tab.id, resolve)); + const waitsForLoad = params.waitUntilLoad !== false; + const wait = waitsForLoad ? waitForTabComplete(tab.id, params.timeoutMs || 15000) : Promise.resolve(undefined); + wait.catch(() => undefined); // if the dialog wins the race, a later load timeout is not an error + try { + const updated = await chrome.tabs.update(tab.id, { url: params.url }); + // Without waiting for the load, still give a prompt a moment to show up. + const outcome = await Promise.race([ + wait.then(() => ({ loaded: true })), + dialogDismissed.then((dialog) => ({ dialog })), + ...(waitsForLoad ? [] : [new Promise((resolve) => setTimeout(() => resolve({ loaded: true }), 700))]), + ]); + if (outcome.dialog) { + throw new Error( + `Navigation to ${params.url} did not happen: the page at ${outcome.dialog.url || tab.url} opened ${describeDialog(outcome.dialog)}, ` + + `and it was dismissed, so the tab stayed where it was. ` + + (outcome.dialog.type === "beforeunload" + ? "Nothing was lost, but the page may hold unsaved changes. If you know nothing unsaved matters here (you just saved or submitted, or this is a throwaway tab), call chrome_navigate again with leaveIfAsked: true. Otherwise ask the user." + : "Look at the page to see what it was asking."), + ); + } + return await formatTab(await chrome.tabs.get(updated.id)); } finally { + dialogPolicy.delete(tab.id); + dialogWaiters.delete(tab.id); if (params.initScript) await unregisterInitScript(tab.id).catch(() => undefined); } - return await formatTab(await chrome.tabs.get(updated.id)); } case "page.screenshot": return takeScreenshot(params); + case "overlay.clear": + return clearOverlay(params); case "cdp.targets": { // Diagnostics: CDP targets anchored to the resolved tab, including foreign overlay targets // (password managers, autofill, DevTools front-ends) that explain "Detached while handling @@ -2276,6 +2470,28 @@ async function withOptionalSnapshot(params, actionFn) { // chrome.scripting.executeScript({ files }). That file is free of eval/new Function, so it works // on strict-CSP pages, and it installs globalThis.__piChromeSnapshotPage / __piChromeInspectTarget. // It shares window.__PI_CHROME_STATE__ (same el- uid scheme) with the CDP-injected input helpers. +// Injects snapshot_injected.js and runs `scriptOptions.func` (which must return an +// { ok, value | error } envelope) in the tab's top frame. chrome.scripting is primary; if it +// resolves without an envelope, the page ran no extension script (CSP `sandbox` without +// allow-scripts, e.g. raw.githubusercontent.com), so both steps are redone over CDP. +async function runSnapshotInjected(tab, kind, scriptOptions) { + const inject = { target: { tabId: tab.id, frameIds: [0] }, world: "MAIN", files: ["snapshot_injected.js"] }; + const run = { target: { tabId: tab.id, frameIds: [0] }, world: "MAIN", ...scriptOptions }; + const injectLabel = `inject ${kind} script in tab ${tab.id}`; + const runLabel = `run ${kind} script in tab ${tab.id}`; + await executeScriptWithFallback(inject, injectLabel); + const results = await executeScriptWithFallback(run, runLabel); + const first = results?.[0]; + if (first?.error || first?.result != null || !chrome.debugger) return results; + recordAttachEvent({ kind: "scripting-no-result-cdp-fallback", tabId: tab.id, files: inject.files }); + await executeScriptViaCdp(inject, injectLabel); + const viaCdp = await executeScriptViaCdp(run, runLabel); + if (viaCdp?.[0]?.result == null) { + throw new Error(`Chrome ${kind} script returned no result in tab ${tab.id} (${tab.url || "unknown url"}); the page may block script execution.`); + } + return viaCdp; +} + async function snapshotInTab(params) { const tab = await getTabByParams(params); await bringToFront(tab, params); @@ -2288,14 +2504,7 @@ async function snapshotInTab(params) { params.query ?? null, params.maxTextChars ?? null, ]; - await executeScriptWithFallback({ - target: { tabId: tab.id, frameIds: [0] }, - world: "MAIN", - files: ["snapshot_injected.js"], - }, `inject snapshot script in tab ${tab.id}`); - const results = await executeScriptWithFallback({ - target: { tabId: tab.id, frameIds: [0] }, - world: "MAIN", + const results = await runSnapshotInjected(tab, "snapshot", { func: async (invocationArgs) => { try { const snapshotPage = globalThis.__piChromeSnapshotPage; @@ -2306,7 +2515,7 @@ async function snapshotInTab(params) { } }, args: [args], - }, `run snapshot script in tab ${tab.id}`); + }); const first = results?.[0]; if (first?.error) { const message = typeof first.error === "string" ? first.error : (first.error.message || JSON.stringify(first.error)); @@ -2324,14 +2533,7 @@ async function inspectInTab(params) { const tab = await getTabByParams(params); await bringToFront(tab, params); const args = [params.uid ?? null, params.selector ?? null, params.scrollIntoView === true]; - await executeScriptWithFallback({ - target: { tabId: tab.id, frameIds: [0] }, - world: "MAIN", - files: ["snapshot_injected.js"], - }, `inject inspect script in tab ${tab.id}`); - const results = await executeScriptWithFallback({ - target: { tabId: tab.id, frameIds: [0] }, - world: "MAIN", + const results = await runSnapshotInjected(tab, "inspect", { func: async (invocationArgs) => { try { const inspectTarget = globalThis.__piChromeInspectTarget; @@ -2342,7 +2544,7 @@ async function inspectInTab(params) { } }, args: [args], - }, `run inspect script in tab ${tab.id}`); + }); const first = results?.[0]; if (first?.error) { const message = typeof first.error === "string" ? first.error : (first.error.message || JSON.stringify(first.error)); @@ -2412,6 +2614,9 @@ function foregroundRequested(params) { async function bringToFront(tab, params) { if (!foregroundRequested(params)) return tab; await chrome.windows.update(tab.windowId, { focused: true }); + if (typeof tab.groupId === "number" && tab.groupId >= 0 && chrome.tabGroups) { + await chrome.tabGroups.update(tab.groupId, { collapsed: false }).catch(() => undefined); + } return chrome.tabs.update(tab.id, { active: true }); } |
