Chrome extension, focus on the page instead of omnibox after selected tab is updated?
Asked Answered
I

4

10

I created an extension called quickmarks which will open bookmark by keyword at currently selected tab. I am using omnibox to select the bookmark (chrome.omnibox.onInputEntered), and chrome.tabs.update API to open bookmark's url in current tab, by providing the url in updateProperties. However after the tab is updated, focus still remains in omnibox, which make the user- experience not as good as I desired. So is there a way to set the focus to the page, instead of the omnibox.

Btw, I have tried to open a new tab by using chrome.tabs.create. The page will be focused instead of omnibox, which is my desired behaviour.

Thanks.

Ingleside answered 3/3, 2011 at 15:21 Comment(0)
B
8

Using chrome.tabs.update(tab.id, {url: url, selected: true}); does the trick for me.

Bulgar answered 3/3, 2011 at 17:7 Comment(0)
B
3

The accepted answer no longer works in Chrome 31. I had hoped it was a simple matter of the selected property being deprecated but the replacement highlighted property did not assign focus to the tab's content either.

I was only able to steal focus from the Omnibox by closing the current tab and then creating a new tab in its place. Here's the code that works in Chrome 31:

chrome.tabs.getSelected(null, function(tab) {
    chrome.tabs.remove(tab.id, function() {
        chrome.tabs.create({url:url, active:true});
    });
});

While this is certainly not ideal, the current tab is closed and a new one opened so fast you barely notice any difference.

Borrego answered 5/1, 2014 at 23:52 Comment(2)
I'm currently using this method as well but I'd suggest duplicating the tab (you can do this after removal) instead of just removing it so that you can preserve its history.Kierkegaardian
Updated tweak to this answer that works in 2024: https://mcmap.net/q/1087408/-chrome-extension-focus-on-the-page-instead-of-omnibox-after-selected-tab-is-updatedHomegrown
K
0

Dave Teare is right that this no longer works in current version of Chrome, however his method did not work for me. It seemed like the chrome.tabs.create did not get called after the tab was removed.

I use the Chrome extension iChrome and I wanted it to be selected when I created a new tab, so I installed another extension that redirects new tabs to iChrome. It unfortunately used the same deprecated "selected:true" method.

From what I can tell there is currently no way to do this cleanly, you cannot have Chrome make the updated tab's input focused nor can you have it select the text in onmibar so you cant just start searching after creating a new tab. So this is what I came up with:

chrome.tabs.create({url:url, active:true}, function(){
    chrome.tabs.remove(tab.id);
});

Definitely still not ideal, and you can see a flash when it closes old tab, but it works. The input is focused.

Keil answered 24/4, 2014 at 16:21 Comment(0)
H
0

Dave Teare's method didn't work for me, chrome.tabs.getSelected was deprecated in favor of chrome.tabs.query. I've tweaked his method to:

chrome.tabs
    .query({
        active: true,
        windowId: chrome.windows.WINDOW_ID_CURRENT,
    })
    .then((tabs) => {
        const tab = tabs[0];

        chrome.tabs.remove(tab.id, function () {
            chrome.tabs.create({
                url: url,
                active: true,
            });
        });
    });
Homegrown answered 6/9 at 8:35 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.