Last active
March 6, 2025 20:04
-
-
Save azdanov/f0e30fb4f95d770c97879cc8a7d31abc to your computer and use it in GitHub Desktop.
Reloads Facebook and Messenger pages when leaving the tab to prevent high CPU usage
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
// ==UserScript== | |
// @name Facebook & Messenger Auto-Reloader | |
// @namespace http://tampermonkey.net/ | |
// @version 1.1 | |
// @description Reloads Facebook and Messenger pages when leaving the tab to prevent high CPU usage | |
// @author Me | |
// @match https://*.facebook.com/* | |
// @match https://*.messenger.com/* | |
// @grant none | |
// @run-at document-idle | |
// ==/UserScript== | |
(function() { | |
'use strict'; | |
// Configuration | |
const DEBUG = false; // Set to true to see log messages | |
const RELOAD_DELAY = 10; // Seconds to wait before reloading after leaving the tab | |
// Variable to store the reload timeout | |
let reloadTimeout = null; | |
// Logging utility | |
const log = (message) => { | |
if (DEBUG) { | |
console.log(`[FB/Messenger Reloader] ${message}`); | |
} | |
}; | |
// Function to handle visibility change | |
const handleVisibilityChange = () => { | |
if (document.hidden) { | |
// Schedule the reload when the user leaves | |
log(`Tab hidden, scheduling reload in ${RELOAD_DELAY} seconds`); | |
reloadTimeout = setTimeout(() => { | |
log('Executing scheduled reload'); | |
window.location.reload(); | |
}, RELOAD_DELAY * 1000); | |
} else { | |
// Cancel the reload if the user returns before the timeout expires | |
if (reloadTimeout) { | |
log('Tab visible again, cancelling scheduled reload'); | |
clearTimeout(reloadTimeout); | |
reloadTimeout = null; | |
} | |
} | |
}; | |
// Check if the Page Visibility API is supported | |
if (typeof document.hidden !== "undefined") { | |
log('Script initialized'); | |
document.addEventListener('visibilitychange', handleVisibilityChange, false); | |
} else { | |
log('Page Visibility API not supported'); | |
} | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment