chrome扩展名"sendResponse"不起作用 [英] chrome extension `sendResponse` does not work

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

问题描述

我正在编写chrome扩展名,但sendResponse方法不起作用.

I am writing a chrome extension but the sendResponse method 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);
                                                                                                                                                           
            });

功能alert(data)起作用.它为我提供了JSON格式的正确数据.但是,alert(response)不显示任何消息.谁能给我一些想法,为什么它不起作用?

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

提前谢谢!

推荐答案

您尚未声明此代码位于内容脚本或背景页面中.通过查看,我认为它是内容脚本的一部分.

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.

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

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

这是后台脚本:

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

每次脚本接收到一条消息时,您都必须使用"sendResponse"功能来发送响应.

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

希望这会有所帮助.

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

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