I'm trying to navigate the current tab to a URL in a browser_action Chrome extension, in response to a keyword that a user has entered. What's the best way to do this?
First I tried a simple form with javascript, but I realized that the javascript was not setting window.location.href on the current tab because I wasn't using executeScript.
So far the best method I've found is to use executeScript:
chrome.tabs.executeScript(null, {code:"window.location.href = '" + url + "';"});
This also involves adding permissions to the manifest:
"permissions": [
"tabs",
"http://*/",
"https://*/"
],
The problem is that:
This approach doesn't work on newtab pages. (adding "chrome://*/" breaks the extension and prevents it from being installed)
It requires that permission be explicitly set for every single protocol type, else the extension won't work on some types of pages.
Is there a more robust way of making a Chrome tab navigate the open tab to a particular page from a browser_action popup?
I'm trying to navigate the current tab to a URL in a browser_action Chrome extension, in response to a keyword that a user has entered. What's the best way to do this?
First I tried a simple form with javascript, but I realized that the javascript was not setting window.location.href on the current tab because I wasn't using executeScript.
So far the best method I've found is to use executeScript:
chrome.tabs.executeScript(null, {code:"window.location.href = '" + url + "';"});
This also involves adding permissions to the manifest:
"permissions": [
"tabs",
"http://*/",
"https://*/"
],
The problem is that:
This approach doesn't work on newtab pages. (adding "chrome://*/" breaks the extension and prevents it from being installed)
It requires that permission be explicitly set for every single protocol type, else the extension won't work on some types of pages.
Is there a more robust way of making a Chrome tab navigate the open tab to a particular page from a browser_action popup?
Share Improve this question asked Jul 24, 2013 at 13:58 JamesJames 6512 gold badges18 silver badges30 bronze badges1 Answer
Reset to default 6There's no need for content scripts or host permissions. Just use chrome.tabs.update
(the tabs
permission is not needed):
chrome.browserAction.onClicked.addListener(function(tab) {
chrome.tabs.update(tab.id, {
url: url
});
});