Skip to content

Instantly share code, notes, and snippets.

@amitparida
Created June 14, 2024 14:57
Show Gist options
  • Save amitparida/adfd147123f60ad23508a356ff182f8e to your computer and use it in GitHub Desktop.
Save amitparida/adfd147123f60ad23508a356ff182f8e to your computer and use it in GitHub Desktop.
Chromium crash with async onBeforeRequest Handler
<!DOCTYPE html>
<html>
<head>
<title>Electron Webview Example</title>
<link rel="stylesheet" type="text/css" href="styles.css">
</head>
<body>
<div id="url-input-container">
<input type="text" id="url-input" placeholder="Enter URL">
<button id="load-url-button">Load URL</button>
<button id="open-devtools-button">Open DevTools</button>
</div>
<div id="webview-container">
<div id="loading-icon" hidden>Loading...</div>
<webview id="webview" src="" preload="renderer.js"></webview>
</div>
<script src="renderer.js"></script>
</body>
</html>
const { app, BrowserWindow } = require('electron');
const remote = require('@electron/remote/main');
remote.initialize();
function createWindow () {
const mainWindow = new BrowserWindow({
width: 800,
height: 600,
webPreferences: {
nodeIntegration: true,
contextIsolation: false,
webviewTag: true,
enableRemoteModule: true
}
});
remote.enable(mainWindow.webContents);
mainWindow.loadFile('index.html');
}
app.on('ready', createWindow);
app.on('window-all-closed', function () {
if (process.platform !== 'darwin') {
app.quit();
}
});
app.on('activate', function () {
if (BrowserWindow.getAllWindows().length === 0) {
createWindow();
}
});
{
"name": "wrong-jelly-strike-us3sx",
"productName": "wrong-jelly-strike-us3sx",
"description": "My Electron application description",
"keywords": [],
"main": "./main.js",
"version": "1.0.0",
"author": "amitpari",
"scripts": {
"start": "electron ."
},
"dependencies": {
"@electron/remote": "2.1.2"
},
"devDependencies": {
"electron": "31.0.1"
}
}
const inputElement = document.getElementById('url-input');
const loadButton = document.getElementById('load-url-button');
const openDevToolsButton = document.getElementById('open-devtools-button');
const webview = document.getElementById('webview');
const loadingIcon = document.getElementById('loading-icon');
const remote = process.type === 'browser' ? require('@electron/remote/main') : require('@electron/remote');
loadButton.addEventListener('click', () => {
let url = inputElement.value;
if (!url.startsWith('http://') && !url.startsWith('https://')) {
url = 'https://' + url;
}
webview.src = url;
});
inputElement.addEventListener('keyup', (event) => {
if (event.key === 'Enter') {
loadButton.click();
}
});
webview.addEventListener('dom-ready', () => {
const session = remote.webContents.fromId(this.webview.getWebContentsId()).session;
session.webRequest.onBeforeSendHeaders({ urls: ["<all_urls>"] }, (details, callback) => {
let requestHeaders = { ...details.requestHeaders };
callback({ requestHeaders });
});
});
html, body {
margin: 0;
padding: 0;
width: 100%;
height: 100%;
overflow: hidden;
display: flex;
flex-direction: column;
}
#url-input-container {
display: flex;
padding: 10px;
background-color: #f0f0f0;
border-bottom: 1px solid #ccc;
}
#url-input {
flex: 1;
padding: 5px;
margin-right: 10px;
}
#load-url-button {
padding: 5px 10px;
}
#webview-container {
flex: 1;
width: 100%;
}
webview {
width: 100%;
height: 100%;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment