From be9016fec2f3de127c566855fe4b978ec79354d3 Mon Sep 17 00:00:00 2001 From: Oliver Blanthorn Date: Wed, 22 Jul 2026 15:05:16 +0200 Subject: [PATCH] Fix #4401: fall back to click handler --- src/lib/dom.ts | 27 +++++++++++++++++++++++---- 1 file changed, 23 insertions(+), 4 deletions(-) diff --git a/src/lib/dom.ts b/src/lib/dom.ts index 467623bb..0284d2e3 100644 --- a/src/lib/dom.ts +++ b/src/lib/dom.ts @@ -117,6 +117,7 @@ export function mouseEvent( const event = new MouseEvent(type, { bubbles: true, cancelable: true, + composed: type === "click", view: window, detail: 1, // usually the click count ...modifierKeys, @@ -940,7 +941,7 @@ const tabTargetToModifierKey = { [TabTarget.NewWindow]: { shiftKey: true }, } -/** if `target === _blank` clicking the link is treated as opening a popup and is blocked. Use webext API to avoid that. */ +/** Run page handlers before using the webext API for popup-blocked `_blank`/`_new` links. */ export function simulateClick( target: HTMLElement, tabTarget: TabTarget = TabTarget.CurrentTab, @@ -953,9 +954,11 @@ export function simulateClick( let usePopupBlockerWorkaround = (target as HTMLAnchorElement).target === "_blank" || (target as HTMLAnchorElement).target === "_new" - const href = (target instanceof SVGAElement) - ? target.href.animVal - : (target as HTMLAnchorElement).href; + const getHref = () => + target instanceof SVGAElement + ? target.href.animVal + : (target as HTMLAnchorElement).href + let href = getHref() if (href?.startsWith("file:")) { // file URLS cannot be opend with browser.tabs.create // see https://developer.mozilla.org/en-US/docs/Mozilla/Add-ons/WebExtensions/API/tabs/create#url @@ -966,6 +969,22 @@ export function simulateClick( usePopupBlockerWorkaround = false } if (usePopupBlockerWorkaround) { + let pagePreventedDefault: boolean + const interceptClick = (event: MouseEvent) => { + if (!event.composedPath().includes(target)) return + pagePreventedDefault = event.defaultPrevented + href = getHref() + usePopupBlockerWorkaround = + !href?.startsWith("file:") && + ((target as HTMLAnchorElement).target === "_blank" || + (target as HTMLAnchorElement).target === "_new") + if (usePopupBlockerWorkaround) event.preventDefault() + } + const eventWindow = target.ownerDocument.defaultView + eventWindow?.addEventListener("click", interceptClick) + mouseEvent(target, "click", tabTargetToModifierKey[tabTarget]) + eventWindow?.removeEventListener("click", interceptClick) + if (pagePreventedDefault !== false || !usePopupBlockerWorkaround) return // Try to open the new tab in the same container as the current one. activeTabContainerId().then(containerId => { if (containerId)