I've been trying to get message passing for a chrome extension to work but console.log(response) gives me undefined. I've looked at the docs for message passing and on stackoverflow, but in my case it won't work. I am at a loss as to why.
myscript.js:
chrome.extension.sendRequest({greeting: "hello"}, function(response) {
console.log(response);
return true;
});
background.js:
chrome.runtime.onMessage.addListener(
function(request, sender, sendResponse) {
sendResponse({farewell: "goodbye"});
return true;
});
manifest.json
{
"manifest_version": 2,
"name": "nameasf",
"description": "asfasaf",
"version": "1.0",
"permissions": [
"tabs",
"http://somedomain.com/"
],
"content_scripts": [
{
"matches": ["http://somedomain.com/*"],
"css": ["mystyles.css"],
"js": ["jquery.js", "myscript.js"],
"all_frames": true
}
],
"background": {
"scripts": ["background.js"]
},
"browser_action": {
"default_icon": "icon.png",
"default_popup": "popup.html"
}
}
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…