Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 516
wyz Avatar asked Mar 03 '11 15:03

wyz


2 Answers

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

like image 146
serg Avatar answered Nov 15 '22 21:11

serg


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.

like image 40
Dave Teare Avatar answered Nov 15 '22 21:11

Dave Teare