Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Chrome tabs.onActivated.addListener throws an "undefined" TypeError

I'm doing a chrome extension, and there is in the doc this statement about chrome.tabs.onActivated.

Whenever I try to place chrome.tabs.onActivated.addListener, it says Uncaught TypeError: Cannot call method 'addListener' of undefined.

The whole background.html :

<script>
chrome.tabs.onActivated.addListener(function(info) {
    var tab = chrome.tabs.get(info.tabId, function(tab) {
        localStorage["current_url"] = tab.url;
    });
});
</script>
like image 874
azenet Avatar asked Mar 31 '12 08:03

azenet


1 Answers

The documentation is incomplete. As of Chrome 18, chrome.tabs.onActiveChanged is replaced with chrome.tabs.onActivated. In Chrome 17, the onActivated event did not exist.

chrome.tabs.onActivated.addListener( function(info) {
    var tabId    = info.tabId,
        windowId = info.windowId;
});
chrome.tabs.onActiveChanged.addListener( function(tabId, info) {
    tabId        = tabId;         // For comparison
    var windowId = info.windowId;
});

I obtained this function name by opening the console in the context of an extension, and inspecting the keys of chrome.tabs.

like image 121
Rob W Avatar answered Oct 03 '22 11:10

Rob W