[英]Chrome extension messaging from popup to content script
I'm trying to send a message from my popup.js to a content.js and I just don't get why it doesn't work. 我试图将消息从我的popup.js发送到content.js,但我不明白为什么它不起作用。
manifest.json 的manifest.json
{
"manifest_version": 2,
"name": "Form Builder",
"description": "This extension populates a form.",
"version": "0.1",
"content_scripts": [{
"all_frames": true,
"matches": ["http://*/*", "https://*/*"],
"js" : [
"/js/jquery/jquery.min.js",
"/js/formLoader.js",
"/js/content.js"
]
}],
"options_page": "/html/options.html",
"permissions": ["https://localhost:8443/",
"tabs", "activeTab", "storage", "background"
],
"icons": { "16": "/images/icons/wb_icon_16.png",
"48": "/images/icons/wb_icon_48.png",
"128": "/images/icons/wb_icon_128.png"
},
"browser_action": {
"default_popup": "/html/popup.html"
}
}
content.js content.js
chrome.extension.onRequest.addListener(function(request, sender, sendResponse){
sendResponse("received message: " + request);
formLoader.setFormData(request, sendResponse);
// Why isn't this getting executed when I send a message from popup?
sendResponse("Thank you");
});
popup.js popup.js
var popup = {
// a few parts snipped out for brevity
sendMessage: function(message) {
chrome.tabs.sendMessage(tab.id, message, function(message) {
console.log(chrome.runtime.lastError);
popup.handleCallbacks(message);
});
},
handleCallbacks: function (message) {
console.log("handleCallbacks: " + message);
}
}
formLoader.js formLoader.js
var formLoader = {
init : function() {
$("#PostingBody").val("formLoader.init");
},
setFormData: function(data, callback) {
callback("[formLoader.js] got data: " + data);
}
}
formLoader.init();
When I do something like: 当我做类似的事情时:
popup.sendMessage(data);
In my console I get: 在我的控制台中,我得到:
Object {message: "Could not establish connection. Receiving end does not exist."}
handleCallbacks: undefined
Why isn't there a receiving end? 为什么没有接收端? What am I doing wrong with the listener in the content.js?
我对content.js中的侦听器有什么错?
Thanks to: This answer from another question 感谢: 来自另一个问题的答案
For Chrome 26+, use chrome.runtime.onMessage and chrome.runtime.sendMessage. 对于Chrome 26+,请使用chrome.runtime.onMessage和chrome.runtime.sendMessage。
even though I'm positive I've found several pages and references that fail to mention this. 即使我很肯定,我也发现了几页并没有提及此内容的参考。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.