【问题标题】:chrome extension `sendResponse` does not workchrome扩展`sendResponse`不起作用
【发布时间】:2013-12-24 11:46:41
【问题描述】:

我正在编写一个 chrome 扩展,但 sendResponse 方法不起作用。

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) 不显示任何消息。谁能给我一些想法为什么它不起作用?

提前谢谢你!

【问题讨论】:

标签: javascript google-chrome-extension messaging


【解决方案1】:

我正在寻找从弹出窗口内的内容脚本中获取数据的解决方案。是的,您可以将它与存储一起使用,但这适用于所有选项卡,因此最好将消息发送到活动选项卡并阅读响应:

在 popup.js 中

chrome.tabs.query({
    active: true,
    lastFocusedWindow: true
}, function (tabs) {
    chrome.tabs.sendMessage(tabs[0].id, {data: 1}, function (response) {
        console.log(response.data)
    });
});

在 content.js 中

chrome.runtime.onMessage.addListener(function (message, sender, sendResponse) {
    if (message.data === 1) {
        // accepts only 1 field
        sendResponse({
            data: 2
        });
        // very important to return true 
        return true; 
    }
});

【讨论】:

  • 尽管有些人传播了都市神话,但只有在onMessage内部的异步代码中调用sendResponse时才需要“返回true”。换句话说,这个答案的代码不需要它。
  • 兄弟,需要lastFocusedWindow,如果我为同一页面打开了4个标签,我只希望当前查看的标签使用chrome.tabs.sendMessage发送消息
【解决方案2】:

同样的问题Chrome Extension Message passing: response not sent 会有所帮助。

您只需在侦听器上添加return true

【讨论】:

  • 这对我有用!感谢您发布您的解决方案
  • 如果您看到一个问题可以用与另一个问题相同的答案来回答,请将该问题标记为重复;不要添加答案。但是,从问题的代码中可以看出,OP 已经在其侦听器中执行了return true;。因此,这不是 this 问题的答案,而是许多有相同问题的人的答案。
  • 我认为这是一个不同的问题,但症状相似。这里的问题是 OP 如何选择显示数据。正如 Makyen 所说,return true; 无法解决这个问题。消息侦听器完成后,Chrome 将删除 sendResponse。当需要在 sendResponse... 之前使用另一个异步调用时,这是一个完全不同的问题。在这种情况下,return true; 告诉 chrome 异步运行 sendResponse(也就是保持 sendReponse 回调通道打开)。它将保持有效,直到它被调用。此外,自动 sendResponse 调用被禁用。
【解决方案3】:

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

我在我自己的一个扩展中尝试了您的代码,它警告“[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
    });
}

这是后台脚本:

//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”函数发送响应。

希望这会有所帮助。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2022-01-22
    • 2017-05-26
    • 2012-01-22
    • 2012-08-08
    • 1970-01-01
    相关资源
    最近更新 更多