Obtaining “this” tab ID from content script in Chrome extension?

Tab id is automatically passed inside MessageSender object:

chrome.runtime.onMessage.addListener(function(request, sender, sendResponse) {
    console.log("sent from tab.id=", sender.tab.id);
});

Note: According to docs, this property is not always available:

This property will only be present when the connection was opened from a tab (including content scripts), and only if the receiver is an extension, not an app.

Leave a Comment