Search code examples
javascriptgoogle-chromechromium

Why does `chrome.tabs.create` create a tab and make it active even though active: false is being passed on mobile Chromium browsers?


I'm working on an MV3 Chromium extension. Within the extension, I need to create a new tab (doing so with the use of chrome.tabs.create) and navigate the user to a specific site. I want the new tab to open in the background so that the currently active tab doesn't change.

On the desktop Chrome browser, this functionality works fine. However, on mobile Chromium-based browsers that support extensions, such as the Kiwi browser, the tab that is supposed to open in the background steals focus and moves to the foreground.

How can I overcome this issue?

chrome.tabs.create({ url, active: false, pinned: false, selected: false });


Solution

  • You can switch back to the original tab using chrome.tabs.update:

    // Save the current tab ID for later use
    const [currentTab] = await chrome.tabs.query({ active: true, lastFocusedWindow: true })
    
    // Your actions to open a new tab
    chrome.tabs.create({ url, active: false, pinned: false, selected: false })
    ...
    
    // Switch back to the original tab
    await chrome.tabs.update(currentTab.id, {active: true})