I am writing an extension supposed to catch any kind of redirections through a backgroud page, keep track of them for each tab, and outline them in a browser action for each tab. Thus i want the action popup script to get the active tab's Id to display only datas related to this active tab in the action popup.
I already declared the permission "activeTab" in the manifest.
I tried chrome.tabs.getCurrent(function(tab){})
before noticing in the documentation that the browser actions are mentionned as instance of contexts where tab
is undefined. https://developer.chrome.com/extensions/tabs#method-getCurrent
I considered messaging a content script to get the tab id, but i did not find a suitable method to connect a content script and an action popup.
How could i get the active tab's ID in a browser action popup if it is possible ?
You need tabs.query
:
chrome.tabs.query({active: true, currentWindow: true}, function(tabs) {
var currTab = tabs[0];
if (currTab) { // Sanity check
/* do stuff */
}
});
"Rare cases" are mostly limited to a detached DevTools window being the current window (looks like this case is fixed); should not happen during normal operation.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With