You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
docs: update Quick Start Guide for Electron 12 (electron#28223)
* docs: Update Quick Start Guide for Electron 12
With `contextIsolation` enabled by default in Electron 12, the Getting Started Guide no longer works as it is written. In order for the basic example to display values from `process.versions`, we need to add a `preload.js` to the example.
* fix: annotate preload code block with a language
* docs: update quick-start Fiddle example to use preload to provide version info
* fix: ensure example files end in a newline
* docs: add security warning to instructions for turning off contextIsolation
Co-authored-by: John Kleinschmidt <jkleinsc@github.com>
* docs: treat preload as an adjective instead of a noun
Co-authored-by: John Kleinschmidt <jkleinsc@github.com>
Co-authored-by: John Kleinschmidt <jkleinsc@github.com>
Copy file name to clipboardExpand all lines: docs/tutorial/quick-start.md
+46-17Lines changed: 46 additions & 17 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -32,6 +32,7 @@ From a development perspective, an Electron application is essentially a Node.js
32
32
my-electron-app/
33
33
├── package.json
34
34
├── main.js
35
+
├── preload.js
35
36
└── index.html
36
37
```
37
38
@@ -55,45 +56,49 @@ The main script may look as follows:
55
56
56
57
```javascript fiddle='docs/fiddles/quick-start'
57
58
const { app, BrowserWindow } =require('electron')
59
+
constpath=require('path')
58
60
59
61
functioncreateWindow () {
60
62
constwin=newBrowserWindow({
61
63
width:800,
62
64
height:600,
63
65
webPreferences: {
64
-
nodeIntegration:true
66
+
preload:path.join(__dirname, 'preload.js')
65
67
}
66
68
})
67
69
68
70
win.loadFile('index.html')
69
71
}
70
72
71
-
app.whenReady().then(createWindow)
73
+
app.whenReady().then(() => {
74
+
createWindow()
75
+
76
+
app.on('activate', () => {
77
+
if (BrowserWindow.getAllWindows().length===0) {
78
+
createWindow()
79
+
}
80
+
})
81
+
)
72
82
73
83
app.on('window-all-closed', () => {
74
84
if (process.platform!=='darwin') {
75
85
app.quit()
76
86
}
77
87
})
78
-
79
-
app.on('activate', () => {
80
-
if (BrowserWindow.getAllWindows().length===0) {
81
-
createWindow()
82
-
}
83
-
})
84
88
```
85
89
86
90
##### What is going on above?
87
91
88
92
1. Line 1: First, you import the `app` and `BrowserWindow` modules of the `electron` package to be able to manage your application's lifecycle events, as well as create and control browser windows.
89
-
2. Line 3: After that, you define a function that creates a [new browser window](../api/browser-window.md#new-browserwindowoptions) with node integration enabled, loads `index.html` file into this window (line 12, we will discuss the file later).
90
-
3. Line 15: You create a new browser window by invoking the `createWindow` function once the Electron application [is initialized](../api/app.md#appwhenready).
91
-
4. Line 17: You add a new listener that tries to quit the application when it no longer has any open windows. This listener is a no-op on macOS due to the operating system's [window management behavior](https://support.apple.com/en-ca/guide/mac-help/mchlp2469/mac).
92
-
5. Line 23: You add a new listener that creates a new browser window only if when the application has no visible windows after being activated. For example, after launching the application for the first time, or re-launching the already running application.
93
+
2. Line 2: Second, you import the `path` package which provides utility functions for file paths.
94
+
3. Line 4: After that, you define a function that creates a [new browser window](../api/browser-window.md#new-browserwindowoptions) with a preload script, loads `index.html` file into this window (line 13, we will discuss the file later).
95
+
4. Line 16: You create a new browser window by invoking the `createWindow` function once the Electron application [is initialized](../api/app.md#appwhenready).
96
+
5. Line 18: You add a new listener that creates a new browser window only if when the application has no visible windows after being activated. For example, after launching the application for the first time, or re-launching the already running application.
97
+
6. Line 25: You add a new listener that tries to quit the application when it no longer has any open windows. This listener is a no-op on macOS due to the operating system's [window management behavior](https://support.apple.com/en-ca/guide/mac-help/mchlp2469/mac).
93
98
94
99
#### Create a web page
95
100
96
-
This is the web page you want to display once the application is initialized. This web page represents the Renderer process. You can create multiple browser windows, where each window uses its own independent Renderer. Each window can optionally be granted with full access to Node.js API through the `nodeIntegration` preference.
101
+
This is the web page you want to display once the application is initialized. This web page represents the Renderer process. You can create multiple browser windows, where each window uses its own independent Renderer. You can optionally grant access to additional Node.js APIs by exposing them from your preload script.
97
102
98
103
The `index.html` page looks as follows:
99
104
@@ -108,14 +113,38 @@ The `index.html` page looks as follows:
108
113
<body style="background: white;">
109
114
<h1>Hello World!</h1>
110
115
<p>
111
-
We are using node <script>document.write(process.versions.node)</script>,
and Electron <script>document.write(process.versions.electron)</script>.
116
+
We are using Node.js<span id="node-version"></span>,
117
+
Chromium <span id="chrome-version"></span>,
118
+
and Electron <span id="electron-version"></span>.
114
119
</p>
115
120
</body>
116
121
</html>
117
122
```
118
123
124
+
#### Define a preload script
125
+
126
+
Your preload script acts as a bridge between Node.js and your web page. It allows you to expose specific APIs and behaviors to your web page rather than insecurely exposing the entire Node.js API. In this example we will use the preload script to read version information from the `process` object and update the web page with that info.
1. On line 1: First you define an event listener that tells you when the web page has loaded
144
+
2. On line 2: Second you define a utility function used to set the text of the placeholders in the `index.html`
145
+
3. On line 7: Next you loop through the list of components whose version you want to display
146
+
4. On line 8: Finally, you call `replaceText` to look up the version placeholders in `index.html` and set their text value to the values from `process.versions`
147
+
119
148
#### Modify your package.json file
120
149
121
150
Your Electron application uses the `package.json` file as the main entry point (as any other Node.js application). The main script of your application is `main.js`, so modify the `package.json` file accordingly:
> NOTE: To access the Node.js API from the Renderer process, you need to set the `nodeIntegration` preference to `true`.
315
+
> NOTE: To access the Node.js API from the Renderer process, you need to set the `nodeIntegration` preference to `true` and the `contextIsolation` preference to `false`. Please note that access to the Node.js API in any renderer that loads remote content is not recommended for [security reasons](../tutorial/security.md#2-do-not-enable-nodejs-integration-for-remote-content).
287
316
288
317
Electron exposes full access to Node.js API and its modules both in the Main and the Renderer processes. For example, you can read all the files from the root directory:
0 commit comments