Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

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.

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.

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
429 views
Welcome To Ask or Share your Answers For Others

1 Answer

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);

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...