我正在尝试构建一个Google Chrome扩展程序,该扩展程序可以从内容脚本向后台页面发送消息,并根据发送的消息显示警报。
的manifest.json
{
"manifest_version": 2,
"name": "fb_rainbow",
"description": "Change Facebook Themes Colors",
"version": "1.1",
"background": {
"page":"background.html"
},
"browser_action": {
"default_icon": "icon.png",
"popup":"popup.html"
},
"permissions": [
"tabs",
"http://*/*",
"https://*/*",
"notifications",
"contextMenus",
"background"
],
"content_security_policy": "script-src 'self' 'unsafe-eval'; object-src 'self'",
"web_accessible_resources":["injectin.js"],
"content_scripts": [
{
"matches": ["*://*.facebook.com/*"],
"js": ["color_change.js"]
}
]
color_change.js(工作并可以获得警报)
chrome.extension.sendMessage( {message:'execute'} , function() { alert('message sent!'); });
background.html
chrome.extension.onMessage.addListener(function(request, sender, sendResponse) {
if(request.message=="execute"){
alert("message received!");
}else if{
alert("message not received!");
}
});
但是我没有从后台页面收到任何提醒。
答案 0 :(得分:0)
if(request.message=="execute"){
alert("message received!");
}else if{
alert("message not received!");
}
java脚本中没有else if{
使用else {
。
<html>
<head>
<script src="background.js"></script>
</head>
<body>
</body>
</html>
chrome.extension.onMessage.addListener(function(request, sender, sendResponse) {
if(request.message=="execute"){
alert("message received!");
}else{
alert("message not received!");
}
});
答案 1 :(得分:0)
在js中有 else if ,但如果() 则应为 else。所以你省略了 else if() 。 在您的情况下, else 就足够了。