翻译正在进行中。

丢弃一个或多个标签页。

一些浏览器会自动“丢弃”它们认为近期不再被用户所需要的标签页。这些标签页会在标签栏中保持可见,浏览器会记住它们的状态,所以,如果用户选中了被丢弃的标签页,它会立即还原到被丢弃之前的状态。

对于不同的浏览器,被丢弃内容的详细内容是有所不同的,但是从大体上来说,丢弃一个标签页允许浏览器释放一些该标签页所占用的内存。

The tabs.discard() API enables an extension to discard one or more tabs. It's not possible to discard the currently active tab, or a tab whose document contains a beforeunload listener that would display a prompt.

This is an asynchronous function that returns a Promise.

语法

var discarding = browser.tabs.discard(
  tabIds          // integer or integer array
)

Parameters

tabIds
integer or array of integer. The IDs of the tab or tabs to discard.

Return value

A Promise that will be fulfilled with no arguments when all the specified tabs have been discarded. If any error occurs (for example, invalid tab IDs), the promise will be rejected with an error message.

If the ID of the active tab is passed in, it will not be discarded, but the promise will be fulfilled and any other tabs passed in will be discarded.

浏览器兼容性

ChromeEdgeFirefoxFirefox for AndroidOpera
Basic support541 2 3 41458 No Yes1 2 3 4

1. Only accepts a single tab ID as a parameter, not an array.

2. The tab ID argument is optional: if it is omitted, the browser discards the least important tab.

3. The callback is passed a Tab object representing the tab that was discarded.

4. Tabs whose document contains a beforeunload listener that displays a prompt will be discarded.

示例

丢弃一个标签页:

function onDiscarded() {
  console.log(`Discarded`);
}

function onError(error) {
  console.log(`Error: ${error}`);
}

var discarding = browser.tabs.discard(2);
discarding.then(onDiscarded, onError);

丢弃多个标签页:

function onDiscarded() {
  console.log(`Discarded`);
}

function onError(error) {
  console.log(`Error: ${error}`);
}

var discarding = browser.tabs.discard([15, 14, 1]);
discarding.then(onDiscarded, onError);

Acknowledgements

This API is based on Chromium's chrome.tabs API.

文档标签和贡献者

此页面的贡献者: RainSlide
最后编辑者: RainSlide,