VR test

fah tracker

Bookmarklet:
Just drag and drop the link in your bookmarks. fah_tracker. fah tracker

The code:

javascript: (function () {
    function callback() {
        (function ($) {
            var jQuery = $; /* check on what page we are */
            if (window.location.href.startsWith("https://apps.foldingathome.org/teamstats/")) {
                console.log("apps");
            } /* Add reset/store button if absent */
            if (!$("#update").length) {
                var r = $('<input type="button" value="new button" id="update" />');
                $("#header").append(r);
                re = new RegExp("Team (\\d+) stats");
                teamNumber = re.exec($("h2").html())[1];
                
                firstTable=$("table:first").find("td");
                score=$(firstTable)[1].innerText;
                wu=$(firstTable)[2].innerText;
                rank=$(firstTable)[3].innerText;

                table = $(".members");
                $('#update').on('click', function () {
                    if (localStorage.getItem("fah_new_table_"+teamNumber)) {
                        localStorage.setItem("fah_table_"+teamNumber, localStorage.getItem("fah_new_table_"+teamNumber));
                        localStorage.setItem("fah_date_"+teamNumber, $("h3").html());
                        localStorage.setItem("fah_score_"+teamNumber, localStorage.getItem("fah_new_score_"+teamNumber));
                        localStorage.setItem("fah_wus_"+teamNumber, localStorage.getItem("fah_new_wus_"+teamNumber));
                        localStorage.setItem("fah_rank_"+teamNumber,localStorage.getItem("fah_new_rank_"+teamNumber));
                        window.location.reload();
                    } else {
                        localStorage.setItem("fah_table_"+teamNumber, table.html());
                        localStorage.setItem("fah_date_"+teamNumber, $("h3").html());
                        localStorage.setItem("fah_score_"+teamNumber, score);
                        localStorage.setItem("fah_wus_"+teamNumber, wu);
                        localStorage.setItem("fah_rank_"+teamNumber, rank);
                        window.location.reload();
                    }
                });
                if (localStorage.getItem("fah_table_"+teamNumber)) {
                    $("#update").attr("value", "Update ref");
                    /* store the unmodified data in case the user wants to reset the ref */
                    localStorage.setItem("fah_new_table_"+teamNumber, table.html());
                    localStorage.setItem("fah_new_score_"+teamNumber, score);
                    localStorage.setItem("fah_new_wus_"+teamNumber, wu);
                    localStorage.setItem("fah_new_rank_"+teamNumber, rank);

                    var oldTable = $(localStorage.getItem("fah_table_"+teamNumber));
                    date = $("h3");
                    date.hide();
                    r = $('<div id="date_field"/></div>');
                    r.insertAfter(date);
                    var parsedDateLog = Date.parse(date.html()); /*var usaTime = parsedDate.toLocaleString("en-US", {timeZone: "America/Los_Angeles"});*/
                    var usaTime = new Date(parsedDateLog);
                    parsedDate = Date.parse(localStorage.getItem("fah_date_"+teamNumber));
                    var refTime = new Date(parsedDate);
                    r.html('<h2>Log dates</h2><table><tbody><tr><th>Log date</th><td>' + usaTime + "</td></tr><tr><th>ref date</th><td>" + refTime + "</td></tr><tr><th>delta</th><td>" + Math.floor((parsedDateLog - parsedDate) / (1000 * 3600)) + "hrs</td></tr></tbody></table><h2>Team details</h2>");
                    
                    delta_team_score = score-localStorage.getItem("fah_score_"+teamNumber);
                    if(delta_team_score) {
                    	$(firstTable[1]).html(" "+score +" <font color=\"green\"><strong>(+"+delta_team_score+")</strong></font>");
                    }

                    delta_team_wus = wu-localStorage.getItem("fah_wus_"+teamNumber);
                    if(delta_team_wus) {
                    	$(firstTable[2]).html(" "+wu +" <font color=\"green\"><strong>(+"+delta_team_wus+")</strong></font>");
                    }

					delta_team_rank = rank-localStorage.getItem("fah_rank_"+teamNumber);
                    if(delta_team_rank<0) {
                    	$(firstTable[3]).html(" "+rank +" <font color=\"green\"><strong>("+delta_team_rank+")</strong></font>");
                    } else if(delta_team_rank>0) {
                    	$(firstTable[3]).html(" "+rank +" <font color=\"red\"><strong>(+"+delta_team_rank+")</strong></font>");
                    }

                    table.find("tr").each(function () {
                        var td = $(this).find("td"); /* only process the row that are relevant, not tr for example */
                        if (td.length !== 0) {
                            var user_data = $(td).map(function () {
                                return this.innerHTML;
                            }); /* Find a similarly named user in the old table */
                            var old_user_row = oldTable.find("tr:contains(" + user_data[2] + ")");
                            if (old_user_row.length === 0) {
                                $(td[3]).html(user_data[3] + " (<font color=\"green\">NEW</font>)");
                                $(td[0]).html(user_data[0] + " (<font color=\"green\">NEW</font>)");
                                $(td[1]).html(user_data[1] + " (<font color=\"green\">NEW</font>)");
                            } else if (old_user_row.length > 1) {
                                /* similar names, need to dig deeper */ } else {
                                var old_user_data = $(old_user_row[0]).find("td").map(function () {
                                    return this.innerHTML;
                                });
                                var delta_points = (user_data[3] - old_user_data[3]);
                                var delta_rank = (user_data[0] - old_user_data[0]);
                                var delta_team_rank = (user_data[1] - old_user_data[1]);
                                var delta_wus = (user_data[4] - old_user_data[4]);
                                if (delta_points) {
                                    var cellUpdate = $(td[3]);
                                    cellUpdate.html(user_data[3] + " (<font color=\"green\">+" + delta_points + "</font>)");
                                }
                                if (delta_rank) {
                                    cellUpdate = $(td[0]);
                                    if (delta_rank > 0) {
                                        cellUpdate.html(user_data[0] + " (<font color=\"red\">+" + delta_rank + "</font>)");
                                    } else {
                                        cellUpdate.html(user_data[0] + " (<font color=\"green\"> " + delta_rank + "</font>)");
                                    }
                                }
                                if (delta_team_rank) {
                                    cellUpdate = $(td[1]);
                                    if (delta_team_rank > 0) {
                                        cellUpdate.html(user_data[1] + " (<font color=\"red\">+" + delta_team_rank + "</font>)");
                                    } else {
                                        cellUpdate.html(user_data[1] + " (<font color=\"green\"> " + delta_team_rank + "</font>)");
                                    }
                                }
                                if (delta_wus) {
                                    cellUpdate = $(td[4]);
                                    cellUpdate.html(user_data[4] + " (<font color=\"green\">+" + delta_wus + "</font>)");
                                }
                            }
                        }
                    });
                } else {
                    $("#update").attr("value", "Store");
                }
            }
        })(jQuery.noConflict(true))
    }
    var s = document.createElement("script");
    s.src = "https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js";
    if (s.addEventListener) {
        s.addEventListener("load", callback, false)
    } else if (s.readyState) {
        s.onreadystatechange = callback
    }
    document.body.appendChild(s);
})()