Auto Scroll 自动滚屏

Auto Scroll Pages (double click / ctrl+arrow / alt+arrow)

  1. // ==UserScript==
  2. // @name Auto Scroll 自动滚屏
  3. // @description Auto Scroll Pages (double click / ctrl+arrow / alt+arrow)
  4. // @include *
  5. // @version 0.17
  6. // @author Erimus
  7. // @grant none
  8. // @namespace https://greasyfork.org/users/46393
  9. // ==/UserScript==
  10.  
  11. (function(document) {
  12.  
  13. // speed controlled by the following 2 variables
  14. let scroll_interval = 15, // every xx ms
  15. scroll_distance = 1 // move xx pixel
  16.  
  17. let scrolling = false, // status
  18. auto_scroll, // scroll function
  19. last_click = Date.now()
  20.  
  21. // main function
  22. let toggle_scroll = function(dire) {
  23. scrolling = !scrolling
  24. if (scrolling) {
  25. console.log('Start scroll', dire)
  26. dire = dire == 'up' ? -1 : 1
  27. auto_scroll = setInterval(function() {
  28. document.documentElement.scrollTop += (dire * scroll_distance)
  29. }, scroll_interval)
  30. } else {
  31. console.log('Stop scroll')
  32. clearInterval(auto_scroll)
  33. }
  34. }
  35.  
  36. // double click near edge can trigger (Prevent accidental touch)
  37. // 双击靠近边缘的位置可以触发滚屏 (防止误触发)
  38. let dblclick_check = function(e) {
  39. if (Date.now() - last_click < 500) { return } //just stopped by click
  40. let range = 50 // effective range
  41. let w = window.innerWidth
  42. let h = window.innerHeight
  43. console.log('double click: x' + e.x + '/' + w + '| y' + e.y + '/' + h)
  44. // Except top edge, because of search bar is mostly at top.
  45. if (e.x < range || w - e.x < range || h - e.y < range) {
  46. toggle_scroll()
  47. }
  48. }
  49.  
  50. // toogle scrolling by double click
  51. // if you want to trigger with double click , remove '//' before 'document'.
  52. // 你想用双击触发,删除下一行前的 '//'。
  53. document.body.addEventListener('dblclick', dblclick_check)
  54.  
  55. // single click to stop scroll
  56. document.body.addEventListener('click', function() {
  57. if (scrolling) {
  58. scrolling = false
  59. console.log('Stop scroll')
  60. clearInterval(auto_scroll)
  61. last_click = Date.now()
  62. }
  63. })
  64.  
  65. // toogle scrolling by hotkey
  66. // if you want set your own hotkey, find the key code on following site.
  67. // 如果你想要设置其它快捷键,查看以下网址以找到对应的按键码。
  68. // https://www.w3.org/2002/09/tests/keys.html
  69. document.onkeydown = function(e) {
  70. let keyCode = e.keyCode || e.which || e.charCode,
  71. fnKey = e.ctrlKey || e.metaKey || e.altKey
  72. if (fnKey && keyCode == 40) {
  73. console.log('Press Ctrl/Alt + Down arrow')
  74. toggle_scroll()
  75. } else if (fnKey && keyCode == 38) {
  76. console.log('Press Ctrl/Alt + Up arrow')
  77. toggle_scroll('up')
  78. }
  79. }
  80.  
  81. })(document)