Chrome扩展发送响应不起作用 [英] chrome extension send response does not work

查看:222
本文介绍了Chrome扩展发送响应不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  chrome.runtime.onMessage.addListener(
函数(request,sender,sendResponse){

if(!request.method){

return false;

}

if(request.method =='postList'&&request.post_list){
// alert(1);
a_facebook_api.getPostFromDB(request.post_list,function(数据){
alert(data);
sendResponse(data);

}); (request.method =='postAdd'&&&request.post_data){

a_facebook_api.addPostToDB(request.post_data,function(data){

}

sendResponse(data);

});

}

返回true;

}
);

chrome.runtime.sendMessage({method:postList,post_list:post_list},function(response){

alert(response);

});

函数警报数据有效。它给了我JSON格式的数据。但是,警报(响应)不显示任何消息。谁能给我一些想法,为什么它不起作用?

您尚未说明此代码位于内容脚本或背景页面中。从看它我认为它是内容脚本的一部分。



我在我自己的扩展中尝试了您的代码,它警告[object Object],它是当您发出不是字符串或数字值的变量时会发生什么情况。如果您将警报数据更改为response.responseData,它会在后台页面中提醒您标记为responseData的值。



因为它没有提醒任何您认为正在侦听邮件的脚本没有正确响应。



我的代码正常工作。
这是内容脚本:

  // Document ready 
window.onload = function(){
alert('Ready');
//发送消息
sendMessage();
}

//从后台页面获取消息
chrome.runtime.onMessage.addListener(function(request,sender,sendResponse){
//提示消息
alert(来自后台页面的消息:+ request.greeting); //您必须选择要显示的响应的哪一部分,即request.greeting
//构造&发送回应
sendResponse({
response:收到的消息
});
});

//发送消息到后台页面
函数sendMessage(){
//构造&发送消息
chrome.runtime.sendMessage({
method:postList,
post_list:ThePostList
},function(response){
// Alert消息
alert(来自后台页面的响应:+ response.response); //你必须选择你想要显示的响应部分,即response.response
});

$ / code>

这是后台脚本:

  //从内容脚本获取消息
chrome.runtime.onMessage.addListener(
函数(request,sender,sendResponse){
//提醒消息
alert('来自内容脚本的消息:'+ request.method); //你必须选择要显示的响应的哪一部分,即request.method
//构造和发送响应
sendResponse({
response:收到的消息
});
}
);

//发送消息到内容脚本
函数sendDetails(sendData){
//选择标签
chrome.tabs.query({active:true,currentWindow: true},function(tabs){
//构造和发送消息
chrome.tabs.sendMessage(tabs [0] .id,{
greeting:sendData
},函数(响应){
//响应警告响应
alert(来自内容脚本的响应:+ response.response); //你必须选择你想要的响应的哪一部分显示即response.response
});
});
}

每次脚本收到消息时,都必须使用sendResponse函数发送回复。



希望这有帮助。


I am writing a chrome extension but the send response does not work.

chrome.runtime.onMessage.addListener(
    function(request, sender, sendResponse) {

            if(!request.method){

                    return false;

            }

            if(request.method=='postList' && request.post_list){
                // alert(1);                                                                                                                                                                            
                    a_facebook_api.getPostFromDB(request.post_list, function(data){
                            alert(data);                                                                                                                                                              
                            sendResponse(data);                                                                                                                                                       

                    });

            } else if(request.method=='postAdd' && request.post_data){

                    a_facebook_api.addPostToDB(request.post_data, function(data){

                            sendResponse(data);

                    });

    }

            return true;

}
);

 chrome.runtime.sendMessage({method: "postList",post_list: post_list}, function(response) {

         alert(response);

            });

the function alert data works. It gives me data with JSON format. However, the alert(response) does not show any message. Can anyone give me some ideas why it does not work?

Thank you in advance!

解决方案

You haven't stated weather you this code is in the content script or the background page. From looking at it I assume it it part of the content script.

I tried your code in one of my own extensions and it alerted "[object Object]" which is what happens when you alert a variable that isn't a string or numerical value. If you change the alert data to "response.responseData" it will alert the value you labeled "responseData" from the background page.

As it wasn't alerting anything for you I think that the script that is listening for the message is not responding properly.

I got the code working. This is the content script:

//Document ready
window.onload = function() {
    alert('Ready');
    //Send a message
    sendMessage();
}

//Get message from background page
chrome.runtime.onMessage.addListener(function(request, sender, sendResponse) {
    //Alert the message
    alert("The message from the background page: " + request.greeting);//You have to choose which part of the response you want to display ie. request.greeting
    //Construct & send a response
    sendResponse({
        response: "Message received"
    });
});

//Send message to background page
function sendMessage() {
    //Construct & send message
    chrome.runtime.sendMessage({
        method: "postList",
        post_list: "ThePostList"
    }, function(response) {
        //Alert the message
        alert("The response from the background page: " + response.response);//You have to choose which part of the response you want to display ie. response.response
    });
}

This is the background script:

//Get message from content script
chrome.runtime.onMessage.addListener(
    function(request, sender, sendResponse) {
        //Alert the message
        alert('The message from the content script: ' + request.method);//You have to choose which part of the response you want to display ie. request.method
        //Construct & send a response
        sendResponse({
            response: "Message received"
        });
    }
);

//Send message to content script
function sendDetails(sendData) {
    //Select tab
    chrome.tabs.query({active: true, currentWindow: true}, function(tabs) {
        //Construct & send message
        chrome.tabs.sendMessage(tabs[0].id, {
            greeting: sendData
        }, function(response) {
            //On response alert the response
            alert("The response from the content script: " + response.response);//You have to choose which part of the response you want to display ie. response.response
        });
    });
}

Every time a script receives a message you have to use the "sendResponse" function to send a response.

Hope this helped.

这篇关于Chrome扩展发送响应不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆