Trying to redirect Chrome to new URL using Chrome extension











up vote
0
down vote

favorite












I am just starting out writing chrome extensions.
y first project: For certain sites, if you click on the extension, the extension will redirect Chrome to the current domainName/archive.



I am using chrome.tabs.update(); to redirect.

If I try to redirect to a hard-coded URL such asYahoo.com, it works correctly.



However, if I try to redirect so what I really want, such as developer.google.com/archive (which I know does not exist), the resulting URL Chrome tries to fetch is:



chrome-extension://injepfpghgbmjnecbgiokedggknlmige/developer.chrome.com/archive


Chrome is prepending the extension's ID to the URL for some reason?



In my background.js, I have the following:



 chrome.runtime.onMessage.addListener(function(request, sender) {
chrome.tabs.update( {url: request.redirect});


I got the code above from this article.



Someone else had the same issue here (but not answered)



The full options.js is:



function createButton () {
chrome.tabs.getSelected(null, function(tab) {
var url = new URL(tab.url);
var domain = url.hostname;
archiveURL = domain + "/archive";

let page = document.getElementById('buttonDiv');
let button = document.createElement('button');

button.addEventListener('click', function() {
chrome.runtime.sendMessage({redirect: archiveURL}); // works if I send "Yahoo.com"
});
button.textContent = "> " + archiveURL; // So I know I am sending the right URL
page.appendChild(button);

})
}


createButton();


SUMMARY:
If I set:



 archiveURL = "http://sundxwn.tumblr.com/archive";


the system correctly goes to http://sundxwn.tumblr.com/archive.



But... if I set



archiveURL = domain + "/archive";


and confirm that the button text shows: http://sundxwn.tumblr.com/archive,
Chrome tries to go to: chrome-extension://injepfpghgbmjnecbgiokedggknlmige/sundxwn.tumblr.com/archive



Any help would be truly appreciated.










share|improve this question






















  • Should be var domain = url.origin;
    – wOxxOm
    Nov 12 at 4:34










  • WOW. THANK YOU !!!! Can you please explain? url.hostname seemed to give the right answer when I put it as text in the button... what is the difference? Hard to find this online...
    – Ed Landau
    Nov 13 at 2:46















up vote
0
down vote

favorite












I am just starting out writing chrome extensions.
y first project: For certain sites, if you click on the extension, the extension will redirect Chrome to the current domainName/archive.



I am using chrome.tabs.update(); to redirect.

If I try to redirect to a hard-coded URL such asYahoo.com, it works correctly.



However, if I try to redirect so what I really want, such as developer.google.com/archive (which I know does not exist), the resulting URL Chrome tries to fetch is:



chrome-extension://injepfpghgbmjnecbgiokedggknlmige/developer.chrome.com/archive


Chrome is prepending the extension's ID to the URL for some reason?



In my background.js, I have the following:



 chrome.runtime.onMessage.addListener(function(request, sender) {
chrome.tabs.update( {url: request.redirect});


I got the code above from this article.



Someone else had the same issue here (but not answered)



The full options.js is:



function createButton () {
chrome.tabs.getSelected(null, function(tab) {
var url = new URL(tab.url);
var domain = url.hostname;
archiveURL = domain + "/archive";

let page = document.getElementById('buttonDiv');
let button = document.createElement('button');

button.addEventListener('click', function() {
chrome.runtime.sendMessage({redirect: archiveURL}); // works if I send "Yahoo.com"
});
button.textContent = "> " + archiveURL; // So I know I am sending the right URL
page.appendChild(button);

})
}


createButton();


SUMMARY:
If I set:



 archiveURL = "http://sundxwn.tumblr.com/archive";


the system correctly goes to http://sundxwn.tumblr.com/archive.



But... if I set



archiveURL = domain + "/archive";


and confirm that the button text shows: http://sundxwn.tumblr.com/archive,
Chrome tries to go to: chrome-extension://injepfpghgbmjnecbgiokedggknlmige/sundxwn.tumblr.com/archive



Any help would be truly appreciated.










share|improve this question






















  • Should be var domain = url.origin;
    – wOxxOm
    Nov 12 at 4:34










  • WOW. THANK YOU !!!! Can you please explain? url.hostname seemed to give the right answer when I put it as text in the button... what is the difference? Hard to find this online...
    – Ed Landau
    Nov 13 at 2:46













up vote
0
down vote

favorite









up vote
0
down vote

favorite











I am just starting out writing chrome extensions.
y first project: For certain sites, if you click on the extension, the extension will redirect Chrome to the current domainName/archive.



I am using chrome.tabs.update(); to redirect.

If I try to redirect to a hard-coded URL such asYahoo.com, it works correctly.



However, if I try to redirect so what I really want, such as developer.google.com/archive (which I know does not exist), the resulting URL Chrome tries to fetch is:



chrome-extension://injepfpghgbmjnecbgiokedggknlmige/developer.chrome.com/archive


Chrome is prepending the extension's ID to the URL for some reason?



In my background.js, I have the following:



 chrome.runtime.onMessage.addListener(function(request, sender) {
chrome.tabs.update( {url: request.redirect});


I got the code above from this article.



Someone else had the same issue here (but not answered)



The full options.js is:



function createButton () {
chrome.tabs.getSelected(null, function(tab) {
var url = new URL(tab.url);
var domain = url.hostname;
archiveURL = domain + "/archive";

let page = document.getElementById('buttonDiv');
let button = document.createElement('button');

button.addEventListener('click', function() {
chrome.runtime.sendMessage({redirect: archiveURL}); // works if I send "Yahoo.com"
});
button.textContent = "> " + archiveURL; // So I know I am sending the right URL
page.appendChild(button);

})
}


createButton();


SUMMARY:
If I set:



 archiveURL = "http://sundxwn.tumblr.com/archive";


the system correctly goes to http://sundxwn.tumblr.com/archive.



But... if I set



archiveURL = domain + "/archive";


and confirm that the button text shows: http://sundxwn.tumblr.com/archive,
Chrome tries to go to: chrome-extension://injepfpghgbmjnecbgiokedggknlmige/sundxwn.tumblr.com/archive



Any help would be truly appreciated.










share|improve this question













I am just starting out writing chrome extensions.
y first project: For certain sites, if you click on the extension, the extension will redirect Chrome to the current domainName/archive.



I am using chrome.tabs.update(); to redirect.

If I try to redirect to a hard-coded URL such asYahoo.com, it works correctly.



However, if I try to redirect so what I really want, such as developer.google.com/archive (which I know does not exist), the resulting URL Chrome tries to fetch is:



chrome-extension://injepfpghgbmjnecbgiokedggknlmige/developer.chrome.com/archive


Chrome is prepending the extension's ID to the URL for some reason?



In my background.js, I have the following:



 chrome.runtime.onMessage.addListener(function(request, sender) {
chrome.tabs.update( {url: request.redirect});


I got the code above from this article.



Someone else had the same issue here (but not answered)



The full options.js is:



function createButton () {
chrome.tabs.getSelected(null, function(tab) {
var url = new URL(tab.url);
var domain = url.hostname;
archiveURL = domain + "/archive";

let page = document.getElementById('buttonDiv');
let button = document.createElement('button');

button.addEventListener('click', function() {
chrome.runtime.sendMessage({redirect: archiveURL}); // works if I send "Yahoo.com"
});
button.textContent = "> " + archiveURL; // So I know I am sending the right URL
page.appendChild(button);

})
}


createButton();


SUMMARY:
If I set:



 archiveURL = "http://sundxwn.tumblr.com/archive";


the system correctly goes to http://sundxwn.tumblr.com/archive.



But... if I set



archiveURL = domain + "/archive";


and confirm that the button text shows: http://sundxwn.tumblr.com/archive,
Chrome tries to go to: chrome-extension://injepfpghgbmjnecbgiokedggknlmige/sundxwn.tumblr.com/archive



Any help would be truly appreciated.







javascript google-chrome-extension






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 11 at 20:38









Ed Landau

4672816




4672816












  • Should be var domain = url.origin;
    – wOxxOm
    Nov 12 at 4:34










  • WOW. THANK YOU !!!! Can you please explain? url.hostname seemed to give the right answer when I put it as text in the button... what is the difference? Hard to find this online...
    – Ed Landau
    Nov 13 at 2:46


















  • Should be var domain = url.origin;
    – wOxxOm
    Nov 12 at 4:34










  • WOW. THANK YOU !!!! Can you please explain? url.hostname seemed to give the right answer when I put it as text in the button... what is the difference? Hard to find this online...
    – Ed Landau
    Nov 13 at 2:46
















Should be var domain = url.origin;
– wOxxOm
Nov 12 at 4:34




Should be var domain = url.origin;
– wOxxOm
Nov 12 at 4:34












WOW. THANK YOU !!!! Can you please explain? url.hostname seemed to give the right answer when I put it as text in the button... what is the difference? Hard to find this online...
– Ed Landau
Nov 13 at 2:46




WOW. THANK YOU !!!! Can you please explain? url.hostname seemed to give the right answer when I put it as text in the button... what is the difference? Hard to find this online...
– Ed Landau
Nov 13 at 2:46












1 Answer
1






active

oldest

votes

















up vote
0
down vote













url.hostname is just that, the hostname. For the original URL https://example.com/some/path, it would be example.com.



Then, your constructed URL is example.com/archive. It does not start with a protocol (e.g. http://) or a slash, so it's interpreted as a relative URL to the current page's URL.



And you are calling this from some extension page, e.g. page.html or the auto-generated background page. Its URL is, for example:



chrome-extension://injepfpghgbmjnecbgiokedggknlmige/page.html



So, to construct an absolute URL from a relative URL, Chrome chops that path off at the last slash and adds your relative part.



You need to specify the protocol so that Chrome understands it's a URL that has nothing to do with the current document. And url.origin does that, it would be https://example.com, which then is properly interpreted.






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',
    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%2f53253005%2ftrying-to-redirect-chrome-to-new-url-using-chrome-extension%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes








    up vote
    0
    down vote













    url.hostname is just that, the hostname. For the original URL https://example.com/some/path, it would be example.com.



    Then, your constructed URL is example.com/archive. It does not start with a protocol (e.g. http://) or a slash, so it's interpreted as a relative URL to the current page's URL.



    And you are calling this from some extension page, e.g. page.html or the auto-generated background page. Its URL is, for example:



    chrome-extension://injepfpghgbmjnecbgiokedggknlmige/page.html



    So, to construct an absolute URL from a relative URL, Chrome chops that path off at the last slash and adds your relative part.



    You need to specify the protocol so that Chrome understands it's a URL that has nothing to do with the current document. And url.origin does that, it would be https://example.com, which then is properly interpreted.






    share|improve this answer

























      up vote
      0
      down vote













      url.hostname is just that, the hostname. For the original URL https://example.com/some/path, it would be example.com.



      Then, your constructed URL is example.com/archive. It does not start with a protocol (e.g. http://) or a slash, so it's interpreted as a relative URL to the current page's URL.



      And you are calling this from some extension page, e.g. page.html or the auto-generated background page. Its URL is, for example:



      chrome-extension://injepfpghgbmjnecbgiokedggknlmige/page.html



      So, to construct an absolute URL from a relative URL, Chrome chops that path off at the last slash and adds your relative part.



      You need to specify the protocol so that Chrome understands it's a URL that has nothing to do with the current document. And url.origin does that, it would be https://example.com, which then is properly interpreted.






      share|improve this answer























        up vote
        0
        down vote










        up vote
        0
        down vote









        url.hostname is just that, the hostname. For the original URL https://example.com/some/path, it would be example.com.



        Then, your constructed URL is example.com/archive. It does not start with a protocol (e.g. http://) or a slash, so it's interpreted as a relative URL to the current page's URL.



        And you are calling this from some extension page, e.g. page.html or the auto-generated background page. Its URL is, for example:



        chrome-extension://injepfpghgbmjnecbgiokedggknlmige/page.html



        So, to construct an absolute URL from a relative URL, Chrome chops that path off at the last slash and adds your relative part.



        You need to specify the protocol so that Chrome understands it's a URL that has nothing to do with the current document. And url.origin does that, it would be https://example.com, which then is properly interpreted.






        share|improve this answer












        url.hostname is just that, the hostname. For the original URL https://example.com/some/path, it would be example.com.



        Then, your constructed URL is example.com/archive. It does not start with a protocol (e.g. http://) or a slash, so it's interpreted as a relative URL to the current page's URL.



        And you are calling this from some extension page, e.g. page.html or the auto-generated background page. Its URL is, for example:



        chrome-extension://injepfpghgbmjnecbgiokedggknlmige/page.html



        So, to construct an absolute URL from a relative URL, Chrome chops that path off at the last slash and adds your relative part.



        You need to specify the protocol so that Chrome understands it's a URL that has nothing to do with the current document. And url.origin does that, it would be https://example.com, which then is properly interpreted.







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 14 at 16:34









        Xan

        53k10101128




        53k10101128






























            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.





            Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


            Please pay close attention to the following guidance:


            • 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%2f53253005%2ftrying-to-redirect-chrome-to-new-url-using-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

            List item for chat from Array inside array React Native

            Thiostrepton

            Caerphilly