Google chrome extension google chrome扩展消息背景到上下文

Google chrome extension google chrome扩展消息背景到上下文,google-chrome-extension,messaging,Google Chrome Extension,Messaging,我正在为谷歌浏览器写一个扩展。内容脚本从未看到sendNextProfile请求已从后台页面发送。至少,控制台日志上从未出现接收到的请求下一个配置文件的消息,后台也没有看到新的请求 以下是内容脚本中的代码 //send request for first profile var currentProfile=0; chrome.extension.sendRequest({cmd: "openProfile", url: profileLinks[currentProfile]}); //li

我正在为谷歌浏览器写一个扩展。内容脚本从未看到sendNextProfile请求已从后台页面发送。至少,控制台日志上从未出现接收到的请求下一个配置文件的消息,后台也没有看到新的请求

以下是内容脚本中的代码

//send request for first profile
var currentProfile=0;
chrome.extension.sendRequest({cmd: "openProfile", url: profileLinks[currentProfile]});

//listen for request to send next profile
chrome.extension.onRequest.addListener(function(request, sender, sendResponse) {
    if(request.cmd == "sendNextProfile") {
        console.log("RECEIVED REQUEST FOR NEXT PROFILE");
        ++currentProfile;
        chrome.extension.sendRequest({cmd: "openProfile", url: profileLinks[currentProfile]});
    }
});
下面是背景页面中的代码

//detect when message tab is closed and request new profile
//var closedTabId=null;
chrome.tabs.onRemoved.addListener(function(tabid, removeInfo) { 
    console.log("TAB CLOSED "+tabid);
    if (tabid==msgTabId) {
        chrome.extension.sendRequest({cmd: "sendNextProfile"});
        console.log("REQUESTED NEW PROFILE");
    }   
});
在后台,控制台消息按预期显示,因此请求似乎已发送。那么这个代码是怎么回事

而不是:

chrome.extension.sendRequest({cmd: "sendNextProfile"});
应该是:

chrome.tabs.sendRequest(tabId, {cmd: "sendNextProfile"});
但是,如果您的选项卡被删除,那么就没有必要向该选项卡发送请求,因为它还不存在。也许你需要把它发送到其他选项卡

而不是:

chrome.extension.sendRequest({cmd: "sendNextProfile"});
应该是:

chrome.tabs.sendRequest(tabId, {cmd: "sendNextProfile"});

但是,如果您的选项卡被删除,那么就没有必要向该选项卡发送请求,因为它还不存在。也许你需要把它发送到其他选项卡

在后台,控制台消息按预期显示。
确切显示的消息是什么?选项卡关闭或请求新配置文件或两者都关闭?
在后台,控制台消息按预期显示。
确切显示的消息是什么?选项卡关闭或请求新配置文件或两者都关闭?实际上搜索页面仍应打开。但是后台没有打开它,所以后台还没有学会搜索页面的tabid。这里的误解是,消息被定向到选项卡,而不是广播给任何选项卡听。但是考虑到这一点,更好的方法是将整个搜索结果数组发送到后台,并让后台控制处理,因为搜索页面不需要做任何事情,只需要将链接收集到一个数组中。实际上,搜索页面应该仍然是打开的。但是后台没有打开它,所以后台还没有学会搜索页面的tabid。这里的误解是,消息被定向到选项卡,而不是广播给任何选项卡听。但是考虑到这一点,更好的方法是将整个搜索结果数组发送到后台,并让后台控制处理,因为搜索页面只需将链接收集到一个数组中即可。