9gag - Scroll down to current position

Scroll down back where the focus was when you clicked the arrow-up icon or scrolled up

当前为 2023-06-20 提交的版本,查看 最新版本

  1. // ==UserScript==
  2. // @name 9gag - Scroll down to current position
  3. // @namespace https://github.com/Procyon-b
  4. // @version 0.2
  5. // @description Scroll down back where the focus was when you clicked the arrow-up icon or scrolled up
  6. // @author Achernar
  7. // @match https://9gag.com/*
  8. // @grant none
  9. // ==/UserScript==
  10.  
  11. (function() {
  12. "use strict";
  13.  
  14. var pos=[0,0], btt=document.querySelector('.back-to-top')
  15.  
  16. if (!btt) return;
  17.  
  18. window.addEventListener('scroll', savePos);
  19.  
  20. function savePos(r=0) {
  21. var d=window.scrollY - pos[1];
  22. if (d < 0) showHide(true);
  23. if (d >= 0) {
  24. pos=[window.scrollX, window.scrollY];
  25. showHide();
  26. }
  27. }
  28.  
  29. var disp=false;
  30. function showHide(show=false) {
  31. if (show == disp) return;
  32. back.style.display= show?'':'none';
  33. disp=!disp;
  34. }
  35.  
  36. var back=document.createElement('a');
  37.  
  38. document.querySelector('header#top-nav').appendChild(back);
  39.  
  40. back.href='javascript:;';
  41. back.classList='back-to-top';
  42. back.style='transform: rotate(180deg); top: 5em; display: none;';
  43. back.title='Scroll back to position';
  44.  
  45. back.addEventListener('click', function(e){
  46. if (pos) window.scrollTo.apply(null, pos);
  47. showHide();
  48. });
  49.  
  50. })();