Tab switch event available for Google Chrome extension?

17,788

Solution 1

In your manifest.json file add the following:

"permissions":[
    "background",
    "tabs"
],

"background" : {
    "scripts": ["scripts/background.js"],
    "persistent": false
}

In your background.js:

chrome.tabs.onActivated.addListener(function(activeInfo) {
    console.log(activeInfo.tabId);
});

Solution 2

It's possible, see onActivated at https://developer.chrome.com/docs/extensions/reference/tabs/#event-onActivated

Share:
17,788
Kelley van Evert
Author by

Kelley van Evert

Updated on June 05, 2022

Comments

  • Kelley van Evert
    Kelley van Evert almost 2 years

    As a Google Chrome extension, is it possible to listen to tab switches? That is, to be notified when a tab switch has just happened?

    (I want to make an extension that, in fullscreen, when switching tabs with the usual Ctrl+PageUp and Ctrl+PageDown keyboard shortcuts, gives visual feedback of the switch and other currently available tabs.)

  • Waqar Bin Kalim
    Waqar Bin Kalim over 2 years
    Does this also work if the tab you're switching to is in a different window?