MyBot WebSocket Sniffer
当前为
此脚本不应直接安装。它是供其他脚本使用的外部库,要使用该库请加入元指令 // @require https://update.cn-greasyfork.org/scripts/479125/1275785/MyBot%20CWSS.js
// ==UserScript==
// @license MIT
// @name MyBot CWSS
// @version 1.0
// @description MyBot WebSocket Sniffer
// @author SamaelWired
// @match http://*/*
// @match https://*/*
// @grant none
// @run-at document-start
// @namespace https://greasyfork.org/tr/users/976572
// ==/UserScript==
const MyBotCWSS = (() => {
const MyBotCWSS = window.MyBotCWSS || {};
if (MyBotCWSS.ws) return MyBotCWSS;
const proto = WebSocket.prototype;
const def = Object.defineProperty;
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 pipe = (type, ...next) => async function() {
for (const hook of MyBotCWSS.hooks.sort((a, b) => b.priority - a.priority)) {
if (!hook[type]) continue;
if (!arguments) break;
arguments = await hook[type].call(this, ...arguments);
}
if (!arguments) return;
next.flat().forEach(func => func.call(this, ...arguments));
};
const pipeSync = type => function() {
for (const hook of MyBotCWSS.hooks.sort((a, b) => b.priority - a.priority)) {
if (!hook[type]) continue;
if (!arguments) break;
arguments = hook[type].call(this, ...arguments);
}
return arguments;
};
MyBotCWSS.ws = window.WebSocket;
MyBotCWSS.send = proto.send;
MyBotCWSS.addList = proto.addEventListener;
MyBotCWSS.sockets = [];
MyBotCWSS.hooks = [];
MyBotCWSS.setHook = hook => {
MyBotCWSS.hooks.push(hook);
return MyBotCWSS;
};
MyBotCWSS.setHooks = (...hooks) => {
MyBotCWSS.hooks.push(...hooks.flat());
return MyBotCWSS;
};
proto.send = pipe('send', MyBotCWSS.send);
proto.addEventListener = function() {
const type = arguments[0];
const func = arguments[1];
const list = this.listeners[type];
if (list) list.push(func);
else MyBotCWSS.addList.call(this, ...arguments);
};
window.WebSocket = function() {
arguments = pipeSync('args').call(this, ...arguments);
const ws = new MyBotCWSS.ws(...arguments);
for (const hook of MyBotCWSS.hooks.sort((a, b) => b.priority - a.priority))
Object.assign(hook, {
ws,
async sendServer(data) {
MyBotCWSS.send.call(ws, data);
},
async sendClient(data) {
ws.listeners.message
.forEach(func =>
func.call(ws, {data})
);
},
});
MyBotCWSS.sockets.push(ws);
pipe('init').call(ws);
ws.listeners = {};
for (const key of ['open', 'message', 'close']) {
const list = ws.listeners[key] = [];
MyBotCWSS.addList.call(ws, key, pipe(key, list));
rebase(ws, 'on'+key, list);
}
return ws;
};
for (const k in MyBotCWSS.ws)
if (k != 'prototype')
window.WebSocket[k] = MyBotCWSS.ws[k];
for (const k in proto)
if (k != 'constructor')
try {
window.WebSocket.prototype[k] = proto[k];
} catch (e) {};
native(proto.send, MyBotCWSS.send);
native(proto.addEventListener, MyBotCWSS.addList);
native(window.WebSocket, MyBotCWSS.ws);
window.MyBotCWSS = MyBotCWSS;
return MyBotCWSS;
})();