Always on focus

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

当前为 2023-04-19 提交的版本,查看 最新版本

  1. // ==UserScript==
  2. // @name Always on focus
  3. // @namespace https://github.com/daijro/always-on-focus
  4. // @author daijro
  5. // @version 1.2
  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", value: "visible"});
  29. Object.defineProperty(document, "webkitVisibilityState", {get: () => "visible", value: "visible"});
  30.  
  31. unsafeWindow.document.onvisibilitychange = undefined;
  32.  
  33.  
  34. var event_handler = (event) => {
  35. if (event.type === 'blur' && event.target instanceof HTMLInputElement) {
  36. return // exclude input elements
  37. }
  38. event.preventDefault();
  39. event.stopPropagation();
  40. event.stopImmediatePropagation();
  41. }
  42.  
  43. // kill event listeners
  44. [
  45. "visibilitychange",
  46. "webkitvisibilitychange",
  47. "blur",
  48. "mozvisibilitychange",
  49. "msvisibilitychange"
  50. ].forEach(event_name => {
  51. window.addEventListener(event_name, event_handler, true);
  52. document.addEventListener(event_name, event_handler, true);
  53. })