playerx / Skill Index Calculator - Playing XI

// ==UserScript==
// @name       Skill Index Calculator - Playing XI
// @namespace  http://playerx.hitwicket.com/
// @version    1.2
// @description  Script to calculate skill index of playing xi
// @include      http*://*hitwicket.com/match/show/*details=1*
// @grant        GM_addStyle
// @copyright  2012+, PlayerX
// ==/UserScript==
GM_addStyle(".title { color: #eee;font-family: 'LeagueGothicRegular', 'HelveticaNeue-Light', 'Helvetica Neue Light', 'Helvetica Neue', Helvetica, Arial, 'Lucida Grande', sans-serif; text-transform: uppercase;font-size: 2rem;width: 100%; }");
    
    
function interceptAjax () {
    var playerList = [];
    
    function addPlaceholderTeamLeft() {
     if($("#left_si").size() === 0) {
            var element = $("a.team_left").parent();
            element.append("<div><div class='title' >Playing XI SI : <span id='left_si' class='si' style='color:#4faaff;'>---</span></div></div>");

        }   
        
    }
    
    function addPlaceholderTeamRight() {
     if($("#right_si").size() === 0) {
            console.debug("adding");
            var element = $("a.team_right").parent();
            console.debug("element:" + element);
            element.append("<div><div class='title' >Playing XI SI : <span id='right_si' style='color:#ff4d4d !important;'>---</span></div></div>");

        }   
        
    }
    
    function updateSkillIndexCalculation() {
        console.debug("Skill index calculator playing xi");        
        setTimeout(function() { 
            var totalSkillIndex = 0;
            var skillIndexArray = [];
            
            addPlaceholderTeamLeft();
            addPlaceholderTeamRight();
            var teamLeft = $("a.team_left").text().trim();
            var teamRight = $("a.team_right").text().trim();
            
            var teamLeftDivId = "second_innings_scorecard";
            var teamRightDivId = "first_innings_scorecard";
            if($("div#first_innings_scorecard span.teamname").text().trim() == teamLeft) {
                teamLeftDivId = "first_innings_scorecard";
                teamRightDivId = "second_innings_scorecard";
            }
            var teamLeftPlayers = $("div#" + teamLeftDivId + " table.match_table").first().find("span.player_popover");
            var teamRightPlayers = $("div#" + teamRightDivId + " table.match_table").first().find("span.player_popover");
           for(var i=0; i<=1;i++) {
               var players = i == 0 ? teamLeftPlayers : teamRightPlayers;
               $.each( players, function( index, player ) {
                var content = $(player).attr("data-content");
                var skillIndex = $($("<div>" + content + "</div>").find("span strong")[1]).text();
                skillIndex = skillIndex.split(",");
                skillIndex = skillIndex[0] + skillIndex[1];
                skillIndexArray.push(parseInt(skillIndex));
                totalSkillIndex += parseInt(skillIndex);
               });
               $(i==0 ? "#left_si" : "#right_si").html(totalSkillIndex.toLocaleString());
               totalSkillIndex = 0;
           }
            
                   
                   
                   
        },1000);
        
    }

    $("body").ajaxSuccess (
        function (event, requestData)
        {
            updateSkillIndexCalculation();
        }
    );
    updateSkillIndexCalculation();
}

function addJS_Node (text, s_URL, funcToRun) {
    var D                                   = document;
    var scriptNode                          = D.createElement ('script');
    scriptNode.type                         = "text/javascript";
    if (text)       scriptNode.textContent  = text;
    if (s_URL)      scriptNode.src          = s_URL;
    if (funcToRun)  scriptNode.textContent  = '(' + funcToRun.toString() + ')()';

    var targ    = D.getElementsByTagName('head')[0] || D.body || D.documentElement;
    targ.appendChild (scriptNode);
}

addJS_Node ("updateSkillIndexCalculation", null, interceptAjax);
interceptAjax();