【发布时间】:2016-05-28 03:17:35
【问题描述】:
我正在尝试编写我的第一个 chrome 扩展,并致力于让消息传递和 background.js 正常工作。看起来我的 background.js 从未被调用或接收任何消息。有人可以告诉我我做错了什么吗?这是一个最小的例子:
manifest.json
{
"manifest_version": 2,
"name": "RAData",
"version": "0.1",
"background": {
"scripts": ["background.js"] },
"content_scripts": [{
"matches": [
"<all_urls>"],
"js": ["content.js"] }]
}
content.js
chrome.runtime.sendMessage("message from content");
console.log("content.js loaded");
background.js
chrome.runtime.onMessage.addListener(function(message, sender, sendResponse) {
console.log("message received");
});
console.log("background.js loaded");
我认为应该发生的是每当我加载页面时,应该加载 content.js,将“content.js 加载”打印到控制台,然后用消息唤醒 background.js。然后,Background.js 应将“收到的消息”和“已加载的background.js”打印到控制台。
相反,控制台只显示“content.js 已加载”——它看起来不像 background.js 中的任何代码被执行过。
【问题讨论】:
标签: javascript google-chrome-extension