tabs.discard()

Discard one or more tabs.

Some browsers automatically "discard" unused tabs to free memory. Discarded tabs stay visible in the tabstrip. The browser remembers the tab's state and restores it when the user selects the tab. The details of when tabs are and what is discarded are browser-specific.

You can control whether the browser or this API discards a tab by setting its autoDiscardable property to false in tabs.update. This setting prevents the browser from discarding the tab. The tab can then only be discarded with this API.

It's not possible to discard the active tab or a tab whose document contains a beforeunload listener that would display a prompt.

这是一个异步函数,返回一个 Promise

语法

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

参数

tabIds

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

返回值

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.

示例

Discard a single tab

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

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

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

Discard multiple tabs

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

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

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

浏览器兼容性

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