Last active
March 28, 2019 22:16
-
-
Save violetcode/449d2458657bc75f7dfef83664e51b1b to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
javascript:(function(window, document) { | |
var isScrolling, scrollTimeout; | |
var defaultScrollSpeed = 50; | |
var scrollSpeed = 10; | |
var adjustIncrement = 2; | |
var jumpIncrement = 50; | |
var stopPoint = -1; | |
var scrolled = 0; | |
var scrollDelay = 0; | |
var reverse = false; | |
function start(){ | |
var prompt_text = "Enter distance to scroll (in pixels, or fraction of the page '1/5'), the time it takes to scroll 1px (in milliseconds), and the delay before the scroll happens (in milliseconds). Add '-r' to the end to scroll up instead of down.\nEnter 0 for distance to go to the bottom of the page. Default speed is 10, default delay is 0.\n\nExamples:\n'800 10 1000'\n'1/5 10'\n'800 5 0 -r"; | |
var result = prompt(prompt_text); | |
if(result){ | |
if(/(1\/)?\d{1,}( \d{1,})*( -r)?/.test(result)){ | |
var parts = result.split(' '); | |
stopPoint = parts[0]; | |
if(stopPoint.includes('/')){ | |
fraction = parseInt(stopPoint.split('/')[1]); | |
stopPoint = (document.documentElement.scrollHeight - window.innerHeight) / fraction; | |
} else { | |
stopPoint = parseInt(stopPoint); | |
} | |
speed = parseInt(parts[1]); | |
delay = parseInt(parts[2]); | |
if(speed){ | |
scrollSpeed = speed; | |
} | |
if(delay){ | |
scrollDelay = delay; | |
} | |
if(parts.includes("-r")){ | |
reverse = true; | |
} | |
setTimeout(startScroll, scrollDelay); | |
} else { | |
start(); | |
} | |
} | |
} | |
function startScroll() { | |
window.addEventListener('keydown', keyPressed); | |
window.addEventListener('keyup', keyReleased, false); | |
autoScroll(); | |
} | |
function autoScroll() { | |
if(scrollTimeout) { | |
clearTimeout(scrollTimeout); | |
} | |
if(stopPoint == 0 || (stopPoint > 0 && scrolled < stopPoint)){ | |
if(reverse){ | |
window.scrollBy(0,-1); | |
} else { | |
window.scrollBy(0,1); | |
} | |
scrolled += 1; | |
scrollTimeout = setTimeout(autoScroll, scrollSpeed); | |
isScrolling = true; | |
} else { | |
isScrolling = false; | |
} | |
} | |
var keysPressed = []; | |
function keyPressed(event) { | |
keysPressed[event.keyCode] = true; | |
var commandKey = (keysPressed[91] || keysPressed[93]); | |
var leftArrow = keysPressed[37]; | |
var rightArrow = keysPressed[39]; | |
var upArrow = keysPressed[38]; | |
var downArrow = keysPressed[40]; | |
var escKey = keysPressed[27]; | |
if (escKey) { | |
if (isScrolling){ | |
turnOff(); | |
} else { | |
autoScroll(); | |
} | |
} | |
if (downArrow) { | |
event.preventDefault(); | |
window.scrollBy(0, jumpIncrement); | |
scrolled += jumpIncrement; | |
autoScroll(); | |
} | |
if (upArrow) { | |
event.preventDefault(); | |
window.scrollBy(0, -jumpIncrement); | |
scrolled -= jumpIncrement; | |
autoScroll(); | |
} | |
if (rightArrow) { | |
event.preventDefault(); | |
scrollSpeed -= adjustIncrement; | |
autoScroll(); | |
} | |
if (leftArrow) { | |
event.preventDefault(); | |
scrollSpeed += adjustIncrement; | |
autoScroll(); | |
} | |
} | |
function keyReleased(event) { | |
delete keysPressed[event.keyCode]; | |
} | |
function turnOff() { | |
clearTimeout(scrollTimeout); | |
isScrolling = false; | |
} | |
start(); | |
})(window, document); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment