1

I am working on a Chrome extension where we can share the current URL of tab or window, which is active. I am having a bit of trouble with obtaining the currently active URL in javascript.

tried

var current_url = window.location.host;
, var current_url = window.location.hostname;
, var current_url = window.location.href;
and var current_url = document.location.href;

here is the code for URL passing function:(background.js):

 chrome.runtime.onMessage.addListener(function(message,sender,sendResponse){
      if(message.message=='openurl'){

                chrome.tabs.query({active: true, currentWindow: true}, function(tabs){
                  var tab = tabs[0];
                  var page_url = tab.url;
                  wayback_url = message.wayback_url;
                  var pattern = /https:\/\/web\.archive\.org\/web\/(.+?)\//g;
                  url = page_url.replace(pattern, "");
                  open_url = wayback_url+encodeURI(url);

                  if(message.method!='save'){
                    status = wmAvailabilityCheck(page_url,
                                        function(){ chrome.tabs.create({ url:  open_url});
                                            sendResponse({status:true});
                                          },
                                        function(){
                                            //alert("URL not found in wayback archives!");
                                            sendResponse({status:false});
                                        });
                 }
                else{
                    chrome.tabs.create({ url:  open_url});
                  }

              });
      }
      else if(message.message=='geturl') {
            chrome.tabs.query({active: true, currentWindow: true}, function(tabs) {
              var tab = tabs[0];
              var page_url = tab.url;
              if(isValidSnapshotUrl(page_url)){
                sendResponse({url: page_url});
              }
          });
      }
       return true; 
    });

The code for popup.js:

  function get_alexa_info(){
    chrome.runtime.sendMessage({message: "geturl" }, function(response) {
     shareon_facebook(response.url);
    shareon_twitter(response.url);
    shareon_googleplus(response.url);
  }

function shareon_facebook(url)
{
    var current_url = url;
    var fbshr_url = "https://www.facebook.com/sharer/sharer.php?u="
    window.open(fbshr_url + current_url , 'newwindow', 'width=500, height=400');
}

function shareon_twitter(current_url)
{

    var twitshr_url = "https://twitter.com/home?status=";
    window.open(twitshr_url + current_url , 'newwindow', 'width=500, height=400');
}

function shareon_googleplus(url)
{

    var gplusshr_url = "https://plus.google.com/share?url="; 
    window.open(gplusshr_url + url , 'newwindow', 'width=500, height=400');
}

I cannot provide more information about this extension, due to other reasons.

  • `window.location.href` is generally the way to do it. What happens when you use that? What's the error/result? – Norsk Jun 21 '17 at 04:18
  • 4
    Make sure you've read the extension architecture overview: only content script runs alongside a web page and has direct access to its DOM and URL. Other parts of an extension need chrome.tabs.query (you can find examples yourself). Also, usually the additional URL parameters are encoded via encodeURIComponent. – wOxxOm Jun 21 '17 at 04:31
  • Please [edit] the question to be on-topic: include a [mcve] that duplicates the problem. For Chrome extensions or Firefox WebExtensions this almost always means including your *manifest.json* and some of the background, content, and/or popup scripts/HTML. Questions seeking debugging help ("why isn't this code working the way I want?") must include: (1) the desired behavior, (2) a specific problem or error and (3) the shortest code necessary to reproduce it *in the question itself*. Please also see: [What topics can I ask about here?](http://stackoverflow.com/help/on-topic), and [ask]. – Makyen Jun 21 '17 at 06:03
  • I suggest you read the [Chrome extension overview](https://developer.chrome.com/extensions/overview) (perhaps along with the pages linked from the overview). The [architecture section](https://developer.chrome.com/extensions/overview#arch) has overall architecture information which should help your understanding of how things are generally organized/done. You will probably also want to read [Content Scripts](https://developer.chrome.com/extensions/content_scripts). – Makyen Jun 21 '17 at 06:03
  • Presumably, you mean you want to get it in a background script, but you have not said so. If so: Possible duplicate of: [How can I get the URL of the current tab from a Google Chrome extension?](https://stackoverflow.com/q/1979583) – Makyen Jun 21 '17 at 06:06
  • yes @Makyen, sorry for not mentioning the background script, I need to pass the url from the background script to the popup script – Rakesh Naga Chinta Jun 22 '17 at 01:03
  • I already wrote and added the "URL passing function" in the backgrouund.js script, I even used the following code in popup to fetch it, But the problem is that I don't know where to put the code properly in the popup script – Rakesh Naga Chinta Jun 22 '17 at 01:05
  • code in background.js: – Rakesh Naga Chinta Jun 22 '17 at 01:05
  • A) If you want, you can do the `tabs.query()` API call in the popup script instead of having to message the information back and forth. B) Please include your *manifest.json* file in the question, as you may be running into a permissions issue. – Makyen Jun 22 '17 at 01:29
  • yes, but is there a way to do it in my present method, I seem to be doing a mistake. Thanks ! :) – Rakesh Naga Chinta Jun 22 '17 at 01:56
  • @RakeshNagaChinta have you given the tab permission in menifest.json file ? – Vishal Kumar Oct 02 '18 at 09:30

1 Answers1

1

If you want to get the url of the active tab try this:

chrome.tabs.query({'active': true, 'lastFocusedWindow': true}, function 
(tabs) {
    var url = tabs[0].url;
    document.getElementById("host").innerHTML = url;
});

Then you can display it in an html file like this:

<div id="host"></div>
Juan Gerardo
  • 31
  • 1
  • 8