Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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
});
like image 757
Haider Avatar asked Jun 22 '12 12:06

Haider


People also ask

How do I use Chrome tabs?

Or, use a keyboard shortcut: Windows & Linux: Ctrl + t. Mac: ⌘ + t.


1 Answers

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);
  });
}); 
like image 179
user278064 Avatar answered Oct 13 '22 00:10

user278064