我正在构建一个Chrome扩展程序,当用户单击扩展程序图标时会注入一些HTML和JS。因此,单击图标后,它将在bg脚本中注册,并向内容脚本发出如下通知:
后台脚本
chrome.browserAction.onClicked.addListener(iconClicked)
function iconClicked(tab) {
var visitors = window.AllVisitors.get(tab.id),
data = {
message: 'toggleMenu',
user: window.user
};
chrome.tabs.sendMessage(tab.id, data);
}
现在,问题来了:在清单文件中,我还为
chrome://newtab
页面添加了自定义页面。在访问此自定义newtab页面时单击扩展图标时,内容脚本将不会收到任何消息。默认的newtab页面实际上会收到该消息以及任何其他网页。我认为也许与
externally_connectable
有关,但是添加此内容无济于事:"externally_connectable": {
"matches": ["chrome://newtab/"],
}
有人知道为什么我的自定义newtab页没有从后台脚本收到任何消息吗?任何帮助都非常感谢!
清单文件:
{
"manifest_version": 2,
"name": "Orbit",
"version": "0.0.1",
"web_accessible_resources": [
"templates.html",
"img/icon48.png",
"fonts/*.woff",
"img/*"
],
"externally_connectable": {
"matches": ["chrome://newtab/"],
},
"chrome_url_overrides" : {
"newtab": "tab/newtab.html"
},
"icons": {
"16": "img/icon16.png",
"48": "img/icon48.png",
"128": "img/icon128.png"
},
"browser_action": {
"default_icon": {
"16": "img/icon16.png",
"48": "img/icon48.png",
"128": "img/icon128.png"
},
"default_title": "Orbit"
},
"content_scripts": [
{
"matches": ["<all_urls>"],
"js": ["vendor/chrome-promise.js", "vendor/jquery.js", "vendor/underscore.js", "vendor/backbone.js", "orbit.js"]
},
{
"matches": ["<all_urls>"],
"css": ["sidebar.css"]
}
],
"background": {
"scripts": ["vendor/socket.io.js", "vendor/jquery.js", "vendor/underscore.js", "vendor/backbone.js", "vendor/chrome-promise.js", "vendor/jquery.js", "eventPage.js"],
"persistent": true
},
"permissions": [
"http://fonts.googleapis.com/*",
"https://fonts.googleapis.com/*",
"https://get-orbit.com:8080/*",
"activeTab",
"tabs",
"storage"
]
}
最佳答案
内容脚本不会注入chrome-extension://
页面。
只需将脚本手动添加到newtab.html中:
<html>
<head>
<script src="your-content-script.js"></script>
</head>
关于javascript - 自定义newtab页面:内容脚本不会收到来自bgscript的消息,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/32597138/