Ad

Getting URL When Window Refreshed Doesn't Work In Chrome Extension

- 1 answer

I want to get the URL of the current window when my web page is refreshed in my Chrome Extension .. here is what I have done :

Part of my manifest.json :

  "content_scripts": [
{
  "matches": ["http://*/*","https://*/*"],
  "js": ["temp.js"]
}
]

My temp.js :

        chrome.tabs.getSelected(null, function(tab) 
    {      var tabId = tab.id;
           tabUrl = tab.url;
           alert(tabUrl);
    });

But this doesn't work.

Please help .. I'm still a beginner x)

Ad

Answer

chrome.tabs is not available in content scripts. You should create a background script like this:

include this in your manifest.js:

"background": {
  "scripts": ["background.js"]
},
"permissions": [
  "tabs"
]

then in background.js:

chrome.tabs.onUpdated.addListener(function(tabId, changeInfo, tab) {
    // This will give you the url if it's changed
    alert(changeInfo.url);
    // Or to always get the tab's url even when it's unchanged
    alert(tab.url);
}); 

more info

Ad
source: stackoverflow.com
Ad