Skip to content

Instantly share code, notes, and snippets.

@p120ph37
Last active April 8, 2022 22:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save p120ph37/98cf0fb13790feed702f3e90cbddfbf6 to your computer and use it in GitHub Desktop.
Save p120ph37/98cf0fb13790feed702f3e90cbddfbf6 to your computer and use it in GitHub Desktop.
Electron Relaunch Test
<!DOCTYPE html>
<html>
<body>
<h1>Relaunch Test</h1>
<h5>Note: app.relaunch does not work in Electron Fiddle at all. You must launch this via "npm start" instead</h5>
<div style="border: 1px solid black; width: 480px; height: 160px; white-space: nowrap; overflow: scroll;">
<code>
<pre>process.argv = <span id="main-argv"></span></pre>
</code>
</div>
<button id="relaunch">Relaunch...</button>
</body>
</html>
const {app, BrowserWindow, ipcMain} = require('electron')
const path = require('path')
app.whenReady().then(() => {
const mainWindow = new BrowserWindow({
width: 800,
height: 600,
webPreferences: {
preload: path.join(__dirname, 'preload.js')
}
})
ipcMain.handle('getMainArgv', () => process.argv)
ipcMain.on('relaunch', () => {
app.relaunch({
execPath: process.argv[0],
args: process.argv.slice(1).concat(['--relaunch'])
})
app.exit(0)
})
mainWindow.loadFile('index.html')
})
app.on('window-all-closed', () => app.quit())
{
"name": "elfin-passion-shoe-ix5xe",
"productName": "elfin-passion-shoe-ix5xe",
"description": "My Electron application description",
"keywords": [],
"main": "./main.js",
"version": "1.0.0",
"author": "anm",
"scripts": {
"start": "electron ."
},
"dependencies": {},
"devDependencies": {
"electron": "17.0.0-alpha.4"
}
}
const {ipcRenderer} = require('electron')
window.addEventListener('DOMContentLoaded', () => {
ipcRenderer.invoke('getMainArgv').then(mainArgv => {
document.getElementById('main-argv').innerText = JSON.stringify(mainArgv, null, 2)
})
document.getElementById('relaunch').addEventListener('click', () => ipcRenderer.send('relaunch'))
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment