Spencer Carnage
Spencer Carnage

Reputation: 2066

Add event listener to only 1 tab in Chrome extension

In my Chrome extension, I want to listen for changes to one particular tab only. Using the chrome.tabs.onUpdated.addListener method, my observation is that this runs on ALL tabs. I have implemented a way to capture the id of the tab that I'm interested in and then check that first, like so:

var extTabId = 10; // captured when this tab is created

chrome.tabs.onUpdated.addListener(function(tabId, changeInfo, tab) {
    if (tabId !== extTabId) {
        return false;
    }

    // do whatever else I need this specific tab in question to do
}); 

Is there an easier way do this so that I only add a listener for extTabId?

Upvotes: 3

Views: 3518

Answers (1)

serg
serg

Reputation: 111265

There is no simpler solution, your approach is the way to go.

Upvotes: 4

Related Questions