PreventPageVisibility


SUBMITTED BY: ccont2246

DATE: Dec. 22, 2022, 12:43 p.m.

FORMAT: Text only

SIZE: 1.5 kB

HITS: 24225

  1. // ==UserScript==
  2. // @name PreventPageVisibility
  3. // @namespace https://github.com/IceWreck
  4. // @match *://*/*
  5. // @run-at document-start
  6. // @grant none
  7. // @version 1.1
  8. // @author IceWreck
  9. // @description Block websites from knowing if you switched tabs/windows
  10. // ==/UserScript==
  11. // This userscript blocks the page visibility API and to some extent the old blur/focus APIs.
  12. let events_to_block = [
  13. "visibilitychange",
  14. "webkitvisibilitychange",
  15. "mozvisibilitychange",
  16. "hasFocus",
  17. "blur",
  18. "focus",
  19. "mouseleave"
  20. ]
  21. for (event_name of events_to_block) {
  22. document.addEventListener(event_name, function (event) {
  23. event.preventDefault();
  24. event.stopPropagation();
  25. event.stopImmediatePropagation();
  26. }, true);
  27. }
  28. for (event_name of events_to_block) {
  29. window.addEventListener(event_name, function (event) {
  30. event.preventDefault();
  31. event.stopPropagation();
  32. event.stopImmediatePropagation();
  33. }, true);
  34. }
  35. document.hasFocus = function () { return true; };
  36. document.onvisibilitychange = null;
  37. Object.defineProperty(document, "visibilityState", { value: "visible" });
  38. Object.defineProperty(document, "hidden", { value: false });
  39. Object.defineProperty(document, "mozHidden", { value: false });
  40. Object.defineProperty(document, "webkitHidden", { value: false });
  41. Object.defineProperty(document, "webkitVisibilityState", { value: "visible" });

comments powered by Disqus