Google chrome extension chrome扩展连接问题

Google chrome extension chrome扩展连接问题,google-chrome-extension,Google Chrome Extension,我已经为google chrome编写了一个扩展,我有一个bug需要帮助解决。 我所做的是在flickr上使用文本选择或文本搜索输入来搜索照片,然后创建一个结果选项卡。 扩展在大多数情况下都有效。但有时它会创建一个没有结果的空白选项卡,当我重复相同的搜索时,它会显示结果。我认为这与html文件的消息传递有关,可能与它们的通信有关。我必须说,我总是从flickr接收结果,这样flickr的请求/响应就可以正常工作。有时,当我在等待结果的同时玩其他选项卡或在其他选项卡上执行某些操作时,会发生错误。你

我已经为google chrome编写了一个扩展,我有一个bug需要帮助解决。 我所做的是在flickr上使用文本选择或文本搜索输入来搜索照片,然后创建一个结果选项卡。 扩展在大多数情况下都有效。但有时它会创建一个没有结果的空白选项卡,当我重复相同的搜索时,它会显示结果。我认为这与html文件的消息传递有关,可能与它们的通信有关。我必须说,我总是从flickr接收结果,这样flickr的请求/响应就可以正常工作。有时,当我在等待结果的同时玩其他选项卡或在其他选项卡上执行某些操作时,会发生错误。你能帮我找出毛病在哪里吗

背景文件:

function searchSelection(info,tab){
var updated;
    if(info.selectionText==null){
        var value = prompt("Search Flickr", "Type in the value to search"); 
        updated=makeNewString(value);
    }
    else{
        updated=makeNewString(info.selectionText);
    }
    var resultHtml;
    var xhReq = new XMLHttpRequest();
    xhReq.open(
    "GET",
    "http://api.flickr.com/services/rest/?method=flickr.photos.search&text="+updated+
    "&api_key=a0a60c4e0ed00af8d70800b0987cae70&content_type=7&sort=relevance&per_page=500",
    true);
    xhReq.onreadystatechange = function () {

        if (xhReq.readyState == 4) {
            if (xhReq.status == 200) {
            chrome.tabs.executeScript(tab.id, {code:"document.body.style.cursor='auto';"});
                var photos = xhReq.responseXML.getElementsByTagName("photo");
                if(photos.length==0){
                    alert("No results found for this selection");
                    chrome.tabs.executeScript(tab.id, {code:"document.body.style.cursor='auto';"});
                    return;
                }
                var myJSPhotos=[];
                for(var i=0; i<photos.length; i++){
                    var data={"id":photos[i].getAttribute("id"),"owner":photos[i].getAttribute("owner"),
                                "secret":photos[i].getAttribute("secret"),"server":photos[i].getAttribute("server"),
                                "farm":photos[i].getAttribute("farm"),"title":photos[i].getAttribute("title")};
                    myJSPhotos[i]=data;
                }

                    chrome.tabs.create({"url":"results.html"},function(thistab){
                    var port= chrome.tabs.connect(thistab.id);
                    port.postMessage({photos:myJSPhotos});
                });

            }
        };
    };
    xhReq.send(null);
    chrome.tabs.executeScript(tab.id, {code:"document.body.style.cursor='wait';"});

}


var context="selection";
var id = chrome.contextMenus.create({"title": "search Flickr", "contexts":[context,'page'],"onclick":searchSelection});
我必须提到,当选项卡为空时,如果我在
//******/
部分上添加了警报,则不会显示 开火。 但是,当我在选项卡创建回调函数部分打印出
photos.length
时,它会打印出正确的结果。

尝试在清单中为您的
res.js
设置
“run\u at”:“document\u start”
选项

我认为,
chrome.tabs.create
的回调会在不等待页面脚本加载的情况下立即启动,因此您可以尝试以下方法:

//global vars
var createdTabId = null;
var myJSPhotos = null;

xhReq.onreadystatechange = function () {

    //assign myJSPhotos to a global var

    chrome.tabs.create({"url":"results.html"},function(thistab){
        createdTabId = thistab.id;
    });
}

chrome.tabs.onUpdated.addListener(function(tabId, changeInfo, tab) {
    if(changeInfo.status == "complete" && tab.id == createdTabId) {
        createdTabId = null;

        //now page is loaded and content scripts injected
        var port = chrome.tabs.connect(tab.id);
        port.postMessage({photos:myJSPhotos});

    }
});

请提供此部分的代码:
//这是我向flickr发送请求并将结果编辑为JSon的地方
,但为什么大部分时间它工作正常并在新页面上显示图像,而只有一小部分时间显示空白页面?我的清单:{“名称”:“Flickr照片搜索”,“版本”:“1.0”,“描述”:“我做的第一个扩展”,“图标”:{“16”:“icon.png”},“背景页面”:“background.html”,“权限”:[“tabs”,“*/*”,“https://*/*”]}@mary我认为这是因为创建选项卡时的回调是在不等待内容脚本加载的情况下启动的,因此,当您通过端口发布消息时,内容脚本可能尚未收到。正如您所见,我根本没有定义内容脚本,但这是因为我动态创建了扩展名文件夹中的html文件,在这种情况下,我不知道如何创建“匹配”在内容脚本中。@mary好的,我现在才意识到res.js不是一个内容脚本,它只是一个包含在页面上的脚本。在这种情况下,
“run\u at”:“document\u start”
是不需要的,但是原始答案的第二部分仍然值得尝试。
//global vars
var createdTabId = null;
var myJSPhotos = null;

xhReq.onreadystatechange = function () {

    //assign myJSPhotos to a global var

    chrome.tabs.create({"url":"results.html"},function(thistab){
        createdTabId = thistab.id;
    });
}

chrome.tabs.onUpdated.addListener(function(tabId, changeInfo, tab) {
    if(changeInfo.status == "complete" && tab.id == createdTabId) {
        createdTabId = null;

        //now page is loaded and content scripts injected
        var port = chrome.tabs.connect(tab.id);
        port.postMessage({photos:myJSPhotos});

    }
});