Skip to content

Instantly share code, notes, and snippets.

@kevee
Created March 27, 2014 00:17
Show Gist options
  • Select an option

  • Save kevee/9796984 to your computer and use it in GitHub Desktop.

Select an option

Save kevee/9796984 to your computer and use it in GitHub Desktop.
Auto-finding the content area of a page - adapted from readability
getElement : function($) {
var contentNode = false;
var $topScore = $('body');
$('p').each(function() {
var $parent = $(this).parent();
var element = $parent.get(0);
var contentScore = $parent.data('content-score') || 0;
if(!$parent.data('content-score')) {
contentScore = $parent.find('p').length;
if(element.className.match(/(comment|meta|footer|footnote)/))
contentScore -= 50;
else if(element.className.match(/((^|\\s)(post|hentry|entry[-]?(content|text|body)?|article[-]?(content|text|body)?)(\\s|$))/))
contentScore += 25;
if(element.id.match(/(comment|meta|footer|footnote)/))
contentScore -= 50;
else if(element.id.match(/^(post|hentry|entry[-]?(content|text|body)?|article[-]?(content|text|body)?)$/))
contentScore += 25;
$parent.data('content-score', contentScore);
}
contentScore += $(this).text().split(',').length;
if(typeof $topScore.data('content-score') === 'undefined' || contentScore > $topScore.data('content-score')) {
$topScore = $parent;
}
});
return $topScore;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment