// ==UserScript==
// @name hwmBattleLinks
// @author Tamozhnya1
// @namespace Tamozhnya1
// @description Быстрые ссылки на итоги боя; начало, конец, чат боя
// @version 4.8
// @encoding utf-8
// @include https://*heroeswm.ru/*
// @include https://*lordswm.com/*
// @include https://*hwmguide.ru/*
// @exclude */rightcol.php*
// @exclude */ch_box.php*
// @exclude */chat*
// @exclude */ticker.html*
// @exclude */frames*
// @exclude */brd.php*
// @exclude */auction.php*
// @grant GM_deleteValue
// @grant GM_getValue
// @grant GM_setValue
// @grant GM.xmlHttpRequest
// @license MIT
// ==/UserScript==
const isEn = document.documentElement.lang == "en";
if(typeof GM_deleteValue != 'function') {
this.GM_getValue = function(key,def) { return localStorage[key] || def; };
this.GM_setValue = function(key,value) { return localStorage[key] = value; };
this.GM_deleteValue = function(key) { return delete localStorage[key]; };
}
main();
function main() {
const warRefs = Array.from(document.querySelectorAll("a[href*='warid=']")).filter(x => !getUrlParamValue(x.href, "show_enemy"));
for(const warRef of warRefs) {
const warId = getUrlParamValue(warRef.href, "warid");
const show = getUrlParamValue(warRef.href, "show");
const showForAll = getUrlParamValue(warRef.href, "show_for_all");
const showForAllSnippet = (showForAll || show) ? `&show_for_all=${(showForAll || show)}` : "";
const battleLinksSpan = document.createElement('span');
battleLinksSpan.innerHTML = ` [<a href="/war.php?warid=${warId}<=-1${showForAllSnippet}" target="_blank">#
</a> <a href="/battlechat.php?warid=${warId}${showForAllSnippet}" target="_blank">chat
</a> <a href="/war.php?warid=${warId}${showForAllSnippet}" target="_blank">$
</a> <a href="/battle.php?warid=${warId}&lastturn=-3${showForAllSnippet}" target="_blank">E</a>]`;
warRef.parentNode.insertBefore(battleLinksSpan, warRef.nextSibling);
warRef.addEventListener("click", function(e) { e.preventDefault(); showBattleResult(this); }, false);
}
}
async function showBattleResult(warRef) {
const warId = getUrlParamValue(warRef.href, "warid");
const show = getUrlParamValue(warRef.href, "show");
const showForAll = getUrlParamValue(warRef.href, "show_for_all");
const showForAllSnippet = (showForAll || show) ? `&show_for_all=${(showForAll || show)}` : "";
let battleContainer = document.getElementById(`battleContainer${warId}`);
const isShown = battleContainer && battleContainer.style.display == "flex";
Array.from(document.querySelectorAll("div[id^='battleContainer']")).forEach(x => { x.style.display = "none"; });
if(battleContainer) {
if(!isShown) {
battleContainer.style.display = "flex";
}
return;
}
const warRefRect = warRef.getBoundingClientRect();
battleContainer = addElement("div", document.body, { id: `battleContainer${warId}`, style: `position: absolute; left: ${warRefRect.left + window.scrollX + 10}px; top: ${warRefRect.bottom + window.scrollY + 1}px; border: 2px solid #000000; padding: 0; z-index: 3; background-image: linear-gradient(to right, #eea2a2 0%, #bbc1bf 19%, #57c6e1 42%, #b49fda 79%, #7ac5d8 100%); display: flex;` });
const battleResultContainer = addElement("div", battleContainer, { innerHTML: getWheelImage(), style: "text-align: center;" });
const divClose = addElement("div", battleContainer, { title: isEn ? "Close" : "Закрыть", innerText: "x", style: "border: 1px solid #abc; flex-basis: 15px; height: 15px; text-align: center; cursor: pointer;" });
divClose.addEventListener("click", function() { battleContainer.style.display = "none"; }, false);
const responseText = await getRequestText(`/battle.php?warid=${warId}&lastturn=-2${showForAllSnippet}`, "text/html; charset=UTF-8");
const battleResultsStartIndex = responseText.indexOf("<font");
if(battleResultsStartIndex == -1) {
battleResultContainer.innerHTML = `<br>${isEn ? "Parse error." : "Результаты боя не найдены."}`;
return;
}
const battleResults = responseText.substring(battleResultsStartIndex);
const battleResultsParts = battleResults.split("|#", 2);
let battleResult = battleResultsParts[0];
if(isEn && battleResultsParts.length > 1 && battleResultsParts[1].startsWith("f_en")) {
battleResult = battleResultsParts[1].substring(4);
}
battleResult = battleResult.replace(/ size="18"/g, '').replace(/font color="/g, 'font style="color: ');
const battleResultParts = battleResult.split("<br />");
const regexp_exp = isEn ? /([\d\,]+) exp/ : /([\d\,]+) опыт/;
const regexp_skill = isEn ? /(\d*\.?\d+) skill/ : /(\d*\.?\d+) умени/;
let i = 0;
for(const battleResultPart of battleResultParts) {
const exp1 = regexp_exp.exec(battleResultPart);
if(exp1) {
const skillNumber = parseFloat(regexp_skill.exec(battleResultPart)[1]);
if(skillNumber > 0) {
battleResultParts[i] = `${battleResultPart} <span title="${isEn ? "Experience number per skill point" : "Количество опыта на одно очко умения"}">(${Math.round(parseFloat(exp1[1].replaceAll(",", "")) / skillNumber)})</span>`;
}
}
i++;
}
battleResultContainer.innerHTML = battleResultParts.join("<br />");
}
function getWheelImage() { return '<img border="0" align="absmiddle" height="11" src="https://dcdn.heroeswm.ru/css/loading.gif">'; }
function getUrlParamValue(url, paramName) { return (new URLSearchParams(url.split("?")[1])).get(paramName); }
function addElement(type, parent, data) {
let el = createElement(type, data);
if(parent) {
parent.appendChild(el);
}
return el;
}
function createElement(type, data) {
let el = document.createElement(type);
if(data) {
for(let key in data) {
if(key == "innerText" || key == "innerHTML") {
el[key] = data[key];
} else {
el.setAttribute(key, data[key]);
}
}
}
return el;
}
function getRequestText(url, overrideMimeType = "text/html; charset=windows-1251") {
return new Promise((resolve, reject) => {
GM.xmlHttpRequest({ method: "GET", url: url, overrideMimeType: overrideMimeType,
onload: function(response) { resolve(response.responseText); },
onerror: function(error) { reject(error); }
});
});
}