Created
March 18, 2012 02:18
-
-
Save vegetableman/2067935 to your computer and use it in GitHub Desktop.
Also expand on click of HNCollapse Bookmarklet
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
(function() { | |
/*if (window.location.hostname != 'news.ycombinator.com') { | |
window.location = 'http://news.ycombinator.com'; | |
}*/ | |
if (window.HNCOLLAPSE) { | |
window.HNCOLLAPSE = false; | |
} | |
else{ | |
window.HNCOLLAPSE = true; | |
} | |
function collapse(cells, startIndex, atIndent) { | |
var thisCell = jQuery(cells[startIndex]); | |
var children = jQuery(); | |
var numDescendants = 0; | |
for (var i=startIndex; i<cells.length; i++) { | |
var cell = jQuery(cells[i]); | |
if (cell.data('indent') == atIndent) { | |
cell.find('td:eq(2)').each(function(x, td) { | |
console.log('indent : ' + atIndent); | |
jQuery(td).find('p:last>font').append(' ', collapse(cells, i+1, atIndent +1)); | |
children = children.add(cell); | |
}); | |
} else if (cell.data('indent') < atIndent) { | |
break; | |
} | |
numDescendants += 1; | |
console.log('numDescendants : ' + numDescendants); | |
} | |
if (children.length > 0 && atIndent > 0) { | |
children.hide(); | |
if (children.length < numDescendants) { | |
var expand = jQuery(' <u><a href=\'#\' id=\'comment\'>'+ children.length +' comments ('+numDescendants+')</a></u>'); | |
} else { | |
var expand = jQuery(' <u><a href=\'#\' id=\'comment\'>'+ numDescendants +' comments</a></u>'); | |
} | |
expand.click(function(e) { | |
expand.remove(); | |
children.show(); | |
return false; | |
}); | |
return expand; | |
} else { | |
return null; | |
} | |
} | |
function expand(cells,i) { | |
if($(cells[i]).css('display') === 'none') $(cells[i]).show(); | |
if(i > 0) | |
expand(cells,i-1) | |
else if(i === 0) | |
$('body').find('a#comment').remove(); | |
} | |
function onLoad() { | |
var cells = jQuery('table:eq(0)>tbody>tr:eq(2)>td:eq(0)>table:eq(1)>tbody>tr>td>table>tbody>tr'); | |
cells.each(function(i, cell) { | |
var width_px = jQuery(cell).find('>td>img').css('width'); | |
var width = Number(width_px.substr(0,width_px.length-2)); | |
var indent = width/40; | |
jQuery(cell).data('indent', indent); | |
}); | |
if(window.HNCOLLAPSE) | |
jQuery(document.body).append(collapse(cells, 0, 0)); | |
else | |
jQuery(document.body).append(expand(cells,cells.length)); | |
} | |
var headID = document.getElementsByTagName('head')[0]; | |
var newScript = document.createElement('script'); | |
newScript.type = 'text/javascript'; | |
newScript.onload=function() { onLoad(); }; | |
newScript.src = 'public/bookmarklet/HNCE/jquery.js'; | |
headID.appendChild(newScript); | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment