daniel_bloodbeak / GI Match Ticker (Game section)

// ==UserScript==
// @name        GI Match Ticker (Game section)
// @namespace   https://grid-iron.org/
// @description Adds division-specific match ticker to league/AFLC games
// @include     https://grid-iron.org/match/*
// @version     1
// @grant       xttp
// @author      daniel_bloodbeak
// @license     MIT
// ==/UserScript==


// delete the line above "@license MIT" - it was only necessary to get the script uploaded to the website

window.setTimeout(function () {

  var matchNums = 0;
  var storeItem = ["match1", "match2", "match3", "match4", "match5", "match6", "match7", "match8"];
  var divisionDetails = sessionStorage.getItem("leagueDetails");
  var divisionArr = JSON.parse(divisionDetails);
  var matchList = [];
  var aflc = parseInt(sessionStorage.aflc);
  var leagueHeader = sessionStorage.leagueHeader;
  var bodyWidth = [380, 60, 420];
  var timeArr = [
    [18, 26, 1],
    [11, 11, 0]
  ];
  var playTimeArr = [
    [10, 10],
    [9, 25]
  ];
  var bodyCellId = ["gameDiv", "clockDiv", "lastPlayDiv"];
  var boxNameId = ["homeName", "awayName"];
  var boxScoreId = ["homeScore", "awayScore"];
  var playCellId = ["basics", "details"];
  var timeBoxId = ["quarter", "toGo"];
  var day = ["SUN", "MON", "TUE", "WED", "THU", "FRI", "SAT"];

  if (aflc === 1) {
    matchNums = 8;
  }
  else {
    matchNums = 5;
  }

  for (var a = 0; a < matchNums; a++) {
    var getMatch = sessionStorage.getItem(storeItem[a]);
    matchList.push(getMatch);
  }

  var count = 0;

  function spaceOut() {
    var interval = setInterval(doEverything, 5500);
  }

  function resetColor() {
    for (var dd = 0; dd < boxNameId.length; dd++) {
      document.getElementById(boxNameId[dd]).style.background = "linear-gradient(#eeeeee, #aaaaaa)";
      document.getElementById(boxScoreId[dd]).style.background = "linear-gradient(#eeeeee, #aaaaaa)";
    }
  }

  var matchBox = document.createElement("div");
  matchBox.setAttribute("id", "matchTicker");
  matchBox.setAttribute("style", "margin-left: auto; margin-right: auto; margin-top: 1px; margin-bottom: 25px; visibility: hidden; background: transparent; width: 780px; height: 58px; position: relative;");
  var divHeader = document.createElement("div");
  divHeader.innerHTML = "&nbsp;&nbsp;&nbsp;" + leagueHeader.toUpperCase();
  divHeader.setAttribute("style", "padding-bottom: 2px; margin-bottom: 1px; font-style: italic; position: relative; color: white; font-weight: bold; border-radius: 5px; left: 2px; width: 776px; height: 16px; background: linear-gradient(to right, #777777, #dddddd); font-size: 12px; text-shadow: 1px 1px #884400; ");
  var linkButton = document.createElement("div");
  linkButton.innerHTML = "&nbsp;&nbsp;GAME LINK";
  linkButton.setAttribute("id", "linkButton");
  linkButton.setAttribute("class", "gui_button2");
  linkButton.setAttribute("style", "font-weight: bold; color: #222222; display: inline-flex; align-items: center; text-align: center; width: 78px; float: right; height: 12px; position: relative; top: 1px; left: -2px; color: #222222; text-shadow: 1px 0px #eeeeee;");
  divHeader.appendChild(linkButton);
  var gameTable = document.createElement("table");
  var gameBody = document.createElement("tbody");
  gameBody.setAttribute("style", "width: 770px; top: -1px; left: 0px; border: 2px transparent; border-radius: 5px; background: transparent; width: 660px; position: relative;");
  var bodyRow = document.createElement("tr");
  for (var b = 0; b < bodyWidth.length; b++) {
    var bodyCell = document.createElement("td");
    bodyCell.setAttribute("id", bodyCellId[b]);
    bodyCell.setAttribute("style", "border: 2px solid; margin-right: 38px; border-radius: 5px; background: linear-gradient(#999999, #555555); width: " + bodyWidth[b] + "px; height: 44px; position: relative; top: 0px; left: 0px;");
    bodyRow.appendChild(bodyCell);
  }
  gameBody.appendChild(bodyRow);
  gameTable.appendChild(gameBody);

  matchBox.appendChild(divHeader);
  matchBox.appendChild(gameTable);

  var iconContainer = document.getElementById("content_container");
  iconContainer.insertBefore(matchBox, iconContainer.childNodes[0]);

  var boxScore = document.createElement("table");
  var boxScoreBody = document.createElement("tbody");
  for (b = 0; b < 2; b++) {
    var gameRow = document.createElement("tr");
    var gameCell = document.createElement("td");
    gameCell.setAttribute("id", boxNameId[b]);
    gameCell.setAttribute("style", "text-shadow: 1px 1px #cccccc; font-weight: bold; font-size: 14px; width: 290px ; text-align: center; height: 20px; border: 1px solid; border-radius: 3px; background: linear-gradient(#eeeeee, #aaaaaa);");

    var scoreCell = document.createElement("td");
    scoreCell.setAttribute("id", boxScoreId[b]);
    scoreCell.setAttribute("style", "text-shadow: 1px 1px #cccccc; font-weight: bold; font-size: 18px; width: 40px ; text-align: center; height: 20px; border: 1px solid; border-radius: 3px; background: linear-gradient(#eeeeee, #aaaaaa);");
    gameRow.appendChild(gameCell);
    gameRow.appendChild(scoreCell);
    boxScoreBody.appendChild(gameRow);
  }

  boxScore.appendChild(boxScoreBody);
  document.getElementById("gameDiv").appendChild(boxScore);

  var timeBox = document.createElement("table");
  var timeBody = document.createElement("tbody");
  timeBody.setAttribute("id", "timeBody");
  var timeRow, timeCell;
  for (var c = 0; c < 2; c++) {
    timeRow = document.createElement("tr");
    timeCell = document.createElement("td");
    timeCell.setAttribute("id", timeBoxId[c]);
    timeCell.setAttribute("style", "text-shadow: 1px 1px #aaaaaa; position: relative; top: " + timeArr[c][2] + "px; font-weight: bold; font-size: " + timeArr[c][0] + "px; width: 55px ; text-align: center; height: " + timeArr[c][1] + "px; border: 1px solid; border-radius: 3px; background: linear-gradient(#eeeeee, #aaaaaa);");
    timeRow.appendChild(timeCell);
    timeBody.appendChild(timeRow);
  }

  timeBox.appendChild(timeBody);
  document.getElementById("clockDiv").appendChild(timeBox);

  var playBox = document.createElement("table");
  var playBody = document.createElement("tbody");
  var playRow, playCell;
  for (c = 0; c < 2; c++) {
    playRow = document.createElement("tr");
    playCell = document.createElement("td");
    playCell.setAttribute("id", playCellId[c]);
    playCell.setAttribute("style", "text-shadow: 1px 1px #aaaaaa; font-weight: bold; font-size: " + playTimeArr[c][0] + "px; width: 365px ; text-align: center; height: " + playTimeArr[c][1] + "px; border: 1px solid; border-radius: 3px; background: linear-gradient(#eeeeee, #aaaaaa);");
    playRow.appendChild(playCell);
    playBody.appendChild(playRow);
  }

  playBox.appendChild(playBody);
  document.getElementById("lastPlayDiv").appendChild(playBox);

  spaceOut();

  function doEverything() {
    var homeName, awayName, homeFlag, awayFlag, homeScore, awayScore, qtr, possession, timeLeft, homeStatus, awayStatus, mins, playLine, lastPlay, playType;
    var distance, yardLineFind, yardLineClean, yardLine, playLocate, playSort, playFilter, attendanceLocate, attendanceGather, attendance, weather;
    var firstArr = [];
    var secondArr = [];
    var clockArr = [];
    var dataArr = [];
    var statusArr = [];
    var playArr = [];

    var xhttp = new XMLHttpRequest();
    xhttp.onreadystatechange = function () {
      if (this.readyState == 4 && this.status == 200) {
        var dataBlock = this.responseText.split("top_panel");
        var testBlock = dataBlock[1].split("div");
        var testItem = testBlock[2].slice(8, 20);
        if (testItem.match(/match_review/i)) {
          var teamHunt = testBlock[4].split(">");
          homeName = teamHunt[7].slice(0, -4);
          awayName = teamHunt[11].slice(0, -4);
          for (var z = 0; z < divisionArr.length; z++) {
            if (homeName === divisionArr[z][0]) {
              homeFlag = divisionArr[z][1];
              homeScore = divisionArr[z][3] + "-" + divisionArr[z][4];
            }
            if (awayName === divisionArr[z][0]) {
              awayFlag = divisionArr[z][1];
              awayScore = divisionArr[z][3] + "-" + divisionArr[z][4];
            }
          }

          homeStatus = "";
          awayStatus = "";
          playLine = "";
          lastPlay = "";
          possession = "";
          var dateHunt = new Date();
          var yearHunt = dateHunt.getUTCFullYear();
          var monthHunt = dateHunt.getUTCMonth();
          var dayHunt = dateHunt.getUTCDay();
          var dateNum = dateHunt.getUTCDate();
          var gameMonth = parseInt(teamHunt[18].slice(5, -6));
          var gameDate = parseInt(teamHunt[18].slice(8, -3));
          var daysToGo;
          if (gameMonth > (monthHunt + 1) || gameMonth < monthHunt) {
            if (gameMonth === 3 && yearHunt / (yearHunt / 4) !== 4) {
              gameDate += 28;
            }
            else if (gameMonth === 3 && yearHunt / (yearHunt / 4) === 4) {
              gameDate += 29;
            }
            else if (gameMonth === 5 || gameMonth === 7 || gameMonth === 10 || gameMonth === 12) {
              gameDate += 30;
            }
            else {
              gameDate += 31;
            }
          }
          daysToGo = gameDate - dateNum;
          var actualDay = dayHunt + daysToGo;
          if (actualDay > 6) {
            actualDay -= 7;
          }
          qtr = day[actualDay];
          timeLeft = teamHunt[20].slice(0, -3);
          matchBox.style.visibility = "visible";
          for (var de = 0; de < boxNameId.length; de++) {
            document.getElementById(boxNameId[de]).style.background = "linear-gradient(#eeeeee, #aaaaaa)";
            document.getElementById(boxScoreId[de]).style.background = "linear-gradient(#eeeeee, #aaaaaa)";
          }
        }
        else {
          var dataSplit = dataBlock[1].split("div");
          var homeLocate = dataSplit[9].split(">");
          homeName = homeLocate[3].slice(0, -3);
          var homeHunt = homeLocate[1].split("png");
          homeFlag = homeHunt[1].slice(1, -1);
          var awayLocate = dataSplit[11].split(">");
          awayName = awayLocate[3].slice(0, -3);
          var awayHunt = awayLocate[1].split("png");
          awayFlag = awayHunt[1].slice(1, -1);
          var homeStatusLocate = dataSplit[17].split(">");
          var awayStatusLocate = dataSplit[19].split(">");
          homeStatus = homeStatusLocate[2].slice(0, -6);
          awayStatus = awayStatusLocate[2].slice(0, -6);
          var minFinder = dataSplit[28].slice(-4);
          mins = minFinder.slice(0, 2);
          homeScore = dataSplit[22].slice(17, -2);
          awayScore = dataSplit[26].slice(17, -2);
          var qLocate = dataBlock[1].split("comments");
          var qtrLocate = qLocate[1].split(">");
          qtr = qtrLocate[2].slice(6, 9);
          var timeLocate = qtrLocate[4].split("<");
          timeLeft = timeLocate[0].slice(-5);
          possession = timeLocate[0].slice(6, -6);
          var downLocate = qtrLocate[7].split("-");
          var down = downLocate[0].slice(0, 3);
          if (down === "\</d" || down === "&nb") {
            down = "a";
          }
          if (down === "a") {
            playType = qtrLocate[9].slice(0, 4);
          }
          else {
            playType = qtrLocate[9].slice(0, 4);
            distance = downLocate[1];
            yardLineFind = downLocate[2].slice(0, 5);
            yardLineClean = yardLineFind.replace(/\D/g, "");
            yardLine = parseInt(yardLineClean);
          }
          playLocate = dataBlock[1].split("event_description");
          playSort = playLocate[1].split("\</div>");
          playFilter = playSort[0].split(",,");
          lastPlay = playFilter[0].slice(2);
          var fgSort = playLocate[2].split("\</div>");
          var fgFilter = fgSort[0].slice(2);
          var tdSort = playLocate[3].split("!");
          var tdFilter = tdSort[0];
          var lastTD = tdFilter.slice(2);
          if (mins === "FT" && homeScore === awayScore) {
            qtr = "OT";
            var otBlock = dataBlock[1].split("quarter_5");
            var possBlock = otBlock[2].split("owner_switch");
            var possLocate = possBlock[possBlock.length - 1].split(">");
            possession = possLocate[1].slice(6);
            var lastPlayBlock = possBlock[possBlock.length - 1].split("event_down");
            var lpbDetails = lastPlayBlock[lastPlayBlock.length - 1].split("event_");
            var playDepth = lpbDetails[0].split("-");
            down = playDepth[0].slice(-3);
            if (down === "\</d" || down === "&nb") {
              down = "a";
            }
            if (down === "a") {
              playType = lpbDetails[1].slice(6, 10);
            }
            else {
              distance = playDepth[1];
              playType = lpbDetails[1].slice(6, 10);
              yardLineFind = playDepth[2].slice(0, 5);
              yardLineClean = yardLineFind.replace(/\D/g, "");
              yardLine = parseInt(yardLineClean);
            }
            playLocate = possBlock[possBlock.length - 1].split("event_description");
            playSort = playLocate[playLocate.length - 1].split("\</div>");
            playFilter = playSort[0].split(",,");
            lastPlay = playFilter[0].slice(2);
          }
          var weatherLocate = dataBlock[1].split("weather_icon");
          var weatherClean = weatherLocate[1].split(".gif");
          weather = parseInt(weatherClean[0].slice(-1));
          if (weather === 0) {
            weather = 1;
          }
          attendanceLocate = dataBlock[1].split("venue_info");
          attendanceGather = attendanceLocate[1].split("\</div>");
          attendance = attendanceGather[0].slice(2);

          matchBox.style.visibility = "visible";
          for (var d = 0; d < boxNameId.length; d++) {
            document.getElementById(boxNameId[d]).style.background = "linear-gradient(#eeeeee, #aaaaaa)";
            document.getElementById(boxScoreId[d]).style.background = "linear-gradient(#eeeeee, #aaaaaa)";
          }

          if (playType === "Kick") {
            if (fgFilter.search(/field goal/i) === -1) {
              lastPlay = lastTD + "! \<br></b>Extra point attempt is good.";
              playType = "TOUCHDOWN";
            }
            else {
              lastPlay = fgFilter;
              playType = "FIELD GOAL";
            }
            if (homeName === possession) {
              document.getElementById("homeName").style.background = "linear-gradient(#aaffaa, #eeeeee)";
              document.getElementById("homeScore").style.background = "linear-gradient(#aaffaa, #eeeeee)";
              document.getElementById("awayName").style.background = "linear-gradient(#eeeeee, #aaaaaa)";
              document.getElementById("awayScore").style.background = "linear-gradient(#eeeeee, #aaaaaa)";
            }
            else if (awayName === possession) {
              document.getElementById("awayName").style.background = "linear-gradient(#aaffaa, #eeeeee)";
              document.getElementById("awayScore").style.background = "linear-gradient(#aaffaa, #eeeeee)";
              document.getElementById("homeName").style.background = "linear-gradient(#eeeeee, #aaaaaa)";
              document.getElementById("homeScore").style.background = "linear-gradient(#eeeeee, #aaaaaa)";
            }
            else {
              resetColor();
            }
          }

          if (playType === "Fiel") {
            playType = "FIELD GOAL";
          }

          if (down === "a") {
            playLine = possession.toUpperCase() + " " + playType.toUpperCase();
          }
          else {
            playLine = "\<b>" + down.toUpperCase() + " AND " + distance + " - " + possession.toUpperCase() + "\</b> at the " + yardLine + " yard line";
            if (yardLine > 79 && homeName === possession) {
              document.getElementById("homeName").style.background = "linear-gradient(#ffaaaa, #eeeeee)";
              document.getElementById("homeScore").style.background = "linear-gradient(#ffaaaa, #eeeeee)";
            }
            else if (yardLine > 79 && awayName === possession) {
              document.getElementById("awayName").style.background = "linear-gradient(#ffaaaa, #eeeeee)";
              document.getElementById("awayScore").style.background = "linear-gradient(#ffaaaa, #eeeeee)";
            }
            else {
              resetColor();
            }
          }

        }

        matchBox.style.visibility = "visible";

        clockArr.push(qtr, timeLeft);
        statusArr.push(homeStatus, awayStatus);
        playArr.push(playLine, lastPlay);
        firstArr.push(homeName, homeScore, homeFlag, homeStatus);
        secondArr.push(awayName, awayScore, awayFlag, awayStatus);
        dataArr.push(firstArr, secondArr);

        for (var b = 0; b < 2; b++) {
          var gameCellUpdate = document.getElementById(boxNameId[b]);
          var scoreCellUpdate = document.getElementById(boxScoreId[b]);
          var playCellUpdate = document.getElementById(playCellId[b]);
          gameCellUpdate.innerHTML = dataArr[b][0];
          var flagImg = document.createElement("img");
          flagImg.setAttribute("src", "https://grid-iron.org/images/flags/byid/png/" + dataArr[b][2] + ".png");
          flagImg.setAttribute("style", "float: left; margin-left: 2px; margin-top: 1px;");
          gameCellUpdate.appendChild(flagImg);

          var statusImg = document.createElement("img");
          if (testItem !== "match_review") {
            if (statusArr[b] === "Online!") {
              statusImg.setAttribute("src", "https://grid-iron.org/images/forum/online.png");
            }
            else {
              statusImg.setAttribute("src", "https://grid-iron.org/images/forum/offline.png");
            }
            statusImg.setAttribute("style", "float: left; border: 1px solid; border-radius: 4px; margin-top: 3px; margin-left: 2px; border-color: #cccccc;");
            gameCellUpdate.appendChild(statusImg);
          }
          scoreCellUpdate.innerHTML = dataArr[b][1];
          if (testItem === "match_review") {
            scoreCellUpdate.style.fontSize = "12px";
          }
          playCellUpdate.innerHTML = playArr[b];
        }

        var scoreHome = parseInt(dataArr[0][1]);
        var scoreAway = parseInt(dataArr[1][1]);
        var timeRow, timeCell;
        if (mins === "FT" && qtr !== "OT") {
          document.getElementById("timeBody").innerHTML = "";
          timeRow = document.createElement("tr");
          timeCell = document.createElement("td");
          timeCell.innerHTML = "FINAL";
          timeCell.setAttribute("id", timeBoxId[0]);
          timeCell.setAttribute("style", "text-shadow: 1px 1px #aaaaaa; font-weight: bold; font-size: 14px; width: 55px ; text-align: center; height: 48px; border: 1px solid; border-radius: 3px; background: linear-gradient(#eeeeee, #aaaaaa);");
          timeRow.appendChild(timeCell);
          timeBody.appendChild(timeRow);
          timeBox.appendChild(timeBody);
          document.getElementById("clockDiv").appendChild(timeBox);
          var newBasic;
          var plural = "s";
          if (scoreHome - scoreAway === 1 || scoreAway - scoreHome === 1) {
            plural = "";
          }
          switch (true) {
            case (scoreHome > scoreAway):
              newBasic = document.getElementById("basics");
              newBasic.innerHTML = homeName + " won by " + (scoreHome - scoreAway) + " point" + plural + ".";
              break;
            default:
              newBasic = document.getElementById("basics");
              newBasic.innerHTML = awayName + " won by " + (scoreAway - scoreHome) + " point" + plural + ".";
          }
          var weatherIcon = document.createElement("img");
          weatherIcon.setAttribute("src", "https://grid-iron.org/images/icons/" + weather + ".gif");
          weatherIcon.setAttribute("style", "float: left; height: 25px; width: 30px; padding-left: 5px;");

          var wipeDetails = document.getElementById("details");
          wipeDetails.innerHTML = "Played at " + attendance;
          wipeDetails.appendChild(weatherIcon);
        }
        else if (mins === "FT" && qtr === "OT") {
          document.getElementById("timeBody").innerHTML = "";
          timeRow = document.createElement("tr");
          timeCell = document.createElement("td");
          timeCell.setAttribute("id", timeBoxId[0]);
          timeCell.innerHTML = "OT";
          timeCell.setAttribute("style", "text-shadow: 1px 1px #aaaaaa; font-weight: bold; font-size: 14px; width: 55px ; text-align: center; height: 48px; border: 1px solid; border-radius: 3px; background: linear-gradient(#eeeeee, #aaaaaa);");
          timeRow.appendChild(timeCell);
          timeBody.appendChild(timeRow);
          timeBox.appendChild(timeBody);
          document.getElementById("clockDiv").appendChild(timeBox);
        }
        else {
          document.getElementById("timeBody").innerHTML = "";

          for (var c = 0; c < 2; c++) {
            timeRow = document.createElement("tr");
            timeCell = document.createElement("td");
            timeCell.setAttribute("id", timeBoxId[c]);
            timeCell.setAttribute("style", "text-shadow: 1px 1px #aaaaaa; position: relative; top: " + timeArr[c][2] + "px; font-weight: bold; font-size: " + timeArr[c][0] + "px; width: 55px ; text-align: center; height: " + timeArr[c][1] + "px; border: 1px solid; border-radius: 3px; background: linear-gradient(#eeeeee, #aaaaaa);");
            timeRow.appendChild(timeCell);
            timeBody.appendChild(timeRow);
            var timeCellUpdate = document.getElementById(timeBoxId[c]);
            timeCellUpdate.innerHTML = clockArr[c].toUpperCase();
          }
        }
        var hBall = document.createElement("img");
        hBall.setAttribute("src", "https://grid-iron.org/images/match/ball.png");
        hBall.setAttribute("style", "height: 10px; width: 15px; padding-top: 4px; padding-right: 4px; float: right; opacity: 0.7;");
        var aBall = document.createElement("img");
        aBall.setAttribute("src", "https://grid-iron.org/images/match/ball.png");
        aBall.setAttribute("style", "height: 10px; width: 15px; padding-top: 4px; padding-right: 4px; float: right; opacity: 0;");
        var homeBall = document.getElementById("homeName");
        var awayBall = document.getElementById("awayName");
        if (homeName === possession) {
          homeBall.appendChild(hBall);
          awayBall.appendChild(aBall);
        }
        if (awayName === possession) {
          homeBall.appendChild(aBall);
          awayBall.appendChild(hBall);
        }

      }
    };
    xhttp.open("GET", "https://grid-iron.org/match/" + matchList[count], true);
    xhttp.send();

    var addLink = document.getElementById("linkButton");
    addLink.setAttribute("onclick", "document.location.href = 'https://grid-iron.org/match/match_id/" + matchList[count] + "'");
    count += 1;

    if (count > 7 && Number(aflc) === 1) {
      count = 0;
    }
    if (count > 4 && Number(aflc) === 0) {
      count = 0;
    }

  }
}, 100);