Skip to content

Instantly share code, notes, and snippets.

@putermancer
Created December 11, 2012 19:30
Show Gist options
  • Save putermancer/4261328 to your computer and use it in GitHub Desktop.
Save putermancer/4261328 to your computer and use it in GitHub Desktop.
Proboards: jump to best post (first unread or most recent) in a thread
<script type="text/javascript">
<!--
// Jump to the best post (first first unread or most recent) in a thread.
// Originally by Todge, but completely rewritten by Ben Loveridge.
if(document.location.href.match('board=')&&!document.location.href.match('action=')) {
var table_cells = document.getElementsByTagName('td'),
create_cell_click_fn = function(href) {
return function() { if (!window.pb_bubble) { window.location.href = href; } };
},
cell, is_topic_cell, has_new_post, new_post_href, best_post_href, last_post_cell, last_post_groups, last_post_href;
for(var i=0; i<table_cells.length; i++) {
cell = table_cells[i];
// the cell that lists the topic will always have the "windowbg" class and be either 43 or 48% width
is_topic_cell = cell.className === "windowbg" && cell.width.match(/4(3|8)/);
if(is_topic_cell) {
// if there is a new post, the first child is an A tag which is a "NEW" icon
has_new_post = cell.firstChild.nodeName === "A";
new_post_href = has_new_post ? cell.firstChild.href : null;
// the last cell always contains a link to the most recent post; grab the href
last_post_cell = table_cells[i+4];
last_post_groups = new RegExp("href=['\"']([^'\"']+)").exec(String(last_post_cell.onclick));
last_post_href = last_post_groups && last_post_groups[1];
// the "best" link is the a) first unread or b) most recent post.
best_post_href = new_post_href || last_post_href;
// ERROR, this should never happen, but don't throw exceptions in case it does
// (this should only happen if Proboards changes their page layout)
if (!best_post_href) { continue; }
// make clicking on the cell always jump to the best candidate
cell.onclick = create_cell_click_fn(best_post_href);
// also replace the link href for the title link (which normally links to the first post)
cell.getElementsByTagName('a')[has_new_post ? 1 : 0].href=best_post_href;
}
}
}
//-->
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment