Created
March 27, 2014 00:17
-
-
Save kevee/9796984 to your computer and use it in GitHub Desktop.
Auto-finding the content area of a page - adapted from readability
This file contains hidden or 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
| 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