Created
April 14, 2025 03:48
-
-
Save ryancat/a97866ee8b98e949228a596f9722cde6 to your computer and use it in GitHub Desktop.
Simple chrome extension script to do what "The Great Suspender" does
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const SUSPEND_AFTER_MS = 5 * 60 * 1000; // 5 minutes | |
const tabActivity = new Map(); | |
let lastActiveTabId = null; | |
chrome.tabs.onActivated.addListener(activeInfo => { | |
if (lastActiveTabId !== null) { | |
// Reset the last active tab's activity time | |
tabActivity.set(lastActiveTabId, Date.now()); | |
} | |
// Update the last active tab ID | |
lastActiveTabId = activeInfo.tabId; | |
}); | |
setInterval(() => { | |
chrome.tabs.query({}, tabs => { | |
const now = Date.now(); | |
for (const tab of tabs) { | |
const isEligible = | |
!tab.active | |
&& !tab.pinned | |
&& !tab.discarded | |
&& tab.status !== "unloaded" | |
&& tab.url.startsWith("http") | |
if (isEligible) { | |
if (!tabActivity.has(tab.id)) { | |
tabActivity.set(tab.id, now); | |
continue; | |
} | |
if (tabActivity.get(tab.id) == null) { | |
// This tab has been suspended already | |
continue; | |
} | |
const lastActive = tabActivity.get(tab.id); | |
if (now - lastActive > SUSPEND_AFTER_MS) { | |
chrome.tabs.discard(tab.id); | |
tabActivity.set(tab.id, null); // Mark as suspended | |
} | |
} | |
} | |
}); | |
}, 60 * 1000); // Check every minute |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment