Skip to content

Instantly share code, notes, and snippets.

@PetarKirov
Last active December 15, 2020 07:48
Show Gist options
  • Save PetarKirov/70f00b19e76f6426ae5b146f7179c2b8 to your computer and use it in GitHub Desktop.
Save PetarKirov/70f00b19e76f6426ae5b146f7179c2b8 to your computer and use it in GitHub Desktop.
Chromium / Google Chrome: Discard / freeze all tabs without closing (working in incognito mode as well)

Discard all tabs in Google Chrome / Chromium

Why?

There are multiple extensions that claim to do this, but I didn't find one that was working in incognito mode.

Steps:

  1. Go to chrome://discards/
  2. Open the DevTools and click on the Console tab (or press [Esc])
  3. Paste whole script and execute it (by pressing [Enter])
  4. Observe that all tabs (excluding the current one) have their Lifecycle State changed to "discarded (urgent) at <time>"
  5. Profit!

image

function discardAllTabs() {
const tbl = document
.querySelector("body > discards-main")
.shadowRoot.querySelector("iron-pages > discards-tab")
.shadowRoot.querySelector("#tab-discards-info-table-body");
if ((tbl?.childElementCount ?? 0) === 0) {
console.log("Error: '#tab-discards-info-table-body' not found.");
console.log(tbl?.childElementCount ?? 0, tbl?.childElementCount, tbl);
return;
}
let discardedTabsCount = 0;
for (const row of tbl.children) {
if (row.childElementCount != 12) continue;
const title = row.children[3].innerText;
if (
(title.match(/Discards/gi)?.length ?? 0) > 0 ||
(title.match(/Dev *Tools/gi)?.length ?? 0) > 0
) {
console.log(`Skipping tab '${title}'`);
continue;
}
console.log(`Discarding tab: '${title}'`);
row.children[11].children[1].click();
discardedTabsCount++;
}
return {
discardedTabsCount,
tableRowsCount: tbl.childElementCount,
};
}
discardAllTabs()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment