Skip to content

Instantly share code, notes, and snippets.

@martinth
Created February 4, 2015 11:07
Show Gist options
  • Save martinth/6ae38fafb4a53e20a33e to your computer and use it in GitHub Desktop.
Save martinth/6ae38fafb4a53e20a33e to your computer and use it in GitHub Desktop.
SCP Foundation reading helper
// ==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, '&nbsp;', 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