I am new to Chrome extensions. I am trying to communicate between the content script and the background.html page. The background.html sends a request, "hello", to the content script and the content script should respond back with "hello background" alert. But it's just not happening. My background.html code is:
function testRequest() {
chrome.tabs.getSelected(null, function(tab) {
chrome.tabs.sendRequest(tab.id, {greeting: "hello"});
});
}
content.js code:
chrome.extension.onMessage.addListener(
function(request, sender, sendResponse) {
if (request.greeting == "hello")
alert("hello background");
}
);
popup.html code:
<!doctype html>
<html>
<head></head>
<body>
<form>
<input type="button" value="sendMessage" onclick="testRequest()" />
</form>
</body>
</html>
manifest.json:
{
"browser_action": {
"default_icon": "icon.png",
"popup": "popup.html"
},
"background": {
"page": "background.html"
},
"permissions": [
"tabs",
"http://*/*",
"https://*/*",
"notifications",
"contextMenus"
],
"content_scripts": [
{
"matches": ["http://*/*","https://*/*"],
"js": ["content.js"]
}
],
"name": "FirstExtension",
"version": "1.0"
}
Please help!
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…