TM League Match History With Spoilers

Add last match results and link to the matchs in the league table.

  1. // ==UserScript==
  2. // @name TM League Match History With Spoilers
  3. // @name:pt TM Histórico de partidas para Liga Com Spoilers
  4. // @version 1.1
  5. // @description Add last match results and link to the matchs in the league table.
  6. // @description:pt diciona os últimos jogos e o link para o jogo na tabela da Liga.
  7. // @author Irreal Madrid FC. Club ID: 4402745
  8. // @namespace https://github.com/mayc0njr/trophyManager/spoilers/
  9. // @include https://trophymanager.com/league/*
  10. // @license MIT
  11. // ==/UserScript==
  12.  
  13. (function () {
  14.  
  15. //Custumizable variables, used to render variables on screen, this values can be changed
  16. /**
  17. * Wait till the end of the match to show the match on last column.
  18. * 0: Show the match in history even if it's still running.
  19. * 1: Show the match but not the color/result.
  20. * 2: Show the match only after it's end.
  21. */
  22. const WAIT_TILL_END = 0;
  23.  
  24. const LAST_MATCHES = 5; // number of last matches displayed
  25. const columnAdjustment = 50; //adjust the size of the middle block of the page (the one that contains the league table)
  26. const ONGOING_SUBTITLE = "Game in Progress";
  27.  
  28. const CIRCLE = '\u2b24'; //Character used as coloured ball for each one of the last matches: ⬤
  29. const WIN_COLOR = '#0fdf0f'; // RGB color for WIN
  30. const DRAW_COLOR = '#bfbfbf'; // RGB color for DRAW
  31. const LOSE_COLOR = '#d01f0f'; // RGB color for LOSE
  32. const ONGOING_COLOR = '#ffbf3f' //RGB color for ONGOING MATCHES #ffff7f
  33.  
  34. const LAST_HEADER = "Últimos"; // Last matches column header description
  35. const FONT_SIZE = "xx-small"; // size of the last matches ball
  36. const LETTER_SPACING = "2px"; // Space between last matches balls
  37.  
  38. //===================================================================
  39. // Functional variables, used to fetch and process match data.
  40. const LEAGUE = 1;
  41. const COUNTRY = 2;
  42. const DIVISION = 3;
  43. const GROUP = 4;
  44. const FIXTURES = '/ajax/fixtures.ajax.php';
  45. const WIN = 'W';
  46. const DRAW = 'D';
  47. const LOSE = 'L';
  48. const ONGOING = 'O';
  49. const TIME_REGEX = /\d{2}:\d{2}/;
  50. const WIN_INDEX = 3;
  51. const DRAW_INDEX = 4;
  52. const LOSE_INDEX = 5;
  53. const SHOW_EVERYTHING = 0;
  54. const SHOW_MATCH_LINK = 1;
  55. const SHOW_NOTHING = 2;
  56. let now = new Date();
  57. let today = new Date(); today.setUTCHours(0,0,0,0);
  58. let endOffsetHours = 2;
  59.  
  60. function adjustSize(width) {
  61. let adjust = $('.column2_a').width() + width;
  62. $('.column2_a').width(adjust);
  63. adjust = $('.main_center').width() + width;
  64. $('.main_center').width(adjust);
  65. }
  66.  
  67. function addTableHeader() {
  68. let header = $('#overall_table thead tr');
  69. let streak = document.createElement('TH');
  70. streak.textContent = LAST_HEADER;
  71. $(streak).addClass('align_center');
  72. $(streak).addClass('header');
  73.  
  74. header.append(streak);
  75. }
  76. function generateMatchsHistory() {
  77. let url = $('.content_menu .calendar').attr('href').split(`/`).filter(function (el) {
  78. return el.length > 0
  79. });
  80. console.log(url);
  81. var postobj = {
  82. 'type': url[LEAGUE],
  83. 'var1': url[COUNTRY],
  84. 'var2': url.length > (DIVISION) ? url[DIVISION] : '',
  85. 'var3': url.length > (GROUP) ? url[GROUP] : ''
  86. };
  87. console.log(postobj);
  88. $.post(FIXTURES,{
  89. 'type': url[LEAGUE],
  90. 'var1': url[COUNTRY],
  91. 'var2': url.length > (DIVISION) ? url[DIVISION] : '',
  92. 'var3': url.length > (GROUP) ? url[GROUP] : ''
  93. },function(data){
  94. if(data != null)
  95. {
  96. applyResults(data);
  97. }
  98. },'json');
  99. }
  100.  
  101. function adjustHighlight(row) {
  102. row.children().last().removeClass('highlight_td_right');
  103. }
  104. function adjustBorder(cell) {
  105. cell.removeClass('highlight_td_right_std');
  106. cell.addClass('border_right');
  107. }
  108. function filterFixtures(data) {
  109. let months = [];
  110. let matches = [];
  111. for (const key in data) {
  112. if (data.hasOwnProperty(key)) {
  113. months.push(data[key]);
  114. }
  115. }
  116. for (let index = 0; index < months.length; index++) {
  117. const thisMonth = months[index];
  118. matches = matches.concat(thisMonth.matches.filter(function testUnPlayed(match) {
  119. return match.result != null;
  120. }));
  121. }
  122. return matches;
  123. }
  124. function getTeamResults(matches, team) {
  125. let results = [];
  126. for(let index = matches.length-1; index >= 0 && results.length < LAST_MATCHES; index--) {
  127. const match = matches[index];
  128. let score = match.result.split('-');
  129. score[0] = parseInt(score[0]);
  130. score[1] = parseInt(score[1]);
  131. let result = {};
  132. let teamScore;
  133. let advScore;
  134. result.matchLink = $(match.match_link).attr('href');
  135. result.date = match.date;
  136. if(match.hometeam_name == team) {
  137. teamScore = 0;
  138. advScore = 1;
  139. }
  140. else if(match.awayteam_name == team) {
  141. teamScore = 1;
  142. advScore = 0;
  143. } else
  144. continue;
  145. result.finished = alreadyFinished(result.date);
  146. console.log("date: " + result.date);
  147. console.log("date: " + result.date);
  148. result.tooltip = match.hometeam_name + ' ' + match.result + ' ' + match.awayteam_name;
  149. if(!result.finished)
  150. {
  151. if(WAIT_TILL_END == SHOW_NOTHING)
  152. continue;
  153. if(WAIT_TILL_END == SHOW_MATCH_LINK)
  154. {
  155. result.tooltip = match.hometeam_name + ' - ' + match.awayteam_name;
  156. result.result = ONGOING;
  157. }
  158. }
  159. else if(score[teamScore] > score[advScore])
  160. result.result = WIN;
  161. else if(score[teamScore] < score[advScore])
  162. result.result = LOSE;
  163. else
  164. result.result = DRAW;
  165. results.splice(0,0,result);
  166. }
  167. return results;
  168. }
  169.  
  170. function applyResults(data) {
  171. let fixtures = filterFixtures(data);
  172. let teams = $('#overall_table tbody td a');
  173. teams.each(function(index, team) {
  174. if(team.text.length == 0)
  175. return;
  176. let row = team.parentElement.parentElement;
  177. adjustBorder($(row).children().last());
  178. if($(row).children().first().hasClass('highlight_td')) {
  179. adjustHighlight($(row));
  180. }
  181. let streak = row.insertCell(-1);
  182. let results = getTeamResults(fixtures, team.textContent);
  183. $(streak).addClass('cell_padding');
  184. $(streak).addClass('highlight_td_right_std');
  185. $(streak).css('display', 'flex');
  186. $(streak).css('justify-content','space-between');
  187. $(streak).css('font-size', FONT_SIZE);
  188. $(streak).css('letter-spacing', LETTER_SPACING);
  189. $(streak).css('cursor', 'default');
  190. $(streak).disableSelection();
  191. for (const result of results) {
  192. let res = document.createElement('A');
  193. res.textContent = CIRCLE;
  194. $(res).attr('href', result.matchLink);
  195. $(res).attr('title', result.tooltip);
  196. console.log(result);
  197. switch (result.result) {
  198. case WIN:
  199. $(res).css('color', WIN_COLOR);
  200. break;
  201. case DRAW:
  202. $(res).css('color', DRAW_COLOR);
  203. break;
  204. case LOSE:
  205. $(res).css('color', LOSE_COLOR);
  206. break;
  207. case ONGOING:
  208. $(res).css('color', ONGOING_COLOR);
  209. break;
  210. default:
  211. break;
  212. }
  213. streak.appendChild(res);
  214. }
  215. if($(row).children().first().hasClass('highlight_td')) {
  216. $(streak).removeClass('highlight_td_right_std');
  217. $(streak).addClass('highlight_td_right');
  218. $(streak).addClass('highlight_td');
  219. }
  220. });
  221. adjustSize(columnAdjustment);
  222. addTableHeader();
  223. addTableFooter();
  224. }
  225. function isToday(matchDay) { //Checks if the game is today or after
  226. console.log("isToday");
  227. console.log("matchDay: " + matchDay);
  228. let date = new Date(matchDay);
  229. console.log("date: " + date);
  230. console.log("today: " + today);
  231. console.log("date >= today: " + (date >= today));
  232. return date >= today;
  233. }
  234.  
  235. function getEndGameTime() { //calculates the endGameTime (gameStart + offsetHours (default 2 hours))
  236. startGameTime = TIME_REGEX.exec($("#next_round strong").text())[0].trim().split(":");
  237. startGameTime[0] = Number(startGameTime[0]) + endOffsetHours;
  238. startGameTime[1] = Number(startGameTime[1]);
  239. let endGameTime = new Date();
  240. endGameTime.setHours(startGameTime[0],
  241. startGameTime[1],
  242. 0);
  243. return endGameTime;
  244. }
  245.  
  246. function alreadyFinished(matchDay) {//Checks if the game is already ended
  247. if(isToday(matchDay) && WAIT_TILL_END) {
  248. let gameEnd = getEndGameTime();
  249. let endTime = new Date();
  250. endTime.setUTCHours(gameEnd.getUTCHours(),
  251. gameEnd.getUTCMinutes(),
  252. gameEnd.getUTCSeconds());
  253. console.log("now: " + now);
  254. console.log("gameEnd: " + endTime);
  255. console.log("now: " + now);
  256. console.log("ja acabou: " + now >= endTime);
  257. return now >= endTime;
  258. }
  259. return true;
  260. }
  261. function addSpaces(text) {
  262. return '\t\t' + text + ' ';
  263. }
  264. function addTableFooter() {
  265. let div = document.createElement('div');
  266. $(div).addClass('align_center');
  267. $(div).addClass('std');
  268. let win = document.createElement('span');
  269. let draw = document.createElement('span');
  270. let lose = document.createElement('span');
  271. $(win).css('background-color', WIN_COLOR);
  272. $(draw).css('background-color', DRAW_COLOR);
  273. $(lose).css('background-color', LOSE_COLOR);
  274. $([win, draw, lose]).addClass('table_color');
  275. $([win, draw, lose]).css('border-radius', '50%');
  276. let winSubtitle = $($('#overall_table th')[WIN_INDEX]).attr('tooltip');
  277. let drawSubtitle = $($('#overall_table th')[DRAW_INDEX]).attr('tooltip');
  278. let loseSubtitle = $($('#overall_table th')[LOSE_INDEX]).attr('tooltip');
  279. $(div).append(addSpaces(winSubtitle));
  280. $(div).append(win);
  281. $(div).append(addSpaces(drawSubtitle));
  282. $(div).append(draw);
  283. $(div).append(addSpaces(loseSubtitle));
  284. $(div).append(lose);
  285. if(WAIT_TILL_END == 1)
  286. {
  287. let ongoing = document.createElement('span');
  288. $(ongoing).css('background-color', ONGOING_COLOR);
  289. $(ongoing).addClass('table_color');
  290. $(ongoing).css('border-radius', '50%');
  291. $(div).append(addSpaces(ONGOING_SUBTITLE));
  292. $(div).append(ongoing);
  293. }
  294. $('.tab_container').parent().parent().append(div);
  295. }
  296.  
  297. generateMatchsHistory();
  298. })();

QingJ © 2025

镜像随时可能失效,请加Q群300939539或关注我们的公众号极客氢云获取最新地址