Created
October 20, 2023 16:37
-
-
Save Monsterovich/3858e4957eb684237d28e2853d85ef9b to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// ==UserScript== | |
// @name Irina Bot Game list Table sorter and plugin | |
// @version 1.6 | |
// @grant none | |
// @match https://irinabot.ru/gamelist | |
// @match https://irinabot.ru/ | |
// @author Monsterovich | |
// ==/UserScript== | |
// выкинуть хостботы из списка | |
const FILTER_HOST_BOTS = false; | |
// отслеживать свои игры | |
const WATCH_MY_GAME = true; | |
// отслеживать игры с хостами | |
const WATCH_HOSTS = ["Monsterovich7"]; | |
// отслеживать игры с названиями (можно кусок названия) | |
const WATCH_NAMES = []; | |
// Установите Always visible Plugin чтобы постоянно получать оповещния о присоеденившихся игроках, даже когда вкладка браузера не в фокусе | |
// https://chrome.google.com/webstore/detail/always-active-window-alwa/ehllkhjndgnlokhomdlhgbineffifcbj | |
// https://addons.mozilla.org/en-US/firefox/addon/always-visible/ | |
function showNotification(title, text) { | |
if ("Notification" in window) { | |
if (Notification.permission === "granted") { | |
const notification = new Notification(title, { | |
body: text, | |
}); | |
} else if (Notification.permission !== "denied") { | |
Notification.requestPermission().then(function (permission) { | |
if (permission === "granted") { | |
const notification = new Notification(title, { | |
body: text, | |
}); | |
} | |
}); | |
} | |
} | |
} | |
function findAddedAndRemovedItems(array1, array2) { | |
const addedItems = array2.filter(item => !array1.includes(item)); | |
const removedItems = array1.filter(item => !array2.includes(item)); | |
return { addedItems, removedItems }; | |
} | |
var gamesCache = {}; | |
function sortTable(table) { | |
const rowObjects = []; | |
for (let i = 1; i < table.rows.length; i++) { | |
const row = table.rows[i]; | |
const tokens = row?.cells[1]?.textContent?.split("/"); | |
const playerList = row?.cells[3]?.children[0]?.children; | |
const playerNames = []; | |
if (playerList) { | |
for (let i = 0; i < playerList.length; i++) { | |
playerNames.push(playerList[i].textContent); | |
} | |
} | |
if (tokens) { | |
const players = Number(tokens[0]); | |
const gameName = row.cells[2]?.textContent; | |
const hostName = row.cells[4]?.textContent; | |
const hostBot = FILTER_HOST_BOTS ? (gameName?.match(/(\+[0-9]+)|(\#[0-9]+)/g)?.length > 0) : false; | |
const rowStructure = { | |
row: row, | |
players: players, | |
// put self-hosted game on top | |
myGame: row?.cells[5]?.querySelectorAll("button")?.length > 1, | |
hostBot: hostBot, | |
playerNames: playerNames | |
}; | |
let matchedGame = WATCH_HOSTS.some(host => { | |
if (host == hostName) { return true; } | |
}); | |
matchedGame = WATCH_HOSTS.some(name => { | |
if (gameName.includes(name)) { return true; } | |
}); | |
if ((WATCH_MY_GAME && rowStructure.myGame) || matchedGame) { | |
if (gamesCache[gameName]) { | |
const changes = findAddedAndRemovedItems(gamesCache[gameName].playerNames, rowStructure.playerNames); | |
changes.addedItems.forEach(playerName => { | |
showNotification("Новый игрок", "Игра: " + gameName + "\n" + playerName + " присоеденился!"); | |
}); | |
changes.removedItems.forEach(playerName => { | |
showNotification("Ливер", "Игра: " + gameName + "\n" + playerName + " вышел!"); | |
}); | |
} | |
gamesCache[gameName] = rowStructure; | |
} | |
rowObjects.push(rowStructure); | |
} | |
} | |
rowObjects.sort((a, b) => { | |
if (a.myGame) { | |
return 0; | |
} else if (b.myGame) { | |
return 1; | |
} else { | |
return a.players < b.players; | |
} | |
}); | |
rowObjects.forEach((rowObject, index) => { | |
if (rowObject.hostBot) { | |
rowObject.row.style = "display: none"; | |
} | |
table.rows[index + 1].after(rowObject.row); | |
}); | |
} | |
window.addEventListener("load", () => { | |
const tables = document.getElementsByClassName("table"); | |
if (tables.length < 1) { | |
console.log("No tables found!"); | |
} | |
// fixme: watch for table to change | |
setInterval(function () { | |
const gamesTable = tables[0]; | |
if (gamesTable) { | |
sortTable(gamesTable); | |
} | |
}, 1000) | |
}, false); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment