我正在开发Chrome扩展程序。我正在使用chrome.runtime.sendMessage()
API将URL发送到另一个js文件。当我通过chrome.runtime.onMessage.addlistener(function(sent,sender,sendResponse){...})
收到其他文件中的网址时。我调用另一个方法或函数checkUrl(url){...}
,我将其他文件发送的URL作为消息传递给我。并且此函数返回对该URL的Ajax调用。然后我在我调用.then() promise
函数的范围内使用checkUrl(url){...}
。我完全得到了响应,在控制台中显示响应后,我将该响应发送回发送方文件,我从中收到消息。但我没有得到任何关于内容脚本的回复我不知道是否有错误...
发送消息的内容脚本代码
chrome.runtime.sendMessage({type:"postUrl",mess:url},function(r){
console.log(r.res);
});
脚本我收到消息然后尝试发送回复
chrome.runtime.onMessage.addListener(function(sent,sender,sendResponse){
alert("message recieved");
var str;
switch(sent.type){
case "postUrl":{
checkUrl(sent.mess).then(function(r){
console.log("Promise Test : " + r);
sendResponse({res:r})
});
break;
}
}
});
function checkUrl(url){
return $.ajax({
method:"GET",
url:"http://www.bitbaysolutions.com/connections.php?fbPostUrl=true",
data:{
url:url
}
})
}