MatchResult-Review Script

View match result in advance

От 25.07.2018. Виж последната версия.

// ==UserScript==
// @name         MatchResult-Review Script
// @namespace    https://greasyfork.org/users/198348
// @version      0.0.1
// @description  View match result in advance
// @author       TM
// @include	     http://trophymanager.com/matches/*
// @include	     https://trophymanager.com/matches/*
// ==/UserScript==

function insertBefore(el, referenceNode) {
  referenceNode.parentNode.insertBefore(el, referenceNode);
}

function insertAfter(el, referenceNode) {
  referenceNode.parentNode.insertBefore(el, referenceNode.nextSibling);
}

function showMatchResult() {
  var resultDiv = document.createElement('div');
  var matchID = location.href.match(/([^\/]*)\/*$/)[1];
  var xhr = new XMLHttpRequest();
  var url = 'https://trophymanager.com/ajax/match.ajax.php?id=' + matchID;
  resultDiv.className = 'main_center';

  xhr.open('GET', url, true);
  xhr.send();
  xhr.onreadystatechange = function() {
    if (this.readyState == 4 && this.status == 200) {
	  var data = JSON.parse(this.responseText);
      var homeClub = data.club.home.club_name;
	  var awayClub = data.club.away.club_name;
      var report = data.report;
      var scoreData = report[Object.keys(report).sort().pop()];
      var finalText = scoreData[0].chance.text[0];
      var finalScore = 'vs';
      if (report) {
        var finalTxtArray = finalText.toString().split(' ');
        finalTxtArray.forEach(function (str) {
	      if (str.indexOf('-') > -1) {
		    finalScore = str;
		  }
	    });
	  }
	  var htmlTxt = '<div style="width:100%;text-align:center;margin-top:10px;padding:10px;background-color:#000000;font-family:arial;font-size:15px;font-weight:bold;">';
	  htmlTxt += homeClub + '<span style="padding-left:10px;padding-right:10px;color:#CF0;">' + finalScore + '</span>' + awayClub + '</div>'
      resultDiv.innerHTML = htmlTxt;
      var mainCenters = document.getElementsByClassName('main_center');
	  var lastMainDiv = mainCenters[mainCenters.length - 1];
        if (lastMainDiv) {
		  insertBefore(resultDiv, lastMainDiv);
        }
    }
  };
}

(function() {
  'use strict';
  showMatchResult();
})();