Skip to content

Commit 834099d

Browse files
committed
add local time button, understood the concept of IPC with preload.js
1 parent 8a0c7d9 commit 834099d

File tree

4 files changed

+45
-31
lines changed

4 files changed

+45
-31
lines changed

index.html

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,10 @@ <h1>Hello World!</h1>
1313
Chromium <span id="chrome-version"></span>,
1414
and Electron <span id="electron-version"></span>.
1515

16-
<!-- You can also require other files to run in this process -->
16+
<button id="timeBtn">Get Local Time</button>
17+
18+
<!-- Div to display received time -->
19+
<div id="output"></div>
1720
<script src="./renderer.js"></script>
1821
</body>
1922
</html>

main.js

Lines changed: 22 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,43 +1,43 @@
1-
// Modules to control application life and create native browser window
2-
const { app, BrowserWindow } = require('electron')
1+
// Modules to control application life, create windows, and handle IPC
2+
const { app, BrowserWindow, ipcMain } = require('electron')
33
const path = require('node:path')
44

5-
function createWindow () {
6-
// Create the browser window.
5+
// Function to create the main browser window
6+
function createWindow() {
77
const mainWindow = new BrowserWindow({
88
width: 800,
99
height: 600,
1010
webPreferences: {
11-
preload: path.join(__dirname, 'preload.js')
11+
// Preload script exposes safe APIs to renderer
12+
preload: path.join(__dirname, 'preload.js'),
13+
contextIsolation: true, // keep renderer isolated for security
14+
nodeIntegration: false // disable Node access in renderer
1215
}
1316
})
1417

15-
// and load the index.html of the app.
16-
mainWindow.loadFile('index.html')
17-
18-
// Open the DevTools.
19-
// mainWindow.webContents.openDevTools()
18+
mainWindow.loadFile('index.html') // load UI
19+
// mainWindow.webContents.openDevTools() // optional for debugging
2020
}
2121

22-
// This method will be called when Electron has finished
23-
// initialization and is ready to create browser windows.
24-
// Some APIs can only be used after this event occurs.
22+
// Called when Electron has finished initialization
2523
app.whenReady().then(() => {
2624
createWindow()
2725

28-
app.on('activate', function () {
29-
// On macOS it's common to re-create a window in the app when the
30-
// dock icon is clicked and there are no other windows open.
26+
// macOS: re-create window when dock icon clicked and no windows open
27+
app.on('activate', () => {
3128
if (BrowserWindow.getAllWindows().length === 0) createWindow()
3229
})
3330
})
3431

35-
// Quit when all windows are closed, except on macOS. There, it's common
36-
// for applications and their menu bar to stay active until the user quits
37-
// explicitly with Cmd + Q.
38-
app.on('window-all-closed', function () {
32+
// Quit app when all windows are closed (except macOS)
33+
app.on('window-all-closed', () => {
3934
if (process.platform !== 'darwin') app.quit()
4035
})
4136

42-
// In this file you can include the rest of your app's specific main process
43-
// code. You can also put them in separate files and require them here.
37+
// ----------------------
38+
// IPC: listen for time requests from renderer
39+
// Sends back local system time as string
40+
ipcMain.on('get-time', (event) => {
41+
const now = new Date() // get local system time
42+
event.reply('time-response', now.toLocaleTimeString()) // send formatted time back
43+
})

preload.js

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,12 @@
66
*
77
* https://www.electronjs.org/docs/latest/tutorial/sandbox
88
*/
9-
window.addEventListener('DOMContentLoaded', () => {
10-
const replaceText = (selector, text) => {
11-
const element = document.getElementById(selector)
12-
if (element) element.innerText = text
13-
}
9+
const { contextBridge, ipcRenderer } = require('electron')
1410

15-
for (const type of ['chrome', 'node', 'electron']) {
16-
replaceText(`${type}-version`, process.versions[type])
17-
}
11+
contextBridge.exposeInMainWorld('electronAPI', {
12+
// Function to request local time from main process
13+
getTime: () => ipcRenderer.send('get-time'),
14+
15+
// Function to listen for the time response
16+
onTimeResponse: (callback) => ipcRenderer.on('time-response', callback)
1817
})

renderer.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,15 @@
55
* `contextIsolation` is turned on. Use the contextBridge API in `preload.js`
66
* to expose Node.js functionality from the main process.
77
*/
8+
// Renderer process: handles UI interactions
9+
10+
// When button is clicked, request local time from main process
11+
document.getElementById('timeBtn').addEventListener('click', () => {
12+
window.electronAPI.getTime()
13+
})
14+
15+
// When main process replies, update output div with the received time
16+
window.electronAPI.onTimeResponse((event, time) => {
17+
document.getElementById('output').innerText = time
18+
})
19+

0 commit comments

Comments
 (0)