MCPcopy Create free account
hub / github.com/Useful-Scripts-Extension/useful-script / initWS

Function initWS

scripts/libs/ajax-hook/index.js:188–271  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

186let readyWs = false;
187
188function initWS() {
189 const _WS = WebSocket;
190 WebSocket = function (url, protocols) {
191 let WSObject;
192 for (let { fn } of modifyUrlWsFn) {
193 let _url = fn?.(url);
194 if (_url) url = _url;
195 if (_url === null) return null;
196 }
197 this.url = url;
198 this.protocols = protocols;
199 if (!this.protocols) {
200 WSObject = new _WS(url);
201 } else {
202 WSObject = new _WS(url, protocols);
203 }
204
205 const _send = WSObject.send;
206 WSObject.send = function () {
207 let arg0 = arguments[0];
208 for (const { fn } of onBeforeWSFn) {
209 const res = fn?.(arg0, WSObject.url, WSObject);
210 if (res) arg0 = res;
211 if (res === null) return;
212 }
213 arguments[0] = arg0;
214 _send.apply(this, arguments);
215 };
216
217 // Events needs to be proxied and bubbled down.
218 WSObject._addEventListener = WSObject.addEventListener;
219 WSObject.addEventListener = function () {
220 const eventThis = this;
221 // if eventName is 'message'
222 if (arguments[0] === "message") {
223 arguments[1] = (function (userFunc) {
224 return async function instrumentAddEventListener() {
225 let arg0 = arguments[0];
226 for (const { fn } of onAfterWSFn) {
227 const res = await fn?.(
228 new MutableMessageEvent(arg0),
229 WSObject.url,
230 WSObject
231 );
232 if (res) arg0 = res;
233 if (res === null) return;
234 }
235 arguments[0] = arg0;
236 userFunc.apply(eventThis, arguments);
237 };
238 })(arguments[1]);
239 }
240 return WSObject._addEventListener.apply(this, arguments);
241 };
242
243 Object.defineProperty(WSObject, "onmessage", {
244 set: function () {
245 const eventThis = this;

Callers 1

hookWSFunction · 0.85

Calls 1

fnFunction · 0.50

Tested by

no test coverage detected