WS Hook

WebSocket Hook

Ekde 2021/09/19. Vidu La ĝisdata versio.

This script should not be not be installed directly. It is a library for other scripts to include with the meta directive // @require https://update.greasyfork.org/scripts/432651/972034/WS%20Hook.js

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

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

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

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

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

You will need to install a user script manager extension to install this script.

(I already have a user script manager, let me install it!)

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

(I already have a user style manager, let me install it!)

var webHook=(function() {
    'use strict';
    var backup={};
    var api={
        filter:(args)=>{return true;},
        WSList:[],
        messageHandler:(event)=>{
            var e = new Event('globalWebsocketMessage')
            e.data=event.data
            window.document.dispactEvent(e);
        },
        closeHandler:(event)=>{
            var e = new Event('globalWebsocketClose')
            e.data=event.data
            window.document.dispactEvent(e);
            event.target.removeEventListener('message', api.messageHandler);
            event.target.removeEventListener('close', api.closeHandler);
        },
        beginSend:(webSocket,args)=>{},
        endSend:(webSocket,args)=>{},
        filterSend:(webSocket,args)=>{return true;}

    }
    backup.WebSocket=window.WebSocket;
    window.WebSocket=new Proxy(window.WebSocket,{
        construct:function(target,args){
            if(api.filter(args)){
                let ws=new target(...args);
                api.WSList.push(ws);
                ws.addEventListener('message', api.messageHandler);
                ws.addEventListener('close', api.closeHandler);
                backup.WSSend==ws.send;
                ws.send= new Proxy(ws.send, {
                    apply: function(target, _this, _arguments) {
                        if(ws.readyState === ws.OPEN && api.filterSend(_this, _arguments)){
                            api.beginSend(_this,_arguments)
                            Function.prototype.apply.apply(target, [_this, _arguments]);
                            api.endSend(_this,_arguments);

                        }
                    }
                });
            }
        }
    });
    console.log(api);
    return api;
})();