我正在构建一个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页面确实接收消息以及任何其他网页。
我在想这可能与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"
]
}
答案 0 :(得分:1)
内容脚本不会在chrome-extension://
个页面上注入。
只需在newtab.html中手动添加脚本:
<html>
<head>
<script src="your-content-script.js"></script>
</head>