How to redirect current tab's url in Chrome extension?










1















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
























  • Which part presents a problem? Have you already done the keyword detection and click handlers in the popup?

    – Xan
    May 10 '16 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
    May 10 '16 at 9:43















1















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
























  • Which part presents a problem? Have you already done the keyword detection and click handlers in the popup?

    – Xan
    May 10 '16 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
    May 10 '16 at 9:43













1












1








1








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
















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?







javascript google-chrome google-chrome-extension






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited May 10 '16 at 9:44







zhm

















asked May 10 '16 at 9:35









zhmzhm

2,17631728




2,17631728












  • Which part presents a problem? Have you already done the keyword detection and click handlers in the popup?

    – Xan
    May 10 '16 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
    May 10 '16 at 9:43

















  • Which part presents a problem? Have you already done the keyword detection and click handlers in the popup?

    – Xan
    May 10 '16 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
    May 10 '16 at 9:43
















Which part presents a problem? Have you already done the keyword detection and click handlers in the popup?

– Xan
May 10 '16 at 9:37





Which part presents a problem? Have you already done the keyword detection and click handlers in the popup?

– Xan
May 10 '16 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
May 10 '16 at 9:43





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
May 10 '16 at 9:43












2 Answers
2






active

oldest

votes


















4














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.






share|improve this answer























  • That works great! :-D Though I have a confusion. Why chrome.tabs.update only redirect current tab, not all tabs?

    – zhm
    May 10 '16 at 9:57











  • It usually takes a first argument, tabId, but when it's not provided it defaults to currently active tab.

    – Xan
    May 10 '16 at 9:59











  • Actually, same with tabs.sendMessage: it defaults to active tab.

    – Xan
    May 10 '16 at 10:01











  • That explains a lot. Thanks!

    – zhm
    May 10 '16 at 10:09


















3














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.






share|improve this answer
























    Your Answer






    StackExchange.ifUsing("editor", function ()
    StackExchange.using("externalEditor", function ()
    StackExchange.using("snippets", function ()
    StackExchange.snippets.init();
    );
    );
    , "code-snippets");

    StackExchange.ready(function()
    var channelOptions =
    tags: "".split(" "),
    id: "1"
    ;
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function()
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled)
    StackExchange.using("snippets", function()
    createEditor();
    );

    else
    createEditor();

    );

    function createEditor()
    StackExchange.prepareEditor(
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader:
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    ,
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    );



    );













    draft saved

    draft discarded


















    StackExchange.ready(
    function ()
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f37134567%2fhow-to-redirect-current-tabs-url-in-chrome-extension%23new-answer', 'question_page');

    );

    Post as a guest















    Required, but never shown

























    2 Answers
    2






    active

    oldest

    votes








    2 Answers
    2






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    4














    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.






    share|improve this answer























    • That works great! :-D Though I have a confusion. Why chrome.tabs.update only redirect current tab, not all tabs?

      – zhm
      May 10 '16 at 9:57











    • It usually takes a first argument, tabId, but when it's not provided it defaults to currently active tab.

      – Xan
      May 10 '16 at 9:59











    • Actually, same with tabs.sendMessage: it defaults to active tab.

      – Xan
      May 10 '16 at 10:01











    • That explains a lot. Thanks!

      – zhm
      May 10 '16 at 10:09















    4














    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.






    share|improve this answer























    • That works great! :-D Though I have a confusion. Why chrome.tabs.update only redirect current tab, not all tabs?

      – zhm
      May 10 '16 at 9:57











    • It usually takes a first argument, tabId, but when it's not provided it defaults to currently active tab.

      – Xan
      May 10 '16 at 9:59











    • Actually, same with tabs.sendMessage: it defaults to active tab.

      – Xan
      May 10 '16 at 10:01











    • That explains a lot. Thanks!

      – zhm
      May 10 '16 at 10:09













    4












    4








    4







    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.






    share|improve this answer













    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.







    share|improve this answer












    share|improve this answer



    share|improve this answer










    answered May 10 '16 at 9:50









    XanXan

    53.7k10104131




    53.7k10104131












    • That works great! :-D Though I have a confusion. Why chrome.tabs.update only redirect current tab, not all tabs?

      – zhm
      May 10 '16 at 9:57











    • It usually takes a first argument, tabId, but when it's not provided it defaults to currently active tab.

      – Xan
      May 10 '16 at 9:59











    • Actually, same with tabs.sendMessage: it defaults to active tab.

      – Xan
      May 10 '16 at 10:01











    • That explains a lot. Thanks!

      – zhm
      May 10 '16 at 10:09

















    • That works great! :-D Though I have a confusion. Why chrome.tabs.update only redirect current tab, not all tabs?

      – zhm
      May 10 '16 at 9:57











    • It usually takes a first argument, tabId, but when it's not provided it defaults to currently active tab.

      – Xan
      May 10 '16 at 9:59











    • Actually, same with tabs.sendMessage: it defaults to active tab.

      – Xan
      May 10 '16 at 10:01











    • That explains a lot. Thanks!

      – zhm
      May 10 '16 at 10:09
















    That works great! :-D Though I have a confusion. Why chrome.tabs.update only redirect current tab, not all tabs?

    – zhm
    May 10 '16 at 9:57





    That works great! :-D Though I have a confusion. Why chrome.tabs.update only redirect current tab, not all tabs?

    – zhm
    May 10 '16 at 9:57













    It usually takes a first argument, tabId, but when it's not provided it defaults to currently active tab.

    – Xan
    May 10 '16 at 9:59





    It usually takes a first argument, tabId, but when it's not provided it defaults to currently active tab.

    – Xan
    May 10 '16 at 9:59













    Actually, same with tabs.sendMessage: it defaults to active tab.

    – Xan
    May 10 '16 at 10:01





    Actually, same with tabs.sendMessage: it defaults to active tab.

    – Xan
    May 10 '16 at 10:01













    That explains a lot. Thanks!

    – zhm
    May 10 '16 at 10:09





    That explains a lot. Thanks!

    – zhm
    May 10 '16 at 10:09













    3














    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.






    share|improve this answer





























      3














      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.






      share|improve this answer



























        3












        3








        3







        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.






        share|improve this answer















        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.







        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited Nov 12 '18 at 17:09









        Sasikanth

        2,62011242




        2,62011242










        answered May 10 '16 at 9:48









        Haibara AiHaibara Ai

        7,77521539




        7,77521539



























            draft saved

            draft discarded
















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid


            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.

            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f37134567%2fhow-to-redirect-current-tabs-url-in-chrome-extension%23new-answer', 'question_page');

            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            Kleinkühnau

            Makov (Slowakei)

            Deutsches Schauspielhaus