Google chrome extension 在这种情况下,向内容脚本发送popup.html加载请求不会收到任何结果
我的要求是每当我点击扩展图标时,它应该向内容脚本发送请求,并且应该发送带有所需属性的回复。我可以发送请求。当我检查控制台内容时,脚本正在接收请求并进行处理。但在弹出端,我无法接收任何内容。Google chrome extension 在这种情况下,向内容脚本发送popup.html加载请求不会收到任何结果,google-chrome-extension,Google Chrome Extension,我的要求是每当我点击扩展图标时,它应该向内容脚本发送请求,并且应该发送带有所需属性的回复。我可以发送请求。当我检查控制台内容时,脚本正在接收请求并进行处理。但在弹出端,我无法接收任何内容。 以下是内容脚本中的请求处理程序 chrome.extension.onRequest.addListener(function ListeningMethod(request, sender, callback) { switch(request.action) { case
以下是内容脚本中的请求处理程序
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.
});
});
});
您的
content\u脚本
没有调用正确的方法来发送响应。您的侦听器函数将其命名为callback
,然后尝试使用sendRequest
。为了清晰起见,您还应该删除函数名或在addListener之外定义它
chrome.extension.onRequest.addListener(function(request, sender, callback)
{
switch(request.action)
{
case "QuestionProperties":
callback({attributes: {"h":"s","r":"t"} });
break;
}
});
它很有魅力。。非常感谢。多谢各位。