Greasy Fork

Greasy Fork is available in English.

CWSS

Complete WebSocket Sniffer

当前为 2022-03-21 提交的版本,查看 最新版本

此脚本不应直接安装。它是供其他脚本使用的外部库,要使用该库请加入元指令 // @require https://update.greasyfork.icu/scripts/438408/1030553/CWSS.js

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Greasemonkey 油猴子Violentmonkey 暴力猴,才能安装此脚本。

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

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

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

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

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

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

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

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

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

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

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

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

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

作者
Exnonull
版本
2.1
创建于
2022-01-12
更新于
2022-03-21
大小
3.2 KB
许可证
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 http://greasyfork.icu/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