Skip to content

Instantly share code, notes, and snippets.

@ArthurKun21
Last active February 5, 2024 02:54
Show Gist options
  • Save ArthurKun21/00cba91929681537e37641172cc0dd5e to your computer and use it in GitHub Desktop.
Save ArthurKun21/00cba91929681537e37641172cc0dd5e to your computer and use it in GitHub Desktop.
// ==UserScript==
// @name Always On Focus
// @namespace http://tampermonkey.net/
// @version 0.1
// @description Prevent websites from detecting when you switch tabs or unfocus the window
// @author You
// @match *://*/*
// @grant none
// ==/UserScript==
(function() {
// Store the initial state of document.hasFocus
const originalHasFocus = document.hasFocus;
// Override document.hasFocus based on window focus
window.addEventListener('focus', function() {
document.hasFocus = function() { return true; };
});
window.addEventListener('blur', function() {
document.hasFocus = function() { return false; };
});
// Restore the original behavior after the script runs
setTimeout(function() {
document.hasFocus = originalHasFocus;
}, 0);
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment