diff options
| author | Igor Soarez <igor@soarez.org> | 2026-11-08 16:31:10 +0000 |
|---|---|---|
| committer | Igor Soarez <igor@soarez.org> | 2026-11-08 16:31:10 +0000 |
| commit | 9d4e47337b9c857b70ea9caf116adb935e18e31c (patch) | |
| tree | 7ad91a9ae5fdcea5f8cfa128e879a16a2ca6f02f /extensions/chrome-profile-bridge/browser-extension | |
| parent | 516d6f05e7f67a1ef48f1ffc28d27e136e87cd1a (diff) | |
overlay.clear: read closed shadow roots and remove the host element
Diagnostics on 1Password's inline prompt: the page holds a <com-1password-uso>
element and no iframe in its own DOM; the frame is inside a closed shadow root.
Run in the isolated world, where chrome.dom.openOrClosedShadowRoot can read it,
and remove the element that hosts a foreign extension frame. Drop the body-click
events: they were never shown to help.
Diffstat (limited to 'extensions/chrome-profile-bridge/browser-extension')
| -rw-r--r-- | extensions/chrome-profile-bridge/browser-extension/service_worker.js | 56 |
1 files changed, 35 insertions, 21 deletions
diff --git a/extensions/chrome-profile-bridge/browser-extension/service_worker.js b/extensions/chrome-profile-bridge/browser-extension/service_worker.js index de4f340..0ceabda 100644 --- a/extensions/chrome-profile-bridge/browser-extension/service_worker.js +++ b/extensions/chrome-profile-bridge/browser-extension/service_worker.js @@ -714,34 +714,48 @@ async function overlayBlockedError(tabId) { ); } -// Injected into the page by overlay.clear. Removes other extensions' iframes (including inside -// open shadow roots), blurs the focused field, sends Escape and clicks the page root. +// 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 visit = (root) => { + 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 (el.tagName === "IFRAME" && /^chrome-extension:\/\//.test(el.src) && !el.src.startsWith(`chrome-extension://${ownId}/`)) { - removed.push(el.src.replace(/[?#].*$/, "")); - el.remove(); - } else if (el.shadowRoot) { - visit(el.shadowRoot); - } + if (isForeignFrame(el)) found.push(el.src.replace(/[?#].*$/, "")); + const inner = rootOf(el); + if (inner) found.push(...framesIn(inner)); } + return found; }; - visit(document); + 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 })); - // A click outside is what closes a password manager's inline menu (measured on 1Password: this, - // not Escape or blur, is what freed the tab). Same event shape as domClickFallback, which worked, - // sent to the page root so no page control is clicked. - const root = document.documentElement; - const init = { bubbles: true, cancelable: true, view: window, clientX: 900, clientY: 500, button: 0, buttons: 1 }; - root.dispatchEvent(new PointerEvent("pointerdown", { ...init, pointerId: 1, pointerType: "mouse", isPrimary: true })); - root.dispatchEvent(new MouseEvent("mousedown", init)); - root.dispatchEvent(new PointerEvent("pointerup", { ...init, pointerId: 1, pointerType: "mouse", isPrimary: true, buttons: 0 })); - root.dispatchEvent(new MouseEvent("mouseup", { ...init, buttons: 0 })); - root.click(); 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 } }; } @@ -750,7 +764,7 @@ async function clearOverlay(params) { const tab = await getTabByParams(params, { createOwnedTarget: false }); let injected = { removed: [] }; try { - const [first] = await executeScriptTimed({ target: { tabId: tab.id, frameIds: [0] }, world: "MAIN", func: clearForeignOverlays, args: [chrome.runtime.id] }, "chrome_clear_overlay"); + 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}`); |
