Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

chrome.tabs.sendMessage when tab is not focused

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?

like image 443
Mark Avatar asked Oct 21 '25 13:10

Mark


1 Answers

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

like image 141
Mark Avatar answered Oct 23 '25 05:10

Mark



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!