I’ve been trying to make a JavaScript function for a Google Chrome web-application that checks to see if there are any open instances of the application, and if there are, forces them to refresh the page.
My original code is as follows:
chrome.tabs.getAllInWindow(undefined, function(tabs) {
for (var i = 0, tab; tab = tabs[i]; i++) {
if (tab.url == "chrome-extension://" + chrome.i18n.getMessage("@@extension_id") + "/html/application.html") {
chrome.tabs.update(tab.id, {url: "chrome-extension://" + chrome.i18n.getMessage("@@extension_id") + "/html/application.html"});
}
}
});
But this only works for all tabs inside the current window. If there are instances in their own window or in another window, they will not be refreshed.
I adapted it in an attempt to make it work for all open pages, and not just those in the currently selected window like so:
chrome.windows.getAll({populate: true}, function(tabs) {
for (var i = 0, tab; tab = tabs[i]; i++) {
if (tab.url == "chrome-extension://" + chrome.i18n.getMessage("@@extension_id") + "/html/application.html") {
chrome.tabs.update(tab.id, {url: "chrome-extension://" + chrome.i18n.getMessage("@@extension_id") + "/html/application.html"});
}
}
});
While the new code does not return an error in the JavaScript console, it does not seem to do what it is supposed to do; refresh any open instances of the application page.
Have I misunderstood the windows.getAll module? Can anybody offer a working solution?
Instead of iterating all tabs, you can just iterate your “own” extensions opened pages:
The above should work, cleaner, and faster than iterating all windows.