I was experimenting with chrome extension. My goal is to send a message from background script to injected script and let the injected script return back the result.
Code is as below:
background.js
chrome.tabs.onUpdated.addListener(function (tabId, changeInfo, tab){
chrome.tabs.executeScript(tabId, {'file': "content.js"},
function(){
chrome.tabs.sendMessage(tabId, {msg: 'test'},
function(response){
console.log(response);
}
);
return true;
}
);
});
content.js
chrome.runtime.onMessage.addListener(
function(request, sender, sendResponse) {
sendResponse('test message');
return true;
}
);
I get the error message
Could not send response: The chrome.runtime.onMessage listener must return true if you want to send a response after the listener returns
when calling sendResponse
inside content.js
I am already returning true from the listener.. confused?
Am I missing something obvious?