Skip to content

Instantly share code, notes, and snippets.

@ckerr
Created February 24, 2021 18:15
Show Gist options
  • Save ckerr/2f264a9ae74ea876e98490b7288857f5 to your computer and use it in GitHub Desktop.
Save ckerr/2f264a9ae74ea876e98490b7288857f5 to your computer and use it in GitHub Desktop.
const {app, BrowserWindow} = require('electron');
const path = require('path');
var mainWindow;
async function init(url) {
// load about:blank first, just in case, before attaching debugger
// (doesn't seem to make a difference)
await mainWindow.loadURL("about:blank");
mainWindow.webContents.debugger.attach("1.3");
await initTarget();
mainWindow.webContents.debugger.on('message', async (event, method, params, sessionId) => {
if (method === 'Fetch.requestPaused') {
console.log("url", params.request.url);
await send("Fetch.continueRequest", {requestId: params.requestId }, sessionId);
}
if (method === "Target.attachedToTarget") {
await initTarget(params.sessionId);
if (params.waitingForDebugger) {
await send('Runtime.runIfWaitingForDebugger', null, params.sessionId);
}
}
});
await mainWindow.loadURL(url);
}
async function initTarget(sessionId) {
await send("Fetch.enable", {patterns: [{urlPattern: "*", requestStage: "Response"}]}, sessionId);
await send("Target.setAutoAttach", {autoAttach: true, waitForDebuggerOnStart: true, flatten: true }, sessionId);
}
async function send(method, params, sessionId) {
if (sessionId) {
await mainWindow.webContents.debugger.sendCommand(method, params, sessionId);
} else {
await mainWindow.webContents.debugger.sendCommand(method, params);
}
}
app.whenReady().then(() => {
mainWindow = new BrowserWindow({
width: 1024,
height: 768,
webPreferences: {
contextIsolation: true,
}
});
init("https://oembed.link/https://www.youtube.com/watch?v=M7lc1UVf-VE");
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment