From 19788f8fbe10de4777d4c51c64b1d964a6b6901d Mon Sep 17 00:00:00 2001 From: Mark Lee Date: Fri, 7 Jun 2019 09:23:51 -0700 Subject: [PATCH] feat: use a preload script instead of enabling nodeIntegration (#279) --- index.html | 13 +++++-------- main.js | 3 ++- preload.js | 7 +++++++ 3 files changed, 14 insertions(+), 9 deletions(-) create mode 100644 preload.js diff --git a/index.html b/index.html index ca8a1f8..bced89e 100644 --- a/index.html +++ b/index.html @@ -6,14 +6,11 @@

Hello World!

- - We are using Node.js , - Chromium , - and Electron . + We are using Node.js , + Chromium , + and Electron . - + + diff --git a/main.js b/main.js index 857a312..3508c8e 100644 --- a/main.js +++ b/main.js @@ -1,5 +1,6 @@ // Modules to control application life and create native browser window const {app, BrowserWindow} = require('electron') +const path = require('path') // Keep a global reference of the window object, if you don't, the window will // be closed automatically when the JavaScript object is garbage collected. @@ -11,7 +12,7 @@ function createWindow () { width: 800, height: 600, webPreferences: { - nodeIntegration: true + preload: path.join(__dirname, 'preload.js') } }) diff --git a/preload.js b/preload.js new file mode 100644 index 0000000..fc67c97 --- /dev/null +++ b/preload.js @@ -0,0 +1,7 @@ +// All of the Node.js APIs are available in the preload process. +// It has the same sandbox as a Chrome extension. +window.addEventListener('DOMContentLoaded', () => { + for (const versionType of ['chrome', 'electron', 'node']) { + document.getElementById(`${versionType}-version`).innerText = process.versions[versionType] + } +})