最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

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

programmeradmin1浏览0评论

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
    });
});
发布评论

评论列表(0)

  1. 暂无评论