Skip to content

Instantly share code, notes, and snippets.

@samdenty
Last active April 10, 2018 00:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save samdenty/d39ce82624ae1b35b0bdf1432e35c261 to your computer and use it in GitHub Desktop.
Save samdenty/d39ce82624ae1b35b0bdf1432e35c261 to your computer and use it in GitHub Desktop.
Websocket emulator for Chrome devtools
/**
* Transport layer emulator
*/
class Transport {
constructor(/** @type {WebSocket} */ websocket) {
this.send = (...args) => websocket._send(...args)
this.close = () => websocket.close()
this.onOpen()
}
onOpen() {}
handleMessage(message) {
if (message.method === 'Runtime.runIfWaitingForDebugger') {
this.send({"id":1,"result":{}},
{"method":"Runtime.executionContextCreated","params":{"context":{"id":1,"origin":"","name":"Worker"}}} ,
{"method":"Runtime.consoleAPICalled","params":{"type":"error","args":[{"type":"string","value":"WOOT!"},{"type":"object","className":"MyCustomClass","description":"Request","objectId":"{\"injectedScriptId\":1,\"id\":1}"}],"executionContextId":1,"timestamp":0,"stackTrace":{"callFrames":[{"functionName":"fetchAndLog","scriptId":"11","url":"","lineNumber":9,"columnNumber":10}]}}} ,
{"method":"Runtime.consoleAPICalled","params":{"type":"log","args":[{"type":"string","value":"CONSOLE %cFORMATTING :D"},{"type":"string","value":"color: red"}],"executionContextId":1,"timestamp":0,"stackTrace":{"callFrames":[{"functionName":"","scriptId":"41","url":"","lineNumber":0,"columnNumber":8}]}}},
{"id":2,"result":{}},
{"method":"Debugger.scriptParsed","params":{"scriptId":"11","url":"","startLine":0,"startColumn":0,"endLine":13,"endColumn":1,"executionContextId":1,"hash":"BE635EC24CF67059AB309333C8446C66D17DD491","isLiveEdit":false,"sourceMapURL":"","hasSourceURL":false,"isModule":false,"length":340}} ,
{"id":3,"result":{"debuggerId":"(CA8E9A9BF34D00D1C7F3EDDA505A85A7)"}},
{"id":4,"result":{}})
}
}
}
SDK.Logs = []
class WebSocket {
readyState = 1
url = null
onopen = null
onclose = null
onmessage = null
constructor(url) {
this.url = url
setTimeout(() => {
this.check()
})
}
check() {
if (this.onopen) {
this.open()
} else {
setTimeout(() => {
this.check()
}, 100)
}
}
open() {
this.transport = new Transport(this)
this.onopen()
}
send(message) {
try {
message = JSON.parse(message)
} catch (e) {
console.error(`[WEBSOCKET-EMULATOR]`, 'Failed to parse message!', e)
return
}
SDK.Logs.push(['SEND', message])
this.transport.handleMessage(message)
}
_send(...messages) {
for (let message of messages) {
try {
message = JSON.stringify(message)
} catch (e) {
console.error(
`[WEBSOCKET-EMULATOR]`,
'Failed to convert message into JSON!',
e
)
return
}
SDK.Logs.push(['RECEIVE', message])
if (this.onmessage) {
this.onmessage({
data: message
})
}
}
}
close() {
if (this.onclose) {
this.onclose()
}
this.readyState = 3
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment