Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

"open in background tab" behaves different from "open link in new tab" from the context menu #379

Open
Aran-Fey opened this issue Mar 22, 2023 · 1 comment

Comments

@Aran-Fey
Copy link

When opening multiple tabs in a row, there's a difference in behavior between the "open link in new tab" tab from Firefox's context menu and FoxyGestures' "Open Link in New Background Tab".

Imagine a website with 2 links. I open each link in a new tab with right click -> open link in new tab, creating the 2 tabs New Tab 1, and New Tab 2. I activate New Tab 1. If I now close this tab, Firefox automatically activates New Tab 2. However, if the tabs had been opened by FoxyGestures' "Open Link in New Background Tab" gesture, closing New Tab 1 would put me back on the original tab.

Is it possible to emulate Firefox's behavior, where it cycles through the new tabs instead of putting you back where you came from?

@Aran-Fey
Copy link
Author

I made this userscript as a workaround:

const url = data.element && data.element.linkHref;
if (url){
    executeInBackground(async (url) => {
        // If this is the first time this script is executed, initialize some stuff
        if (window._lastOpenedTabId === undefined){
            window._lastOpenedTabId = new Map();
            window._successorTabId = new Map();
            
            // When a tab is closed, activate the next one
            browser.tabs.onRemoved.addListener(function(tabId){
                if (!window._successorTabId.has(tabId)){
                    return;
                }
                
                const successorTabId = window._successorTabId.get(tabId);
                browser.tabs.update(successorTabId, {active: true});
            });
        }
        
        const openerTab = (await browser.tabs.query({active: true}))[0];
        const lastOpenedTabId = window._lastOpenedTabId.get(openerTab.id);
        
        let newTabIndex = openerTab.index + 1;
        if (lastOpenedTabId !== undefined){
            try {
                const lastOpenedTab = await browser.tabs.get(lastOpenedTabId);
                newTabIndex = lastOpenedTab.index + 1;
            } catch (error){
                // That tab has been closed
            }
        }
        
        const newTab = await browser.tabs.create({
            url: url,
            active: false,
            index: newTabIndex,
            openerTabId: openerTab.id,
        });
        
        window._lastOpenedTabId.set(openerTab.id, newTab.id);
        if (lastOpenedTabId !== undefined){
            window._successorTabId.set(lastOpenedTabId, newTab.id);
        }
    }, [url]);
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant