Scrollbar Hider

Hides scrollbars globally but keeps scrolling functionality

当前为 2024-09-10 提交的版本,查看 最新版本

  1. // ==UserScript==
  2. // @name Scrollbar Hider
  3. // @description Hides scrollbars globally but keeps scrolling functionality
  4. // @author SSL-ACTX
  5. // @version 1.0.2
  6. // @license MIT
  7. // @grant none
  8. // @run-at document-start
  9. // @match *://*/*
  10. // @namespace https://greasyfork.org/users/1365732
  11. // ==/UserScript==
  12.  
  13. (function() {
  14. 'use strict';
  15.  
  16. // CSS rules to hide scrollbars but retain scrolling functionality
  17. const scrollbarHiderCSS = `
  18. /* Remove WebKit-based browsers' scrollbars */
  19. *::-webkit-scrollbar {
  20. width: 0;
  21. height: 0;
  22. }
  23.  
  24. /* Hide scrollbars in Firefox, IE, and Edge */
  25. * {
  26. scrollbar-width: none;
  27. -ms-overflow-style: none;
  28. }
  29. `;
  30.  
  31. // Flag to track if scrollbars are currently hidden
  32. let scrollbarsHidden = true;
  33.  
  34. /**
  35. * Injects the provided CSS into the document.
  36. */
  37. const injectCSS = (cssRules) => {
  38. const styleElement = document.createElement('style');
  39. styleElement.id = 'scrollbar-hider-style';
  40. styleElement.type = 'text/css';
  41. styleElement.textContent = cssRules;
  42. document.head.appendChild(styleElement);
  43. };
  44.  
  45. /**
  46. * Removes the injected CSS.
  47. */
  48. const removeCSS = () => {
  49. const styleElement = document.getElementById('scrollbar-hider-style');
  50. if (styleElement) {
  51. styleElement.remove();
  52. }
  53. };
  54.  
  55. /**
  56. * Toggles the visibility of scrollbars. Idk, but I think this is important.
  57. */
  58. const toggleScrollbars = () => {
  59. if (scrollbarsHidden) {
  60. removeCSS();
  61. } else {
  62. injectCSS(scrollbarHiderCSS);
  63. }
  64. scrollbarsHidden = !scrollbarsHidden;
  65. };
  66.  
  67. /**
  68. * Adds event listener for keypress to toggle scrollbars.
  69. */
  70. const addToggleListener = () => {
  71. document.addEventListener('keydown', (event) => {
  72. if (event.ctrlKey && event.altKey && (event.key === 'M' || event.key === 'm')) { // I forgot this part! lol
  73. event.preventDefault();
  74. toggleScrollbars();
  75. }
  76. });
  77. };
  78.  
  79. // Initial setup
  80. injectCSS(scrollbarHiderCSS);
  81. addToggleListener();
  82. })();