Skip to content

Instantly share code, notes, and snippets.

@VerteDinde
Created March 23, 2023 22:25
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 VerteDinde/1749f4f33969e6f20dcaeb808cdcc3c1 to your computer and use it in GitHub Desktop.
Save VerteDinde/1749f4f33969e6f20dcaeb808cdcc3c1 to your computer and use it in GitHub Desktop.
Testing WebContentsView
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<!-- https://developer.mozilla.org/en-US/docs/Web/HTTP/CSP -->
<meta http-equiv="Content-Security-Policy" content="default-src 'self'; script-src 'self'">
<title>Hello World!</title>
</head>
<body>
<h1>Hello World!</h1>
We are using Node.js <span id="node-version"></span>,
Chromium <span id="chrome-version"></span>,
and Electron <span id="electron-version"></span>.
<!-- You can also require other files to run in this process -->
<script src="./renderer.js"></script>
</body>
</html>
// const { app, BrowserView, BrowserWindow } = require('electron')
const { app, BaseWindow, WebContentsView } = require('electron')
// Broken BrowserView Example
// app.whenReady().then(() => {
// const win = new BrowserWindow({ width: 800, height: 600 })
// const view = new BrowserView()
// view.setBounds({ x: 0, y: 0, width: 150, height: 150 })
// win.setBrowserView(view)
// console.log('getBounds: ', view.getBounds())
// view.webContents.loadURL('https://electronjs.org')
// setTimeout(() => {
// view.setBounds({ x: 0, y: 0, width: 300, height: 300 })
// console.log('getBounds: ', view.getBounds())
// }, 25); // This is ne sweet spot for me, below 25 the BrowserView never shows
// });
app.whenReady().then(() => {
// Example One
const win = new BaseWindow({ width: 800, height: 400 })
// win.webContents.openDevTools();
const view1 = new WebContentsView()
win.contentView.addChildView(view1)
view1.setBounds({ x: 0, y: 0, width: 400, height: 400 })
view1.webContents.loadURL('https://electronjs.org')
const view2 = new WebContentsView()
win.contentView.addChildView(view2)
view2.setBounds({ x: 400, y: 0, width: 400, height: 400 })
view2.webContents.loadURL('https://electronforge.io')
// Example Two
// const mainWindow = new BaseWindow({ width: 1000, height: 1000 })
// const leftView = new WebContentsView()
// leftView.setBounds({ x: 0, y: 0, width: 400, height: 600 })
// leftView.webContents.loadURL('https://electronjs.org')
// mainWindow.contentView.addChildView(leftView)
// const rightView = new WebContentsView()
// rightView.setBounds({ x: 400, y: 0, width: 400, height: 600 })
// rightView.webContents.loadURL('https://slack.com')
// mainWindow.contentView.addChildView(rightView)
// rightView.setBounds({ x: 400, y: 0, width: 600, height: 800 })
// leftView.setBounds({ x: 0, y: 0, width: 400, height: 600 })
// rightView.setBounds({ x: 400, y: 0, width: 400, height: 600 })
});
{
"name": "disgusting-tooth-finalize-aw66c",
"productName": "disgusting-tooth-finalize-aw66c",
"description": "My Electron application description",
"keywords": [],
"main": "./main.js",
"version": "1.0.0",
"author": "khammond",
"scripts": {
"start": "electron ."
},
"dependencies": {},
"devDependencies": {
"electron": "999.999.999"
}
}
// All of the Node.js APIs are available in the preload process.
// It has the same sandbox as a Chrome extension.
window.addEventListener('DOMContentLoaded', () => {
const replaceText = (selector, text) => {
const element = document.getElementById(selector)
if (element) element.innerText = text
}
for (const type of ['chrome', 'node', 'electron']) {
replaceText(`${type}-version`, process.versions[type])
}
})
// This file is required by the index.html file and will
// be executed in the renderer process for that window.
// No Node.js APIs are available in this process because
// `nodeIntegration` is turned off. Use `preload.js` to
// selectively enable features needed in the rendering
// process.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment