Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

MaxTools #1667

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions Info.plist
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,7 @@ a good reason to. They are for version bumps in Makefile.
<string>content/information-aggregation/last-login.js</string>
<string>content/information-aggregation/match-weather.js</string>
<string>content/information-aggregation/mercattrick-stats.js</string>
<string>content/information-aggregation/maxtools.js</string>
<string>content/information-aggregation/my-monitor.js</string>
<string>content/information-aggregation/nt-peek.js</string>
<string>content/information-aggregation/player-birthday.js</string>
Expand Down Expand Up @@ -294,6 +295,7 @@ a good reason to. They are for version bumps in Makefile.
<string>*.hattrick-youthclub.org</string>
<string>www.fantamondi.it</string>
<string>api.openweathermap.org</string>
<string>ht-mt.org</string>
</array>
<key>Include Secure Pages</key>
<true/>
Expand Down
4 changes: 2 additions & 2 deletions content/background.html
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<!DOCTYPE html>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
Expand Down Expand Up @@ -104,6 +104,7 @@
<script type="application/x-javascript" src="./information-aggregation/last-login.js"></script>
<script type="application/x-javascript" src="./information-aggregation/match-weather.js"></script>
<script type="application/x-javascript" src="./information-aggregation/mercattrick-stats.js"></script>
<script type="application/x-javascript" src="./information-aggregation/maxtools.js"></script>
<script type="application/x-javascript" src="./information-aggregation/my-monitor.js"></script>
<script type="application/x-javascript" src="./information-aggregation/nt-peek.js"></script>
<script type="application/x-javascript" src="./information-aggregation/player-birthday.js"></script>
Expand Down Expand Up @@ -216,7 +217,6 @@
<script type="application/x-javascript" src="./shortcuts-and-tweaks/transfer-search-filters.js"></script>
<script type="application/x-javascript" src="./shortcuts-and-tweaks/transfer-search-result-filters.js"></script>
<!-- end categorized modules -->

<!-- platform-specific -->
<script type="application/x-javascript" src="./entry.js"></script>
<script type="application/x-javascript" src="./background.js"></script>
Expand Down
33 changes: 33 additions & 0 deletions content/foxtrick.properties
Original file line number Diff line number Diff line change
Expand Up @@ -2110,3 +2110,36 @@ CopyLeagueStatsAd.maxstats=maximum statistics
CopyLeagueStatsAd.avgstats=average statistics
CopyFinancesAd.copy=Copy economy report to cliboard
CopyFinancesAd.copied=The economy report has been copied to the clipboard.

# MaxTools
module.Maxtools.desc=Show last match U20/U21/Senior
module.Maxtools.RomanNumberEdition.desc=Use Roman numerals for the championship edition
module.Maxtools.YouthPlayers.desc=Show last match U20/U21/Senior on the Youth Player Details page.
module.Maxtools.SeniorPlayers.desc=Show last match U20/U21/Senior on the Senior Player Details page.
module.Maxtools.AllPlayers.desc=Show last match U20/U21/Senior on the Senior/Youth/NT Players page.
module.Maxtools.TransfersResults.desc=Show last match U20/U21/Senior on the Transfers Search Results page.

MaxTools=MaxTools:
MaxTools.MoreDetails=(more details)
MaxTools.match=Match %1 of %2
MaxTools.edition=Edition
MaxTools.lastMatch=Last match

# %1 = MaxTools.lastMatch
# %2 = type, e.g. U20, U21, Senior
# %3 = scheduler, e.g. New or Old
# %4 = MaxTools.edition
# %5 = WC number, e.g. XIV
# %6 = phase, e.g. CC
# %7 = MaxTools.match
MaxTools.templateWithoutTable=%1: %2 (%3) - %4 %5 - %6 - %7
MaxTools.eligibilityTitle=MaxTools
MaxTools.eligibilitySubtitle=NT Eligibility
MaxTools.eligibilityText=This player will be able to play:

# %1 = MaxTools.lastMatch
# %2 = type, e.g. U20, U21, Senior
# %3 = phase, e.g. CC
# %4 = WC number, e.g. XIV
# %5 = MaxTools.match
MaxTools.templateShortWithoutTable=%1: %2 - %3 %4 - %5
234 changes: 234 additions & 0 deletions content/information-aggregation/maxtools.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,234 @@
/**
* maxtools.js
* Shows which would be the last official u20/u21 match of a certain player.
* @author: KaueFelipeBR, MaxDareDevil
*/

'use strict';

/* eslint-disable no-magic-numbers */

Foxtrick.modules.Maxtools = {
MODULE_CATEGORY: Foxtrick.moduleCategories.INFORMATION_AGGREGATION,
PAGES: [
'youthPlayerDetails', 'playerDetails',
'allPlayers', 'youthPlayers',
'transferSearchResult',
],
OPTIONS: ['RomanNumberEdition', 'YouthPlayers', 'SeniorPlayers', 'AllPlayers'],



/** @param {document} doc */
// eslint-disable-next-line complexity
run: function (doc) {
const module = this;

var isYouthPlayerDetailsPage = Foxtrick.isPage(doc, 'youthPlayerDetails');
var isSeniorPlayerDetailsPage = Foxtrick.isPage(doc, 'playerDetails');
var isPlayersPage = Foxtrick.isPage(doc, 'allPlayers');
var isYouthPlayersPage = Foxtrick.isPage(doc, 'youthPlayers');

var isYouthEnabled = Foxtrick.Prefs.isModuleOptionEnabled(module, 'YouthPlayers');
var isSeniorsEnabled = Foxtrick.Prefs.isModuleOptionEnabled(module, 'SeniorPlayers');
var isPlayersEnabled = Foxtrick.Prefs.isModuleOptionEnabled(module, 'AllPlayers');


// If the option isn't enabled for this page, don't show.
if (isYouthPlayerDetailsPage && !isYouthEnabled)
return;
if (isSeniorPlayerDetailsPage && !isSeniorsEnabled)
return;
if (isPlayersPage && !isPlayersEnabled)
return;

if (isYouthPlayerDetailsPage || isSeniorPlayerDetailsPage) {
module.runPlayer(doc);
}
else if (isPlayersPage || isYouthPlayersPage)
module.runPlayerList(doc);
},

/**
* @typedef MaxToolPerfectAge
* @prop {string} type
* @prop {string} scheduler
* @prop {number} edition
* @prop {string} phase
* @prop {number} match
* @prop {number} last_phase_match
* @prop {number} training_weeks
*/

/**
* @param {string} text
* @return {Array<MaxToolPerfectAge>}
*/
buildMaxToolPerfectAge: function (text) {
var champsList = JSON.parse(text);
return champsList;
},

/**
* @param {number} years
* @param {number} days
* @param {document} doc
* @return {HTMLAnchorElement}
*/
getLink: function (years, days, doc) {
let lang = Foxtrick.Prefs.getString('htLanguage');
let HTMT_PATH = 'https://ht-mt.org/nt/perfect-age';
let skillQuery = `?lang=${lang}&years=${years}&days=${days}`;

let link = doc.createElement('a');
link.textContent = Foxtrick.L10n.getString('MaxTools.MoreDetails');
link.href = HTMT_PATH + skillQuery;
link.target = '_blank';
link.rel = 'noopener';
return link;
},

/** @param {document} doc */
runPlayer: function (doc) {
const module = this;

const TITLE_STR = Foxtrick.L10n.getString('MaxTools.eligibilityTitle');
const SUBTITLE_STR = Foxtrick.L10n.getString('MaxTools.eligibilitySubtitle');

if (Foxtrick.Pages.Player.wasFired(doc))
return;

let age = Foxtrick.Pages.Player.getAge(doc);

const HTMT_PATH = 'https://ht-mt.org';
var url = `${HTMT_PATH}/maxtools-api/perfect-age/${age.years}/${age.days}`;

Foxtrick.load(url).then(function (r) {
var text = /** @type {string} */ (r);
let champsList = module.buildMaxToolPerfectAge(text);

let entryPoint =
doc.querySelector('#mainBody > .mainBox') ||
doc.querySelector('#mainBody > .playerInfo');

let wrapper = Foxtrick.createFeaturedElement(doc, module, 'div');
Foxtrick.addClass(wrapper, 'mainBox');

let titleElement = doc.createElement('h2');
titleElement.textContent = TITLE_STR;
wrapper.appendChild(titleElement);

let subtitleElement = doc.createElement('h3');
subtitleElement.textContent = SUBTITLE_STR;
wrapper.appendChild(subtitleElement);

module.appendChampsDetail(champsList, doc, wrapper, true);

let textElement = doc.createElement('div');
textElement.appendChild(module.getLink(age.years, age.days, doc));
wrapper.appendChild(textElement);

entryPoint.parentNode.insertBefore(wrapper, entryPoint.nextSibling);
}, (er) => {
console.log(er);
}).catch(Foxtrick.catch(module));
},

/** @param {document} doc */
runPlayerList: function (doc) {
const module = this;

let players = Foxtrick.modules.Core.getPlayerList();
for (let player of players) {

const HTMT_PATH = 'https://ht-mt.org';
var url = `${HTMT_PATH}/maxtools-api/perfect-age/${player.age.years}/${player.age.days}`;

Foxtrick.load(url).then(function (r) {
var text = /** @type {string} */ (r);
let champsList = module.buildMaxToolPerfectAge(text);

let container = Foxtrick.createFeaturedElement(doc, module, 'p');
Foxtrick.addClass(container, 'ft-u20lastmatch');
container.appendChild(module.getLink(player.age.years, player.age.days, doc));
container.appendChild(doc.createTextNode(' '));


module.appendChampsDetail(champsList, doc, container, false);

let entry = player.playerNode.querySelector('table') || player.playerNode.lastChild;
while (entry.parentElement && entry.parentElement.matches('.flex'))
entry = entry.parentElement;

Foxtrick.insertAfter(container, entry);
}, (er) => {
console.log(er);
}).catch(Foxtrick.catch(module));
}
},

/**
* @param {Array<MaxToolPerfectAge>} champsList
* @param {document} doc
* @param {boolean} extendedDescription
* @param {HTMLParagraphElement} container
*/
appendChampsDetail: function (champsList, doc, container, extendedDescription) {
const module = this;

var isRomanNumberEditionEnabled = Foxtrick.Prefs.isModuleOptionEnabled(module, 'RomanNumberEdition');

champsList.forEach(function (championship) {
if (Object.keys(championship).length !== 0) {
let editionNum = isRomanNumberEditionEnabled ? Foxtrick.decToRoman(championship.edition) : championship.edition;

let tmplStr = module.buildTmplStr(championship, editionNum, extendedDescription);

let champDiv = doc.createElement('div');
champDiv.textContent = tmplStr;
container.appendChild(champDiv);
}
});
},

/**
* @param {MaxToolPerfectAge} championship
* @param {string|number} editionNum
* @param {boolean} extendedDescription
* @returns {string}
*/
buildTmplStr: function (championship, editionNum, extendedDescription) {
const MATCH_STR = Foxtrick.L10n.getString('MaxTools.match');
const TMPL_STR = Foxtrick.L10n.getString('MaxTools.templateWithoutTable');

var textFormat = extendedDescription ? MATCH_STR : "%1/%2";

let strMatch = textFormat.replace(/%1/, championship.match.toString());
strMatch = strMatch.replace(/%2/, championship.last_phase_match.toString());

const TMPL_STR_SHORT = Foxtrick.L10n.getString('MaxTools.templateShortWithoutTable');
const LAST_MATCH_STR = Foxtrick.L10n.getString('MaxTools.lastMatch');
let contentChamp = extendedDescription ? TMPL_STR : TMPL_STR_SHORT;

contentChamp = contentChamp.replace(/%1/, LAST_MATCH_STR);
contentChamp = contentChamp.replace(/%2/, championship.type);

if (extendedDescription) {
const EDITION_STR = Foxtrick.L10n.getString('MaxTools.edition');

contentChamp = contentChamp.replace(/%3/, championship.scheduler);
contentChamp = contentChamp.replace(/%4/, EDITION_STR);
contentChamp = contentChamp.replace(/%5/, editionNum.toString());
contentChamp = contentChamp.replace(/%6/, championship.phase);
contentChamp = contentChamp.replace(/%7/, strMatch);
} else {
contentChamp = contentChamp.replace(/%3/, championship.phase);
contentChamp = contentChamp.replace(/%4/, editionNum.toString());
contentChamp = contentChamp.replace(/%5/, strMatch);
}


return contentChamp;
}
};

6 changes: 2 additions & 4 deletions content/preferences.html
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<!DOCTYPE html>
<!DOCTYPE html>
<html>
<head>
<!-- options/preferences page for mozilla and chrome -->
Expand Down Expand Up @@ -62,7 +62,6 @@
<script type="application/x-javascript" src="./util/tabs.js"></script>
<script type="application/x-javascript" src="./util/time.js"></script>
<!-- end util -->

<!-- core -->
<script type="application/x-javascript" src="./add-class.js"></script>
<script type="application/x-javascript" src="./core.js"></script>
Expand All @@ -71,7 +70,6 @@
<script type="application/x-javascript" src="./read-ht-prefs.js"></script>
<script type="application/x-javascript" src="./redirections.js"></script>
<!-- end core -->

<!-- categorized modules -->
<script type="application/x-javascript" src="./access/aria-landmarks.js"></script>
<script type="application/x-javascript" src="./alert/live-alert.js"></script>
Expand Down Expand Up @@ -111,6 +109,7 @@
<script type="application/x-javascript" src="./information-aggregation/last-login.js"></script>
<script type="application/x-javascript" src="./information-aggregation/match-weather.js"></script>
<script type="application/x-javascript" src="./information-aggregation/mercattrick-stats.js"></script>
<script type="application/x-javascript" src="./information-aggregation/maxtools.js"></script>
<script type="application/x-javascript" src="./information-aggregation/my-monitor.js"></script>
<script type="application/x-javascript" src="./information-aggregation/nt-peek.js"></script>
<script type="application/x-javascript" src="./information-aggregation/player-birthday.js"></script>
Expand Down Expand Up @@ -223,7 +222,6 @@
<script type="application/x-javascript" src="./shortcuts-and-tweaks/transfer-search-filters.js"></script>
<script type="application/x-javascript" src="./shortcuts-and-tweaks/transfer-search-result-filters.js"></script>
<!-- end categorized modules -->

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this file (and others) should not be edited manually, we have module-update.py for it

<!-- platform-specific -->
<script type="application/x-javascript" src="./entry.js"></script>
</head>
Expand Down
6 changes: 6 additions & 0 deletions defaults/preferences/foxtrick.js
Original file line number Diff line number Diff line change
Expand Up @@ -290,6 +290,12 @@ pref("extensions.foxtrick.prefs.module.MatchWeather.enabled", true);
pref("extensions.foxtrick.prefs.module.MatchWeather.Irl.enabled", true);
pref("extensions.foxtrick.prefs.module.MercattrickStats.enabled", true);
pref("extensions.foxtrick.prefs.module.MercattrickStats.enableTLPage.enabled", true);
pref("extensions.foxtrick.prefs.module.Maxtools.enabled", true);
pref("extensions.foxtrick.prefs.module.Maxtools.RomanNumberEdition.enabled", true);
pref("extensions.foxtrick.prefs.module.Maxtools.YouthPlayers.enabled", true);
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

valid-ids should also be updated, for now

pref("extensions.foxtrick.prefs.module.Maxtools.SeniorPlayers.enabled", true);
pref("extensions.foxtrick.prefs.module.Maxtools.AllPlayers.enabled", true);
pref("extensions.foxtrick.prefs.module.Maxtools.TransfersResults.enabled", true);
pref("extensions.foxtrick.prefs.module.MoveLinks.enabled", false);
pref("extensions.foxtrick.prefs.module.MoveManagerOnline.enabled", false);
pref("extensions.foxtrick.prefs.module.MovePlayerSelectbox.enabled", false);
Expand Down
4 changes: 3 additions & 1 deletion manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,7 @@
"content/information-aggregation/last-login.js",
"content/information-aggregation/match-weather.js",
"content/information-aggregation/mercattrick-stats.js",
"content/information-aggregation/maxtools.js",
"content/information-aggregation/my-monitor.js",
"content/information-aggregation/nt-peek.js",
"content/information-aggregation/player-birthday.js",
Expand Down Expand Up @@ -283,7 +284,8 @@
"clipboardWrite",
"*://chpp.hattrick.org/*",
"*://*.foxtrick.org/*",
"http://www.fantamondi.it/*"
"http://www.fantamondi.it/*",
"https://ht-mt.org/*"
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

have you considered using CORS headers instead?

],
"optional_permissions": [
"https://*.hattrick-youthclub.org/*",
Expand Down
1 change: 1 addition & 0 deletions modules
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ information-aggregation/htms-points.js
information-aggregation/last-login.js
information-aggregation/match-weather.js
information-aggregation/mercattrick-stats.js
information-aggregation/maxtools.js
information-aggregation/my-monitor.js
information-aggregation/nt-peek.js
information-aggregation/player-birthday.js
Expand Down