Skip to content

Instantly share code, notes, and snippets.

@rudifa
Last active October 26, 2022 15:53
Show Gist options
  • Save rudifa/b187bf68ffb9522e8c13539af930de3a to your computer and use it in GitHub Desktop.
Save rudifa/b187bf68ffb9522e8c13539af930de3a to your computer and use it in GitHub Desktop.
The Electron IPC DOCS/FIDDLES/IPC/PATTERN-1 (21.2.0), Renderer to main (one-way), with comments
// 1. the renderer attaches a listener to the button defined in the html file
// . on user click, the listener calls the function setTitle passing on the value from the input element
// 2. the preload defines the function that will be called by setTitle and pass on the title sending the event set-title
// 3. the main attaches a listener for the event set-title
// that will get the title an put it into the browser window title
<!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'">
<title>Hello World!</title>
</head>
<body>
Title: <input id="title"/>
<button id="btn" type="button">Set</button>
<script src="./renderer.js"></script>
</body>
</html>
const {app, BrowserWindow, ipcMain} = require('electron')
const path = require('path')
function createWindow () {
const mainWindow = new BrowserWindow({
webPreferences: {
preload: path.join(__dirname, 'preload.js')
}
})
// 3. the main attaches a listener for the event set-title
// that will get the title an put it into the browser window title
ipcMain.on('set-title', (event, title) => {
const webContents = event.sender
const win = BrowserWindow.fromWebContents(webContents)
win.setTitle(title)
})
mainWindow.loadFile('index.html')
}
app.whenReady().then(() => {
createWindow()
app.on('activate', function () {
if (BrowserWindow.getAllWindows().length === 0) createWindow()
})
})
app.on('window-all-closed', function () {
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"
}
}
// 2. the preload defines the function that will be called by setTitle and pass on the title sending the event set-title
const { contextBridge, ipcRenderer } = require('electron')
contextBridge.exposeInMainWorld('electronAPI', {
setTitle: (title) => ipcRenderer.send('set-title', title)
})
// 1. the renderer attaches a listener to the button defined in the html file
// . on user click, the listener calls the function setTitle passing on the value from the input element
const setButton = document.getElementById('btn')
const titleInput = document.getElementById('title')
setButton.addEventListener('click', () => {
const title = titleInput.value
window.electronAPI.setTitle(title)
});
/* 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