Created
June 12, 2016 17:09
-
-
Save lorenjohnson/78f0ad22259b760b027326824994aea8 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