Add IPC logging and echo button

This commit is contained in:
Madeline Busig 2026-02-22 02:06:05 -08:00
parent 5723119001
commit 961857ff23
4 changed files with 54 additions and 8 deletions

View File

@ -16,5 +16,12 @@
<body> <body>
<h1>Hello from Electron renderer!</h1> <h1>Hello from Electron renderer!</h1>
<p>👋</p> <p>👋</p>
<button class="echoButton">
ECHO
</button>
<button class="echoButton">
ECHO 2
</button>
</body> </body>
<script src="pagejs/echo_button.js"></script>
</html> </html>

29
main.js
View File

@ -1,17 +1,24 @@
console.log('Hello world') console.log('Hello world')
const { app, BrowserWindow } = require('electron') const { app, BrowserWindow, ipcMain } = require('electron')
const path = require('node:path')
const createWindow = () => { app.on('window-all-closed', windowsClosed)
app.whenReady().then(appReady)
function createWindow() {
const win = new BrowserWindow({ const win = new BrowserWindow({
width: 800, width: 400,
height: 600 height: 600,
webPreferences: {
preload: path.join(__dirname, 'preload.js')
}
}) })
win.loadFile('index.html') win.loadFile('index.html')
} }
app.whenReady().then(() => { function appReady() {
createWindow() createWindow()
// MacOS window activation stuffs (??) (idk I don't use Apple products) // MacOS window activation stuffs (??) (idk I don't use Apple products)
@ -20,11 +27,17 @@ app.whenReady().then(() => {
createWindow() createWindow()
} }
}) })
})
app.on('window-all-closed', () => { ipcMain.on('log', ipcLog)
}
function windowsClosed() {
// Windows and Linux need to call app.quit manually // Windows and Linux need to call app.quit manually
// otherwise process will not exit // otherwise process will not exit
if (process.platform !== 'darwin') app.quit() if (process.platform !== 'darwin') app.quit()
}) }
function ipcLog(event, msg) {
console.log(msg)
}

18
pagejs/echo_button.js Normal file
View File

@ -0,0 +1,18 @@
window.upbp.log('Loading echo button events')
var echoButtons = document.querySelectorAll('.echoButton')
if (echoButtons.length === 0) {
window.upbp.log('WARN: No echo buttons (.echoButton)')
}
window.upbp.log(echoButtons)
for (btn of echoButtons) {
btn.addEventListener('click', echoButtonClick)
}
function echoButtonClick() {
window.upbp.log('Echo button clicked')
}

8
preload.js Normal file
View File

@ -0,0 +1,8 @@
const { contextBridge, ipcRenderer } = require('electron')
contextBridge.exposeInMainWorld('upbp', {
log: (msg) => {
ipcRenderer.send('log', msg)
}
})