开发者

Forcing all open instances of a web-page to refresh in a Google Chrome web-application

开发者 https://www.devze.com 2023-01-29 08:33 出处:网络
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

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, ta开发者_C百科b; 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?


chrome.windows.getAll returns an array of windows, not tabs. Each window contain an array of tabs. I don't remember right now how tabs array is called, I am assuming it is tabs (just dump returned windows into console and check):

chrome.windows.getAll({populate: true}, function(windows) {
    console.log(windows);
    for (var w = 0; w < windows.length; w++) {
        for (var i = 0; i < windows[w].tabs.length; i++) {
            var tab = windows[w].tabs[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"});
            }
        }
    }
});


Instead of iterating all tabs, you can just iterate your "own" extensions opened pages:

var views = chrome.extension.getViews();
for (var i in views) {
  var location = views[i].location;
  if (location.pathname == '/html/application.html') {
    location.reload();
  }
}

The above should work, cleaner, and faster than iterating all windows.


For other searching Google, this code may be of interest to you. I used it to refresh a browser action icon on the active tab of each window, so when the extension updates or reloads, the icon has a status present:

chrome.tabs.query({active: true}, function queryCallback(tabs){
  var length = tabs.length;
  for (var i = 0; i < length; i++) {
    handleTab(tabs[i]);
  }
});
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号