From 961857ff2369bc43c379c567f65be7d0427972e6 Mon Sep 17 00:00:00 2001 From: Madeline Busig Date: Sun, 22 Feb 2026 02:06:05 -0800 Subject: [PATCH] Add IPC logging and echo button --- index.html | 7 +++++++ main.js | 29 +++++++++++++++++++++-------- pagejs/echo_button.js | 18 ++++++++++++++++++ preload.js | 8 ++++++++ 4 files changed, 54 insertions(+), 8 deletions(-) create mode 100644 pagejs/echo_button.js create mode 100644 preload.js diff --git a/index.html b/index.html index 4f45de4..047d60a 100644 --- a/index.html +++ b/index.html @@ -16,5 +16,12 @@

Hello from Electron renderer!

👋

+ + + diff --git a/main.js b/main.js index 135ede4..6e1a94e 100644 --- a/main.js +++ b/main.js @@ -1,17 +1,24 @@ 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({ - width: 800, - height: 600 + width: 400, + height: 600, + webPreferences: { + preload: path.join(__dirname, 'preload.js') + } }) win.loadFile('index.html') } -app.whenReady().then(() => { +function appReady() { createWindow() // MacOS window activation stuffs (??) (idk I don't use Apple products) @@ -20,11 +27,17 @@ app.whenReady().then(() => { createWindow() } }) -}) -app.on('window-all-closed', () => { + ipcMain.on('log', ipcLog) +} + +function windowsClosed() { // Windows and Linux need to call app.quit manually // otherwise process will not exit if (process.platform !== 'darwin') app.quit() -}) +} + +function ipcLog(event, msg) { + console.log(msg) +} diff --git a/pagejs/echo_button.js b/pagejs/echo_button.js new file mode 100644 index 0000000..6c0066d --- /dev/null +++ b/pagejs/echo_button.js @@ -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') +} + diff --git a/preload.js b/preload.js new file mode 100644 index 0000000..a1e19f8 --- /dev/null +++ b/preload.js @@ -0,0 +1,8 @@ +const { contextBridge, ipcRenderer } = require('electron') + +contextBridge.exposeInMainWorld('upbp', { + log: (msg) => { + ipcRenderer.send('log', msg) + } +}) +