Created
February 3, 2012 23:21
-
-
Save jelmervdl/1733615 to your computer and use it in GitHub Desktop.
Link to the last page of the thread if possible
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 SV Cover last thread | |
// @namespace http://ikhoefgeen.nl/userscripts | |
// @description Programmeurs kunnen geen interfaces maken. | |
// @include http://www.svcover.nl/* | |
// ==/UserScript== | |
function setLastPage(thread_id, page) | |
{ | |
localStorage['thread-' + thread_id] = page; | |
} | |
function getLastPage(thread_id) | |
{ | |
return parseInt(localStorage['thread-' + thread_id]) || 0; | |
} | |
function isLinkToThreadStart(url) | |
{ | |
return /\/forum\.php\?thread=\d+$/.test(url); | |
} | |
function isLinkToThreadPage(url) | |
{ | |
return /\/forum\.php\?thread=\d+&page=\d+$/.test(url); | |
} | |
function getThreadId(url) | |
{ | |
return url.match(/[\?&]thread=(\d+)/)[1]; | |
} | |
function getPageNum(url) | |
{ | |
return parseInt(url.match(/[\?&]page=(\d+)/)[1]); | |
} | |
function foreach(traversable, callback) | |
{ | |
Array.prototype.forEach.call(traversable, callback); | |
} | |
function main() | |
{ | |
foreach(document.getElementsByTagName('a'), function(link) | |
{ | |
if (isLinkToThreadStart(link.href)) | |
{ | |
var last_page = getLastPage(getThreadId(link.href)); | |
if (last_page) | |
link.href += '&page=' + last_page; | |
} | |
}); | |
if (isLinkToThreadPage(document.location.href)) | |
{ | |
var thread_id = getThreadId(document.location.href), | |
page_num = getPageNum(document.location.href); | |
if (getLastPage(thread_id) < page_num) | |
setLastPage(thread_id, page_num); | |
} | |
} | |
main(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment