Reputation: 615
I have a chrome extension that sends and receives messages back and forth between content and background scripts.
Everything works fine with chrome.runtime
, but I am using chrome.tabs.sendMessage
to send messages to the content scripts using something like this:
chrome.tabs.query({active: true, currentWindow: true}, function(tabs) {
chrome.tabs.sendMessage(tabs[0].id, {method: "stuff"}, function(response) { });
});
The extension runs only on a few specific urls, and when that code executes while im on a different tab than the one the extension is running on, or in the background inspect view, it doesn't work.
How can I make it work even when tab is active/focused at the moment the code run?
Upvotes: 4
Views: 1460
Reputation: 615
chrome.runtime.onMessage.addListener(function(request, sender, sendResponse) {
console.log(sender.tab.id);
});
Using sender.tab.id
instead of tabs[0].id
seems to do the job
Upvotes: 2