AutoScroll2

AutoScroll - Automatically scroll down the page, supporting hotkey control switches and speed adjustment (自动向下滚动页面,支持热键控制开关和速度调节)

  1. // ==UserScript==
  2. // @name AutoScroll2
  3. // @description AutoScroll - Automatically scroll down the page, supporting hotkey control switches and speed adjustment (自动向下滚动页面,支持热键控制开关和速度调节)
  4. // @include http*
  5. // @version 1.0.2
  6. // @author Eilen https://github.com/EilenC
  7. // @grant none
  8. // @namespace https://gf.qytechs.cn/users/319354
  9. // @supportURL https://github.com/EilenC/Tampermonkey-Scripts/blob/master/AutoScroll2/AutoScroll2.js
  10. // ==/UserScript==
  11.  
  12. (function (document) {
  13. 'use strict';
  14.  
  15. function roundToTwoDecimalPlaces(number) {
  16. return Number(number.toFixed(2));
  17. }
  18.  
  19. let notificationTimer = null;
  20. let notification = document.createElement('div');
  21. let tipsTime = 300;
  22.  
  23. function showNotification(message, duration) {
  24. clearTimeout(notificationTimer);
  25. notification.textContent = message;
  26. notification.style.position = 'fixed';
  27. notification.style.top = '50%';
  28. notification.style.left = '50%';
  29. notification.style.transform = 'translate(-50%, -50%)';
  30. notification.style.background = 'black';
  31. notification.style.color = 'white';
  32. notification.style.fontWeight = 'bold';
  33. notification.style.fontSize = '40px';
  34. notification.style.padding = '20px';
  35. notification.style.borderRadius = '10px';
  36. notification.style.zIndex = '9999';
  37. document.body.appendChild(notification);
  38. notificationTimer = setTimeout(function () {
  39. document.body.removeChild(notification);
  40. notificationTimer = null;
  41. }, duration);
  42. }
  43.  
  44. let scrollY = 0;
  45. let scrollSpeed = 0.5; // 默认滚动速度
  46. let isScrolling = false;
  47.  
  48. let lastScrollTime = performance.now();
  49. const targetFrameDelay = 1000 / 60; // 目标帧率为每秒 60 帧
  50. let animationFrameId = null;
  51.  
  52. function scrollAnimation(currentTime) {
  53. if (isScrolling) {
  54. const elapsedTime = currentTime - lastScrollTime;
  55. if (elapsedTime >= targetFrameDelay) {
  56. const deltaScroll = scrollSpeed * (elapsedTime / targetFrameDelay);
  57. scrollY += deltaScroll;
  58. window.scrollTo(0, scrollY);
  59. lastScrollTime = currentTime;
  60. }
  61. animationFrameId = requestAnimationFrame(scrollAnimation);
  62. } else {
  63. cancelAnimationFrame(animationFrameId);
  64. animationFrameId = null;
  65. }
  66. }
  67.  
  68. // 开始滚动
  69. function startScroll() {
  70. isScrolling = true;
  71. scrollY = window.scrollY || document.documentElement.scrollTop;
  72. lastScrollTime = performance.now();
  73. animationFrameId = requestAnimationFrame(scrollAnimation);
  74. }
  75.  
  76. // 停止滚动
  77. function stopScroll() {
  78. isScrolling = false;
  79. if (animationFrameId !== null) {
  80. cancelAnimationFrame(animationFrameId);
  81. animationFrameId = null;
  82. }
  83. scrollY = 0; // 清空滚动位置
  84. lastScrollTime = 0; // 重置上次滚动时间
  85. }
  86.  
  87. let lastKeyDownTime = 0;
  88. let keyDownCount = 0;
  89. // 键盘事件处理
  90. document.addEventListener('keydown', (event) => {
  91. let currentTime = new Date().getTime();
  92. switch (event.key) {
  93. case 's':
  94. case 'ArrowLeft':
  95. if(isScrolling){
  96. let step = 0.01
  97. if (scrollSpeed > 2) {
  98. step = 0.1
  99. }
  100. scrollSpeed = Math.max(scrollSpeed - step, 0.01);
  101. showNotification('AutoScroll:Speed: ' + roundToTwoDecimalPlaces(scrollSpeed), tipsTime);
  102. }
  103. break;
  104. case 'a':
  105. case 'ArrowRight':
  106. if(isScrolling){
  107. scrollSpeed = Math.min(scrollSpeed + 0.1, 99);
  108. showNotification('AutoScroll:Speed: ' + roundToTwoDecimalPlaces(scrollSpeed), tipsTime);
  109. }
  110. break;
  111. case "`":
  112. if (currentTime - lastKeyDownTime < 300) {
  113. keyDownCount++;
  114. } else {
  115. keyDownCount = 1;
  116. }
  117.  
  118. lastKeyDownTime = currentTime;
  119.  
  120. if (keyDownCount === 2) {
  121. if (isScrolling) {
  122. stopScroll();
  123. showNotification('AutoScroll:Off', tipsTime);
  124. } else {
  125. startScroll();
  126. showNotification('AutoScroll:On', tipsTime);
  127. }
  128. keyDownCount = 0;
  129. }
  130. break;
  131. }
  132. });
  133. })(document);

QingJ © 2025

镜像随时可能失效,请加Q群300939539或关注我们的公众号极客氢云获取最新地址