Monday, May 20, 2024
99
rated 0 times [  103] [ 4]  / answers: 1 / hits: 41428  / 13 Years ago, wed, june 1, 2011, 12:00:00

From a content script, is it possible to access that tab's id?


I want to send a message to the background page from the content script that tells my extension to "do something with this tab" using the chrome.tabs.* API.


A tabID is needed, and there is no point in doing a bunch of logic in the background page to hunt for a tabID when my content script can simply tell it the tabID in the message contents.


More From » google-chrome-extension

 Answers
65

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.



[#91929] Tuesday, May 31, 2011, 13 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
samara

Total Points: 326
Total Questions: 106
Total Answers: 103

Location: Cook Islands
Member since Thu, May 21, 2020
4 Years ago
;