Cuberealm.io socket tools library

sendChat function and chat handlers

此脚本不应直接安装。它是供其他脚本使用的外部库,要使用该库请加入元指令 // @require https://update.greasyfork.org/scripts/584240/1889547/Cuberealmio%20socket%20tools%20library.js

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Greasemonkey 油猴子Violentmonkey 暴力猴,才能安装此脚本。

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

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Violentmonkey 暴力猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Userscripts ,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展后才能安装此脚本。

(我已经安装了用户脚本管理器,让我安装!)

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

(我已经安装了用户样式管理器,让我安装!)

// license: GPL3

(function(){
    const colorChar = "∁";
    const originalSend = WebSocket.prototype.send;
    let sockets = [];

    const colors = {
        white: "ffffff",
        blue: "6ab4ff",
        gold: "ffd700",
        error: "ff5050",
        info: "40FFFF",
        success: "40FF40",
        help: "ffa540",
        convert(color){
            return colorChar + (colors[color]||color);
        }
    };
    
    let handlers = {
        gameStart: ((socket)=>{}), // when the first packet is sent to the server
        chat: ((text)=>{}), // outgoing text from the client
    };
    
    function setHandler(handler, func){
        if(typeof func !== "function"){
            throw new TypeError("The callback function for setHandler must be a function");
        }
        handlers[handler] = func;
    }
    
    WebSocket.prototype.send = function(...args) {
        if (sockets.indexOf(this) === -1 && this.readyState === WebSocket.OPEN) {
            sockets.push(this);
            handlers.gameStart(this);
            setTimeout(() => printColored("Socket Tools Library Loaded - by coolussr1481"), 50);
        }
        
        const socket = this;
        socket.addEventListener("close", function() {
            sockets[sockets.indexOf(socket)] = undefined;
        });
        
        socket.addEventListener("error", function() {
            sockets[sockets.indexOf(socket)] = undefined;
        });
        
        const u8 = new Uint8Array(args[0]);
        const type = u8[1];
        let r = false;
        
        if(type === 0x0d){
            r = handlers.chat(decoder.decode(u8.slice(3)));
        } else if(handlers[type]){
            r = handlers[type](u8);
            
        }
        
        
        if(!(typeof r === "string" && r === "__preventDefaultSend"))
        return originalSend.call(this, ...args);
    };
    
    const encoder = new TextEncoder('utf-8');
    const decoder = new TextDecoder('utf-8');
    
    function sendChat(text){
        if(sockets[sockets.length - 1])
        originalSend.call(sockets[sockets.length - 1], appendBuffer(toBuffer([0x11, 0x0d, 0x0]), encoder.encode(text))); 
    }
    
    function printChat(text){
        sockets[sockets.length - 1].dispatchEvent(new MessageEvent('message', { 
            data: appendBuffer(toBuffer([0x11, 0x0d, 0x0]), encoder.encode(text.toString())),
            orgin: undefined,
            lastEventid: "",
            source: null,
        }));
    }
    
    function printColored(text, hex){
        if(typeof hex !== 'string'){
            throw new TypeError("the hex argument of printColored must be a hex string");
        }
        printChat(`${colorChar}${hex}${text}`);
    }
    
    function printMessage(msg, replaceFunc){
        const m = msg;
        function time(i){
            if(i < m.length){
                if(replaceFunc){
                    printChat(replaceFunc(m[i]));
                } else {
                    printChat(m[i]);
                }
                setTimeout(() => time(i + 1), 5);
            }
        }
        time(0);
    }
    
    function toBuffer(array){
    	return Uint8Array.from(array).buffer;
    }
    
    function appendBuffer(a, b) {
        let temp = new Uint8Array(a.byteLength + b.byteLength);
        temp.set(new Uint8Array(a), 0);
        temp.set(new Uint8Array(b), a.byteLength);
        return temp.buffer;
    }
    
    const toExport = { originalSend, sockets, setHandler, sendChat, printChat, printColored, colorChar, colors, printMessage };
    window.socketToolsExports = {};
    for(let k in toExport){
        window.socketToolsExports[k] = toExport[k];
    }
    
})();