9gag - Scroll down to current position (local)

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

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

  1. // ==UserScript==
  2. // @name 9gag - Scroll down to current position (local)
  3. // @namespace https://github.com/Procyon-b
  4. // @version 0.3
  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], allPos={}, L, btt=document.querySelector('.back-to-top')
  15.  
  16. if (!btt) return;
  17.  
  18. window.addEventListener('scroll', savePos);
  19.  
  20. function savePos() {
  21. var d=window.scrollY - pos[1];
  22. if (d < 0) showHide(true);
  23. if (d >= 0) {
  24. pos[0]=window.scrollX;
  25. pos[1]=window.scrollY;
  26. showHide();
  27. }
  28. }
  29.  
  30. function showHide(show=false) {
  31. if (show == L.disp) return;
  32. back.style.display= show?'':'none';
  33. L.disp=show;
  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. var hRS=history.replaceState;
  51.  
  52. history.replaceState=function() {
  53. setTimeout(LocPos, 0);
  54. // unlink "pos" "L"
  55. pos=[0,0];
  56. L={disp:false};
  57. return hRS.apply(history, arguments);
  58. }
  59.  
  60. function LocPos() {
  61. if (!allPos[location.href]) allPos[location.href]={pos:[0,0], disp:false};
  62. L=allPos[location.href];
  63. pos=L.pos;
  64. back.style.display= L.disp ? '':'none';
  65. }
  66.  
  67. LocPos();
  68.  
  69. })();