Created
May 17, 2017 21:05
-
-
Save prendradjaja/799553ce846a22eb1a6c14220f6c4aea to your computer and use it in GitHub Desktop.
xkcd permalink bookmarklet (navigates to permalink URL)
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
BOOKMARKLET_NAME = 'xkcd-permalink.js'; | |
function main() { | |
var prev = $('a[rel="prev"]')[0].href; | |
var parts = prev.split('/'); | |
if (parts.length !== 5) { | |
fail('Expected exactly 4 slashes in "prev" link URL, e.g. https://www.xkcd.com/1000/. Actual URL: ' + prev); | |
} | |
if (!Number.isInteger(+parts[3])) { | |
fail('URL segment before last slash must be an integer. Actual: ' + parts[3]); | |
} | |
parts[3] = (+parts[3] + 1) + ''; | |
location.href = parts.join('/'); | |
} | |
function fail(reason) { | |
alert('Bookmarklet "' + BOOKMARKLET_NAME + '" failed: ' + reason); | |
throw reason; | |
} | |
main(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment