CWSS

Complete WebSocket Sniffer

目前為 2022-01-16 提交的版本,檢視 最新版本

此腳本不應該直接安裝,它是一個供其他腳本使用的函式庫。欲使用本函式庫,請在腳本 metadata 寫上: // @require https://update.cn-greasyfork.org/scripts/438408/1009009/CWSS.js

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

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

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

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

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

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

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

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

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

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

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

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

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

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

// ==UserScript==
// @license      MIT
// @name         CWSS
// @version      1.1
// @description  Complete WebSocket Sniffer
// @author       0vC4
// @match        http://*/*
// @match        https://*/*
// @grant        none
// @run-at       document-start
// @namespace    https://greasyfork.org/users/670183
// ==/UserScript==
const CWSS = (() => {
    const _hooks_ = 'hooks'; // all hooks WebSocket.prototype[_hooks_]
    const eventKey = key => key+'_listener'; // all event listeners WebSocket.prototype[eventKey('open'|'message'|'close')]
    const listeners = ['open', 'message', 'close']; // (+init&send hooks available by default)
    const proto = window.WebSocket.prototype;
    const def = Object.defineProperty;
    const hidden = (obj, key, value) => def(obj, key, {configurable: true, value});
    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 sockets = [];
    const hooks = (() => {
        if (_hooks_ in proto) return proto[_hooks_];
        hidden(proto, _hooks_, []);
        const hooks = proto[_hooks_];
        const {send, addEventListener} = proto;
        const pipe = (type, ...next) => function() {
            for (const hook of hooks.sort((a, b) => b.priority - a.priority)) {
                if (hook[type]) arguments = hook[type].call(this, ...arguments);
                if (!arguments) return;
            }
            next.flat().forEach(func => func.call(this, ...arguments));
        };
        proto.send = pipe('send', send);
        native(proto.send, send);
        proto.addEventListener = function() {
            const type = arguments[0];
            const func = arguments[1];
            const list = this[eventKey(type)];
            if (list) list.push(func);
            else addEventListener.call(this, ...arguments);
        }
        native(proto.addEventListener, addEventListener);
        const Ows = window.WebSocket;
        window.WebSocket = function() {
            const ws = new Ows(...arguments);
            sockets.push(ws);
            pipe('init').call(ws);
            for(const key of listeners) {
                const list_key = eventKey(key);
                const list = hidden(ws, list_key, [])[list_key];
                addEventListener.call(ws, key, pipe(key, list));
                rebase(ws, 'on'+key, list);
            }
            return ws;
        }
        for(const k in Ows) if (k != 'prototype') window.WebSocket[k] = Ows[k];
        for(const k in Ows.prototype) if (k != 'constructor') try {window.WebSocket.prototype[k] = Ows.prototype[k];} catch(e) {};
        WebSocket.prototype[_hooks_] = Ows.prototype[_hooks_];
        native(window.WebSocket, Ows);
        return hooks;
    })();
    const root = {
        sockets,
        setHook(hook) {
            hooks.push(hook);
            return root;
        },
        setHooks(..._hooks) {
            hooks.push(..._hooks.flat());
            return root;
        }
    };
    return root;
})();
// 0vC4#7152