Always on focus

Prevents websites from knowing that you switched tabs or unfocused the window

  1. // ==UserScript==
  2. // @name Always on focus
  3. // @namespace https://github.com/daijro/always-on-focus
  4. // @author daijro
  5. // @version 1.5.3
  6. // @description Prevents websites from knowing that you switched tabs or unfocused the window
  7. // @include *
  8. // @run-at document-start
  9. // ==/UserScript==
  10.  
  11.  
  12. unsafeWindow.onblur = null;
  13. unsafeWindow.blurred = false;
  14.  
  15. unsafeWindow.document.hasFocus = () => true;
  16. unsafeWindow.window.onFocus = () => true;
  17.  
  18. // kill dom property names
  19. [
  20. "hidden",
  21. "mozHidden",
  22. "msHidden",
  23. "webkitHidden"
  24. ].forEach(prop_name => {
  25. Object.defineProperty(document, prop_name, {value: false});
  26. })
  27.  
  28. Object.defineProperty(document, "visibilityState", {get: () => "visible"});
  29. Object.defineProperty(document, "webkitVisibilityState", {get: () => "visible"});
  30.  
  31. unsafeWindow.document.onvisibilitychange = undefined;
  32.  
  33.  
  34. // element constructors to allow blur events on
  35. const blurWhitelist = [
  36. HTMLInputElement,
  37. HTMLAnchorElement,
  38. HTMLSpanElement,
  39. HTMLParagraphElement,
  40. ]
  41.  
  42. // element constructors to block mouseleave and mouseout events on
  43. const hoverBlacklist = [
  44. HTMLIFrameElement,
  45. HTMLHtmlElement,
  46. HTMLBodyElement,
  47. HTMLHeadElement,
  48. HTMLFrameSetElement, // obsolete but included for completeness
  49. HTMLFrameElement // obsolete but included for completeness
  50. ];
  51.  
  52. var event_handler = (event) => {
  53. // if the event is blur, and the target is an whitelisted type, allow it
  54. if (event.type === 'blur' &&
  55. ((blurWhitelist.some(type => event.target instanceof type) ||
  56. event.target.classList?.contains('ql-editor')))) { // quill js fix
  57. return;
  58. }
  59. // if the event is mouseleave or mouseout, and the target is an blacklisted type, block it
  60. if (['mouseleave', 'mouseout'].includes(event.type) &&
  61. !hoverBlacklist.some(type => event.target instanceof type)) {
  62. return;
  63. }
  64. event.preventDefault();
  65. event.stopPropagation();
  66. event.stopImmediatePropagation();
  67. }
  68.  
  69. // kill event listeners
  70. [
  71. "visibilitychange",
  72. "webkitvisibilitychange",
  73. "blur",
  74. "hasFocus",
  75. "mouseleave",
  76. "mouseout",
  77. "mozvisibilitychange",
  78. "msvisibilitychange"
  79. ].forEach(event_name => {
  80. window.addEventListener(event_name, event_handler, true);
  81. document.addEventListener(event_name, event_handler, true);
  82. })

QingJ © 2025

镜像随时可能失效,请加Q群300939539或关注我们的公众号极客氢云获取最新地址