// ==UserScript==
// @name	Page Scroll Marker
// @namespace	http://www-ui.is.s.u-tokyo.ac.jp/~kobayash/
// @description	Adds translucent bars to the top and bottom of the viewport, which indicates the previous viewport position when the page is scrolled. This tool will prevents you from getting lost when you press the Page Up/Down keys.
// @include	*
// ==/UserScript==

(function () {
  const generalName = 'Page Scroll Marker';
  const containerID = (generalName + new Date()).replace(/\W/g, '');
  
  GM_addStyle('div#' + containerID + ' { display: none !important; background: transparent !important; border: none !important; } div#' + containerID + '.active { display: block !important; } div#' + containerID + ' div { position: absolute !important; left: 0 !important; width: 100% !important; height: 8px !important; background: #ff8080 !important; z-index: 65535 !important; opacity: 0.25 !important; font: 8px/1 sans-serif !important; color: #ffffff !important; text-align: center !important; border: none !important; }');

  const panel = document.body.appendChild(document.createElement('DIV'));
  
  panel.id = containerID;
  panel.title = generalName;

  const uEdge = panel.appendChild(document.createElement('DIV'));
  const lEdge = panel.appendChild(document.createElement('DIV'));

  uEdge.appendChild(document.createTextNode('\u25bc'));
  lEdge.appendChild(document.createTextNode('\u25b2'));
  
  var tid;

  var update = function () {
    clearTimeout(tid);
    panel.className = '';
    uEdge.style.top = + scrollY + 'px';
    lEdge.style.bottom = - scrollY + 'px';
  }
  
  addEventListener('scroll', function (e) {
    clearTimeout(tid);
    panel.className = 'active';
    tid = setTimeout(update, 1000);
  }, true);
  
  addEventListener('resize', update, true);
  addEventListener('load', update, true);
})()


