iradap

Flexable Chat

Jun 28th, 2022 (edited)
850
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 11.04 KB | None | 0 0
  1. // ==UserScript==
  2. // @name         Flexable Chat
  3. // @namespace    http://czogi.usermd.net
  4. // @version      0.2
  5. // @description  Dodatek do gry umożliwiający modyfikowanie chatu w grze.
  6. // @author       Czogi
  7. // @match        https://alkatria.pl/game
  8. // @grant        none
  9. // ==/UserScript==
  10.  
  11. /*
  12.       !!!UWAGA!!!
  13.  
  14.       Słowo "wspomagacz" zawarte w regulaminie(07.01.2021)(dd:mm:rrrr) gry "Alkatria" w punkcie 8 w podpunkcie c według mnie nie definiuje poniższego dodatku.
  15.       Opieram się na artykule https://pl.wikipedia.org/wiki/Cheat oraz https://www.miejski.pl/tag-wspomagacze zawierających podane słowo kluczowe.
  16.       Zakładając że przyjmujemy taką definicję słowa "wspomagacz" poniższy dodatek nie łamie żadnego z punktów regulaminu na dzień(07.01.2021)(dd:mm:rrrr)
  17.  
  18.       Każdy jednak może inaczej interpretować regulamin gry, więc uczulam każdego kto zdecyduje się na użytkowanie z tego dodatku na ewentualną możliwość złamania regulaminu gry.
  19.  
  20.       Pełny regulamin dostępny na stronie: https://alkatria.pl/rules.
  21.  
  22.       W razie błędów proszę o zgłoszenie w wiadomości prywatnej Сzogi#8027(588099587605135377) bądź na moim serwerze Discord, jeżeli jescze będę zajmować się projektem postaram się naprawić zgłoszone błędy.
  23.  
  24.  
  25.       Instalacja:
  26.       ###  W związku z tym że nie wszyscy mogą wiedzieć w jaki sposób zainstalować userscript poniżej zamieszczam bardzo kródką instrukcję w jaki sposób to zrobić.   ###
  27.  
  28.       #1: Zainstaluj dodatek na twoją przeglądarkę obsługujący skrypty userscript(Przykładowe dodatki na różne przeglądarki):
  29.         -Firefox - Greasemonkey.
  30.         -Chrome, Opera - Tampermonkey.
  31.       #2: Dodaj skrypt z linku w ustawieniach Greasemonkey/Tampermonkey lub przekopiuj całość do edytora posiadanego przez ciebie dodatku i zapisz skrypt.
  32.       #3: Odświerz stronę gry i voilà.
  33.  
  34.  
  35.       Działanie:
  36.         Skrypt zamienia kolory tła kolejnych wiadomości na chacie zgodnie z kolejnością i wartościami rgb podanymi w tablicy w linii 104.
  37.         Kolor godziny przy "nieprzeczytanych" wiadomościach zamienia się na czerwony, wiadomość a także wszystkie poprzednie "nieprzeczytane" staje się "przeczytana" po najechaniu na nią myszką
  38.         Skrypt dodatkowo umożliwia zmianę rozmiaru chatu.
  39.         Długie wiadomości wychodzące poza okno chatu są zwijane do nowej linii.
  40.         Wygląd dodatku w grze: http://czogi.usermd.net/alkatria/obrazy/flexable_chat.png
  41.  
  42.         Changelog:
  43.           -0.1 Pierwsza wersja (07.01.2021).
  44.           -0.2 Zmieniono sposób dodawania listenerów (07.01.2021).
  45.  
  46.         Nie mam nic przeciwko co do modyfikacji i redystrybucji dodatku/kodu.
  47.       !!!UWAGA!!!
  48. */
  49.  
  50. (() => {
  51.   class ChatSettings {
  52.     constructor() {
  53.       this.name = "chatSettings";
  54.       if (!localStorage[this.name]) {
  55.         localStorage.setItem(this.name, "{}");
  56.       }
  57.       this.settings = JSON.parse(localStorage[this.name]);
  58.     }
  59.     get(key, defaultVal) {
  60.       if (typeof this.settings[key] !== "undefined") {
  61.         return this.settings[key];
  62.       }
  63.       return defaultVal;
  64.     }
  65.     set(key, val) {
  66.       this.settings[key] = val;
  67.       localStorage.setItem(this.name, JSON.stringify(this.settings));
  68.     }
  69.   }
  70.   class ChatValues {
  71.     constructor(chatFrameHeight, chatContentHeight, newChatContentHeight, tab) {
  72.       this.frameHeight = this.pxToIntValye(
  73.         getComputedStyle(chatFrameHeight).height
  74.       );
  75.       this.bgGroundHeight = this.pxToIntValye(
  76.         getComputedStyle(chatContentHeight).height
  77.       );
  78.       this.newBgGroundHeight = this.pxToIntValye(
  79.         getComputedStyle(newChatContentHeight).height
  80.       );
  81.       this.tabsHeight = this.pxToIntValye(getComputedStyle(tab).height);
  82.     }
  83.     pxToIntValye(string) {
  84.       return parseInt(string.replace("px", ""));
  85.     }
  86.   }
  87.   class RGB {
  88.     constructor(r, g, b) {
  89.       this.red = r;
  90.       this.green = g;
  91.       this.blue = b;
  92.     }
  93.   }
  94.   const settings = new ChatSettings();
  95.   const newBorder = document.createElement("div");
  96.   const chatBottom = document.querySelector(".chat-frame-bottom");
  97.   const chatContent = document.querySelector(".chat-content");
  98.   const newBgGround = chatContent.cloneNode(false);
  99.   newBgGround.classList = "";
  100.   const chatFrame = document.querySelector(".chat-frame");
  101.  
  102.   const chatTab = document.querySelector(".chat-messages");
  103.   const chatTop = document.querySelector(".w530px");
  104.  
  105.   const colors = [
  106.     new RGB(191, 184, 78),
  107.     new RGB(59, 143, 217),
  108.     new RGB(242, 56, 56)
  109.   ];
  110.  
  111.   let mousePressed = false,
  112.     startMousePosition = 0,
  113.     chatElementsPositions,
  114.     currColor = 0;
  115.  
  116.   for (const tab of Array.from(document.querySelectorAll(".chat-messages"))) {
  117.     tab.style.overflowWrap = "break-word";
  118.   }
  119.  
  120.   newBorder.style.cssText = `border-color: rgb(49, 54, 50);
  121.     border-style: solid;
  122.     border-width: 1px;
  123.     border-collapse: inherit;
  124.     border-spacing: 6px;
  125.     margin: -1px -4px 0px -1px;
  126.     height: ${getComputedStyle(chatContent).height}`;
  127.   chatBottom.style.cursor = "n-resize";
  128.   newBgGround.style.background = getComputedStyle(chatContent).background;
  129.   chatContent.style.background = "none";
  130.   newBgGround.style.backgroundSize = "1000px 1000px";
  131.   newBgGround.style.zIndex = "-1";
  132.   newBgGround.style.backgroundPosition = "-50px -50px";
  133.   newBgGround.style.backgroundRepeat = "no-repeat";
  134.   newBgGround.style.position = "absolute";
  135.   newBgGround.style.width = "523px";
  136.   newBgGround.style.top = "0px";
  137.  
  138.   newBgGround.style.height = getComputedStyle(chatContent).height;
  139.   chatTop.style.background = getComputedStyle(chatBottom).background;
  140.   chatTop.style.left = "3px";
  141.   chatTop.style.transform = "rotate(180deg)";
  142.   chatTop.style.backgroundPosition = "center";
  143.   chatContent.appendChild(newBorder);
  144.   chatContent.appendChild(newBgGround);
  145.   for (const tab of Array.from(document.querySelectorAll(".chat-messages"))) {
  146.     tab.style.backgroundSize = "cover";
  147.   }
  148.   (() => {
  149.     const properYData = settings.get("height", 0);
  150.     if (!properYData) {
  151.       return false;
  152.     }
  153.     chatElementsPositions = new ChatValues(
  154.       chatFrame,
  155.       chatContent,
  156.       newBgGround,
  157.       chatTab
  158.     );
  159.     const contentHeight = properYData + chatElementsPositions.bgGroundHeight;
  160.     const frameHeight = properYData + chatElementsPositions.frameHeight;
  161.     const newContentHeight =
  162.       properYData + chatElementsPositions.newBgGroundHeight;
  163.     const tabsHeight = properYData + chatElementsPositions.tabsHeight;
  164.     chatContent.style.height = `${contentHeight}px`;
  165.     chatFrame.style.height = `${frameHeight}px`;
  166.     newBgGround.style.height = `${newContentHeight}px`;
  167.     newBorder.style.height = `${contentHeight}px`;
  168.     for (const tab of Array.from(document.querySelectorAll(".chat-messages"))) {
  169.       tab.style.height = `${tabsHeight}px`;
  170.     }
  171.   })();
  172.   chatBottom.addEventListener("mousedown", (mouseData) => {
  173.     mousePressed = true;
  174.     chatElementsPositions = new ChatValues(
  175.       chatFrame,
  176.       chatContent,
  177.       newBgGround,
  178.       chatTab
  179.     );
  180.     startMousePosition = mouseData.y;
  181.   });
  182.   document.addEventListener("mouseup", () => {
  183.     if (!mousePressed) {
  184.       return false;
  185.     }
  186.     mousePressed = false;
  187.     startMousePosition = 0;
  188.   });
  189.   document.addEventListener("mousemove", (mouseData) => {
  190.     if (!mousePressed) {
  191.       return false;
  192.     }
  193.     const properYData = (startMousePosition - mouseData.y) * -1;
  194.     settings.set("height", properYData);
  195.  
  196.     const contentHeight = properYData + chatElementsPositions.bgGroundHeight;
  197.     const frameHeight = properYData + chatElementsPositions.frameHeight;
  198.     const newContentHeight =
  199.       properYData + chatElementsPositions.newBgGroundHeight;
  200.     const tabsHeight = properYData + chatElementsPositions.tabsHeight;
  201.     chatContent.style.height = `${contentHeight}px`;
  202.     chatFrame.style.height = `${frameHeight}px`;
  203.     newBgGround.style.height = `${newContentHeight}px`;
  204.     newBorder.style.height = `${contentHeight}px`;
  205.     for (const tab of Array.from(document.querySelectorAll(".chat-messages"))) {
  206.       tab.style.height = `${tabsHeight}px`;
  207.     }
  208.   });
  209.  
  210.   window.game.chatMessage = (data) => {
  211.     data.message = data.message.replace(
  212.       /(http:\/\/[^\s]+)/gi,
  213.       '<a href="$1" target="_blank">$1</a>'
  214.     );
  215.     data.message = data.message.replace(
  216.       /(https:\/\/[^\s]+)/gi,
  217.       '<a href="$1" target="_blank">$1</a>'
  218.     );
  219.     var text = "";
  220.     const dateArray = data.date.split(".");
  221.     const timeStampOfMsg = Date.parse(
  222.       `${data.time} ${dateArray[1]}.${dateArray[0]}.${dateArray[2]}`
  223.     );
  224.     let msgIsNew = false;
  225.     if (settings.get(`${data.channel}|lastRead`, 0) < timeStampOfMsg) {
  226.       msgIsNew = !msgIsNew;
  227.     }
  228.  
  229.     if (data.player != undefined) {
  230.       text =
  231.         `<span style="${msgIsNew ? "color: red" : ""}">${data.time}</span>` +
  232.         ' <span class="player-chat" data-name="' +
  233.         data.name +
  234.         '">' +
  235.         data.player +
  236.         "</span>: " +
  237.         data.message;
  238.     } else if (data.time == undefined) {
  239.       text = data.message;
  240.     } else {
  241.       text =
  242.         `<span style="${msgIsNew ? "color: red" : ""}">${data.time}</span>` +
  243.         ": " +
  244.         data.message;
  245.     }
  246.  
  247.     const bgOfMsg = `background: rgb(${Object.values(
  248.       colors[currColor++ % colors.length]
  249.     ).toString()},0.55)`;
  250.     if (data.color != undefined) {
  251.       $(".chat-messages-" + data.channel).append(
  252.         `<span title=${data.date} style="color: ${data.color} ${bgOfMsg}" onmouseenter="leaveMsg(this)">${text}</span><br>`
  253.       );
  254.     } else {
  255.       $(".chat-messages-" + data.channel).append(
  256.         ` <span title=${data.date} style="${bgOfMsg}" onmouseenter="leaveMsg(this)">${text}</span><br>`
  257.       );
  258.     }
  259.  
  260.     if (this.channel != data.channel) {
  261.       $(".channel-" + data.channel).addClass("new-message");
  262.     }
  263.  
  264.     document.getElementById(
  265.       "chat-messages-" + data.channel
  266.     ).scrollTop = document.getElementById(
  267.       "chat-messages-" + data.channel
  268.     ).scrollHeight;
  269.   };
  270.   window.leaveMsg = (msg) => {
  271.     const timeChild = msg.children[0];
  272.     if (timeChild.style.color !== "red") {
  273.       return false;
  274.     }
  275.     const dateArray = msg.title.split(".");
  276.     const timeStampOfMsg = Date.parse(
  277.       `${timeChild.innerText} ${dateArray[1]}.${dateArray[0]}.${dateArray[2]}`
  278.     );
  279.     settings.set(`${game.channel}|lastRead`, timeStampOfMsg);
  280.     Array.from(
  281.       document.querySelector("#chat-messages-" + game.channel).children
  282.     ).forEach((element) => {
  283.       if (element.tagName === "BR") {
  284.         return false;
  285.       }
  286.       const timeChild = element.children[0];
  287.       if (timeChild.style.color !== "red") {
  288.         return false;
  289.       }
  290.       const dateArray = element.title.split(".");
  291.       const timeStampOfElement = Date.parse(
  292.         `${timeChild.innerText} ${dateArray[1]}.${dateArray[0]}.${dateArray[2]}`
  293.       );
  294.       if (timeStampOfElement > timeStampOfMsg) {
  295.         return;
  296.       }
  297.       timeChild.style.color = "white";
  298.     });
  299.   };
  300. })();
Add Comment
Please, Sign In to add comment