我正在尝试从chrome扩展弹出窗口向页面的内容脚本发送消息,但是侦听器未接收到发送的消息。
popup.js
chrome.runtime.sendMessage({ greeting: "hello" })
main.js(内容脚本,DOM的一部分)
侦听器未接收到发送的消息:
chrome.runtime.onMessage.addListener(function (request) {
console.log("message received")
if (request.greeting == "hello") {
console.log("hello message")
}
})
manifest.json
{
"manifest_version": 3,
"name": "Go Go Ninja Computers",
"version": "0.54",
"description": "This extension is trying to send a message.",
"background": {
"scripts": [
"background.js"
],
"persistent": false
},
"permissions": [
"storage"
],
"content_scripts":
[
{
"matches": ["*://*/*"],
"js": ["main.js"],
"css" : ["style.css"],
"run_at": "document_end"
}
],
"browser_action": {
"default_title": "Ninja Pirate",
"default_popup": "popup.html"
}
}
我只是关注these basic instructions,所以我很困惑为什么这不起作用?我也不确定是否需要包含“ background.js”,但是如果没有它,我将无法在popup.js中使用console.log。