I have following code in my Chrome extension
devtoolsPanel.js
chrome.runtime.sendMessage({
name: 'executeScript',
tabId: chrome.devtools.inspectedWindow.tabId
}, function(response) {
if(response != undefined){
console.log(response.message);
}
});
background.js
chrome.runtime.onMessage.addListener(function(message, sender, sendResponse) {
try {
if (message.name == "executeScript") {
chrome.tabs.executeScript(message.tabId, {
code: 'document.body.style.backgroundColor="red"'
}, function() {
sendResponse({"message":"script executed"});
});
}
} catch (e) {
sendResponse({
"exceptionD": "" + e
});
}
});
I would expect that sendResponse() gets called after executing content script but it gets called immediately after chrome.tabs.executeScript() with undefined.
This happens with any runtime API's that I use in content script. I tries with Localstore API also, same thing happens
for example in my contentscript.js I have the following code
function readOptions(sendResponse) {
var options = {};
chrome.storage.local.get(null, function(items) {
sendResponse(items);
});
}
Even in this case sendResponse() gets called immediately after chrome.storage.local.get() with undefined.
Can some one tell me whats happening here.