// ==UserScript== // @name MatchResult_GoalScorers // @namespace https://greasyfork.org/users/562206 // @version 0.0.6 // @description View match result in advance (under 50 minutes before the match start) // @author Jhonatan Bianchi // @include http://trophymanager.com/matches/* // @include https://trophymanager.com/matches/* // @downloadURL none // ==/UserScript== function insertBefore(el, referenceNode) { referenceNode.parentNode.insertBefore(el, referenceNode); } function insertAfter(el, referenceNode) { referenceNode.parentNode.insertBefore(el, referenceNode.nextSibling); } Object.filter = (obj, predicate) => Object.keys(obj) .filter(key => predicate(obj[key])) .reduce((res, key) => (res[key] = obj[key], res), {}); function getEventImage(match, event) { let src = ''; if (getEventIs(event, 'goal')) { src = 'https://trophymanager.com/pics/icons/ball.gif'; } if (getEventIs(event, 'yellow')) { src = 'https://trophymanager.com/pics/icons/yellow_card.gif'; } if (getEventIs(event, 'red')) { src = 'https://trophymanager.com/pics/icons/red_card.gif'; } if (getEventIs(event, 'injury')) { src = 'https://trophymanager.com/pics/icons/injury.gif'; } const playerId = getPlayerIdFromEvent(event); if (isPlayerFromHomeTeam(match, playerId)) { return ''; } else { return '' } } function getEventIs(event, eventNameToCheck) { return event[0].parameters.filter(e => e[eventNameToCheck]).length; } function getGoalDetailsFromGoalEvent(goalEvent) { return goalEvent[0].parameters.filter(e => e.goal)[0].goal; } function getMatchCurrentScoreAfterGoalEvent(goalEvent) { const goal = getGoalDetailsFromGoalEvent(goalEvent); if (goal) { return { homeGoals: goal.score[0], awayGoals: goal.score[1], } } } function isPlayerFromHomeTeam(match, playerId) { return Object.keys(match.lineup.home).includes(playerId); } function getPlayerName(match, playerId) { return isPlayerFromHomeTeam(match, playerId) ? match.lineup.home[playerId].name : match.lineup.away[playerId].name; } function eventIsGoalCardInjury(event) { return event.parameters && event.parameters.filter(event => event.goal || event.yellow || event.red || event.injury).length; } function getWantedEvents(match) { return Object.filter(match.report, (event) => { return event.filter(e => { return eventIsGoalCardInjury(e); }).length; }) } function getGoalsEvents(match) { return Object.filter(match.report, (event) => { return event.filter(e => { return e.parameters && e.parameters.filter(e => e.goal).length; }).length; }) } function getMatchResult(match) { const goalsEvents = getGoalsEvents(match); if (Object.keys(goalsEvents).length === 0) { return { homeGoals: 0, awayGoals: 0 }; } const { [Object.keys(goalsEvents).pop()]: lastGoal } = goalsEvents; return getMatchCurrentScoreAfterGoalEvent(lastGoal); } function getHomeTeamName(match) { return match.club.home.club_name; } function getAwayTeamName(match) { return match.club.away.club_name; } function getPlayerIdFromEvent(event) { if (getEventIs(event, 'goal')) { return event[0].parameters.filter(e => e.goal)[0].goal.player; } if (getEventIs(event, 'yellow')) { return event[0].parameters.filter(e => e.yellow)[0].yellow; } if (getEventIs(event, 'red')) { return event[0].parameters.filter(e => e.red)[0].red; } if (getEventIs(event, 'injury')) { return event[0].parameters.filter(e => e.injury)[0].injury; } } function getFinalResultHtml(match) { const matchResult = getMatchResult(match); return '
' + '
' + getHomeTeamName(match) + '
' + '
' + matchResult.homeGoals + ' - ' + matchResult.awayGoals + '
'+ '
' + getAwayTeamName(match) + '
' + '
'; } function getGoalHtml(match, goalEvent, minute) { const goal = getGoalDetailsFromGoalEvent(goalEvent); const currentMatchResult = getMatchCurrentScoreAfterGoalEvent(goalEvent); if (isPlayerFromHomeTeam(match, goal.player)) { return '
' + '
[' + minute + '] ' + getPlayerName(match, goal.player) + getEventImage(match, goalEvent) +'
' + '
' + currentMatchResult.homeGoals + ' - ' + currentMatchResult.awayGoals + '
' + '
'; } else { return '
' + '
 
' + '
' + currentMatchResult.homeGoals + ' - ' + currentMatchResult.awayGoals + '
' + '
' + getEventImage(match, goalEvent) + getPlayerName(match, goal.player) + ' [' + minute + ']
' + '
'; } } function getNonGoalHtml(match, event, minute) { const playerId = getPlayerIdFromEvent(event); if (isPlayerFromHomeTeam(match, playerId)) { return '
' + '
[' + minute + '] ' + getPlayerName(match, playerId) + getEventImage(match, event) + '
' + '
' + '
'; } else { return '
' + '
 
' + '
' + '
' + getEventImage(match, event) + getPlayerName(match, playerId) + ' [' + minute + ']
' + '
'; } } function getEventHtml(match, event, minute) { if (getEventIs(event, 'goal')) { return getGoalHtml(match, event, minute); } return getNonGoalHtml(match, event, minute); } function showMatchEvents(match) { const mainCentersDivs = document.getElementsByClassName('main_center'); if (mainCentersDivs) { const matchEventsDiv = document.createElement('div'); matchEventsDiv.className = 'main_center'; matchEventsDiv.innerHTML = getFinalResultHtml(match); const matchEvents = getWantedEvents(match); for (const [minute, event] of Object.entries(matchEvents)) { matchEventsDiv.innerHTML = matchEventsDiv.innerHTML + getEventHtml(match, event, minute); } insertBefore(matchEventsDiv, mainCentersDivs[mainCentersDivs.length - 1]) } } var matchID = location.href.match(/([^\/]*)\/*$/)[1]; var url = 'https://trophymanager.com/ajax/match.ajax.php?id=' + matchID; var xhr = new XMLHttpRequest(); xhr.open('GET', url, true); xhr.send(); xhr.onreadystatechange = function() { if (this.readyState === 4 && this.status === 200) { var match = JSON.parse(this.responseText); showMatchEvents(match); } }