我无法在current_script和后台之间构建有效的交换消息。 错误消息是:
Error in event handler for 'undefined': Cannot read property 'farewell' of undefined TypeError: Cannot read property 'farewell' of undefined
at chrome-extension://lanofchlikjdallgfmhbbijdglepcomm/script.js:29:23
at <error: illegal access>
at Event.dispatchToListener (event_bindings:356:21)
at Event.dispatch_ (event_bindings:342:27)
at Event.dispatch (event_bindings:362:17)
at Object.chromeHidden.Port.dispatchOnDisconnect (miscellaneous_bindings:258:27)
我使用了Google API Simple one-time requests并拥有以下星座:
mainfest.json
{
"manifest_version": 2,
"name": "Facebook Cage",
"description": "Facebook Cage hilft dir den Inhalt deiner Facebook Seite anzupassen.",
"version": "1.0",
"browser_action": {
"default_icon": "icon.png",
"default_popup": "popup.html"
},
"background": {
"scripts": ["popup.js"]
},
"content_scripts": [
{
"matches": ["https://www.facebook.com/*"],
"js": ["script.js"]
}
],
"permissions": [
"https://facebook.com/",
"storage",
"tabs"
]
}
的script.js
document.onclick = function (){
if(document.URL=="https://www.facebook.com/"){
chrome.runtime.sendMessage({greeting: "hello"}, function(response) {
console.log(response.farewell);
});
}
}
popup.js
chrome.storage.sync.get('url', function(result) {
!!result['url'] ? $("#input-area").val(result['url']) : "";
});
$("#save").click(function() {
chrome.storage.sync.set({'url': $("#input-area").val()});
window.close();
return(false);
});
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"});
});
也许你可以帮忙,谢谢!
编辑: 问题在于清单逻辑。我有三个组件:popup.js和script.js。 popup.js嵌入在popup.html中。 script.js作为内容脚本。遗漏的是背景逻辑,比如backgorund.js。只需输入代码
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"});
});
在background.js中并通过
将其链接到清单中 "background": {
"scripts": ["background.js"]
},
总而言之,我最终删除了background.js中的后台登录,从那里也可以使用存储getter。