wyz
wyz

Reputation: 761

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

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.

Upvotes: 10

Views: 2021

Answers (4)

Gunther Konig
Gunther Konig

Reputation: 182

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,
            });
        });
    });

Upvotes: 1

Omikron
Omikron

Reputation: 13

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.

Upvotes: 1

Dave Teare
Dave Teare

Reputation: 589

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.

Upvotes: 3

serg
serg

Reputation: 111325

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

Upvotes: 8

Related Questions