例の概要
このセクションでは、Electronアプリケーションで実装する一般的な機能のガイドをまとめました。各ガイドには、最小限で完結したサンプルアプリ内の実践的な例が含まれます。これらの例を実行する最も簡単な方法は、Electron Fiddleをダウンロードすることです。
Fiddleがインストールされると、次の1つのようなコードサンプルの下にある「Fiddleで開く」ボタンを押すことができます。
- main.js
- preload.js
- index.html
const { app, BrowserWindow } = require('electron/main')
const path = require('node:path')
function 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()
}
})
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])
}
})
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Hello World!</title>
<meta http-equiv="Content-Security-Policy" content="script-src 'self' 'unsafe-inline';" />
</head>
<body>
<h1>Hello World!</h1>
<p>
We are using Node.js <span id="node-version"></span>,
Chromium <span id="chrome-version"></span>,
and Electron <span id="electron-version"></span>.
</p>
</body>
</html>
方法...?
「使い方」の完全なリストはサイドバーで確認できます。ドキュメント化されていないことをしたい場合は、Discordサーバーに参加して、お知らせください!