我对chrome扩展程序是陌生的,并且对基础知识一无所知,因此我显然缺少一些东西。已经提出了类似的问题,但似乎无济于事。我正在尝试向内容脚本发送基本消息,但无法console.log该消息。
manifest.json
{
"name": "Ext_Test",
"version": "1.0",
"description": "testing",
"manifest_version": 2,
"background": {
"scripts": ["background.js"],
"persistent": false
},
"content_scripts": [
{
"matches": ["http://*/*", "https://*/*"],
"run_at": "document_start",
"js": ["content.js"]
}
],
"browser_action": {
"default_popup": "popup.html",
"default_title": "Testing"
},
"permissions": [
"tabs",
"*://*/*"
]
}
background.js
chrome.tabs.onUpdated.addListener(function(tabId,info, tab) {
if (info.status == "complete") {
chrome.tabs.sendMessage(tabId, {action: "test", message: tab.url})
}
});
content.js
console.log("before method");
chrome.runtime.onMessage.addListener(function(message,sender,sendResponse){
if(message.action == "test"){
console.log(message);
}
});
console.log("after method");
从控制台日志中,我可以看到正在打印“方法前” 和“方法后” ,但是看不到函数内部的消息。
更新:popup.html
<!DOCTYPE html>
<html lang="en" dir="ltr">
<head>
<meta charset="utf-8">
<title>Test</title>
</head>
<body>
<script type="module" src="content.js"></script>
</body>
</html>