Greasy Fork is available in English.

CWSS

Complete WebSocket Sniffer

Versão de: 21/03/2022. Veja: a última versão.

Este script não deve ser instalado diretamente. Este script é uma biblioteca de outros scripts para incluir com o diretório meta // @require https://update.greasyfork.org/scripts/438408/1030553/CWSS.js

Autor
Exnonull
Versão
2.1
Criado
12/01/2022
Atualizado
21/03/2022
Licença
MIT

// ==UserScript==
// @license MIT
// @name CWSS
// @version 2.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) => async function() {
for (const hook of hooks.sort((a, b) => b.priority - a.priority)) {
if (hook[type]) arguments = await hook[type].call(this, ...arguments);
if (!arguments) return;
}
next.flat().forEach(func => func.call(this, ...arguments));
};
const pipeSync = (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() {
arguments = pipeSync('args').call(arguments);
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);
window.WebSocket._send = send;
return hooks;
})();
const root = {
sockets,
setHook(hook) {
hooks.push(hook);
return root;
},
setHooks(..._hooks) {
hooks.push(..._hooks.flat());
return root;
}
};
return root;
})();
// 0vC4#7152