强制刷新 Google Chrome 网络应用程序中网页的所有打开实例

发布于 2024-10-06 17:35:22 字数 1152 浏览 2 评论 0原文

我一直在尝试为 Google Chrome Web 应用程序创建一个 JavaScript 函数,用于检查该应用程序是否有任何打开的实例,如果有,则强制它们刷新页面。

我的原始代码如下:

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"});
        }
    }
});

但这仅适用于当前窗口内的所有选项卡。如果在自己的窗口或另一个窗口中有实例,则不会刷新它们。

我对其进行了调整,试图使其适用于所有打开的页面,而不仅仅是当前选定窗口中的页面,如下所示:

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"});
        }
    }
});

虽然新代码不会在 JavaScript 控制台中返回错误,但它似乎并没有执行应有的操作应该做的;刷新应用程序页面的所有打开的实例。

我是否误解了 windows.getAll 模块?有人能提供可行的解决方案吗?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

靖瑶 2024-10-13 17:35:22

chrome.windows.getAll 返回窗口数组,而不是选项卡。每个窗口都包含一组选项卡。我现在不记得 tabs 数组是如何调用的,我假设它是 tabs (只需将返回的窗口转储到控制台并检查):

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"});
            }
        }
    }
});

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"});
            }
        }
    }
});
相思碎 2024-10-13 17:35:22

您可以只迭代“自己的”扩展打开的页面,而不是迭代所有选项卡:

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

上面的内容应该比迭代所有窗口更有效、更干净、更快。

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.

仙气飘飘 2024-10-13 17:35:22

对于其他搜索 Google,您可能会对这段代码感兴趣。我用它来刷新每个窗口的活动选项卡上的浏览器操作图标,因此当扩展更新或重新加载时,该图标的状态如下:

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

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]);
  }
});
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文