MineGeekYT / Pikabu Comments

// ==UserScript==
// @name         Pikabu Comments
// @namespace    https://github.com/MineGeekYT
// @version      0.1
// @description  New comments became more user friendly :3
// @author       MineGeek
// @match        http://pikabu.ru/story/*
// @grant        none
// ==/UserScript==

function upTo(el, tagName) {
	//tagName = tagName.toLowerCase();
	while (el && el.parentNode) {
      	el = el.parentNode;
        if (el.id === tagName) {
			//console.log(el.id);
            return el;
		}
        //else {
         	//console.log(el.id);   
        //}
	}
    //console.log("null");
	return null;
}

//var newComments = document.getElementsByClassName("comment_msg");
var newComments = document.getElementsByClassName("comment_msg_new");
console.log("New comments count:",newComments.length);
var rawHiddenBranches = document.getElementsByClassName("b-link b-link_type_expand b-link_chrome_fix");
var rawHiddenBranches2 = document.getElementsByClassName("b-link b-link_type_expand");
console.log("Hidden branches count:",Math.max(rawHiddenBranches.length, rawHiddenBranches2.length));
var hiddenBranches = [];
var hiddenBranchesUsed = [];
if (rawHiddenBranches.length > rawHiddenBranches2.length){
    for (i = 0; i < rawHiddenBranches.length; ++i) {
        t = rawHiddenBranches[i].parentNode.parentNode.parentNode.parentNode.parentNode;
        //console.log(i,' ',t);
        hiddenBranches[i] = t;
        hiddenBranchesUsed[i] = false;
	}
}
else {
 	for (i = 0; i < rawHiddenBranches2.length; ++i) {
        t = rawHiddenBranches2[i].parentNode.parentNode.parentNode.parentNode.parentNode;
        //console.log(i,' ',t);
        hiddenBranches[i] = t;
        hiddenBranchesUsed[i] = false;
	}   
}


for (i = 0; i < newComments.length; ++i) {
    for (j = 0; j < hiddenBranches.length; ++j){
        if  (!(hiddenBranchesUsed[j])){
            hiddenParent = upTo(newComments[i], hiddenBranches[j].id);
            //console.log(hiddenParent,' ',hiddenBranches[j].id);
            if (hiddenParent != null) {
                //console.log(hiddenParent," ",hiddenParent.children[0]);
                cur = hiddenParent.children[0];
                cur.style["background-color"] = "#ffffff";
                cur.children[0].children[0].style["background-color"] = "#ffffcc";
                console.log(hiddenBranches[j].id, "is used");
                hiddenBranchesUsed[j] = true;
            }
        }
    }
        
}