我的要求就是每当我点击应该向内容脚本发送请求的扩展图标时,应该发送带有必需属性的回复。我能够发送请求。当我检查控制台时,内容脚本正在接收请求并处理它。但是在弹出端我无法接收任何内容。
这是内容脚本
chrome.extension.onRequest.addListener(function ListeningMethod(request, sender, callback)
{
switch(request.action)
{
case "QuestionProperties":
sendResponse({attributes: {"h":"s","r":"t"} });
break;
}
});
在popup.html上我发送了这样的请求
$(document).ready(function(){
chrome.tabs.getSelected(null, function(tab) {
chrome.tabs.sendRequest(tab.id, {action: "QuestionProperties"}, function(response){
alert('received something'); // Even this is not alerting
var data = JSON.parse(response.attributes);
alert(JSON.stringify(data)); // Here also I could not recieve anything. At Contentscript side I have checked the response that is being sent. I am able to see the data. But at Popup side I am unable to recieve it. Please help me on this.
});
});
});
答案 0 :(得分:1)
您的content_script
没有调用正确的方法来发送回复。您的侦听器函数将其命名为callback
,但随后尝试使用sendRequest
。为清晰起见,您还应该删除函数名称或在addListener之外定义它。
chrome.extension.onRequest.addListener(function(request, sender, callback)
{
switch(request.action)
{
case "QuestionProperties":
callback({attributes: {"h":"s","r":"t"} });
break;
}
});