📜 ⬆️ ⬇️

Script to filter comments

Thank you for your attention to my " Another filter for comments " topic.
I wondered if what I had invented was so meaningless, and after sitting for a while behind the manuals I created a simple userscript for Greasemonkey / UserJS, which implements the plan. In the body of the script there is a parameter CF_wordThreshold , comments that contain fewer words will be hidden. It was tested in Opera 9.60, FF3.

// ==UserScript==
// @name Length filter for Habrahabr comments
// @author Kapustos
// @include http://*habrahabr.ru/blog*
// ==/UserScript==

// if the browser doesn't support the GM functions
if ( typeof (GM_getValue) == 'undefined' ) var GM_getValue = function () {
return false ;
};
if ( typeof (GM_setValue) == 'undefined' ) var GM_setValue = function () {};
if ( typeof (GM_registerMenuCommand) == 'undefined' ) var GM_registerMenuCommand = function () {};

// ---- Config -----

// hide comments with less words then CF_wordThreshold
var CF_wordThreshold = 20;

// --- /Config -----

window.addEventListener( 'load' ,
function ( event ) {
var comments = document .getElementById( 'comments' );
if (comments != 'undefined' ) {

var tempCommentsCloud = document .evaluate( "//ul[@class='hentry']/li/div[@class='entry-content'][1]" , comments, null , XPathResult.ANY_TYPE, null );
var commentsCloud = new Array();
while (temp = tempCommentsCloud.iterateNext()) {
commentsCloud.push(temp);
}

var comment;
while (comment = commentsCloud.pop()) {
if (comment.getElementsByTagName( "div" ).length < 1 ) {
var commentText = comment.textContent;
var words = commentText.split( ' ' ).length;
if (words < CF_wordThreshold) {
var commentDiv = comment.cloneNode( true );
commentDiv.className = '' ;
commentDiv.style.display = 'none' ;
comment.textContent = "" ;
comment.appendChild(commentDiv);
var appendText = document .createTextNode( ' ' );
var appendLink = document .createElement( 'a' );
appendLink.href = '#' ;
appendLink.addEventListener( 'click' , CF_showComment, false );
appendLink.appendChild(appendText);
comment.appendChild(appendLink);
}
}
}

}
},
false );

function CF_showComment( event ) {
com = event .target.parentNode.getElementsByTagName( "div" );
event .target.className = 'hidden' ;
com[0].style.display = 'block' ;
event .stopPropagation();
event .preventDefault();
return false ;
}


To the case of the code beaten by habraparser, the copy lies on Pastebin: http://pastebin.com/m255bed2e
XPathExpression peeped in the NickMitin script .
PS: Today I used Habrom with this script, I came to the conclusion that for most topics (except for those published on the Humor blog), it’s best for me to hide comments in which there are less than 8 words.

')

Source: https://habr.com/ru/post/39672/


All Articles