Hide Mouse Cursor

Hide Mouse cursor when not in use/Show when in use

  1. // ==UserScript==
  2. // @name Hide Mouse Cursor
  3. // @namespace https://greasyfork.org/users/45933
  4. // @version 0.0.2
  5. // @author Fizzfaldt
  6. // @license MIT
  7. // @description Hide Mouse cursor when not in use/Show when in use
  8. // @run-at document-end
  9. // @grant none
  10. // @noframes
  11. // @match *://*/*
  12. // ==/UserScript==
  13.  
  14. /* jshint esversion: 6 */
  15. (function () {
  16. let hidden = false;
  17.  
  18. const styleEl = document.createElement("style");
  19. // Use some poop clowns to get a high selector specificity so we can override
  20. // other selectors in case they also use !important.
  21. const poopClowns = ":not(#💩🤡)".repeat(20);
  22.  
  23. // Height 100% sometimes needed for firefox to hide cursor
  24. styleEl.textContent = `
  25. html, body {
  26. height: 100%;
  27. }
  28. ${poopClowns} {
  29. cursor: none !important ;
  30. }
  31. `;
  32.  
  33. function hideHandler() {
  34. if (hidden) return;
  35. if (document.activeElement.tagName == "TEXTAREA") {
  36. // Do nothing when inside a text field.
  37. return;
  38. }
  39. hidden = true;
  40. document.head.append(styleEl);
  41. }
  42.  
  43. function showHandler() {
  44. if (!hidden) return;
  45. hidden = false;
  46. styleEl.remove();
  47. }
  48.  
  49. const scrollEl = document.scrollingElement;
  50. const hideEvents = ["scroll", "keyup"];
  51. const showEvents =
  52. (
  53. "PointerEvent" in window
  54. ) ? ["pointerdown", "pointermove"]
  55. : ["mousedown", "mousemove", "touchstart", "touchmove", "wheel"];
  56. const options = {capture: true, passive: true};
  57.  
  58. for (const event of hideEvents) {
  59. scrollEl.addEventListener(event, hideHandler, options);
  60. }
  61.  
  62. for (const event of showEvents) {
  63. scrollEl.addEventListener(event, showHandler, options);
  64. }
  65. })();