Skip to content

Instantly share code, notes, and snippets.

@jelmervdl
Created February 3, 2012 23:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jelmervdl/1733615 to your computer and use it in GitHub Desktop.
Save jelmervdl/1733615 to your computer and use it in GitHub Desktop.
Link to the last page of the thread if possible
// ==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