Skip to content

Instantly share code, notes, and snippets.

@Ali-Cheikh
Created February 7, 2024 16:00
Show Gist options
  • Save Ali-Cheikh/6b1dba7f61b38834c19ff5fcb6f1768c to your computer and use it in GitHub Desktop.
Save Ali-Cheikh/6b1dba7f61b38834c19ff5fcb6f1768c to your computer and use it in GitHub Desktop.
test1
<!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' 'unsafe-inline'">
<link href="./styles.css" rel="stylesheet">
<title>Hello World!</title>
</head>
<body>
<h1>Hello World!</h1>
<!-- 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, crashReporter, ipcMain, BrowserWindow } = require('electron')
const path = require('path')
const electronMajor = Number.parseInt(process.versions.electron)
function createWindow () {
// Create the browser window.
const mainWindow = new BrowserWindow({
webPreferences: {
preload: path.join(__dirname, 'preload.js')
}
})
mainWindow.loadFile('index.html')
}
// 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()
})
function testDone (success, ...logs) {
console.log(`test ${success ? 'passed' : 'failed'}`)
logs.forEach((i) => console.log(i))
process.exit(success ? 0 : 1)
}
{
// companyName required before v10
if (electronMajor >= 10) {
crashReporter.start({ uploadToServer: false, submitURL: '' })
}
ipcMain.on('test-done', (_, success, ...logs) => testDone(success, ...logs))
const failIfBadExit = (details) => {
if (details.reason !== 'clean-exit') testDone(false, new Error('trace'), details)
}
app.on('child-process-gone', (_ev, details) => failIfBadExit(details))
app.on('render-process-gone', (_ev, _, details) => failIfBadExit(details))
}
{
"name": "motionless-indication-avoid-9p4k1",
"productName": "motionless-indication-avoid-9p4k1",
"description": "My Electron application description",
"keywords": [],
"main": "./main.js",
"version": "1.0.0",
"author": "ideapad",
"scripts": {
"start": "electron ."
},
"dependencies": {},
"devDependencies": {
"electron": "28.2.0"
}
}
/**
* The preload script runs before. 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
*/
// Test helpers
const test = {
assert: (ok, ...logs) => {
if (!ok) test.fail(...logs)
},
fail: (...logs) => test.done(false, ...logs),
done: (success = true, ...logs) => {
if (!success) logs.unshift(new Error('test failed'))
require('electron').ipcRenderer.send('test-done', success, ...logs)
}
}
// Example test: check that process.versions.electron
// - is defined in the preload process
// - has major, minor, and patch numbers
// - the numbers are non-negative
try {
const tokens = process.versions.electron.split('.', 3)
test.assert(tokens.length === 3)
for (const token of tokens) {
const num = Number.parseInt(token)
test.assert(!Number.isNaN(num))
test.assert(num >= 0)
}
test.done()
} catch (err) {
test.fail(err)
}
/**
* 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.
*/
/* 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