Created
February 4, 2015 11:07
-
-
Save martinth/6ae38fafb4a53e20a33e to your computer and use it in GitHub Desktop.
SCP Foundation reading helper
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 SCP Foundation reading helper | |
// @namespace http://hrnz.net/ | |
// @version 0.1 | |
// @description Adds simple buttons to SCP page to allow easy paging. | |
// @author Martin Thurau <[email protected]> | |
// @match http://www.scp-wiki.net/scp-* | |
// @grant unsafeWindow | |
// @grant GM_addStyle | |
// @grant GM_setValue | |
// @grant GM_getValue | |
// ==/UserScript== | |
GM_addStyle(".tm_button { cursor: pointer; }"); | |
var $ = unsafeWindow.jQuery, | |
title = $('#page-title'), | |
next = $('<span class="tm_button">►</span>'), | |
prev = $('<span class="tm_button">◄</span>'), | |
lastReadPage = GM_getValue('last-read-page', null), | |
lastRead = $('<span class="tm_button">☛</span>'), | |
titleText = $('<span>' + title.text().trim() + '</span>'); | |
// add elements to DOM | |
title.empty().append(titleText, ' ', prev); | |
if(lastReadPage !== null) { | |
title.append(lastRead); | |
updateLastRead(lastReadPage); | |
} | |
title.append(next); | |
// event handler | |
prev.click(function(e) { | |
e.preventDefault(); | |
changePage(-1); | |
}); | |
next.click(function(e) { | |
e.preventDefault(); | |
changePage(+1); | |
}); | |
lastRead.click(function(e) { | |
e.preventDefault(); | |
loadPage(lastReadPage); | |
}); | |
// change page relative | |
function changePage(delta) { | |
var match = (/\/scp-(\d+).*/).exec(document.location.pathname); | |
if (match) { | |
loadPage(parseInt(match[1]) + delta); | |
} | |
} | |
// load the specified page and insert into DOM | |
function loadPage(page) { | |
var url = document.location.origin + '/scp-' + page; | |
$('#page-content').load(url + ' #page-content', function() { | |
titleText.text('SCP-' + page); | |
history.pushState(null, "", url); | |
updateLastRead(page); | |
}); | |
} | |
// update state and stored value | |
function updateLastRead(page) { | |
lastReadPage = page; | |
GM_setValue('last-read-page', page); | |
lastRead.attr('title', 'Go to last read page: ' + page) | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment