Skip to content

Instantly share code, notes, and snippets.

@BlackHole1
Last active October 10, 2022 09:26
Show Gist options
  • Save BlackHole1/207ad63cf35681805551a98a39524a39 to your computer and use it in GitHub Desktop.
Save BlackHole1/207ad63cf35681805551a98a39524a39 to your computer and use it in GitHub Desktop.
Interception WebSocket failed in electron
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);
}
}
if (method === 'Network.requestIntercepted') {
await _debugger.sendCommand('Network.continueInterceptedRequest', params)
}
});
mainWindow.on('closed', () => {
_debugger.detach();
});
await _debugger.sendCommand('Network.enable');
await _debugger.sendCommand('Target.setAutoAttach', {
autoAttach: true,
waitForDebuggerOnStart: true,
flatten: true
});
await _debugger.sendCommand('Network.setRequestInterception', {
patterns: [
{
resourceType: 'WebSocket'
}
]
});
}
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()
})
{
"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<158blackhole@gmail.com>",
"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