强制刷新 Google Chrome 网络应用程序中网页的所有打开实例
我一直在尝试为 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
chrome.windows.getAll
返回窗口数组,而不是选项卡。每个窗口都包含一组选项卡。我现在不记得 tabs 数组是如何调用的,我假设它是tabs
(只需将返回的窗口转储到控制台并检查):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 istabs
(just dump returned windows into console and check):您可以只迭代“自己的”扩展打开的页面,而不是迭代所有选项卡:
上面的内容应该比迭代所有窗口更有效、更干净、更快。
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.
对于其他搜索 Google,您可能会对这段代码感兴趣。我用它来刷新每个窗口的活动选项卡上的浏览器操作图标,因此当扩展更新或重新加载时,该图标的状态如下:
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: