Table of contents

3. Using Preload Scripts

What is a preload script?

Electron์˜ ๋ฉ”์ธ ํ”„๋กœ์„ธ์Šค๋Š” ์ „์ฒด ์šด์˜ ์ฒด์ œ์— ์•ก์„ธ์Šคํ•  ์ˆ˜ ์žˆ๋Š” Node.js ํ™˜๊ฒฝ์ด๋‹ค. Electron ๋ชจ๋“ˆ๋ฟ๋งŒ ์•„๋‹ˆ๋ผ npm์„ ํ†ตํ•ด ์„ค์น˜ํ•œ ํŒจํ‚ค์ง€๋‚˜ Node.js์˜ ๊ธฐ๋ณธ ๋‚ด์žฅ ๋ชจ๋“ˆ์—๋„ ์•ก์„ธ์Šคํ•  ์ˆ˜ ์žˆ๋‹ค. ๋ฐ˜๋ฉด์—, ๋ณด์•ˆ์ƒ์˜ ์ด์œ ๋กœ ๋ Œ๋”๋Ÿฌ ํ”„๋กœ์„ธ์Šค๋Š” ๊ธฐ๋ณธ์ ์œผ๋กœ Node.js๋ฅผ ์‹คํ–‰ํ•˜์ง€ ์•Š๊ณ  ์›น ํŽ˜์ด์ง€๋ฅผ ์‹คํ–‰ํ•œ๋‹ค.

Electron์˜ ๋‹ค๋ฅธ ํ”„๋กœ์„ธ์Šค ์œ ํ˜•์„ ํ•จ๊ป˜ ์—ฐ๊ฒฐํ•˜๋ ค๋ฉด preload๋ผ๊ณ  ๋ถˆ๋ฆฌ๋Š” ํŠน์ˆ˜ํ•œ ์Šคํฌ๋ฆฝํŠธ๋ฅผ ์‚ฌ์šฉํ•ด์•ผ ํ•œ๋‹ค.


Augmenting the renderer with a preload script

preload.js
preload script๋Š” ๋ Œ๋”๋Ÿฌ์—์„œ ์›น ํŽ˜์ด์ง€๊ฐ€ ๋กœ๋“œ๋˜๊ธฐ ์ „์— ์ฃผ์ž…๋˜๋ฉฐ, ์ด๋Š” ํฌ๋กฌ ์ต์Šคํ…์…˜์˜ ์ปจํ…ํŠธ ์Šคํฌ๋ฆฝํŠธ์™€ ์œ ์‚ฌํ•˜๋‹ค. ํŠน๊ถŒ ์žˆ๋Š” ์•ก์„ธ์Šค๊ฐ€ ํ•„์š”ํ•œ ๋ Œ๋”๋Ÿฌ์— ๊ธฐ๋Šฅ์„ ์ถ”๊ฐ€ํ•˜๋ ค๋ฉด contextBridge API๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์ „์—ญ ๊ฐ์ฒด๋ฅผ ์ •์˜ํ•  ์ˆ˜ ์žˆ๋‹ค.

์ฝ”๋“œ๋ณด๊ธฐ
//preload.js
const { contextBridge } = require('electron')

contextBridge.exposeInMainWorld('versions', {
  node: () => process.versions.node,
  chrome: () => process.versions.chrome,
  electron: () => process.versions.electron
  // we can also expose variables, not just functions
})
์‹คํ–‰ํ™”๋ฉด

web_application44.png

main.js

์ด ์Šคํฌ๋ฆฝํŠธ๋ฅผ ๋ Œ๋”๋Ÿฌ ํ”„๋กœ์„ธ์Šค์— ์—ฐ๊ฒฐํ•˜๋ ค๋ฉด BrowserWindow ์ƒ์„ฑ์ž์˜ webPreferences.preload ์˜ต์…˜์— ํ•ด๋‹น ์Šคํฌ๋ฆฝํŠธ์˜ ๊ฒฝ๋กœ๋ฅผ ์ „๋‹ฌํ•˜๋ฉด ๋œ๋‹ค.

์ฝ”๋“œ๋ณด๊ธฐ
//main.js
const { app, BrowserWindow } = require('electron')
const path = require('node:path')

const createWindow = () => {
  const win = new BrowserWindow({
    width: 800,
    height: 600,
    webPreferences: {
      preload: path.join(__dirname, 'preload.js')
    }
  })

  win.loadFile('index.html')
}

app.whenReady().then(() => {
  createWindow()
})
์‹คํ–‰ํ™”๋ฉด

web_application45.png web_application46.png

renderer.js

ํ˜„์žฌ ์‹œ์ ์—์„œ ๋ Œ๋”๋Ÿฌ๋Š” versions ์ „์—ญ ๋ณ€์ˆ˜์— ์•ก์„ธ์Šคํ•  ์ˆ˜ ์žˆ๋‹ค. ์ด ์ •๋ณด๋ฅผ ์ฐฝ์— ํ‘œ์‹œํ•ด ๋ณด๊ฒ ๋‹ค. ์ด ๋ณ€์ˆ˜๋Š” window.versions ๋˜๋Š” ๊ฐ„๋‹จํžˆ versions๋ฅผ ํ†ตํ•ด ์•ก์„ธ์Šคํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. info๋ผ๋Š” id ์†์„ฑ์„ ๊ฐ€์ง„ HTML ์š”์†Œ์˜ ํ…์ŠคํŠธ๋ฅผ ๋ณ€๊ฒฝํ•˜๋Š” ๋ฐ๋Š” document.getElementById DOM API๋ฅผ ์‚ฌ์šฉํ•˜๊ฒ ๋‹ค.

์ฝ”๋“œ๋ณด๊ธฐ
//renderer.js
const information = document.getElementById('info')
information.innerText = `This app is using Chrome (v${versions.chrome()}), Node.js (v${versions.node()}), and Electron (v${versions.electron()})`
์‹คํ–‰ํ™”๋ฉด

web_application47.png

index.html

index.html์„ ์ˆ˜์ •ํ•˜์—ฌ id ์†์„ฑ์ด โ€˜infoโ€™์ธ ์ƒˆ๋กœ์šด ์š”์†Œ๋ฅผ ์ถ”๊ฐ€ํ•˜๊ณ , renderer.js ์Šคํฌ๋ฆฝํŠธ๋ฅผ ์—ฐ๊ฒฐ

์ฝ”๋“œ๋ณด๊ธฐ

#index.html
<!DOCTYPE html>
<html>
  <head>
    <meta charset="UTF-8" />
    <meta
      http-equiv="Content-Security-Policy"
      content="default-src 'self'; script-src 'self'"
    />
    <meta
      http-equiv="X-Content-Security-Policy"
      content="default-src 'self'; script-src 'self'"
    />
    <title>Hello from Electron renderer!</title>
  </head>
  <body>
    <h1>Hello from Electron renderer!</h1>
    <p>๐Ÿ‘‹</p>
    <p id="info"></p>
  </body>
  <script src="./renderer.js"></script>
</html>
์‹คํ–‰ํ™”๋ฉด

web_application48.png


์‹คํ–‰๊ฒฐ๊ณผ

web_application49.png


ํ”„๋กœ์ ํŠธ ํŠธ๋ฆฌ

web_application50.png