I am writing an extension for Google Chrome. The content script never sees that sendNextProfile request has been sent from background page. At least, the messa开发者_开发知识库ge RECEIVED REQUEST FOR NEXT PROFILE never appears on the console log and no new request is seen by background.
HERE IS THE CODE FROM THE CONTENT 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]});
}
});
HERE IS THE CODE FROM THE BACKGROUND 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");
}
});
On the background side, the console message appear as expected, so it appears the request is send. So what's up with this code?
Instead of:
chrome.extension.sendRequest({cmd: "sendNextProfile"});
it should be:
chrome.tabs.sendRequest(tabId, {cmd: "sendNextProfile"});
But if your tab is removed there is no point in sending request to that tab as it doesn't exist already. Maybe you need to send it to some other tab?
精彩评论