19

I am creating an extension for Chrome. I want to show an alert() with the page URL whenever the user moves from one tab to another, or when the user enters a new URL in a tab.

This is not working:

chrome.tabs.onUpdated.addListener(function(integer tabId, object changeInfo, Tab tab) {
    alert(changeInfo.url);
});

chrome.tabs.onActivated.addListener(function(object activeInfo) {
    // also please post how to fetch tab url using activeInfo.tabid
});
approxiblue
  • 6,982
  • 16
  • 51
  • 59
Haider
  • 938
  • 2
  • 11
  • 25

2 Answers2

45

Remove integer, object and Tab in the functions signature. Also change .onUpdated to .onActivated

chrome.tabs.onUpdated.addListener(function(tabId, changeInfo, tab) {
   alert(changeInfo.url);
}); 

chrome.tabs.onActivated.addListener(function(activeInfo) {
  // how to fetch tab url using activeInfo.tabid
  chrome.tabs.get(activeInfo.tabId, function(tab){
     console.log(tab.url);
  });
}); 
ZenVentzi
  • 3,945
  • 3
  • 33
  • 48
user278064
  • 9,982
  • 1
  • 33
  • 46
  • 7
    Does this code not work anymore? I'm using this as a popup.js included in my popup.html. Won't work in my background or content-script either. – Rohit Tigga Aug 09 '17 at 23:10
  • 13
    This is probably too late for Rohit, but make sure you have `"tabs"` in the `permissions` array in the extension manifest. Otherwise, you won't get the tab URL or title. – jdunning Apr 14 '19 at 22:41
0
chrome.tabs.onUpdated.addListener((tabId, changeInfo, tab)=>{
    chrome.tabs.query({
        active: true,
        currentWindow: true
    },(tabs)=>{
        if(changeInfo.url && tabId === tabs[0].id) {
            console.log("Only Current TAB");
        };
    })})

This will give if only the current tab is updated.

Shubham Gaikwad
  • 553
  • 5
  • 4