moltarze
moltarze

Reputation: 1501

Acquire variable reference to local variable in Chrome

I'm debugging an online program and I ran into a problem. I would like to intercept the messages from a WebSocket object for manipulation in the browser console. However, the websocket is created like this:

(function() {
    var s = new WebSocket();
})()

Therefore it was created locally and I have no reference to s. My question - is there any sort of tool in Chrome DevTools to obtain a reference to this variable, so it can be accessed in the browser console?

I attempted to insert a script to modify the body (and the script) before it loaded, which was ultimately unsuccessful. There's bound to be some sort of introspective tool in Chrome that allows you to do such a thing.

Upvotes: 0

Views: 276

Answers (1)

DragonSGA
DragonSGA

Reputation: 340

Set a breakpoint in that line via DevTool's Debugger, go one step, enter window.yourWsRef = s; into the console. Then you can access it from global scope.

Upvotes: 1

Related Questions