2011-06-19 43 views
1

我正在为谷歌浏览器写一个扩展。内容脚本从不会看到sendNextProfile请求已从后台页面发送。至少,RECEIVED REQUEST FOR NEXT PROFILE不会出现在控制台日志中,并且背景中不会看到新的请求。谷歌铬扩展消息背景到上下文

该处从内容SCRIPT

//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]}); 
    } 
}); 

此处的代码从背景PAGE

//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"); 
    } 
}); 

在背景侧的代码中,出现在控制台消息作为预期的,所以会出现的请求发送。那么这个代码是怎么回事?

+0

'在背景方面,控制台消息按预期显示。' 什么消息完全显示? TAB CLOSED或请求新的配置文件或两者兼而有之? – Achshar

回答

1

相反的:

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

它应该是:

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

但是如果你的标签被删除存在发送请求到该选项卡,因为它已经不存在任何问题。也许你需要将它发送到其他标签?

+0

实际上,搜索页面仍应该打开。但背景没有打开,所以背景还没有学习到搜索页面的tabid。这里的误解是这样的想法,即该消息被定向到标签而不是广播以供任何标签听到。但考虑到它,更好的方法是将整个搜索结果数组发送到后台,并让后台控制处理,因为不需要搜索页面进行任何操作,而只是将链接收集到数组中。 – Jerome