Haider
Haider

Reputation: 958

How do I use chrome.tabs.onUpdated.addListener?

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
});

Upvotes: 20

Views: 49208

Answers (2)

Shubham Gaikwad
Shubham Gaikwad

Reputation: 673

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.

Upvotes: 2

user278064
user278064

Reputation: 10170

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);
  });
}); 

Upvotes: 51

Related Questions