消息回调同步错误

时间:2012-12-01 17:06:59

标签: javascript google-chrome-extension content-script

在我的扩展程序的内容脚本中,我从background.js请求数据,如下所示:

fireOnNewTopic ();  // Initial run on cold start or full reload.

window.addEventListener ("hashchange", fireOnNewTopic,  false);

function fireOnNewTopic () {
/*-- For the pages we want, location.hash will contain values
    like: "#!newtopic/{group title}"
*/
  if (location.hash) {
    var locHashParts    = location.hash.split ('/');
    if (locHashParts.length > 1  &&  locHashParts[0] == '#!newtopic') {
        var subjectStr  = '';
        var bodyStr  = '';
        switch (locHashParts[1]) {
            case 'opencomments-site-discussions':
            chrome.extension.sendMessage({name:"domain"},
                function(response)
                {
                subjectStr = response.domain;   
                });
            chrome.extension.sendMessage({name:"url"},
                function(response)
                {
                bodyStr = "URL of last page visited: " +                        response.url;   
                });
                break;
            default:
                break;
        }

        if (subjectStr && bodyStr) {
            runPayloadCode (subjectStr, bodyStr);
        }
    }
  }
}

不幸的是,由于sendMessage()与回调异步运行,因此在代码到达runPayloadCode()时,subjectStr和bodyStr仍然为null,因为background.js中的代码尚未完成。同步代码的最佳方法是什么,以便在调用runPayloadCode()时填充subjectStr和bodyStr?

2 个答案:

答案 0 :(得分:1)

详细说明Sudarshan关于将两个请求合并为一个的说法(抱歉,需要代码所以不能只评论)这里是你可以做的...... 的发送

chrome.extension.sendMessage({url: true, domain:true}, function(response) {
  console.debug('The url is "'+response.url+'" and the domain is"'+response.domain+'"');
  if (repsone.url && response.domain) {
            runPayloadCode (subjectStr, "URL of last page visited: " + response.domain);
        }
});

或者,如果你因某种原因想要它更像你的情况,也许这就是你想要的那种......

    if(location.hash) {
        var locHashParts = location.hash.split('/');
        if(locHashParts.length > 1 && locHashParts[0] == '#!newtopic') {
            var subjectStr = '';
            var bodyStr = '';
            var request = {};
            switch(locHashParts[1]) {
            case 'opencomments-site-discussions':

                request = {
                    url: true,
                    domain: true
                }
                break;
            default:
                break;
            }

            chrome.extension.sendMessage(request, function(response) {
                console.debug('The url is "' + response.url + '" and the domain is"' + response.domain + '"');
                if(repsone.url && response.domain) {
                    runPayloadCode(subjectStr, "URL of last page visited: " + response.domain);
                }
            });

        }
    }
}  

<强>听

chrome.extension.onMessage.addListener(
function(request, sender, sendResponse) {
    var response = {};
    if(request.url) {
        response.url = "someURL";
    }
    if(request.domain) {
        response.domain = "someDomain";
    }
    sendResponse(response);
});

答案 1 :(得分:0)

您可以尝试以下代码:

case 'opencomments-site-discussions':
chrome.extension.sendMessage({
    name: "domain"
},

function (response) {
    subjectStr = response.domain;
  if(subjectStr){
    chrome.extension.sendMessage({
        name: "url"
    },

    function (response) {
        bodyStr = "URL of last page visited: " + response.url;
        if (bodyStr) {
            runPayloadCode(subjectStr, bodyStr);
        }
    });
}
});

但是,您不能将消息的chrome.extension.sendMessage({name: "url"})chrome.extension.sendMessage({name: "domain"})合并为单个消息(因为我看到它们是独立的)并消除多个嵌套?