我在制作Chrome扩展程序时遇到问题。它给了我一个错误“TypeError:无法读取未定义的属性'告别'”。我是Chrome documentation做的。
后台脚本:
chrome.tabs.query({active: true, currentWindow: true}, function(tabs) {
chrome.tabs.sendMessage(tabs[0].id, {greeting: "hello"}, function(response) {
console.log(response.farewell);
});
});
内容脚本:
chrome.runtime.onMessage.addListener(
function(request, sender, sendResponse) {
console.log(sender.tab ?
"from a content script:" + sender.tab.url :
"from the extension");
if (request.greeting == "hello") sendResponse({farewell: "goodbye"});
});
清单:
{
"name": "Test",
"version": "0.0.1",
"description": "Test app",
"permissions": [ "background", "tts", "storage", "webNavigation", "activeTab", "tabs" ],
"background": { "scripts": ["my.js"] },
"browser_action": {
"default_popup": "my.html"
},
"content_security_policy": "script-src 'self' 'unsafe-eval'; object-src 'self'",
"content_security_policy": "script-src 'self' http://localhost; object-src 'self'",
"content_scripts": [
{
"matches": ["http://*/*", "https://*/*"],
"css": ["mystyles.css"],
"js": ["autofill.js", "smart.js"]
}
],
"manifest_version": 2
}