tabs.warmup() 编辑

To optimize system resource usage, browsers may drop GPU resources associated with tabs that the user has not accessed for a certain amount of time. If a browser has done this for a tab, then reactivating the tab (for example, when the user switches to it) may take longer than it normally would.

The tabs.warmup() API enables an extension to start the process of rendering the resources for an inactive tab, if the extension expects that the user might switch to the tab soon. This then makes the actual tab switch faster than it would be otherwise.

Note this API does not work on discarded tabs and does not need to be called immediately prior to switching tabs. It is merely a performance improvement when the tab switch can be anticipated, such as when hovering over a button that when clicked would switch to the tab.

It is expected that this API would mostly be useful to tab management extensions.

This is an asynchronous function that returns a Promise.

Syntax

var warming = browser.tabs.warmup(
  tabId               // integer
)

Parameters

tabId
integer. ID of the tab to warm up. If the argument passed here is not an integer (in particular, if it is null or undefined) then warmup() will throw an exception synchronously.

Return value

A Promise that will be fulfilled with no arguments if the tab identified by tabId is sucessfully warmed up. If tabId does not identify an open tab, or if warming up fails for some other reason, then the promise will be rejected with an error message.

Examples

This code adds a listener to the browserAction.onClicked event. The listener retrieves all tabs in the current window that contain pages under "/wiki/" and warms up the first one that it finds.

function onSuccess() {
  console.log("success!");
}

function onFailure(error) {
  // e.g. ID of a nonexistent tab
  console.error(error);
}

async function warmupMDN() {

  const mdnTabs = await browser.tabs.query({
    currentWindow: true,
    url: "/wiki/*"
  });

  if (mdnTabs.length > 0) {
    const warming = browser.tabs.warmup(mdnTabs[0].id);
    warming.then(onSuccess, onFailure);
  }

}

browser.browserAction.onClicked.addListener(warmupMDN);

Browser compatibility

BCD tables only load in the browser

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

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

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据

词条统计

浏览:82 次

字数:3187

最后编辑:7 年前

编辑次数:0 次

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