9gag - Scroll down to current position

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

Dovrai installare un'estensione come Tampermonkey, Greasemonkey o Violentmonkey per installare questo script.

You will need to install an extension such as Tampermonkey to install this script.

Dovrai installare un'estensione come Tampermonkey o Violentmonkey per installare questo script.

Dovrai installare un'estensione come Tampermonkey o Userscripts per installare questo script.

Dovrai installare un'estensione come ad esempio Tampermonkey per installare questo script.

Dovrai installare un gestore di script utente per installare questo script.

(Ho già un gestore di script utente, lasciamelo installare!)

Dovrai installare un'estensione come ad esempio Stylus per installare questo stile.

Dovrai installare un'estensione come ad esempio Stylus per installare questo stile.

Dovrai installare un'estensione come ad esempio Stylus per installare questo stile.

Dovrai installare un'estensione per la gestione degli stili utente per installare questo stile.

Dovrai installare un'estensione per la gestione degli stili utente per installare questo stile.

Dovrai installare un'estensione per la gestione degli stili utente per installare questo stile.

(Ho già un gestore di stile utente, lasciamelo installare!)

// ==UserScript==
// @name         9gag - Scroll down to current position
// @namespace    https://github.com/Procyon-b
// @version      0.3.2
// @description  Scroll down back where the focus was when you clicked the arrow-up icon or scrolled up
// @author       Achernar
// @match        https://9gag.com/*
// @grant        none
// ==/UserScript==

(function() {
"use strict";

var pos=[0,0], allPos={}, L, btt=document.querySelector('.back-to-top')

if (!btt) return;

btt.addEventListener('click', toTop);
window.addEventListener('scroll', savePos);

function toTop() {
  if (window.scrollY < pos[1]) {
    L.midpos=[window.scrollX, window.scrollY];
    backMid.style.display='';
    }
  }
  
function savePos() {
  var d=window.scrollY - pos[1];
  if (d < 0) showHide(true);
  if (d >= 0) {
    pos[0]=window.scrollX;
    pos[1]=window.scrollY;
    showHide();
    }
  if (L.midpos && (window.scrollY >= L.midpos[1]) ) {
    hideMBt();
    }
  }

function showHide(show=false) {
  if (show == L.disp) return;
  back.style.display= show?'':'none';
  L.disp=show;
  }

var back=document.createElement('a');

document.querySelector('header#top-nav, header#top-navb, header[id^="top-nav"]').appendChild(back);

back.href='javascript:;';
back.classList='back-to-top';
back.style='transform: rotate(180deg); top: 5em; display: none;';
back.title='Scroll back to position';

back.addEventListener('click', function(e){
  if (pos) window.scrollTo.apply(null, pos);
  showHide();
  });

var backMid=back.cloneNode(false);
backMid.style='transform: rotate(180deg); top: 8em; display: none;';
backMid.title='Scroll back to mid-position';
document.querySelector('header#top-nav, header#top-navb, header[id^="top-nav"]').appendChild(backMid);
backMid.addEventListener('click', function(e){
  if (L.midpos) window.scrollTo.apply(null, L.midpos);
  });

function hideMBt() {
  backMid.style.display='none';
  delete L.midpos;
  }

var hRS=history.replaceState;

history.replaceState=function() {
setTimeout(function(){console.info('%c history.replaceState() ', 'background: lightblue;', location.href, {arguments});}, 0);
  setTimeout(LocPos, 0);
  pos=[0,0];
  L={disp:false};
  return hRS.apply(history, arguments);
  }

function LocPos() {
  if (!allPos[location.href]) {
    if ( (location.hash == '#comment') && allPos[location.href.split('#')[0]] )
         allPos[location.href]=allPos[location.href.split('#')[0]];
    else allPos[location.href]={pos:[0,0], disp:false};
    }
  L=allPos[location.href];
  pos=L.pos;
  
  back.style.display= L.disp ? '':'none';
  backMid.style.display= L.midpos?'':'none';
  }

// set clones
allPos['https://9gag.com/home']=allPos['https://9gag.com/']={pos:[0,0], disp:false};
LocPos();

})();