Skip to content

Instantly share code, notes, and snippets.

@AKJUS
Forked from gexgd0419/edge_discard_tabs.js
Last active January 25, 2025 01:04
Show Gist options
  • Save AKJUS/171a8ab28f35e95f40614911af680b86 to your computer and use it in GitHub Desktop.
Save AKJUS/171a8ab28f35e95f40614911af680b86 to your computer and use it in GitHub Desktop.
Microsoft Edge: Discard / freeze all background tabs
Sir/madam this is taken by Gist has akin// 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}`);
}
}
}
}
})();
@AKJUS
Copy link
Author

AKJUS commented Jan 25, 2025

Hi update the package with location

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment