Skip to content

Instantly share code, notes, and snippets.

@rudifa
Last active October 26, 2022 15:50
Show Gist options
  • Save rudifa/ef8e02dc7518ae5af1541591a557215e to your computer and use it in GitHub Desktop.
Save rudifa/ef8e02dc7518ae5af1541591a557215e to your computer and use it in GitHub Desktop.
A variant of DOCS/FIDDLES/TUTORIAL-PRELOAD (21.2.0)
// 1. in preload, the contextBridge exposes a copy of a process variable for info
// 2. the renderer inserts the info into the view
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8" />
<meta
http-equiv="Content-Security-Policy"
content="default-src 'self'; script-src 'self'"
/>
<meta
http-equiv="X-Content-Security-Policy"
content="default-src 'self'; script-src 'self'"
/>
<title>Hello from Electron renderer!</title>
</head>
<body>
<h1>Hello from Electron renderer!</h1>
<p>👋</p>
<p id="info"></p>
</body>
<script src="./renderer.js"></script>
</html>
const { app, BrowserWindow } = require('electron');
const path = require('path');
const createWindow = () => {
const win = new BrowserWindow({
width: 800,
height: 600,
webPreferences: {
preload: path.join(__dirname, 'preload.js'),
},
});
win.loadFile('index.html');
};
app.whenReady().then(() => {
createWindow();
app.on('activate', () => {
if (BrowserWindow.getAllWindows().length === 0) {
createWindow();
}
});
});
app.on('window-all-closed', () => {
if (process.platform !== 'darwin') {
app.quit();
}
});
{
"name": "five-chocolate-verify-9v1cn",
"productName": "five-chocolate-verify-9v1cn",
"description": "My Electron application description",
"keywords": [],
"main": "./main.js",
"version": "1.0.0",
"author": "rudifarkas",
"scripts": {
"start": "electron ."
},
"dependencies": {},
"devDependencies": {
"electron": "21.2.0"
}
}
// 1. in preload, the contextBridge exposes a copy of a process variable for info
const { contextBridge } = require('electron');
contextBridge.exposeInMainWorld('versions', {
versions: () => process.versions
});
// 2. the renderer inserts the info into the view
const information = document.getElementById('info');
const {chrome, node, electron} = versions.versions()
information.innerText = `This app is using Chrome (v${chrome}), Node.js (v${node}), and Electron (v${electron})`;
/* styles.css */
/* Add styles here to customize the appearance of your app */
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment