9gag - Scroll down to current position

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

  1. // ==UserScript==
  2. // @name 9gag - Scroll down to current position
  3. // @namespace https://github.com/Procyon-b
  4. // @version 0.3.1
  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. btt.addEventListener('click', toTop);
  19. window.addEventListener('scroll', savePos);
  20.  
  21. function toTop() {
  22. if (window.scrollY < pos[1]) {
  23. L.midpos=[window.scrollX, window.scrollY];
  24. backMid.style.display='';
  25. }
  26. }
  27. function savePos() {
  28. var d=window.scrollY - pos[1];
  29. if (d < 0) showHide(true);
  30. if (d >= 0) {
  31. pos[0]=window.scrollX;
  32. pos[1]=window.scrollY;
  33. showHide();
  34. }
  35. if (L.midpos && (window.scrollY >= L.midpos[1]) ) {
  36. hideMBt();
  37. }
  38. }
  39.  
  40. function showHide(show=false) {
  41. if (show == L.disp) return;
  42. back.style.display= show?'':'none';
  43. L.disp=show;
  44. }
  45.  
  46. var back=document.createElement('a');
  47.  
  48. document.querySelector('header#top-nav').appendChild(back);
  49.  
  50. back.href='javascript:;';
  51. back.classList='back-to-top';
  52. back.style='transform: rotate(180deg); top: 5em; display: none;';
  53. back.title='Scroll back to position';
  54.  
  55. back.addEventListener('click', function(e){
  56. if (pos) window.scrollTo.apply(null, pos);
  57. showHide();
  58. });
  59.  
  60. var backMid=back.cloneNode(false);
  61. backMid.style='transform: rotate(180deg); top: 8em; display: none;';
  62. backMid.title='Scroll back to mid-position';
  63. document.querySelector('header#top-nav').appendChild(backMid);
  64. backMid.addEventListener('click', function(e){
  65. if (L.midpos) window.scrollTo.apply(null, L.midpos);
  66. });
  67.  
  68. function hideMBt() {
  69. backMid.style.display='none';
  70. delete L.midpos;
  71. }
  72.  
  73. var hRS=history.replaceState;
  74.  
  75. history.replaceState=function() {
  76. setTimeout(function(){console.info('%c history.replaceState() ', 'background: lightblue;', location.href, {arguments});}, 0);
  77. setTimeout(LocPos, 0);
  78. pos=[0,0];
  79. L={disp:false};
  80. return hRS.apply(history, arguments);
  81. }
  82.  
  83. function LocPos() {
  84. if (!allPos[location.href]) {
  85. if ( (location.hash == '#comment') && allPos[location.href.split('#')[0]] )
  86. allPos[location.href]=allPos[location.href.split('#')[0]];
  87. else allPos[location.href]={pos:[0,0], disp:false};
  88. }
  89. L=allPos[location.href];
  90. pos=L.pos;
  91. back.style.display= L.disp ? '':'none';
  92. backMid.style.display= L.midpos?'':'none';
  93. }
  94.  
  95. // set clones
  96. allPos['https://9gag.com/home']=allPos['https://9gag.com/']={pos:[0,0], disp:false};
  97. LocPos();
  98.  
  99. })();