// ==UserScript==
// @name hwmAdvancedProtocol
// @namespace Tamozhnya1
// @version 1.9
// @description Цветные протоколы боёв, передач, клана, склада, ларцов. Загрузка следующих страниц. Фильтрация.
// @author Tags, Tamozhnya1 2024
// @include /^https{0,1}:\/\/((www|my)\.(heroeswm|lordswm)\.(ru|com))\/(sklad_log|pl_transfers|pl_warlog|clan_log|gift_box_log)\.(php.*)/
// @grant GM_deleteValue
// @grant GM_getValue
// @grant GM_setValue
// @grant GM.xmlHttpRequest
// @license MIT
// ==/UserScript==
// (c) Tags 2023.04 https://greasyfork.org/ru/users/924307-foxxelias, Tamozhnya1 2024.02.26
const playerIdMatch = document.cookie.match(/pl_id=(\d+)/);
const PlayerId = playerIdMatch ? playerIdMatch[1] : "";
const isEn = document.documentElement.lang == "en";
GM_addStyle(`
.battle_row {
width: 100%;
border-right: 1px solid black;
border-bottom: 1px solid black;
border-left: 1px solid black;
}
.battle_row img {
width: 14px;
height: 14px;
vertical-align: bottom;
}
.battle_row > div {
display: inline-block;
vertical-align: bottom;
}
`);
const types = [
{
pattern: "[80/80]",
icon: ``,
color: "#FFF000"
},{
pattern: "[100/100]",
icon: ``,
color: "#00F120"
},{
pattern: "бриллиантов обменяно ",
icon: ``,
color: "#00F120"
}, {
pattern: "вернул c ремонта ",
icon: ``,
color: "#FFF000"
}, {
pattern: "возвращено автоматически c ремонта",
icon: ``,
color: "#0008DC"
}, {
pattern: "Золото о",
icon: ``,
color: "#FFF8DC"
}, {
pattern: "взял в ремонт",
icon: ``,
color: "#FFF8DC"
}, {
pattern: "Куплен",
icon: ``,
color: "#DEB887"
}, {
pattern: "Вернул ",
icon: ``,
color: "#FFDEAD"
}, {
pattern: "арендова",
icon: ``,
color: "#FFEBCD"
}, {
pattern: "Передан",
icon: ``,
color: "#E0FFFF"
}, {
pattern: ", доп. комиссия ",
icon: ``,
color: "#B0C4DE"
}, {
pattern: "Продан ",
icon: ``,
color: "#ADFF2F"
}, {
pattern: "вернул",
icon: ``,
color: "#00FA9A"
}, {
pattern: "возвращ",
icon: ``,
color: "#7FFF00"
}, {
pattern: "Возвращено автоматически ",
icon: ``,
color: "#3CB371"
}, {
pattern: "Взят в ремонт ",
icon: ``,
color: "#7CFC00"
}, {
pattern: "Вернул c ремонта ",
icon: ``,
color: "#FA8072"
}, {
pattern: "Получен элемент ",
icon: ``,
color: "#00FF00"
}, {
pattern: "Получен ",
icon: ``,
color: "#F08080"
}, {
pattern: "<!--0-->",
icon: `<a href="hunter_guild.php" title="${isEn ? "Hunter's guild" : "Гильдия охотников"}" style="height: inherit;"><img src="https://dcdn.heroeswm.ru/i/btns/job_fl_btn_hunters.png" alt="hunt"></a>`,
color: "#cceecc"
}, {
pattern: "<!--7-->",
icon: `<a href="mercenary_guild.php" style="height: inherit;"><img src="https://dcdn.heroeswm.ru/i/rewards/gn/task4.png" style="height: inherit;" ></a>`,
color: "#eeeecc"
}, {
pattern: "<!--8-->",
icon: `<a href="mercenary_guild.php" style="height: inherit;"><img src="https://dcdn3.heroeswm.ru/i/rewards/gn/task5.png" alt="" style="height: inherit;" ></a>`,
color: "#eeeecc"
}, {
pattern: "<!--12-->",
icon: `<a href="mercenary_guild.php" style="height: inherit;"><img src="https://dcdn2.heroeswm.ru/i/rewards/gn/task9.png" alt="" style="height: inherit;" ></a>`,
color: "#eeeecc"
}, {
pattern: "<!--95-->",
icon: `<a href="task_guild.php" style="height: inherit;" title="${isEn ? "Watcher's guild" : "Гильдия стражей"}"><img src="https://dcdn.heroeswm.ru/i/zvezda.png" ></a>`,
color: "#17d3bf"
}, {
pattern: "<!--29-->",
icon: `<a href="mercenary_guild.php" style="height: inherit;"><img src="https://dcdn3.heroeswm.ru/i/rewards/gn/task3.png" alt="" style="height: inherit;"></a>`,
color: "#eeeecc"
}, {
pattern: "<!--10-->",
icon: `<a href="mercenary_guild.php" style="height: inherit;"><img src="https://dcdn3.heroeswm.ru/i/rewards/gn/task7.png" alt="" style="height: inherit;" ></a>`,
color: "#eeeecc"
}, {
pattern: "<!--5-->",
icon: `<a href="mercenary_guild.php" style="height: inherit;"><img src="https://dcdn3.heroeswm.ru/i/rewards/gn/task2.png" alt="" style="height: inherit;" ></a>`,
color: "#eeeecc"
}, {
pattern: "<!--28-->",
icon: `<a href="mercenary_guild.php" style="height: inherit;"><img src="https://dcdn1.heroeswm.ru/i/rewards/gn/task10.png" alt="" style="height: inherit;" ></a>`,
color: "#eeeecc"
}, {
pattern: "<!--66-->",
icon: `<a href="thief_guild.php" style="height: inherit;"><img title="Гильдия воров" src="https://dcdn.heroeswm.ru/i/btns/job_fl_btn_thiefs.png" ></a>`,
color: "#ccccee"
}, {
pattern: "<!--68-->",
icon: `<img src="https://dcdn.heroeswm.ru/i/top/line/2x2fast.gif" title="Турнир" alt="Турнир">`,
color: ""
}, {
pattern: "<!--127-->",
icon: `<a href="leader_guild.php" style="height: inherit;"><img src="https://dcdn.heroeswm.ru/i/icons/attr_leadership.png" title="Гильдия Лидеров" alt="Гильдия Лидеров" style="height: inherit;"></a>`,
color: "#cccccc"
}, {
pattern: "<!--89-->",
icon: `<a href="leader_guild.php" style="height: inherit;"><img src="https://dcdn.heroeswm.ru/i/icons/attr_leadership.png" title="Гильдия Лидеров" alt="Гильдия Лидеров" style="height: inherit;"></a>`,
color: "#cccccc"
}, {
pattern: "<!--94-->",
icon: `<a href="tj_single.php"><img src="//dcdn.heroeswm.ru/i/tj2ev200.jpg" alt="" width="15" height="15" border="0"></a>`,
color: "#cccccc"
}, {
pattern: "<!--117-->",
icon: `<a href="tj_single.php"><img src="//dcdn.heroeswm.ru/i/tj2ev200.jpg" alt="" width="15" height="15" border="0"></a>`,
color: "#cccccc"
}, {
pattern: "<!--110-->",
icon: `<a href="campaign_list.php"><img src="//dcdn.heroeswm.ru/i/camp/dif3.png" alt="" width="15" height="15" border="0"></a>`,
color: "#b5b5b5"
}, {
pattern: "<!--119-->",
icon: `<a href="village_def.php"><img src="//dcdn.heroeswm.ru/i/vil_def_n.jpg" alt="" width="15" height="15" border="0"></a>`,
color: "#9b5755"
}, {
pattern: "<!--120-->",
icon: `<a href="recruit_event.php" title="${isEn ? "Dungeon Caves" : "Подземные пещеры"}"><img src="https://dcdn.lordswm.com/i/portraits/evileyeanip33.png?v=15" alt="" width="15" height="15" border="0"></a>`,
color: "#ffaec8"
}, {
pattern: "<!--126-->",
icon: `<a href="village_def.php"><img src="//dcdn.heroeswm.ru/i/vil_def_n.jpg" alt="" width="15" height="15" border="0"></a>`,
color: "#8FBC8F"
}, {
pattern: "<!--40-->",
icon: `<a href="pvp_guild.php"><img src="https://dcdn.heroeswm.ru/i/pvp_gerb2.png" alt="" width="15" height="15" border="0"></a>`,
color: "#20B2A0"
}, {
pattern: "<!--44-->",
icon: `<a href="village_def.php"><img src="//dcdn.heroeswm.ru/i/vil_def_n.jpg" alt="" width="15" height="15" border="0"></a>`,
color: "#20B2AA"
}, {
pattern: "<!--111-->",
icon: `<a href="village_def.php"><img src="//dcdn.heroeswm.ru/i/vil_def_n.jpg" alt="" width="15" height="15" border="0"></a>`,
color: "#808000"
}, {
pattern: "<!--115-->",
icon: `<a href="village_def.php"><img src="//dcdn.heroeswm.ru/i/vil_def_n.jpg" alt="" width="15" height="15" border="0"></a>`,
color: "#9ACD32"
}, {
pattern: "<!--142-->",
icon: `<a href="village_def.php"><img src="//dcdn.heroeswm.ru/i/vil_def_n.jpg" alt="" width="15" height="15" border="0"></a>`,
color: "#008000"
}, {
pattern: "<!--138-->",
icon: `<a href="map_hero_event.php"><img src="https://dcdn3.heroeswm.ru/i/portraits/cmajakanip33.png" alt="" width="15" height="15" border="0"></a>`,
color: "#2E8B55"
}, {
pattern: "<!--139-->",
icon: `<a href="village_def.php"><img src="//dcdn.heroeswm.ru/i/vil_def_n.jpg" alt="" width="15" height="15" border="0"></a>`,
color: "#2E8B57"
}, {
pattern: "<!--26-->",
icon: `<a href="village_def.php"><img src="//dcdn.heroeswm.ru/i/vil_def_n.jpg" alt="" width="15" height="15" border="0"></a>`,
color: "#FFFF00"
}, {
pattern: "<!--143-->",
icon: `<a href="village_def.php"><img src="//dcdn.heroeswm.ru/i/vil_def_n.jpg" alt="" width="15" height="15" border="0"></a>`,
color: "#AFEEEE"
}, {
pattern: "<!--80-->",
icon: `<a href="mapwars.php" title="${isEn ? "Defences" : "Защиты"}"><img src="//dcdn.heroeswm.ru/i/artifacts/survarts/surv_mamulka.png" alt="" width="15" height="15" border="0"></a>`,
color: "#00FA9A"
}, {
pattern: "<!--140-->",
icon: `<a href="hunting_event.php"><img src="//dcdn.lordswm.com/i/hunting_event2022_08.png" title="${isEn ? "Hunting Season" : "Сезон охоты"}" width="15" height="15" border="0"></a>`,
color: "#7FFFD4"
}, {
pattern: "<!--133-->",
icon: `<a href="village_def.php"><img src="//dcdn.heroeswm.ru/i/vil_def_n.jpg" alt="" width="15" height="15" border="0"></a>`,
color: "#40E0D0"
}, {
pattern: "<!--131-->",
icon: `<a href="village_def.php"><img src="//dcdn.heroeswm.ru/i/vil_def_n.jpg" alt="" width="15" height="15" border="0"></a>`,
color: "#00CED1"
}, {
pattern: "<!--67-->",
icon: `<a href="village_def.php"><img src="//dcdn.heroeswm.ru/i/vil_def_n.jpg" alt="" width="15" height="15" border="0"></a>`,
color: "#5F9EA0"
}, {
pattern: "<!--99-->",
icon: `<a href="treasure_event.php"><img src="https://dcdn.heroeswm.ru/i/klad_event.jpg" alt="" width="15" height="15" border="0"></a>`,
color: "#D2B48C"
}, {
pattern: "<!--81-->",
icon: `<a href="village_def.php"><img src="//dcdn.heroeswm.ru/i/vil_def_n.jpg" alt="" width="15" height="15" border="0"></a>`,
color: "#A52A2A"
},
];
const logContainerSelector = doc => doc.querySelectorAll('div.global_a_hover')[1] || doc.querySelector('div.global_a_hover');
const pagingContainerSelector = doc => location.pathname == '/gift_box_log.php' ? doc.querySelectorAll('td.wbwhite > center')[2] : doc.querySelector("div.hwm_pagination").parentNode;
const p_date = /(\d{2}-\d{2}-\d{2}) (\d{1,2}:\d{2}): /; // pattern
const p_user = /(<a.*pl_info\.php\?id=\d+".*><b>.+<\/b><\/a>)/; // pattern
let loading = false;
let stop = false;
const loadCaption = isEn ? "Load" : "Загрузить";
const stopLoadCaption = isEn ? "Stop" : "Остановить";
main();
function main() {
const header = pagingContainerSelector(document);
const protocolSearchInput = addElement("input", { id: "protocolSearchInput", type: "text", style: "width: 150px; vertical-align: top;", title: isEn ? "Protocol filter" : "Фильтр по протоколу", onfocus: "this.select();" }, header);
protocolSearchInput.addEventListener("input", search);
const toggleLoadingButton = addElement("div", { id: "toggleLoadingButton", class: "home_button2 btn_hover2", style: "display: inline-block; vertical-align: top; padding: 0px 4px; width: fit-content;", innerText: loadCaption }, header);
toggleLoadingButton.addEventListener("click", toggleLoading);
const pageCountInput = addElement("input", { id: "pageCountInput", type: "number", value: getPlayerValue("ProtocolPageAmount", 5), style: "width: 70px; vertical-align: top;", title: isEn ? "Page amount to loading" : "Количество страниц для загрузки", onfocus: "this.select();" }, header);
pageCountInput.addEventListener("change", function() { setPlayerValue("ProtocolPageAmount", this.value); });
processLog();
}
async function toggleLoading() {
const toggleLoadingButton = document.getElementById("toggleLoadingButton");
if(loading) {
stop = true;
return;
}
loading = true;
//stop = false;
const currentPageIndex = parseInt(getUrlParamValue(location.href, "page") || 0);
const pageCount = Number(getPlayerValue("ProtocolPageAmount", 5)) || 5;
for(let newPageIndex = currentPageIndex + 1; newPageIndex <= currentPageIndex + pageCount; newPageIndex++) {
toggleLoadingButton.innerText = `${stopLoadCaption} ${newPageIndex + 1}`;
const nextPageUrl = new URL(location.href);
nextPageUrl.searchParams.set('page', newPageIndex);
console.log(nextPageUrl.toString());
const nextPage = await getRequest(nextPageUrl);
processLog(nextPage);
window.history.replaceState(null, nextPage.title, nextPageUrl);
if(stop) {
break;
}
}
loading = false;
stop = false;
toggleLoadingButton.innerText = loadCaption;
}
function processLog(doc = document) {
remap(doc);
const logContainer = logContainerSelector(document);
if(!logContainer) {
return;
}
logContainer.style.fontSize = "9pt";
logContainer.style.borderTop = "1px solid black";
const nextPageLogContainer = logContainerSelector(doc);
let rawBattles = nextPageLogContainer.innerHTML.split('<br>').filter(x => x && x != "\n" && (!x.startsWith("<center>") || doc == document));
console.log(rawBattles)
//rawBattles.pop(); //location.pathname == '/gift_box_log.php'
if(doc == document) {
logContainer.innerHTML = "";
}
for(let rawBattle of rawBattles) {
if(rawBattle.startsWith("<center>")) {
logContainer.insertAdjacentHTML("beforeend", rawBattle);
continue;
}
const type = types.find(x => rawBattle.includes(x.pattern));
// rawBattle = rawBattle.replace(p_date, "<div>$1</div><div>$2</div>");
// rawBattle = rawBattle.replace(p_user, "<div>$1</div>");
const rowHtml = (type?.icon || "") + rawBattle.replace("vs", " vs ").replace(" ", " ");
const battleContainer = addElement('div', { innerHTML: rowHtml, class: "battle_row", style: `background-color: ${type?.color || "inherit"}` }, logContainer);
}
search();
}
function remap(doc) {
if(location.pathname == '/gift_box_log.php') {
const logContainer = addElement("div", { class: "global_a_hover" });
const td = doc.querySelector('td.wbwhite');
let i = 0;
const children = Array.from(td.childNodes);
for(const child of children) {
if(i == 3) {
child.insertAdjacentElement("afterend", logContainer);
//console.log(logContainer)
}
//console.log(child)
if(i > 3) {
if(child.nodeName.toLowerCase() == "center") {
child.remove();
} else {
logContainer.appendChild(child);
}
}
i++;
}
}
}
function search() {
const value = document.getElementById("protocolSearchInput").value;
const regex = new RegExp(value, "gi");
const logContainer = logContainerSelector(document);
Array.from(logContainer.querySelectorAll("div")).forEach(x => {
x.innerHTML = x.innerHTML.replace(/<\/?mark[^>]*>/g, "");
const searched = regex.test(x.innerHTML);
x.style.display = searched ? "" : "none";
if(searched && value.length > 0) {
x.innerHTML = x.innerHTML.replace(regex, `<mark>$&</mark>`);
}
});
}
// API
function GM_addStyle(css) { addElement("style", { type: "text/css", innerHTML: css }, document.head); }
function addElement(type, data = {}, parent = undefined, insertPosition = "beforeend") {
const el = document.createElement(type);
for(const key in data) {
if(key == "innerText" || key == "innerHTML") {
el[key] = data[key];
} else {
el.setAttribute(key, data[key]);
}
}
if(parent) {
parent.insertAdjacentElement(insertPosition, el);
}
return el;
}
function getRequest(url, overrideMimeType = "text/html; charset=windows-1251") {
return new Promise((resolve, reject) => {
GM.xmlHttpRequest({ method: "GET", url: url, overrideMimeType: overrideMimeType,
onload: function(response) { resolve((new DOMParser).parseFromString(response.responseText, "text/html")); },
onerror: function(error) { reject(error); }
});
});
}
function getUrlParamValue(url, paramName) { return (new URLSearchParams(url.split("?")[1])).get(paramName); }
function getValue(key, defaultValue) { return GM_getValue(key, defaultValue); };
function setValue(key, value) { GM_setValue(key, value); };
function deleteValue(key) { return GM_deleteValue(key); };
function getPlayerValue(key, defaultValue) { return getValue(`${key}${PlayerId}`, defaultValue); };
function setPlayerValue(key, value) { setValue(`${key}${PlayerId}`, value); };
function deletePlayerValue(key) { return deleteValue(`${key}${PlayerId}`); };
function getPlayerBool(valueName, defaultValue = false) { return getBool(valueName + PlayerId, defaultValue); }
function getBool(valueName, defaultValue = false) {
const value = getValue(valueName);
//console.log(`valueName: ${valueName}, value: ${value}, ${typeof(value)}`)
if(value != undefined) {
if(typeof(value) == "string") {
return value == "true";
}
if(typeof(value) == "boolean") {
return value;
}
}
return defaultValue;
}