Taming WebSocket Hook

Hooks taming.io websocket

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

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

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

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

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

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

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

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

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

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

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

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

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

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

// ==UserScript==
// @name Taming WebSocket Hook
// @author Murka
// @description Hooks taming.io websocket
// @icon https://taming.io/img/ui/favicon.png
// @version 1.0.0
// @match *://taming.io/*
// @run-at document-start
// @grant none
// @license MIT
// @namespace https://greasyfork.org/users/919633
// ==/UserScript==
/* jshint esversion:6 */

/*
    Author: Murka
    Github: https://github.com/Murka007
    Greasyfork: https://greasyfork.org/users/919633
    Discord: https://discord.gg/cPRFdcZkeD
*/

(function() {
    "use strict";

    const log = console.log;
    const _Function = Function;
    const _Proxy = Proxy;
    const _apply = Reflect.apply;
    const _WebSocket = WebSocket;
    const _Uint8Array = Uint8Array;
    const _getOwnPropertyDescriptor = Object.getOwnPropertyDescriptor;
    const _defineProperty = Object.defineProperty;

    const _FunctionNew = window.Function = new _Proxy(Function, {
        construct(target, args) {
            if (/debugger/.test(args[0])) {
                return function(){}
            }
            return new target(...args);
        }
    });

    const hookContext = (scope, type, callback) => {
        const proto = scope.HTMLIFrameElement.prototype;
        const descriptor = _getOwnPropertyDescriptor(proto, type);
        _defineProperty(proto, type, {
            get() {
                const target = descriptor.get.call(this);
                callback(target);
                return target;
            },
            configurable: true
        });
    }

    const handleSocket = (socket) => {
        if (socket.onopen === null) return;
        log("socket", socket);

        socket.addEventListener("message", event => {
            const temp = new _Uint8Array(event.data);
            log(temp);
        });
    }

    const scopeList = new Set();
    (function getScope(scope) {
        if (scopeList.has(scope)) return;
        scopeList.add(scope);
        log("found scope", scope);

        const _binaryType = _getOwnPropertyDescriptor(scope.WebSocket.prototype, "binaryType");
        _defineProperty(scope.WebSocket.prototype, "binaryType", {
            set(value) {
                handleSocket(this);
                return _binaryType.set.call(this, value);
            },
            get() {
                return _binaryType.get.call(this);
            },
            configurable: true
        });

        const _createElement = scope.document.createElement;
        scope.document.createElement = function(tagName) {
            if (tagName === "iframe") {
                hookContext(scope, "contentWindow", target => getScope(target));
                hookContext(scope, "contentDocument", target => getScope(target.defaultView));
            }
            return _createElement.call(this, tagName);
        }
    })(window);

    const toStringOld = Function.prototype.toString;
    const toStringNew = Function.prototype.toString = new _Proxy(toStringOld, {
        apply(target, thisArg, args) {
            if (thisArg === _FunctionNew) return _apply(target, _Function, args);
            if (thisArg === toStringNew) return _apply(target, toStringOld, args);
            return _apply(target, thisArg, args);
        }
    });

})();