Universal Auto Scroll

Press Alt+X to start auto-scrolling. Use Alt+Up/Down to adjust speed. Press Esc, Space, or Enter to stop.

当前为 2025-02-17 提交的版本,查看 最新版本

  1. // ==UserScript==
  2. // @name Universal Auto Scroll
  3. // @version 1.0
  4. // @namespace MedX-AA
  5. // @author MedX
  6. // @license MIT
  7. // @description Press Alt+X to start auto-scrolling. Use Alt+Up/Down to adjust speed. Press Esc, Space, or Enter to stop.
  8. // @icon data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAADAAAAAwCAYAAABXAvmHAAABGUlEQVR4nO3VsS4EURQG4E9oRCEh0ZDQi3gDjV7lHXTeAYVo9gl0ahregEapoKEh0SFRbVbIyiSjEYnd2TtzZ5PzJSeZYjbzn/2LQwghlSN8oP/PFO8caqHeAOF/pni3dfpDTuvEArlFA7lFA7lFA7lFA7lFA7lFA7lFA7lFA7m1uoE9dCuErDrd8pvJvDYYvl/OS8oFOhkW6KRcYBKnDYY/x5TEpnHZQPhrzKjJLG5qDH+LOTVbxGMN4Z+xrCGreEsY/h3rGraR6Db0sCmTLXyOEP4L2zLbGWGBXS1xUCH8vhaZwPEQ4U/K37RKca3PBgh/UceVTXmtr3Jd2VTmcfdH+HssGBNLePp1ZVeMmTU8lP988RxCkN43yek7CExEuggAAAAASUVORK5CYII=
  9. // @grant none
  10. // @include *
  11. // ==/UserScript==
  12.  
  13. // === CONFIGURATION ===
  14. const INITIAL_SPEED = 30; // Starting speed in pixels per second
  15. const RESET_THRESHOLD = 10; // Sensitivity for manual scroll detection
  16.  
  17. // === KEY BINDINGS ===
  18. const START_KEY = 88; // Alt + X starts scrolling
  19. const SPEED_UP_KEY = 40; // Alt + Down (only works during scrolling)
  20. const SPEED_DOWN_KEY = 38; // Alt + Up
  21. const STOP_KEYS = new Set([27, 32, 13]); // Escape, Space, Enter
  22. const ALLOWED_KEYS = new Set([33, 34, 38, 40]); // Page Up, Page Down, Up Arrow, Down Arrow
  23.  
  24. // === STATE VARIABLES ===
  25. let isScrolling = false, scrollSpeed = 0, lastUpdateTime, realX, realY, hudTimeout;
  26. const scrollElement = document.scrollingElement || document.documentElement;
  27.  
  28. // === HUD DISPLAY (Speed Indicator) ===
  29. const hud = Object.assign(document.createElement("div"), {
  30. style: `position:fixed; bottom:10px; right:10px; padding:5px 10px; background:rgba(0,0,0,0.7);
  31. color:#fff; font-size:14px; font-family:Arial; border-radius:5px; z-index:9999; display:none`
  32. });
  33. document.body.appendChild(hud);
  34.  
  35. // === LISTENER: Start scrolling with Alt + X (Only if Ctrl/Shift are NOT pressed) ===
  36. window.addEventListener('keydown', (e) => {
  37. if (e.keyCode !== START_KEY || !e.altKey || e.ctrlKey || e.shiftKey) return; // ✅ Exit early if conditions aren't met
  38. if (!isScrolling) {
  39. startScrolling();
  40. e.preventDefault();
  41. }
  42. }, true);
  43.  
  44. // === LISTENER: Adjust speed or stop scrolling (Only active when scrolling) ===
  45. function handleKeydown(e) {
  46. if (!isScrolling) return; // ✅ Ignore input if not scrolling
  47.  
  48. if (e.altKey && e.keyCode === SPEED_UP_KEY) changeSpeed(1.1); // Increase speed
  49. else if (e.altKey && e.keyCode === SPEED_DOWN_KEY) changeSpeed(0.9); // Decrease speed
  50. else if (STOP_KEYS.has(e.keyCode)) {
  51. stopScrolling();
  52. if (e.keyCode === 32) e.preventDefault(); // ✅ Prevent Spacebar from triggering page scroll
  53. }
  54.  
  55. // ✅ Allow Page Up, Page Down, Up Arrow, Down Arrow to work normally
  56. if (!ALLOWED_KEYS.has(e.keyCode)) e.preventDefault();
  57. }
  58.  
  59. // === DETECT MANUAL SCROLLING ===
  60. function updateScrollPosition() {
  61. realX = scrollElement.scrollLeft;
  62. realY = scrollElement.scrollTop;
  63. }
  64.  
  65. // === CHANGE SCROLL SPEED ===
  66. function changeSpeed(multiplier) {
  67. scrollSpeed = Math.max(5, (scrollSpeed || INITIAL_SPEED) * multiplier); // Ensures speed never drops too low
  68.  
  69. // ✅ Show HUD immediately with new speed
  70. hud.textContent = `Speed: ${scrollSpeed.toFixed(1)} px/s`;
  71. hud.style.display = "block";
  72. if (hudTimeout) clearTimeout(hudTimeout);
  73. hudTimeout = setTimeout(() => (hud.style.display = "none"), 2000);
  74. }
  75.  
  76. // === START AUTO-SCROLLING ===
  77. function startScrolling() {
  78. isScrolling = true;
  79. scrollSpeed = INITIAL_SPEED;
  80. updateScrollPosition();
  81. lastUpdateTime = performance.now();
  82.  
  83. // ✅ Show HUD instantly
  84. changeSpeed(1);
  85.  
  86. window.addEventListener('keydown', handleKeydown, true);
  87. window.addEventListener('scroll', updateScrollPosition, { passive: true });
  88.  
  89. requestAnimationFrame(scrollLoop);
  90. }
  91.  
  92. // === STOP AUTO-SCROLLING ===
  93. function stopScrolling() {
  94. isScrolling = false;
  95. scrollSpeed = 0;
  96. hud.style.display = "none";
  97.  
  98. window.removeEventListener('keydown', handleKeydown, true);
  99. window.removeEventListener('scroll', updateScrollPosition, { passive: true });
  100. }
  101.  
  102. // === MAIN SCROLL LOOP (Smooth & Efficient) ===
  103. function scrollLoop(timestamp) {
  104. if (!isScrolling) return;
  105.  
  106. let elapsed = timestamp - lastUpdateTime;
  107. realY += (scrollSpeed * elapsed) / 1000; // Adjust position based on elapsed time
  108. scrollElement.scrollTo(realX, Math.floor(realY));
  109. lastUpdateTime = timestamp;
  110.  
  111. requestAnimationFrame(scrollLoop);
  112. }