javascript - Setting window.location.href of the current tab in a browser_action Chrome Extension - Stack Overflow

I'm trying to navigate the current tab to a URL in a browser_action Chrome extension, in response

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:

  1. This approach doesn't work on newtab pages. (adding "chrome://*/" breaks the extension and prevents it from being installed)

  2. 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:

  1. This approach doesn't work on newtab pages. (adding "chrome://*/" breaks the extension and prevents it from being installed)

  2. 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 badges
Add a ment  | 

1 Answer 1

Reset to default 6

There'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
    });
});

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745173594a4615073.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信