Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Display current URL in a chrome extension

After doing some research, the code that I have come up with is this:

var outUrl;
// first get the windowid
chrome.windows.getCurrent(function(window) {
    // then get the current active tab in that window
    chrome.tabs.query({
        active: true,
        windowId: window.id
    }, function (tabs) {
        var tab = tabs[0];
        document.write(tab.url)
    });
});

This is in a javascript file which is called from my popup html file. It does not, however display the URL of the current website, instead it displays nothing.

I have found multiple posts about this on this and other websites but I haven't been able to implement any of the supposed solutions.

Any help would be greatly appreciated.

like image 919
Josh Avatar asked Jul 15 '12 17:07

Josh


People also ask

How do I find the URL for a Chrome extension?

Setting currentWindow: true allows you to get the current tab in the window where your extension's code is currently executing. For example, this might be useful if your extension creates a new window / popup (changing focus), but still wants to access tab information from the window where the extension was run.

How do I view a whole link?

Just right-click in Chrome's address bar select “Always show full URLs” to make Chrome show full URLs. Chrome will now always show the full URL of every web address you open.


1 Answers

Maybe this is what your looking for....

chrome.tabs.query({'active': true, 'windowId': chrome.windows.WINDOW_ID_CURRENT},
   function(tabs){
      alert(tabs[0].url);
   }
);

And the tabs permission needs to be set in the manifest...

manifest.json

"permissions": [ 
  "tabs"
]
like image 192
PAEz Avatar answered Oct 04 '22 08:10

PAEz