Last active
July 9, 2020 14:24
-
-
Save michd/3e2e3afc88315510d4b37ff42877acc4 to your computer and use it in GitHub Desktop.
XKCD, but with publish dates appended to the title
This file contains hidden or 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 XKCD with publish dates | |
// @author MichD | |
// @include /^https?://xkcd.com.*$/ | |
// @version 1 | |
// @grant none | |
// ==/UserScript== | |
function pad(text) { | |
return (text.length < 2 ? "0" : "") + text; | |
} | |
(function() { | |
var pathSplit = document.location.pathname.split("/"), | |
comicPath = (function() { | |
var comicNumber; | |
// Empty string is request for most recent comic | |
if (pathSplit.length < 2 || pathSplit[1] === "") { | |
return ""; | |
} | |
comicNumber = parseInt(pathSplit[1], 10); | |
// Null means don't bother; not on a comic page | |
if (isNaN(comicNumber)) return null; | |
return comicNumber.toString() + "/"; | |
}()); | |
if (comicPath === null) return; | |
fetch("https://xkcd.com/" + comicPath + "/info.0.json") | |
.then(response => response.json()) | |
.then(function (data) { | |
var date = data.year + "-" + pad(data.month) + "-" + pad(data.day); | |
var $title = document.querySelector("#ctitle"); | |
$title.innerText = data.title + " (" + date + ")"; | |
}); | |
}()); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment