Skip to content

Instantly share code, notes, and snippets.

@gexgd0419
Created March 30, 2024 09:02
Show Gist options
  • Save gexgd0419/97c6b7a9f4c90bed5b367d78b00cc062 to your computer and use it in GitHub Desktop.
Save gexgd0419/97c6b7a9f4c90bed5b367d78b00cc062 to your computer and use it in GitHub Desktop.
Microsoft Edge: Discard / freeze all background tabs
// For tab hoarders using Microsoft Edge (this might work on other Chromiums, but I haven't tested)
// who keep so many tabs that even edge://discards/ page becomes unresponsive.
// Can discard/freeze all background hidden tabs. Foreground (visible) tabs, tabs playing audio,
// and tabs in the "Never put these sites to sleep" list will not be discarded/frozen.
// In the address bar, enter: edge://discards/discards.js
// Then open DevTools (Ctrl+Shift+I or F12), go to Console, and paste the following code.
await (async () => {
// To freeze tabs, set to false; to discard tabs, set to true
const DISCARD_INSTEAD_OF_FREEZE = true;
// Change this if you want to prevent the last X active tabs from being frozen/discarded
const TAB_PRESERVE_COUNT = 0;
// The "cannot freeze reasons" containing the following words will be ignored
// Other reasons, such as "the tab is emitting audio", will be respected so background players won't be closed
const ignoredWords = ["BrowsingInstance", "IndexedDB lock", "notification permission", "WebLock"]
let discards = await import("//discards/discards.js");
let provider = discards.getOrCreateDetailsProvider();
let tabinfos = (await provider.getTabDiscardsInfo()).infos;
const reasonNotIgnorable = reason => !ignoredWords.some(word => reason.includes(word));
for (let i = 0; i < tabinfos.length - TAB_PRESERVE_COUNT; i++) {
let tab = tabinfos[i];
if (tab.loadingState == 2 // loaded
&& tab.visibility == 0 // hidden
&& tab.isAutoDiscardable
&& (DISCARD_INSTEAD_OF_FREEZE || tab.state == 0) // active (do not freeze if already frozen)
) {
if (tab.cannotFreezeReasons.some(reasonNotIgnorable)) { // not ignorable reason
console.log(`Tab ${tab.id} ${tab.title} cannot be ${DISCARD_INSTEAD_OF_FREEZE ? "discarded" : "frozen"} because: `
+ tab.cannotFreezeReasons.filter(reasonNotIgnorable).join(", "));
} else {
if (DISCARD_INSTEAD_OF_FREEZE) {
provider.discardById(tab.id);
console.log(`Discarded tab ${tab.id} ${tab.title}`);
} else {
provider.freezeById(tab.id);
console.log(`Frozen tab ${tab.id} ${tab.title}`);
}
}
}
}
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment