2

我正在尝试构建一个 Google Chrome 扩展程序,它可以从内容脚本向后台页面发送消息,并根据发送的消息显示警报。

清单.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!'); });

背景.html

chrome.extension.onMessage.addListener(function(request, sender, sendResponse) { 
if(request.message=="execute"){
    alert("message received!");
}else if{
    alert("message not received!");
}
});

但我没有收到来自后台页面的任何警报。

4

2 回答 2

0

代码中的问题

if(request.message=="execute"){
    alert("message received!");
}else if{
    alert("message not received!");
}

java脚本中没有else if{使用else {

工作版本

背景.html

<html>
<head>
<script src="background.js"></script>
</head>
<body>
</body>
</html>

背景.js

chrome.extension.onMessage.addListener(function(request, sender, sendResponse) { 
if(request.message=="execute"){
    alert("message received!");
}else{
    alert("message not received!");
}
});
于 2013-02-22T08:59:06.510 回答
0

js中有else if,但应该是else if()。所以你省略了else if()。在你的情况下,else就足够了。

于 2013-02-26T05:58:16.660 回答