Skip to content

Instantly share code, notes, and snippets.

@rudifa
Last active October 26, 2022 15:53
Show Gist options
  • Select an option

  • Save rudifa/b187bf68ffb9522e8c13539af930de3a to your computer and use it in GitHub Desktop.

Select an option

Save rudifa/b187bf68ffb9522e8c13539af930de3a to your computer and use it in GitHub Desktop.

Revisions

  1. rudifa revised this gist Oct 26, 2022. No changes.
  2. rudifa revised this gist Oct 26, 2022. No changes.
  3. rudifa revised this gist Oct 26, 2022. No changes.
  4. rudifa revised this gist Oct 26, 2022. 2 changed files with 8 additions and 1 deletion.
    7 changes: 7 additions & 0 deletions Electron IPC Pattern #1
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,7 @@
    // 1. the renderer attaches a listener to the button defined in the html file
    // . on user click, the listener calls the function setTitle passing on the value from the input element

    // 2. the preload defines the function that will be called by setTitle and pass on the title sending the event set-title

    // 3. the main attaches a listener for the event set-title
    // that will get the title an put it into the browser window title
    2 changes: 1 addition & 1 deletion preload.js
    Original file line number Diff line number Diff line change
    @@ -1,4 +1,4 @@
    // 2. the preload defines the function that will be called by setTitle and pass on the title
    // 2. the preload defines the function that will be called by setTitle and pass on the title sending the event set-title

    const { contextBridge, ipcRenderer } = require('electron')

  5. rudifa revised this gist Oct 26, 2022. 3 changed files with 7 additions and 0 deletions.
    2 changes: 2 additions & 0 deletions main.js
    Original file line number Diff line number Diff line change
    @@ -8,6 +8,8 @@ function createWindow () {
    }
    })

    // 3. the main attaches a listener for the event set-title
    // that will get the title an put it into the browser window title
    ipcMain.on('set-title', (event, title) => {
    const webContents = event.sender
    const win = BrowserWindow.fromWebContents(webContents)
    2 changes: 2 additions & 0 deletions preload.js
    Original file line number Diff line number Diff line change
    @@ -1,3 +1,5 @@
    // 2. the preload defines the function that will be called by setTitle and pass on the title

    const { contextBridge, ipcRenderer } = require('electron')

    contextBridge.exposeInMainWorld('electronAPI', {
    3 changes: 3 additions & 0 deletions renderer.js
    Original file line number Diff line number Diff line change
    @@ -1,3 +1,6 @@
    // 1. the renderer attaches a listener to the button defined in the html file
    // . on user click, the listener calls the function setTitle passing on the value from the input element

    const setButton = document.getElementById('btn')
    const titleInput = document.getElementById('title')
    setButton.addEventListener('click', () => {
  6. rudifa created this gist Oct 26, 2022.
    14 changes: 14 additions & 0 deletions index.html
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,14 @@
    <!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'">
    <title>Hello World!</title>
    </head>
    <body>
    Title: <input id="title"/>
    <button id="btn" type="button">Set</button>
    <script src="./renderer.js"></script>
    </body>
    </html>
    30 changes: 30 additions & 0 deletions main.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,30 @@
    const {app, BrowserWindow, ipcMain} = require('electron')
    const path = require('path')

    function createWindow () {
    const mainWindow = new BrowserWindow({
    webPreferences: {
    preload: path.join(__dirname, 'preload.js')
    }
    })

    ipcMain.on('set-title', (event, title) => {
    const webContents = event.sender
    const win = BrowserWindow.fromWebContents(webContents)
    win.setTitle(title)
    })

    mainWindow.loadFile('index.html')
    }

    app.whenReady().then(() => {
    createWindow()

    app.on('activate', function () {
    if (BrowserWindow.getAllWindows().length === 0) createWindow()
    })
    })

    app.on('window-all-closed', function () {
    if (process.platform !== 'darwin') app.quit()
    })
    16 changes: 16 additions & 0 deletions package.json
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,16 @@
    {
    "name": "five-chocolate-verify-9v1cn",
    "productName": "five-chocolate-verify-9v1cn",
    "description": "My Electron application description",
    "keywords": [],
    "main": "./main.js",
    "version": "1.0.0",
    "author": "rudifarkas",
    "scripts": {
    "start": "electron ."
    },
    "dependencies": {},
    "devDependencies": {
    "electron": "21.2.0"
    }
    }
    5 changes: 5 additions & 0 deletions preload.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,5 @@
    const { contextBridge, ipcRenderer } = require('electron')

    contextBridge.exposeInMainWorld('electronAPI', {
    setTitle: (title) => ipcRenderer.send('set-title', title)
    })
    6 changes: 6 additions & 0 deletions renderer.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,6 @@
    const setButton = document.getElementById('btn')
    const titleInput = document.getElementById('title')
    setButton.addEventListener('click', () => {
    const title = titleInput.value
    window.electronAPI.setTitle(title)
    });
    3 changes: 3 additions & 0 deletions styles.css
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,3 @@
    /* styles.css */

    /* Add styles here to customize the appearance of your app */