Hi DW.
I want to get a message only on that specific targeted tab id. If the messege is sent to tab id 2, it should run a script under tab id 2 to update that page displayed or for simplicity log on tab id 2 console the message.
The tab id is from background script so it then sends a message to that tab id, here is an example:
chrome.webNavigation.onCompleted.addListener(function(details){
doThis("my_Message", details);
}
function doThis(msg, to){
chrome.tabs.query({active: true, currentWindow: true}, function(tabs){
//console.log("vvv: " + tabs[0].id);
chrome.tabs.sendMessage(to.tabId, {action: msg, data: "test"}, function(response){
if(chrome.runtime.lastError) {
const rw = setTimeout(() => {doThis(msg);}, 1000);
clearTimeout(rw);
} else {
console.log("Message sent from background");
}
});
});
}
Now my receiving part:
chrome.runtime.onMessage.addListener(function(msg, sender, response){
console.log(sender.id);
console.log(msg);
return true;
}