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

javascript - How to redirect current tab's url in Chrome extension? - Stack Overflow

programmeradmin2浏览0评论

I'm writing a Chrome extension, used to switch between different search engines. The manifest.json looks like:

{
    "browser_action": {
        "default_popup": "popup.html"
        },
    "permissions": [
        "tabs",
        "activeTab"
        ],
    "content_security_policy": "script-src 'self'; object-src 'self'",
    "content_scripts": [{
        "matches": ["*://*.baidu/*"],
        "js": ["content.js"]
        }]
}
  • content.js is used to get user's keywords in searching.
  • popup.html contains a list of supported search engines.

When user clicks on search engine's icon in popup window, I need to redirect current tab to a new url using the search engine user selected.

Now I can get keyword. Problem is: How to redirect current tab to new url?

I'm writing a Chrome extension, used to switch between different search engines. The manifest.json looks like:

{
    "browser_action": {
        "default_popup": "popup.html"
        },
    "permissions": [
        "tabs",
        "activeTab"
        ],
    "content_security_policy": "script-src 'self'; object-src 'self'",
    "content_scripts": [{
        "matches": ["*://*.baidu.com/*"],
        "js": ["content.js"]
        }]
}
  • content.js is used to get user's keywords in searching.
  • popup.html contains a list of supported search engines.

When user clicks on search engine's icon in popup window, I need to redirect current tab to a new url using the search engine user selected.

Now I can get keyword. Problem is: How to redirect current tab to new url?

Share Improve this question edited May 10, 2016 at 9:44 zhm asked May 10, 2016 at 9:35 zhmzhm 3,6414 gold badges37 silver badges56 bronze badges 2
  • Which part presents a problem? Have you already done the keyword detection and click handlers in the popup? – Xan Commented May 10, 2016 at 9:37
  • Yes, I can get keyword now. But I don't know how to redirect to new url. Do you know how to do this? – zhm Commented May 10, 2016 at 9:43
Add a comment  | 

2 Answers 2

Reset to default 17

See chrome.tabs.update, you could update the url of current tab via the following code

chrome.tabs.update({url: "http://www.baidu.com"});

Considering you get the keyword from content script, and want to redirect the url after user clicks some button in popup page, you may need Message Passing or chrome.storage to share the keywords between content script and popup page.

There are 2 possible approaches:

A. Let the popup handle redirection.
B. Let the content script handle redirection.

In both cases, you need to solve 2 problems:

  1. Communicate information between the two. In approach A, you need to get the keywords from the content script. In approach B, you need to tell the content script which engine to switch to.

    Both are solved using Messaging. I recommend messaging from the popup to the content script using chrome.tabs.sendMessage (and responding in approach A), because in the other direction content script doesn't know when to send the message (popup may be closed).

  2. Actually trigger the change. In approach A, chrome.tabs.update does the trick. In approach B, content script can change window.location to navigate away.

发布评论

评论列表(0)

  1. 暂无评论