Global Chat

You can chat with other players from all servers! When you write something in the chat sploop.io then it is shown in the global chat. (This script does not allow you to communicate with everyone, but allows you to communicate with all users of this script)

您需要先安裝使用者腳本管理器擴展,如 TampermonkeyGreasemonkeyViolentmonkey 之後才能安裝該腳本。

You will need to install an extension such as Tampermonkey to install this script.

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyViolentmonkey 後才能安裝該腳本。

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyUserscripts 後才能安裝該腳本。

你需要先安裝一款使用者腳本管理器擴展,比如 Tampermonkey,才能安裝此腳本

您需要先安裝使用者腳本管理器擴充功能後才能安裝該腳本。

(我已經安裝了使用者腳本管理器,讓我安裝!)

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

(我已經安裝了使用者樣式管理器,讓我安裝!)

// ==UserScript==
// @name         Global Chat
// @namespace    -
// @version      0.1
// @description  You can chat with other players from all servers! When you write something in the chat sploop.io then it is shown in the global chat. (This script does not allow you to communicate with everyone, but allows you to communicate with all users of this script)
// @author       Nudo
// @match        *://sploop.io/*
// @icon         https://www.google.com/s2/favicons?sz=64&domain=sploop.io
// @grant        none
// ==/UserScript==

(function() {
    const SERVER = "globalchatserver.stopitplz.repl.co"
    const WSSERVER = `ws${location.protocol.slice(4, 5) || ""}://${SERVER}`

    let socket = null

    function sendMessage(data) {
        if (!socket) {
            return void 0
        }

        if (socket.readyState !== 1) {
            return void 0
        }

        socket.send(JSON.stringify([localStorage.nickname || "noname", data]))
    }

    function serverSetup() {
        socket = new WebSocket(WSSERVER)
        socket.binaryType = "arraybuffer"

        socket.onopen = () => {
            sendMessage("connected")
        }

        socket.onmessage = (message) => {
            const data = JSON.parse(message.data)

            onMessage(data)
        }
    }

    function onMessage(data) {
        Menu.addMessage(data[0], data[1], data[2])
    }

    window.addEventListener("load", () => serverSetup())

    const Menu = {}

    Menu.html = `
    <div class="global-chat-holder">
      <container class="chat-wrapper"></container>
    </div>

    <style>
    .chat-wrapper main {
      color: #fff;
      margin: 3px;
      font-size: 14px;
    }

    .chat-wrapper {
      display: flex;
      flex-direction: column;
      overflow: hidden;
      max-height: 400px;
    }

    .chat-wrapper::-webkit-scrollbar {
      width: 0;
    }

    .global-chat-holder {
      position: absolute;
      top: 5px;
      left: 5px;
      pointer-events: none;
      z-index: 999999;
    }
    </style>
    `

    Menu.addMessage = function(nickname, message, color) {
        if (!message.length) {
            return void 0
        }

        const wrapper = document.querySelector(".chat-wrapper")

        message = antiSpam(message)

        wrapper.innerHTML += `
        <main class="text-shadowed-3" style="color: ${color || "#fff"}">
        ${nickname}: ${message}
        </main>
        `

        wrapper.scrollTo(0, wrapper.scrollHeight)
    }

    const canvas = document.getElementById("game-canvas")

    canvas.insertAdjacentHTML("beforebegin", Menu.html)

    window.addEventListener("keydown", (event) => {
        if (event.code === "Enter") {
            const chatValue = document.getElementById("chat").value

            sendMessage(chatValue)
        }
    })

    function antiSpamLetters(message) {
        let letterCounter = {}

        for (let i = 0; i < message.length; i++) {
            if (!message[i - 1]) {
                continue
            }

            if (letterCounter[message[i]] == null) {
                letterCounter[message[i]] = 0
            }

            if (message[i].toLowerCase() === message[i - 1].toLowerCase()) {
                letterCounter[message[i]] += 1
            }
        }

        for (const letter in letterCounter) {
            if (letterCounter[letter] >= 5) {
                message = "spam"
            }
        }

        return message
    }

    function antiSpamSymbols(message) {
        let symbolCounter = {}

        const symbols = ["@", "!", "#", "$", "%", "&"]

        for (const symbol of symbols) {
            if (message.match(symbol)) {
                for (const letter of message) {
                    if (symbolCounter[letter] == null) {
                        symbolCounter[letter] = 0
                    }

                    if (letter === symbol) {
                        symbolCounter[letter] += 1
                    }
                }
            }
        }

        for (const symbol in symbolCounter) {
            if (symbolCounter[symbol] >= 5) {
                message = "spam"
            }
        }

        return message
    }

    function antiSpam(message) {
        message = antiSpamLetters(message)
        message = antiSpamSymbols(message)

        return message
    }
})()