0

在我的扩展程序的内容脚本中,我从 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 的最佳方法是什么?

4

2 回答 2

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);
});
于 2012-12-01T18:29:59.043 回答
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"})并消除多重嵌套吗?

于 2012-12-01T17:17:25.347 回答