【发布时间】:2020-03-10 21:36:41
【问题描述】:
我已尝试应用与内容脚本问题相关的其他主题的解决方案,但它们不起作用。 开发工具中的源代码看不到来自该扩展的内容脚本。 我从扩展控制台收到错误
未经检查的 runtime.lastError:无法建立连接。接收端不存在。
看起来扩展根本看不到 content.js。
所以,我的情况 - content.js 没有响应和工作。我是否错过了 manifest.json 中的某些内容?
manifest.json
"name": "Notification Extension",
"version": "1.0",
"description": "Build an Extension!",
"permissions": [
"storage",
"notifications",
"alarms",
"activeTab",
"tabs",
"http://*/*",
"https://*/*"
],
"background": {
"scripts": ["background.js"],
"persistent": false
},
"browser_action": {
"default_popup": "popup.html"
},
"content_scripts": [
{
"matches": [
"http://*/*",
"https://*/*"
],
"js": ["content.js"],
"run_at": "document_end"
}
],
"content_security_policy": "script-src 'self' 'unsafe-eval'; object-src 'self'",
"manifest_version": 2
}
content.js
console.log('content.js is working');
chrome.runtime.onMessage.addListener(
(request, sender, sendResponse) => {
if(request.createAlarm){
console.log('alarm senedet to content.js')
}
}
)
popup.js
chrome.tabs.query({active:true, currentWindow:true}, (tabs) => {
chrome.tabs.sendMessage(tabs[0].id, {createAlarm: true, message: 'sending to extension'}, (response) => {
console.log('message arrived');
})
})
感谢您的帮助!
【问题讨论】:
-
错误消息 syas 没有在该选项卡中运行内容脚本,因此它是 chrome:// 选项卡,或者您需要在重新加载或重新启用扩展程序后 reinject content scripts explicitly。否则,您必须手动重新加载选项卡才能让 Chrome 运行内容脚本。
标签: javascript google-chrome-extension