Here is my problem: I'm developing a Chrome extension for Gmail and needs to apply some changes when the extension is updated.
For example, I want to be sure that the extension will display an alert dialog within Gmail after the extension has been updated. This implies to check whether Gmail is already open in a Chrome window at the time of the update, and if not to create a listener in order to wait for Gmail to be loaded in the future and then to display the alert dialog.
The code below does just that. However, I cannot manage to remove the listener (chrome.tabs.onUpdated.removeListener?) when a new Gmail tab has been discovered.
background.js
// [...] code before // Reload Tabs where Gmail is active function reloadTab(order) { chrome.windows.getCurrent(function(win) { var cwin = win.id; chrome.tabs.query({windowId: cwin}, function(tabs) { var countGmailTabs = 0; for (var i = 0; i < tabs.length; i++) { var t = tabs[i].url; if (t.match('mail.google.com')) { countGmailTabs += 1; if (order === 'set') { chrome.tabs.reload(tabs[i].id); } else if (order !== 'set') { var GmailTab = tabs[i]; NewGmailURL(order, GmailTab); return; } } } // Gmail not found ! if (countGmailTabs < 1 && order !== 'set') { chrome.tabs.onUpdated.addListener(function(tabId, changeInfo, tab) { if ( tab.url.match('mail.google.com') && changeInfo.status === 'loading' ) { NewGmailURL('update', tab); return; // Everything OK, now remove listener. How? } }); } }); }); } // [...] Thanks for your help!
For information, the alert dialog is displayed thanks to URL parameters.
// Modify Gmail URL var updated = false; function NewGmailURL(param, ztab) { if (param === 'updated') { urlMyExtension = ztab.url.replace(urlGmail + '?query=update&', urlGmail +'?query=' + param + '&'); } else { urlMyExtension = ztab.url.replace(urlGmail, urlGmail + '?query=' + param + '&'); } if (!done && param !== 'updated') { chrome.tabs.update(ztab.id, { url: urlMyExtension, highlighted: ztab.highlighted }, null); updated = true; } } [UPDATED] ANSWER
In case anyone meets the same problem, here is the answer thanks to @ExpertSystem:
// Listen to Tabs URL function myListener(tabId, info, tab) { if (tab.url.match("mail.google.com") && (info.status === "loading")) { NewGmailURL("update", tab); /* Now, let's relieve ourselves from our listener duties */ chrome.tabs.onUpdated.removeListener(myListener); return; } } // Reload Tabs where Gmail is active function reloadTab(order) { chrome.tabs.query({ currentWindow: true }, function(tabs) { var countGmailTabs = 0; for (var i = 0; i < tabs.length; i++) { var t = tabs[i].url; if (t.match('mail.google.com')) { countGmailTabs += 1; if (order === 'set') { chrome.tabs.reload(tabs[i].id); } else if (order !== 'set') { var GmailTab = tabs[i]; NewGmailURL(order, GmailTab); return; } } } // Gmail not found ! if (countGmailTabs < 1 && order !== 'set') { chrome.tabs.onUpdated.addListener(GmailListener); } }); }