docs: remove references to remote from docs (#25416)

This commit is contained in:
Jeremy Rose 2020-09-14 10:36:54 -07:00 committed by GitHub
parent 7f885bd266
commit 5de7eb3618
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
24 changed files with 157 additions and 227 deletions

View file

@ -501,7 +501,7 @@ Returns:
Emitted when `desktopCapturer.getSources()` is called in the renderer process of `webContents`. Emitted when `desktopCapturer.getSources()` is called in the renderer process of `webContents`.
Calling `event.preventDefault()` will make it return empty sources. Calling `event.preventDefault()` will make it return empty sources.
### Event: 'remote-require' ### Event: 'remote-require' _Deprecated_
Returns: Returns:
@ -513,7 +513,7 @@ Emitted when `remote.require()` is called in the renderer process of `webContent
Calling `event.preventDefault()` will prevent the module from being returned. Calling `event.preventDefault()` will prevent the module from being returned.
Custom value can be returned by setting `event.returnValue`. Custom value can be returned by setting `event.returnValue`.
### Event: 'remote-get-global' ### Event: 'remote-get-global' _Deprecated_
Returns: Returns:
@ -525,7 +525,7 @@ Emitted when `remote.getGlobal()` is called in the renderer process of `webConte
Calling `event.preventDefault()` will prevent the global from being returned. Calling `event.preventDefault()` will prevent the global from being returned.
Custom value can be returned by setting `event.returnValue`. Custom value can be returned by setting `event.returnValue`.
### Event: 'remote-get-builtin' ### Event: 'remote-get-builtin' _Deprecated_
Returns: Returns:
@ -537,7 +537,7 @@ Emitted when `remote.getBuiltin()` is called in the renderer process of `webCont
Calling `event.preventDefault()` will prevent the module from being returned. Calling `event.preventDefault()` will prevent the module from being returned.
Custom value can be returned by setting `event.returnValue`. Custom value can be returned by setting `event.returnValue`.
### Event: 'remote-get-current-window' ### Event: 'remote-get-current-window' _Deprecated_
Returns: Returns:
@ -548,7 +548,7 @@ Emitted when `remote.getCurrentWindow()` is called in the renderer process of `w
Calling `event.preventDefault()` will prevent the object from being returned. Calling `event.preventDefault()` will prevent the object from being returned.
Custom value can be returned by setting `event.returnValue`. Custom value can be returned by setting `event.returnValue`.
### Event: 'remote-get-current-web-contents' ### Event: 'remote-get-current-web-contents' _Deprecated_
Returns: Returns:

View file

@ -8,9 +8,6 @@ Process: [Main](../glossary.md#main-process)
// In the main process. // In the main process.
const { BrowserWindow } = require('electron') const { BrowserWindow } = require('electron')
// Or use `remote` from the renderer process.
// const { BrowserWindow } = require('electron').remote
const win = new BrowserWindow({ width: 800, height: 600 }) const win = new BrowserWindow({ width: 800, height: 600 })
// Load a remote URL // Load a remote URL

View file

@ -11,14 +11,6 @@ const { dialog } = require('electron')
console.log(dialog.showOpenDialog({ properties: ['openFile', 'multiSelections'] })) console.log(dialog.showOpenDialog({ properties: ['openFile', 'multiSelections'] }))
``` ```
The Dialog is opened from Electron's main thread. If you want to use the dialog
object from a renderer process, remember to access it using the remote:
```javascript
const { dialog } = require('electron').remote
console.log(dialog)
```
## Methods ## Methods
The `dialog` module has the following methods: The `dialog` module has the following methods:

View file

@ -112,13 +112,19 @@ optional parameter can be used to forward mouse move messages to the web page,
allowing events such as `mouseleave` to be emitted: allowing events such as `mouseleave` to be emitted:
```javascript ```javascript
const win = require('electron').remote.getCurrentWindow() const { ipcRenderer } = require('electron')
const el = document.getElementById('clickThroughElement') const el = document.getElementById('clickThroughElement')
el.addEventListener('mouseenter', () => { el.addEventListener('mouseenter', () => {
win.setIgnoreMouseEvents(true, { forward: true }) ipcRenderer.send('set-ignore-mouse-events', true, { forward: true })
}) })
el.addEventListener('mouseleave', () => { el.addEventListener('mouseleave', () => {
win.setIgnoreMouseEvents(false) ipcRenderer.send('set-ignore-mouse-events', false)
})
// Main process
const { ipcMain } = require('electron')
ipcMain.on('set-ignore-mouse-events', (event, ...args) => {
BrowserWindow.fromWebContents(event.sender).setIgnoreMouseEvents(...args)
}) })
``` ```

View file

@ -88,26 +88,17 @@ and preload.js:
```js ```js
// This file is loaded whenever a javascript context is created. It runs in a // This file is loaded whenever a javascript context is created. It runs in a
// private scope that can access a subset of Electron renderer APIs. We must be // private scope that can access a subset of Electron renderer APIs. Without
// careful to not leak any objects into the global scope! // contextIsolation enabled, it's possible to accidentally leak privileged
const { ipcRenderer, remote } = require('electron') // globals like ipcRenderer to web content.
const fs = remote.require('fs') const { ipcRenderer } = require('electron')
// read a configuration file using the `fs` module
const buf = fs.readFileSync('allowed-popup-urls.json')
const allowedUrls = JSON.parse(buf.toString('utf8'))
const defaultWindowOpen = window.open const defaultWindowOpen = window.open
function customWindowOpen (url, ...args) { window.open = function customWindowOpen (url, ...args) {
if (allowedUrls.indexOf(url) === -1) { ipcRenderer.send('report-window-open', location.origin, url, args)
ipcRenderer.sendSync('blocked-popup-notification', location.origin, url) return defaultWindowOpen(url + '?from_electron=1', ...args)
return null
}
return defaultWindowOpen(url, ...args)
} }
window.open = customWindowOpen
``` ```
Important things to notice in the preload script: Important things to notice in the preload script:
@ -115,8 +106,6 @@ Important things to notice in the preload script:
- Even though the sandboxed renderer doesn't have Node.js running, it still has - Even though the sandboxed renderer doesn't have Node.js running, it still has
access to a limited node-like environment: `Buffer`, `process`, `setImmediate`, access to a limited node-like environment: `Buffer`, `process`, `setImmediate`,
`clearImmediate` and `require` are available. `clearImmediate` and `require` are available.
- The preload script can indirectly access all APIs from the main process through the
`remote` and `ipcRenderer` modules.
- The preload script must be contained in a single script, but it is possible to have - The preload script must be contained in a single script, but it is possible to have
complex preload code composed with multiple modules by using a tool like complex preload code composed with multiple modules by using a tool like
webpack or browserify. An example of using browserify is below. webpack or browserify. An example of using browserify is below.
@ -144,15 +133,12 @@ following modules:
- `desktopCapturer` - `desktopCapturer`
- `ipcRenderer` - `ipcRenderer`
- `nativeImage` - `nativeImage`
- `remote`
- `webFrame` - `webFrame`
- `events` - `events`
- `timers` - `timers`
- `url` - `url`
More may be added as needed to expose more Electron APIs in the sandbox, but any More may be added as needed to expose more Electron APIs in the sandbox.
module in the main process can already be used through
`electron.remote.require`.
## Rendering untrusted content ## Rendering untrusted content

View file

@ -9,7 +9,7 @@ the [native modules](../tutorial/using-native-node-modules.md)).
Electron also provides some extra built-in modules for developing native Electron also provides some extra built-in modules for developing native
desktop applications. Some modules are only available in the main process, some desktop applications. Some modules are only available in the main process, some
are only available in the renderer process (web page), and some can be used in are only available in the renderer process (web page), and some can be used in
both processes. either process type.
The basic rule is: if a module is [GUI][gui] or low-level system related, then The basic rule is: if a module is [GUI][gui] or low-level system related, then
it should be only available in the main process. You need to be familiar with it should be only available in the main process. You need to be familiar with
@ -29,15 +29,15 @@ app.whenReady().then(() => {
``` ```
The renderer process is no different than a normal web page, except for the The renderer process is no different than a normal web page, except for the
extra ability to use node modules: extra ability to use node modules if `nodeIntegration` is enabled:
```html ```html
<!DOCTYPE html> <!DOCTYPE html>
<html> <html>
<body> <body>
<script> <script>
const { app } = require('electron').remote const fs = require('fs')
console.log(app.getVersion()) console.log(fs.readFileSync(__filename, 'utf8'))
</script> </script>
</body> </body>
</html> </html>

View file

@ -784,7 +784,7 @@ Returns:
Emitted when `desktopCapturer.getSources()` is called in the renderer process. Emitted when `desktopCapturer.getSources()` is called in the renderer process.
Calling `event.preventDefault()` will make it return empty sources. Calling `event.preventDefault()` will make it return empty sources.
#### Event: 'remote-require' #### Event: 'remote-require' _Deprecated_
Returns: Returns:
@ -795,7 +795,7 @@ Emitted when `remote.require()` is called in the renderer process.
Calling `event.preventDefault()` will prevent the module from being returned. Calling `event.preventDefault()` will prevent the module from being returned.
Custom value can be returned by setting `event.returnValue`. Custom value can be returned by setting `event.returnValue`.
#### Event: 'remote-get-global' #### Event: 'remote-get-global' _Deprecated_
Returns: Returns:
@ -806,7 +806,7 @@ Emitted when `remote.getGlobal()` is called in the renderer process.
Calling `event.preventDefault()` will prevent the global from being returned. Calling `event.preventDefault()` will prevent the global from being returned.
Custom value can be returned by setting `event.returnValue`. Custom value can be returned by setting `event.returnValue`.
#### Event: 'remote-get-builtin' #### Event: 'remote-get-builtin' _Deprecated_
Returns: Returns:
@ -817,7 +817,7 @@ Emitted when `remote.getBuiltin()` is called in the renderer process.
Calling `event.preventDefault()` will prevent the module from being returned. Calling `event.preventDefault()` will prevent the module from being returned.
Custom value can be returned by setting `event.returnValue`. Custom value can be returned by setting `event.returnValue`.
#### Event: 'remote-get-current-window' #### Event: 'remote-get-current-window' _Deprecated_
Returns: Returns:
@ -827,7 +827,7 @@ Emitted when `remote.getCurrentWindow()` is called in the renderer process.
Calling `event.preventDefault()` will prevent the object from being returned. Calling `event.preventDefault()` will prevent the object from being returned.
Custom value can be returned by setting `event.returnValue`. Custom value can be returned by setting `event.returnValue`.
#### Event: 'remote-get-current-web-contents' #### Event: 'remote-get-current-web-contents' _Deprecated_
Returns: Returns:
@ -1457,7 +1457,7 @@ An example of showing devtools in a `<webview>` tag:
<webview id="browser" src="https://github.com"></webview> <webview id="browser" src="https://github.com"></webview>
<webview id="devtools" src="about:blank"></webview> <webview id="devtools" src="about:blank"></webview>
<script> <script>
const { webContents } = require('electron').remote const { ipcRenderer } = require('electron')
const emittedOnce = (element, eventName) => new Promise(resolve => { const emittedOnce = (element, eventName) => new Promise(resolve => {
element.addEventListener(eventName, event => resolve(event), { once: true }) element.addEventListener(eventName, event => resolve(event), { once: true })
}) })
@ -1466,16 +1466,26 @@ An example of showing devtools in a `<webview>` tag:
const browserReady = emittedOnce(browserView, 'dom-ready') const browserReady = emittedOnce(browserView, 'dom-ready')
const devtoolsReady = emittedOnce(devtoolsView, 'dom-ready') const devtoolsReady = emittedOnce(devtoolsView, 'dom-ready')
Promise.all([browserReady, devtoolsReady]).then(() => { Promise.all([browserReady, devtoolsReady]).then(() => {
const browser = webContents.fromId(browserView.getWebContentsId()) const targetId = browserView.getWebContentsId()
const devtools = webContents.fromId(devtoolsView.getWebContentsId()) const devtoolsId = devtoolsView.getWebContentsId()
browser.setDevToolsWebContents(devtools) ipcRenderer.send('open-devtools', targetId, devtoolsId)
browser.openDevTools()
}) })
</script> </script>
</body> </body>
</html> </html>
``` ```
```js
// Main process
const { ipcMain, webContents } = require('electron')
ipcMain.on('open-devtools', (event, targetContentsId, devtoolsContentsId) => {
const target = webContents.fromId(targetContentsId)
const devtools = webContents.fromId(devtoolsContentsId)
target.setDevToolsWebContents(devtools)
target.openDevTools()
})
```
An example of showing devtools in a `BrowserWindow`: An example of showing devtools in a `BrowserWindow`:
```js ```js

View file

@ -137,7 +137,7 @@ This option is disabled by default in the guest page.
``` ```
A `Boolean`. When this attribute is `false` the guest page in `webview` will not have access A `Boolean`. When this attribute is `false` the guest page in `webview` will not have access
to the [`remote`](remote.md) module. The remote module is available by default. to the [`remote`](remote.md) module. The remote module is unavailable by default.
### `plugins` ### `plugins`

View file

@ -69,8 +69,7 @@ way of figuring out which is which.
### Which Process Should I Attach to? ### Which Process Should I Attach to?
Code executed within the main process (that is, code found in or eventually run Code executed within the main process (that is, code found in or eventually run
by your main JavaScript file) as well as code called using the remote by your main JavaScript file) will run inside the main process, while other
(`require('electron').remote`) will run inside the main process, while other
code will execute inside its respective renderer process. code will execute inside its respective renderer process.
You can be attached to multiple programs when you are debugging, but only one You can be attached to multiple programs when you are debugging, but only one

View file

@ -1,7 +1,11 @@
const { BrowserWindow, app } = require('electron') const { BrowserWindow, app, screen, ipcMain } = require('electron')
let mainWindow = null let mainWindow = null
ipcMain.handle('get-screen-size', () => {
return screen.getPrimaryDisplay().workAreaSize
})
function createWindow () { function createWindow () {
const windowOptions = { const windowOptions = {
width: 600, width: 600,

View file

@ -1,5 +1,4 @@
const { desktopCapturer } = require('electron') const { desktopCapturer, shell, ipcRenderer } = require('electron')
const { screen, shell } = require('electron').remote
const fs = require('fs') const fs = require('fs')
const os = require('os') const os = require('os')
@ -8,9 +7,9 @@ const path = require('path')
const screenshot = document.getElementById('screen-shot') const screenshot = document.getElementById('screen-shot')
const screenshotMsg = document.getElementById('screenshot-path') const screenshotMsg = document.getElementById('screenshot-path')
screenshot.addEventListener('click', (event) => { screenshot.addEventListener('click', async (event) => {
screenshotMsg.textContent = 'Gathering screens...' screenshotMsg.textContent = 'Gathering screens...'
const thumbSize = determineScreenShotSize() const thumbSize = await determineScreenShotSize()
const options = { types: ['screen'], thumbnailSize: thumbSize } const options = { types: ['screen'], thumbnailSize: thumbSize }
desktopCapturer.getSources(options, (error, sources) => { desktopCapturer.getSources(options, (error, sources) => {
@ -33,8 +32,8 @@ screenshot.addEventListener('click', (event) => {
}) })
}) })
function determineScreenShotSize () { async function determineScreenShotSize () {
const screenSize = screen.getPrimaryDisplay().workAreaSize const screenSize = await ipcRenderer.invoke('get-screen-size')
const maxDimension = Math.max(screenSize.width, screenSize.height) const maxDimension = Math.max(screenSize.width, screenSize.height)
return { return {
width: maxDimension * window.devicePixelRatio, width: maxDimension * window.devicePixelRatio,

View file

@ -1,23 +1,20 @@
const { app, BrowserWindow } = require('electron') const { app, BrowserWindow, ipcMain } = require('electron')
let mainWindow = null ipcMain.on('create-frameless-window', (event, {url}) => {
const win = new BrowserWindow({ frame: false })
win.loadURL(url)
})
function createWindow () { function createWindow () {
const windowOptions = { const mainWindow = new BrowserWindow({
width: 600, width: 600,
height: 400, height: 400,
title: 'Create a frameless window', title: 'Create a frameless window',
webPreferences: { webPreferences: {
nodeIntegration: true nodeIntegration: true
} }
}
mainWindow = new BrowserWindow(windowOptions)
mainWindow.loadFile('index.html')
mainWindow.on('closed', () => {
mainWindow = null
}) })
mainWindow.loadFile('index.html')
} }
app.whenReady().then(() => { app.whenReady().then(() => {

View file

@ -1,12 +1,8 @@
const { BrowserWindow } = require('electron').remote const { ipcRenderer } = require('electron')
const newWindowBtn = document.getElementById('frameless-window') const newWindowBtn = document.getElementById('frameless-window')
newWindowBtn.addEventListener('click', (event) => { newWindowBtn.addEventListener('click', () => {
let win = new BrowserWindow({ frame: false }) const url = 'data:text/html,<h2>Hello World!</h2><a id="close" href="javascript:window.close()">Close this Window</a>'
ipcRenderer.send('create-frameless-window', { url })
win.on('close', () => { win = null })
win.loadURL('data:text/html,<h2>Hello World!</h2><a id="close" href="javascript:window.close()">Close this Window</a>')
win.show()
}) })

View file

@ -1,13 +1,14 @@
// Modules to control application life and create native browser window // Modules to control application life and create native browser window
const { app, BrowserWindow } = require('electron') const { app, BrowserWindow } = require('electron')
// Keep a global reference of the window object, if you don't, the window will ipcMain.on('create-frameless-window', (event, {url}) => {
// be closed automatically when the JavaScript object is garbage collected. const win = new BrowserWindow({ frame: false })
let mainWindow win.loadURL(url)
})
function createWindow () { function createWindow () {
// Create the browser window. // Create the browser window.
mainWindow = new BrowserWindow({ const mainWindow = new BrowserWindow({
width: 800, width: 800,
height: 600, height: 600,
webPreferences: { webPreferences: {
@ -17,17 +18,6 @@ function createWindow () {
// and load the index.html of the app. // and load the index.html of the app.
mainWindow.loadFile('index.html') mainWindow.loadFile('index.html')
// Open the DevTools.
// mainWindow.webContents.openDevTools()
// Emitted when the window is closed.
mainWindow.on('closed', function () {
// Dereference the window object, usually you would store windows
// in an array if your app supports multi windows, this is the time
// when you should delete the corresponding element.
mainWindow = null
})
} }
// This method will be called when Electron has finished // This method will be called when Electron has finished

View file

@ -1,25 +1,8 @@
const { BrowserWindow } = require('electron').remote const { ipcRenderer } = require('electron')
const shell = require('electron').shell
const framelessWindowBtn = document.getElementById('frameless-window') const newWindowBtn = document.getElementById('frameless-window')
const links = document.querySelectorAll('a[href]') newWindowBtn.addEventListener('click', () => {
const url = 'data:text/html,<h2>Hello World!</h2><a id="close" href="javascript:window.close()">Close this Window</a>'
framelessWindowBtn.addEventListener('click', (event) => { ipcRenderer.send('create-frameless-window', { url })
const modalPath = 'https://electronjs.org'
let win = new BrowserWindow({ frame: false })
win.on('close', () => { win = null })
win.loadURL(modalPath)
win.show()
})
Array.prototype.forEach.call(links, (link) => {
const url = link.getAttribute('href')
if (url.indexOf('http') === 0) {
link.addEventListener('click', (e) => {
e.preventDefault()
shell.openExternal(url)
})
}
}) })

View file

@ -1,9 +1,20 @@
// Modules to control application life and create native browser window // Modules to control application life and create native browser window
const { app, BrowserWindow } = require('electron') const { app, BrowserWindow, ipcMain } = require('electron')
// Keep a global reference of the window object, if you don't, the window will ipcMain.on('create-demo-window', (event) => {
// be closed automatically when the JavaScript object is garbage collected. const win = new BrowserWindow({ width: 400, height: 275 })
let mainWindow
function updateReply() {
event.sender.send('bounds-changed', {
size: win.getSize(),
position: win.getPosition()
})
}
win.on('resize', updateReply)
win.on('move', updateReply)
win.loadURL('https://electronjs.org')
})
function createWindow () { function createWindow () {
// Create the browser window. // Create the browser window.
@ -17,17 +28,6 @@ function createWindow () {
// and load the index.html of the app. // and load the index.html of the app.
mainWindow.loadFile('index.html') mainWindow.loadFile('index.html')
// Open the DevTools.
// mainWindow.webContents.openDevTools()
// Emitted when the window is closed.
mainWindow.on('closed', function () {
// Dereference the window object, usually you would store windows
// in an array if your app supports multi windows, this is the time
// when you should delete the corresponding element.
mainWindow = null
})
} }
// This method will be called when Electron has finished // This method will be called when Electron has finished

View file

@ -1,27 +1,17 @@
const { BrowserWindow } = require('electron').remote const { shell, ipcRenderer } = require('electron')
const shell = require('electron').shell
const manageWindowBtn = document.getElementById('manage-window') const manageWindowBtn = document.getElementById('manage-window')
const links = document.querySelectorAll('a[href]') const links = document.querySelectorAll('a[href]')
let win ipcRenderer.on('bounds-changed', (event, bounds) => {
const manageWindowReply = document.getElementById('manage-window-reply')
const message = `Size: ${bounds.size} Position: ${bounds.position}`
manageWindowReply.textContent = message
})
manageWindowBtn.addEventListener('click', (event) => { manageWindowBtn.addEventListener('click', (event) => {
const modalPath = 'https://electronjs.org' ipcRenderer.send('create-demo-window')
win = new BrowserWindow({ width: 400, height: 275 })
win.on('resize', updateReply)
win.on('move', updateReply)
win.on('close', () => { win = null })
win.loadURL(modalPath)
win.show()
function updateReply () {
const manageWindowReply = document.getElementById('manage-window-reply')
const message = `Size: ${win.getSize()} Position: ${win.getPosition()}`
manageWindowReply.innerText = message
}
}) })
Array.prototype.forEach.call(links, (link) => { Array.prototype.forEach.call(links, (link) => {

View file

@ -7,7 +7,7 @@
<h2>Create a new window</h2> <h2>Create a new window</h2>
<h3>Supports: Win, macOS, Linux <span>|</span> Process: Main</h3> <h3>Supports: Win, macOS, Linux <span>|</span> Process: Main</h3>
<button id="new-window">View Demo</button> <button id="new-window">View Demo</button>
<p>The <code>BrowserWindow</code> module gives you the ability to create new windows in your app. This main process module can be used from the renderer process with the <code>remote</code> module, as is shown in this demo.</p> <p>The <code>BrowserWindow</code> module gives you the ability to create new windows in your app.</p>
<p>There are a lot of options when creating a new window. A few are in this demo, but visit the <a id="browser-window-link" href="">documentation<span>(opens in new window)</span></a> <p>There are a lot of options when creating a new window. A few are in this demo, but visit the <a id="browser-window-link" href="">documentation<span>(opens in new window)</span></a>
<div> <div>
<h2>ProTip</h2> <h2>ProTip</h2>

View file

@ -1,13 +1,14 @@
// Modules to control application life and create native browser window // Modules to control application life and create native browser window
const { app, BrowserWindow } = require('electron') const { app, BrowserWindow, ipcMain } = require('electron')
// Keep a global reference of the window object, if you don't, the window will ipcMain.on('new-window', (event, { url, width, height }) => {
// be closed automatically when the JavaScript object is garbage collected. const win = new BrowserWindow({ width, height })
let mainWindow win.loadURL(url)
})
function createWindow () { function createWindow () {
// Create the browser window. // Create the browser window.
mainWindow = new BrowserWindow({ const mainWindow = new BrowserWindow({
width: 800, width: 800,
height: 600, height: 600,
webPreferences: { webPreferences: {
@ -17,17 +18,6 @@ function createWindow () {
// and load the index.html of the app. // and load the index.html of the app.
mainWindow.loadFile('index.html') mainWindow.loadFile('index.html')
// Open the DevTools.
// mainWindow.webContents.openDevTools()
// Emitted when the window is closed.
mainWindow.on('closed', function () {
// Dereference the window object, usually you would store windows
// in an array if your app supports multi windows, this is the time
// when you should delete the corresponding element.
mainWindow = null
})
} }
// This method will be called when Electron has finished // This method will be called when Electron has finished

View file

@ -1,16 +1,11 @@
const { BrowserWindow } = require('electron').remote const { shell, ipcRenderer } = require('electron')
const { shell } = require('electron').remote
const newWindowBtn = document.getElementById('new-window') const newWindowBtn = document.getElementById('new-window')
const link = document.getElementById('browser-window-link') const link = document.getElementById('browser-window-link')
newWindowBtn.addEventListener('click', (event) => { newWindowBtn.addEventListener('click', (event) => {
const url = 'https://electronjs.org'
let win = new BrowserWindow({ width: 400, height: 320 }) ipcRenderer.send('new-window', { url, width: 400, height: 320 });
win.on('close', () => { win = null })
win.loadURL('https://electronjs.org')
win.show()
}) })
link.addEventListener('click', (e) => { link.addEventListener('click', (e) => {

View file

@ -1,13 +1,9 @@
// Modules to control application life and create native browser window // Modules to control application life and create native browser window
const { app, BrowserWindow } = require('electron') const { app, BrowserWindow, ipcMain } = require('electron')
// 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.
let mainWindow
function createWindow () { function createWindow () {
// Create the browser window. // Create the browser window.
mainWindow = new BrowserWindow({ const mainWindow = new BrowserWindow({
width: 800, width: 800,
height: 600, height: 600,
webPreferences: { webPreferences: {
@ -18,15 +14,27 @@ function createWindow () {
// and load the index.html of the app. // and load the index.html of the app.
mainWindow.loadFile('index.html') mainWindow.loadFile('index.html')
// Open the DevTools. let demoWindow
// mainWindow.webContents.openDevTools() ipcMain.on('show-demo-window', () => {
if (demoWindow) {
demoWindow.focus()
return
}
demoWindow = new BrowserWindow({ width: 600, height: 400 })
demoWindow.loadURL('https://electronjs.org')
demoWindow.on('close', () => {
mainWindow.webContents.send('window-close')
})
demoWindow.on('focus', () => {
mainWindow.webContents.send('window-focus')
})
demoWindow.on('blur', () => {
mainWindow.webContents.send('window-blur')
})
})
// Emitted when the window is closed. ipcMain.on('focus-demo-window', () => {
mainWindow.on('closed', function () { if (demoWindow) demoWindow.focus()
// Dereference the window object, usually you would store windows
// in an array if your app supports multi windows, this is the time
// when you should delete the corresponding element.
mainWindow = null
}) })
} }

View file

@ -1,42 +1,33 @@
const { BrowserWindow } = require('electron').remote const { shell, ipcRenderer } = require('electron')
const shell = require('electron').shell
const listenToWindowBtn = document.getElementById('listen-to-window') const listenToWindowBtn = document.getElementById('listen-to-window')
const focusModalBtn = document.getElementById('focus-on-modal-window') const focusModalBtn = document.getElementById('focus-on-modal-window')
const links = document.querySelectorAll('a[href]') const hideFocusBtn = () => {
focusModalBtn.classList.add('disappear')
focusModalBtn.classList.remove('smooth-appear')
focusModalBtn.removeEventListener('click', focusWindow)
}
let win const showFocusBtn = (btn) => {
focusModalBtn.classList.add('smooth-appear')
focusModalBtn.classList.remove('disappear')
focusModalBtn.addEventListener('click', focusWindow)
}
const focusWindow = () => {
ipcRenderer.send('focus-demo-window')
}
ipcRenderer.on('window-focus', hideFocusBtn)
ipcRenderer.on('window-close', hideFocusBtn)
ipcRenderer.on('window-blur', showFocusBtn)
listenToWindowBtn.addEventListener('click', () => { listenToWindowBtn.addEventListener('click', () => {
const modalPath = 'https://electronjs.org' ipcRenderer.send('show-demo-window')
win = new BrowserWindow({ width: 600, height: 400 })
const hideFocusBtn = () => {
focusModalBtn.classList.add('disappear')
focusModalBtn.classList.remove('smooth-appear')
focusModalBtn.removeEventListener('click', clickHandler)
}
const showFocusBtn = (btn) => {
if (!win) return
focusModalBtn.classList.add('smooth-appear')
focusModalBtn.classList.remove('disappear')
focusModalBtn.addEventListener('click', clickHandler)
}
win.on('focus', hideFocusBtn)
win.on('blur', showFocusBtn)
win.on('close', () => {
hideFocusBtn()
win = null
})
win.loadURL(modalPath)
win.show()
const clickHandler = () => { win.focus() }
}) })
const links = document.querySelectorAll('a[href]')
Array.prototype.forEach.call(links, (link) => { Array.prototype.forEach.call(links, (link) => {
const url = link.getAttribute('href') const url = link.getAttribute('href')
if (url.indexOf('http') === 0) { if (url.indexOf('http') === 0) {

View file

@ -26,7 +26,8 @@ To test In-App Purchase in development with Electron you'll have to change the `
Here is an example that shows how to use In-App Purchases in Electron. You'll have to replace the product ids by the identifiers of the products created with iTunes Connect (the identifier of `com.example.app.product1` is `product1`). Note that you have to listen to the `transactions-updated` event as soon as possible in your app. Here is an example that shows how to use In-App Purchases in Electron. You'll have to replace the product ids by the identifiers of the products created with iTunes Connect (the identifier of `com.example.app.product1` is `product1`). Note that you have to listen to the `transactions-updated` event as soon as possible in your app.
```javascript ```javascript
const { inAppPurchase } = require('electron').remote // Main process
const { inAppPurchase } = require('electron')
const PRODUCT_IDS = ['id1', 'id2'] const PRODUCT_IDS = ['id1', 'id2']
// Listen for transactions as soon as possible. // Listen for transactions as soon as possible.

View file

@ -92,11 +92,7 @@ BrowserWindow.getFocusedWindow = () => {
}; };
BrowserWindow.fromWebContents = (webContents: WebContents) => { BrowserWindow.fromWebContents = (webContents: WebContents) => {
for (const window of BrowserWindow.getAllWindows()) { return webContents.getOwnerBrowserWindow();
if (window.webContents && window.webContents.equal(webContents)) return window;
}
return null;
}; };
BrowserWindow.fromBrowserView = (browserView: BrowserView) => { BrowserWindow.fromBrowserView = (browserView: BrowserView) => {