Skip to content

Instantly share code, notes, and snippets.

@ryancat
Created April 14, 2025 03:48
Show Gist options
  • Save ryancat/a97866ee8b98e949228a596f9722cde6 to your computer and use it in GitHub Desktop.
Save ryancat/a97866ee8b98e949228a596f9722cde6 to your computer and use it in GitHub Desktop.
Simple chrome extension script to do what "The Great Suspender" does
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