为什么“ chrome.runtime.onMessage.addListener”是匿名函数?

时间:2018-10-18 04:02:16

标签: javascript google-chrome google-chrome-extension

所以我正在尝试构建我的第一个Google chrome扩展程序,但是遇到了麻烦。

我正在尝试处理来自内容脚本的消息。我不断从Google扩展程序中收到此错误:

background.js:22(匿名函数)

这是我的代码的样子:

manifest.json:

{
"name": "Getting Started Example",
"version": "1.0",
"description": "Build an Extension!",
"permissions": ["activeTab", "tabs", "storage", "clipboardWrite", "clipboardRead","declarativeContent"],
"background": {
    "scripts": ["background.js"],
    "persistent": false
},
"content_scripts": [ {
    "matches": ["http://*/*"],
    "js": ["content.js", "jquery-1.4.2.min.js"]
}],
"browser_action": {
    "default_popup": "popup.html",
    "default_icon": {
        "16": "images/get_started16.png",
        "32": "images/get_started32.png",
        "48": "images/get_started48.png",
        "128": "images/get_started128.png"
      }
},
"icons": {
    "16": "images/get_started16.png",
    "32": "images/get_started32.png",
    "48": "images/get_started48.png",
    "128": "images/get_started128.png"
  },
"manifest_version": 2
 }

background.js

chrome.runtime.onMessage.addListener(
    function(request, sender, sendResponse) {
        if (request.event == "copy") {
            alert("copy detected");
            console.log("copy");
        }
    sendResponse({});
    }
);

content.js

chrome.runtime.sendMessage({event: "copy"}, function(response) {
    console.log('from content_script: copied!');
});

为什么将其视为匿名函数?我检查了文档是否必须添加一些权限,但是文档说应该不错。

0 个答案:

没有答案