【发布时间】:2011-06-19 09:14:47
【问题描述】:
我正在为 Google Chrome 编写扩展程序。内容脚本永远不会看到已从后台页面发送 sendNextProfile 请求。至少,消息 RECEIVED REQUEST FOR NEXT PROFILE 永远不会出现在控制台日志中,并且后台没有看到任何新请求。
这是内容脚本中的代码
//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");
}
});
在后台,控制台消息按预期显示,因此看起来请求已发送。那么这段代码是怎么回事?
【问题讨论】:
-
On the background side, the console message appear as expected.究竟出现了什么消息?标签已关闭或请求新的配置文件或两者兼而有之?
标签: google-chrome-extension messaging