chrome扩展发送响应不起作用

use*_*204 7 javascript google-chrome

我正在编写chrome扩展,但发送响应不起作用.

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);

            });
Run Code Online (Sandbox Code Playgroud)

功能警报数据有效.它为我提供了JSON格式的数据.但是,警报(响应)不显示任何消息.任何人都可以给我一些想法,为什么它不起作用?

先感谢您!

小智 13

您还没有说明此代码在内容脚本或后台页面中的天气.从它看,我认为它是内容脚本的一部分.

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

由于它没有为您提醒任何事情,我认为正在侦听该消息的脚本没有正确响应.

我得到了代码.这是内容脚本:

//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
    });
}
Run Code Online (Sandbox Code Playgroud)

这是后台脚本:

//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
        });
    });
}
Run Code Online (Sandbox Code Playgroud)

每次脚本收到消息时,您都必须使用"sendResponse"函数发送响应.

希望这有帮助.


Bla*_*ory 9

同样的问题Chrome扩展消息传递:响应未发送将有所帮助.

你只需要添加return true监听器.