Created
June 12, 2016 17:09
-
-
Save lorenjohnson/a99ea9e913858a28db603f9d008a604c to your computer and use it in GitHub Desktop.
Auto Scroll Bookmarklet (for Chrome)
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 = 50; | |
var adjustIncrement = 2; | |
var jumpIncrement = 50; | |
function start() { | |
window.addEventListener('keydown', keyPressed); | |
window.addEventListener('keyup', keyReleased, false); | |
autoScroll(); | |
} | |
function autoScroll() { | |
if(scrollTimeout) { | |
clearTimeout(scrollTimeout); | |
} | |
window.scrollBy(0,1); | |
scrollTimeout = setTimeout(autoScroll, scrollSpeed); | |
isScrolling = true; | |
} | |
var keysPressed = []; | |
function keyPressed(event) { | |
keysPressed[event.keyCode] = true; | |
console.log(keysPressed); | |
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) { | |
turnOff(); | |
} | |
if (downArrow) { | |
event.preventDefault(); | |
window.scrollBy(0, jumpIncrement); | |
autoScroll(); | |
} | |
if (upArrow) { | |
event.preventDefault(); | |
window.scrollBy(0, -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); | |
window.removeEventListener('keydown', keyPressed); | |
window.removeEventListener('keyup', keyReleased, false); | |
isScrolling = false; | |
} | |
start(); | |
})(window, document); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thanks for making this!
If anyone was wondering: it can work when you open PDFs but you have to manually paste this code into the console for it to work.
Here's a fork where the escape button will resume scrolling as well as stop it.
https://gist.github.com/RayBB/359bb5946ac3c45e355bcf238e49a66e