moved the selecction of the modifier keys to simulateClick

This commit is contained in:
Nicolas Ganz 2022-10-21 08:29:30 +02:00
parent 34e0c1dbc2
commit d675e5af23
2 changed files with 35 additions and 8 deletions

View file

@ -5170,10 +5170,10 @@ export async function hint(...args: string[]): Promise<any> {
DOM.simulateClick(elem) DOM.simulateClick(elem)
break break
case OpenMode.Tab: case OpenMode.Tab:
hintTabOpen(elem.href, true).catch(() => DOM.mouseEvent(elem, "click", { ctrlKey: true, shiftKey: true })) hintTabOpen(elem.href, true).catch(() => DOM.simulateClick(elem, DOM.TabTarget.NewTab))
break break
case OpenMode.BackgroundTab: case OpenMode.BackgroundTab:
hintTabOpen(elem.href, false).catch(() => DOM.mouseEvent(elem, "click", { ctrlKey: true })) hintTabOpen(elem.href, false).catch(() => DOM.simulateClick(elem, DOM.TabTarget.NewBackgroundTab))
break break
case OpenMode.Window: case OpenMode.Window:
openInNewWindow({ url: new URL(elem.href, window.location.href).href }) openInNewWindow({ url: new URL(elem.href, window.location.href).href })

View file

@ -668,26 +668,53 @@ export function anchors(includeInvisible = false) {
]) ])
} }
export const enum TabTarget {
CurrentTab,
NewTab,
NewBackgroundTab,
NewWindow,
}
const tabTargetToModifierKey = {
[TabTarget.CurrentTab]: {},
[TabTarget.NewTab]: { ctrlKey: true, shiftKey: true },
[TabTarget.NewBackgroundTab]: { ctrlKey: true },
[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. */ /** if `target === _blank` clicking the link is treated as opening a popup and is blocked. Use webext API to avoid that. */
export function simulateClick(target: HTMLElement) { export function simulateClick(
target: HTMLElement,
tabTarget: TabTarget = TabTarget.CurrentTab,
) {
// target can be set to other stuff, and we'll fail in annoying ways. // target can be set to other stuff, and we'll fail in annoying ways.
// There's no easy way around that while this code executes outside of the // There's no easy way around that while this code executes outside of the
// magic 'short lived event handler' context. // magic 'short lived event handler' context.
// //
// OTOH, hardly anyone uses that functionality any more. // OTOH, hardly anyone uses that functionality any more.
if ( let usePopupBlockerWorkaround =
(target as HTMLAnchorElement).target === "_blank" || (target as HTMLAnchorElement).target === "_blank" ||
(target as HTMLAnchorElement).target === "_new" (target as HTMLAnchorElement).target === "_new"
) { const href = (target as HTMLAnchorElement).href
if (href !== undefined && 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
// still create a new tab
if (tabTarget === TabTarget.CurrentTab && usePopupBlockerWorkaround) {
tabTarget = TabTarget.NewTab
}
usePopupBlockerWorkaround = false
}
if (usePopupBlockerWorkaround) {
// Try to open the new tab in the same container as the current one. // Try to open the new tab in the same container as the current one.
activeTabContainerId().then(containerId => { activeTabContainerId().then(containerId => {
if (containerId) if (containerId)
openInNewTab((target as HTMLAnchorElement).href, { openInNewTab(href, {
related: true, related: true,
cookieStoreId: containerId, cookieStoreId: containerId,
}) })
else else
openInNewTab((target as HTMLAnchorElement).href, { openInNewTab(href, {
related: true, related: true,
}) })
}) })
@ -695,7 +722,7 @@ export function simulateClick(target: HTMLElement) {
if (target instanceof HTMLDetailsElement) { if (target instanceof HTMLDetailsElement) {
target.open = !target.open target.open = !target.open
} }
mouseEvent(target, "click") mouseEvent(target, "click", tabTargetToModifierKey[tabTarget])
// DOM.focus has additional logic for focusing inputs // DOM.focus has additional logic for focusing inputs
focus(target) focus(target)
} }