Scroll down back where the focus was when you clicked the arrow-up icon or scrolled up
当前为
// ==UserScript==
// @name 9gag - Scroll down to current position (local)
// @namespace https://github.com/Procyon-b
// @version 0.3
// @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;
window.addEventListener('scroll', savePos);
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();
}
}
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').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 hRS=history.replaceState;
history.replaceState=function() {
setTimeout(LocPos, 0);
// unlink "pos" "L"
pos=[0,0];
L={disp:false};
return hRS.apply(history, arguments);
}
function LocPos() {
if (!allPos[location.href]) allPos[location.href]={pos:[0,0], disp:false};
L=allPos[location.href];
pos=L.pos;
back.style.display= L.disp ? '':'none';
}
LocPos();
})();