Last active
October 10, 2022 09:23
-
-
Save BlackHole1/2f88d43d7d4893b22e6a43f2b09bad3f to your computer and use it in GitHub Desktop.
Capture WebSocket message in electron
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const {app, BrowserWindow} = require('electron') | |
async function createWindow () { | |
const mainWindow = new BrowserWindow({ | |
width: 800, | |
height: 600, | |
}) | |
mainWindow.loadURL('https://socket.io/demos/chat') | |
const _debugger = mainWindow.webContents.debugger; | |
_debugger.attach(); | |
_debugger.on('message', async (_event, method, params) => { | |
if (method === 'Network.webSocketFrameReceived') { | |
console.log('Received websocket info: ', JSON.stringify(params, null , 2)); | |
} | |
if (method === 'Network.webSocketFrameSent') { | |
console.log('Sent websocket info: ', JSON.stringify(params, null , 2)); | |
} | |
if (method === 'Target.attachedToTarget') { | |
// capture iframe websocket request | |
if (params.targetInfo.type === 'iframe') { | |
await _debugger.sendCommand('Network.enable', null, params.sessionId); | |
await _debugger.sendCommand('Runtime.enable', null, params.sessionId); | |
await _debugger.sendCommand('Runtime.runIfWaitingForDebugger', null, params.sessionId); | |
} | |
} | |
}); | |
mainWindow.on('closed', () => { | |
_debugger.detach(); | |
}); | |
await _debugger.sendCommand('Network.enable'); | |
await _debugger.sendCommand('Target.setAutoAttach', { | |
autoAttach: true, | |
waitForDebuggerOnStart: true, | |
flatten: true | |
}); | |
} | |
app.whenReady().then(async () => { | |
await createWindow() | |
app.on('activate', function () { | |
// On macOS it's common to re-create a window in the app when the | |
// dock icon is clicked and there are no other windows open. | |
if (BrowserWindow.getAllWindows().length === 0) createWindow() | |
}) | |
}) | |
// Quit when all windows are closed, except on macOS. There, it's common | |
// for applications and their menu bar to stay active until the user quits | |
// explicitly with Cmd + Q. | |
app.on('window-all-closed', function () { | |
if (process.platform !== 'darwin') app.quit() | |
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"name": "ruthless-ticket-sneeze-cmfn9", | |
"productName": "ruthless-ticket-sneeze-cmfn9", | |
"description": "My Electron application description", | |
"keywords": [], | |
"main": "./main.js", | |
"version": "1.0.0", | |
"author": "Black-Hole<[email protected]>", | |
"scripts": { | |
"start": "electron ." | |
}, | |
"dependencies": {}, | |
"devDependencies": { | |
"electron": "20.2.0" | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment