Skip to content

Instantly share code, notes, and snippets.

@codebytere
Created December 14, 2020 04:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save codebytere/a786242649a5d2e462c3328c905f0c9f to your computer and use it in GitHub Desktop.
Save codebytere/a786242649a5d2e462c3328c905f0c9f to your computer and use it in GitHub Desktop.
browserview-reparent
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Hello World!</title>
<link rel="stylesheet" type="text/css" href="./styles.css">
</head>
<body>
<h1>Hello World!</h1>
<!-- All of the Node.js APIs are available in this renderer process. -->
We are using Node.js <script>document.write(process.versions.node)</script>,
Chromium <script>document.write(process.versions.chrome)</script>,
and Electron <script>document.write(process.versions.electron)</script>.
<script>
// You can also require other files to run in this process
require('./renderer.js')
</script>
</body>
</html>
const {app, BrowserWindow, BrowserView} = require('electron')
const {once} = require('events')
async function main () {
await app.whenReady()
const width = 400
const height = 300
// Open two windows
const win1 = new BrowserWindow({
title: 'Window-1',
x: 300,
y: 300,
width,
height,
})
const win2 = new BrowserWindow({
title: 'Window-2',
y: 300,
x: 300 + width,
width,
height,
})
// Create BrowserView and append it to Window-1
const view = new BrowserView()
win1.addBrowserView(view)
view.setBounds({
x: 0, y: 0, width, height,
})
// Change BrowserView's owner window to Window-2 (e.g. to move tab between windows)
win2.addBrowserView(view)
view.webContents.loadURL('https://electronjs.org')
await once(view.webContents, 'did-finish-load')
view.webContents.executeJavaScript('alert("Close this window")')
win1.on('closed', () => {
view.webContents.executeJavaScript('alert("first window got closed")')
})
}
main()
.catch(error => {
console.error(error)
app.exit(1)
})
// This file is required by the index.html file and will
// be executed in the renderer process for that window.
// All of the Node.js APIs are available in this process.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment