本文介绍了获取网站的活动 Websockets 可能吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道是否可以获取网站的活动 WebSocket.例如:var x = document.findWebSocket().websockets 将列在 Chrome 的网络选项卡下(在开发工具部分).从那里 websockets 列在WS"下.我也希望能够执行 x.emit(..); .

I would like to know if its possible to get active WebSockets of a Website.An example would be: var x = document.findWebSocket(). The websockets would be listed in Chrome under the Network Tab (In the dev tools section). From there the websockets are listed under "WS". I want to be able to do x.emit(..); as well.

到目前为止我只能想出 var x = new WebSocket("wss://exampleUrl.com/socket.io/?EIO=3&transport=websocket", "protocol1");.但这只会添加一个新的 Websocket,其 sid 与我想从中发出消息的 sid 不同.

So far i could only come up with var x = new WebSocket("wss://exampleUrl.com/socket.io/?EIO=3&transport=websocket", "protocol1");. But this only adds a new Websocket with a different sid from the one that i want to emit messages from.

添加&sid = {SID of Active Websocket}"将不起作用.

adding "&sid = {SID of Active Websocket}" would not work.

推荐答案

这有点 hacky,但是如果您可以注入在站点代码执行之前运行的代码(例如,使用 Tampermonkey 和 @run-at 文档-start),您可以对 window.WebSocket 进行猴子补丁,这样无论何时调用它,您都可以将创建的 websocket 添加到一个数组中,您可以稍后检查该数组.例如,在 Stack Overflow 上运行以下代码:

It's a bit hacky, but if you can inject code that runs before the site's code does (for example, with Tampermonkey and @run-at document-start), you can monkeypatch window.WebSocket so that whenever it's called, you add the created websocket to an array which you can examine later. For example, running the following on Stack Overflow:

// ==UserScript==
// @name             0 New Userscript
// @include          /^https://stackoverflow.com
// @run-at           document-start
// @grant            none
// ==/UserScript==

const sockets = [];
const nativeWebSocket = window.WebSocket;
window.WebSocket = function(...args){
  const socket = new nativeWebSocket(...args);
  sockets.push(socket);
  return socket;
};
setTimeout(() => {
  // or create a button which, when clicked, does something with the sockets
  console.log(sockets);
}, 1000);

导致 [WebSocket] 被记录(并且您可以继续对该实例执行任何您想做的事情,例如调用 emit).

results in [WebSocket] being logged (and you could proceed to do whatever you wanted to do with the instance, such as call emit).

这篇关于获取网站的活动 Websockets 可能吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-24 03:30