Kelley van Evert
Kelley van Evert

Reputation: 1145

Tab switch event available for Google Chrome extension?

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.)

Upvotes: 18

Views: 28215

Answers (3)

GorvGoyl
GorvGoyl

Reputation: 49150

note: tab url may not be available when using chrome.tabs.onActivated so use chrome.tabs.onUpdated instead.

Upvotes: 2

Vaibhav Desai
Vaibhav Desai

Reputation: 2402

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

Upvotes: 22

Related Questions