Page 1 of 1

JS Rank calculator

Posted: Thu Mar 03, 2016 5:56 pm
by Napoleon
Well not to surprise everyone, but the "Rank" does not equal the row you're on, in the top list.

This annoyed me so much that I made a js script that adds the row number to the table.

Code: Select all

if(location.href.substr(-7) == 'top.php'){
    alert("this could take up to 10 sec");
    var tbody = document.getElementsByTagName('tbody')[document.getElementsByTagName('tbody').length-1].getElementsByTagName('tr');
    for(var i=0;i<tbody.length;i++){
        tbody[i].innerHTML += "<td>"+i+"</td>";
    }
}
It would be a lot nicer, if the mods could build this into the system :D

Posted: Fri Mar 04, 2016 11:18 am
by helly0d
I see your point, even though the rank on the challenges section wouldn't be your actual rank.

I think you could speed up your code by avoiding to trigger multiple repaints. Take a look at this:

Code: Select all

(function() {
    if(location.href.substr(-7) !== "top.php"){ 
        return;
    }
    
    // var date = Date.now();
    var tbody = document.getElementsByTagName("tbody");
    tbody = tbody[tbody.length - 1];
    var clonedTable = tbody.cloneNode(true);
    var tRows = clonedTable.getElementsByTagName("tr");

    var header = tRows[0];
    var row = document.createElement("th");
    row.innerHTML = "Rank";
    header.appendChild(row);


    for (var i = 1, n = tRows.length; i < n; i += 1) {
        row = document.createElement("td");
        row.innerHTML = i;
        tRows[i].appendChild(row);
    }

    var parent = tbody.parentNode;
    parent.replaceChild(clonedTable, tbody);
    // console.log(Date.now() - date);
}());
PS: try to encapsulate your code in Self-Invoked-Anonymous (SIA) functions in order to avoid polluting the global object ( window ).