MyBot CWSS

MyBot WebSocket Sniffer

此脚本不应直接安装。它是供其他脚本使用的外部库,要使用该库请加入元指令 // @require https://update.gf.qytechs.cn/scripts/479125/1275822/MyBot%20CWSS.js

您需要先安装一个扩展,例如 篡改猴Greasemonkey暴力猴,之后才能安装此脚本。

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

您需要先安装一个扩展,例如 篡改猴暴力猴,之后才能安装此脚本。

您需要先安装一个扩展,例如 篡改猴Userscripts ,之后才能安装此脚本。

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

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

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

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

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

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

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

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

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

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

// ==UserScript==
// @license      MIT
// @name         MyBot CWSS
// @version      1.0
// @description  MyBot WebSocket Sniffer
// @author       SamaelWired
// @match        http://*/*
// @match        https://*/*
// @grant        none
// @run-at       document-start
// @namespace    https://greasyfork.org/tr/users/976572
// ==/UserScript==





const CWSS = (() => {
    const CWSS = window.CWSS || {};
    if (CWSS.ws) return CWSS;
    
    
    
    
    
    const proto = WebSocket.prototype;
    const def = Object.defineProperty;
    const rebase = (obj, key, list) => def(obj, key, {
        configurable: true,
        enumerable: true,
        set: func => list.push(func)
    });
    const native = (obj, value) => {
        obj.toString = function() {
            return Function.toString.call(value, ...arguments);
        };
    };
    
    
    
    
    
    const pipe = (type, ...next) => async function() {
        for (const hook of CWSS.hooks.sort((a, b) => b.priority - a.priority)) {
            if (!hook[type]) continue;
            if (!arguments) break;
            arguments = await hook[type].call(this, ...arguments);
        }
        
        if (!arguments) return;
        next.flat().forEach(func => func.call(this, ...arguments));
    };
    const pipeSync = type => function() {
        for (const hook of CWSS.hooks.sort((a, b) => b.priority - a.priority)) {
            if (!hook[type]) continue;
            if (!arguments) break;
            arguments = hook[type].call(this, ...arguments);
        }
        return arguments;
    };
    
    
    
    
    
    CWSS.ws = window.WebSocket;
    CWSS.send = proto.send;
    CWSS.addList = proto.addEventListener;
    
    CWSS.sockets = [];
    CWSS.hooks = [];
    CWSS.setHook = hook => {
        CWSS.hooks.push(hook);
        return CWSS;
    };
    CWSS.setHooks = (...hooks) => {
        CWSS.hooks.push(...hooks.flat());
        return CWSS;
    };
    
    
    
    
    
    proto.send = pipe('send', CWSS.send);
    proto.addEventListener = function() {
        const type = arguments[0];
        const func = arguments[1];
        const list = this.listeners[type];
        if (list) list.push(func);
        else CWSS.addList.call(this, ...arguments);
    };
    
    
    
    
    
    window.WebSocket = function() {
        arguments = pipeSync('args').call(this, ...arguments);
        const ws = new CWSS.ws(...arguments);
        
        for (const hook of CWSS.hooks.sort((a, b) => b.priority - a.priority))
            Object.assign(hook, {
                ws,
                async sendServer(data) {
                    CWSS.send.call(ws, data);
                },
                async sendClient(data) {
                    ws.listeners.message
                    .forEach(func => 
                        func.call(ws, {data})
                    );
                },
            });
        
        CWSS.sockets.push(ws);
        pipe('init').call(ws);
        
        ws.listeners = {};
        for (const key of ['open', 'message', 'close']) {
            const list = ws.listeners[key] = [];
            CWSS.addList.call(ws, key, pipe(key, list));
            rebase(ws, 'on'+key, list);
        }
        
        return ws;
    };
    
    
    
    
    
    for (const k in CWSS.ws)
        if (k != 'prototype')
            window.WebSocket[k] = CWSS.ws[k];
    
    for (const k in proto)
        if (k != 'constructor')
            try {
                window.WebSocket.prototype[k] = proto[k];
            } catch (e) {};
    
    
    
    
    
    native(proto.send, CWSS.send);
    native(proto.addEventListener, CWSS.addList);
    native(window.WebSocket, CWSS.ws);
    
    
    
    
    
    window.CWSS = CWSS;
    return CWSS;
})();