Skip to content

Instantly share code, notes, and snippets.

@lingo
Created June 18, 2026 16:44
Show Gist options
  • Select an option

  • Save lingo/f3f24a58330282d166fb41e6475f201b to your computer and use it in GitHub Desktop.

Select an option

Save lingo/f3f24a58330282d166fb41e6475f201b to your computer and use it in GitHub Desktop.
electron_issue_43310
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<!-- https://developer.mozilla.org/en-US/docs/Web/HTTP/CSP -->
<meta http-equiv="Content-Security-Policy" content="default-src 'self'; script-src 'self'; style-src 'self' https://cdn.jsdelivr.net 'unsafe-inline'">
<title>Hello World!</title>
<link rel="stylesheet" href="reset.css" />
<!-- <link rel="stylesheet" href="https://unpkg.com/mvp.css"> -->
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/sakura.css/css/sakura.css" type="text/css">
<style>
body {
/* padding: 1rem; */
}
main {
margin-top: 2rem;
}
</style>
</head>
<body>
<header>
<h1>Hello World!</h1>
We are using Node.js <span id="node-version"></span>,
Chromium <span id="chrome-version"></span>,
and Electron <span id="electron-version"></span>.
</header>
<main>
<button>Open</button>
<output></output>
</main>
<!-- You can also require other files to run in this process -->
<script src="./renderer.js"></script>
</body>
</html>
// Modules to control application life and create native browser window
const { app, dialog, ipcMain, BrowserWindow } = require('electron')
const path = require('node:path')
app.commandLine.appendSwitch('--xdg-portal-required-version=4');
function createWindow () {
// Create the browser window.
const mainWindow = new BrowserWindow({
width: 800,
height: 600,
webPreferences: {
preload: path.join(__dirname, 'preload.js')
}
})
// and load the index.html of the app.
mainWindow.loadFile('index.html')
// Open the DevTools.
// mainWindow.webContents.openDevTools()
}
// This method will be called when Electron has finished
// initialization and is ready to create browser windows.
// Some APIs can only be used after this event occurs.
app.whenReady().then(() => {
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()
})
// In this file you can include the rest of your app's specific main process
// code. You can also put them in separate files and require them here.
let defaultPath = '/tmp';
ipcMain.handle('OPEN', () => {
const f = dialog.showOpenDialogSync(BrowserWindow.getFocusedWindow(), {
title: "Open window",
defaultPath: defaultPath,
properties: ['openFile', 'showHiddenFiles', 'multiSelections'],
message: "Choose wisely",
});
if (f?.length) {
defaultPath = path.dirname(f[0]);
console.log('saving default path', defaultPath);
}
return f;
})
{
"name": "fiddle",
"productName": "fiddle",
"description": "My Electron application description",
"keywords": [],
"main": "./main.js",
"version": "1.0.0",
"author": "lucas",
"scripts": {
"start": "electron ."
},
"dependencies": {
"@electron-forge/cli": "8.0.0-alpha.3",
"@electron-forge/maker-deb": "8.0.0-alpha.3",
"@electron-forge/maker-rpm": "8.0.0-alpha.3",
"@electron-forge/maker-squirrel": "8.0.0-alpha.3",
"@electron-forge/maker-zip": "8.0.0-alpha.3"
},
"devDependencies": {
"electron": "42.3.1"
}
}
const { contextBridge, ipcRenderer } = require( 'electron' )
/**
* The preload script runs before `index.html` is loaded
* in the renderer. It has access to web APIs as well as
* Electron's renderer process modules and some polyfilled
* Node.js functions.
*
* https://www.electronjs.org/docs/latest/tutorial/sandbox
*/
window.addEventListener('DOMContentLoaded', () => {
const replaceText = (selector, text) => {
const element = document.getElementById(selector)
if (element) element.innerText = text
}
for (const type of ['chrome', 'node', 'electron']) {
replaceText(`${type}-version`, process.versions[type])
}
})
contextBridge.exposeInMainWorld('api', {
open: () => ipcRenderer.invoke('OPEN')
})
/**
* This file is loaded via the <script> tag in the index.html file and will
* be executed in the renderer process for that window. No Node.js APIs are
* available in this process because `nodeIntegration` is turned off and
* `contextIsolation` is turned on. Use the contextBridge API in `preload.js`
* to expose Node.js functionality from the main process.
*/
async function promptFile() {
const result = await window.api.open();
console.log(result);
document.querySelector('output').textContent = result;
}
window.onload = () => {
document.querySelector('button').addEventListener('click', promptFile)
};
/*! modern-reset | MIT License | github.com/aprinciple/modern-reset */
*,*::before,*::after{box-sizing:border-box}:where(*:not(dialog,[popover])){margin:0}:where(html){scrollbar-gutter:stable;color-scheme:light dark;-webkit-text-size-adjust:none;text-size-adjust:none;overflow-x:clip}:where(body){max-inline-size:100%;min-block-size:100dvh;line-height:1.5}:where(h1,h2,h3,h4,h5,h6){text-wrap:balance;overflow-wrap:break-word}:where(p){text-wrap:pretty;overflow-wrap:break-word}:where(ol,ul){list-style:none;margin:0;padding:0}:where(a){text-decoration-skip-ink:auto;text-underline-offset:.2ex}:where(sup,sub){font-size:75%;line-height:0;position:relative;vertical-align:baseline}:where(sup){top:-.5em}:where(sub){bottom:-.25em}:where(hr){border:none;border-block-start:1px solid;color:inherit}:where(abbr[title]){text-decoration:underline dotted;cursor:help}:where(code,kbd,samp,pre){font-size:.875em}:not([class]){&:where(h1,h2,h3,h4,h5,h6){margin-block:.75em;line-height:1.25;letter-spacing:-.025ch}&:where(p,ol,ul){margin-block:1em}&:where(ol,ul){list-style:revert;padding-inline-start:1.5em}&:where(li){margin-block:.5em}}:where(img,picture,video,canvas,svg){display:block;max-inline-size:100%;block-size:auto}:where(img){font-style:italic}:where(input,button,textarea,select){font:inherit;letter-spacing:inherit;word-spacing:inherit;color:currentColor}:where(textarea,select,input){field-sizing:content;max-inline-size:100%}:where(textarea){resize:vertical}:where(button){padding:0;border:0;background:none;cursor:pointer}:where(button:disabled){cursor:not-allowed}:where(table){border-collapse:collapse;border-spacing:0}:where(dialog){color:inherit}:where(:target){scroll-margin-top:2rem}:where(#root,#__next){isolation:isolate}@media (prefers-reduced-motion:no-preference){:where(html){scroll-behavior:smooth;interpolate-size:allow-keywords}}@media (hover:none){:where(html){-webkit-tap-highlight-color:#fff0}}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment