Last active
May 17, 2020 13:23
-
-
Save JulianWebb/294150bf18e91cb2b17edd48dbb595c2 to your computer and use it in GitHub Desktop.
Sam And Fuzzy Arrow Keys And Randomizer Userscript
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
// ==UserScript== | |
// @name Sam & Fuzzy Arrow Keys and Randomizer | |
// @namespace https://gist.github.com/JulianWebb | |
// @version 0.2 | |
// @author JulianWebb | |
// @description Adds the ability to navigate the comic using just the left and right arrow keys | |
// @icon https://www.samandfuzzy.com/favicon.ico | |
// @match https://www.samandfuzzy.com/* | |
// @match http://www.samandfuzzy.com/* | |
// @grant none | |
// ==/UserScript== | |
(function() { | |
'use strict'; | |
// Randomizer Init | |
// Does this provide an accurate count? Not really. | |
// Does this provide a close enough count for our purposes? Yeah | |
var start_date = new Date("May 27, 2002"); //Date of first Sam & Fuzzy | |
var today_date = new Date(); | |
var week_in_milliseconds = 604800000; | |
var weeks = 0; | |
for (var date = start_date.getTime(); date <= today_date.getTime(); date += week_in_milliseconds) { | |
weeks++ | |
} | |
var rand_padding = 10; | |
var rand_min = rand_padding; | |
var rand_max = (weeks * 3) - rand_padding; | |
// Only add listeners if there is a comic on the page | |
if (document.querySelector("img.comic-image")) { | |
//Key Listeners | |
document.addEventListener("keydown", event => { | |
switch (event.key) { | |
case "ArrowLeft": | |
let prevPage = document.querySelector("li.prev-page > a"); | |
prevPage.click() | |
break; | |
case "ArrowRight": | |
let nextPage = document.querySelector("li.next-page > a"); | |
nextPage.click(); | |
break; | |
case "r": | |
let rand_page = Math.floor(Math.random() * (rand_max - rand_min)) + rand_min; | |
window.location = "https://www.samandfuzzy.com/" + rand_page; | |
break; | |
default: return; | |
} | |
}); | |
console.log('Sam & Fuzzy Arrow Keys and Randomizer is working!'); | |
} else { | |
console.log('No comic on page; I sleep.'); | |
} | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment