Skip to content

Instantly share code, notes, and snippets.

@andreasdj
Created April 23, 2024 08:48
Show Gist options
  • Save andreasdj/8cebc0fe88155d5310930cfb3bcc25c7 to your computer and use it in GitHub Desktop.
Save andreasdj/8cebc0fe88155d5310930cfb3bcc25c7 to your computer and use it in GitHub Desktop.
WebContentsView_FullscreenPermissionHandling_Issue
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<meta http-equiv="Content-Security-Policy" content="default-src 'self'; script-src 'self'; style-src 'unsafe-inline';">
<style type="text/css">
body {
width: 98vw;
height: 98vh;
margin: 0;
padding: 1vh 1vw;
box-sizing: border-box;
}
section {
background: deeppink;
padding: 10px;
}
</style>
</head>
<body>
<h1>Sub view 1 (BrowserView)</h1>
<section>
<button id="enterFullcreen" type="button">Request fullscreen</button>
<button id="exitFullscreen" type="button">Exit fullscreen</button>
</section>
<script src="./renderer.js"></script>
</body>
</html>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<meta http-equiv="Content-Security-Policy" content="default-src 'self'; script-src 'self'; style-src 'unsafe-inline';">
<style type="text/css">
body {
width: 98vw;
height: 98vh;
margin: 0;
padding: 1vh 1vw;
box-sizing: border-box;
}
section {
background: deepskyblue;
padding: 10px;
}
</style>
</head>
<body>
<h1>Sub view 2 (WebContentsView)</h1>
<section>
<button id="enterFullcreen" type="button">Request fullscreen</button>
<button id="exitFullscreen" type="button">Exit fullscreen</button>
</section>
<script src="./renderer.js"></script>
</body>
</html>
<!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'; style-src 'unsafe-inline';">
<title>Hello Main Window!</title>
<style type="text/css">
section {
background: limegreen;
padding: 10px;
}
</style>
</head>
<body>
<h1>Hello Main Window!</h1>
We are using Node.js <span id="node-version"></span>,
Chromium <span id="chrome-version"></span>,
and Electron <span id="electron-version"></span>.
<section>
<button id="enterFullcreen" type="button">Request fullscreen</button>
<button id="exitFullscreen" type="button">Exit fullscreen</button>
</section>
<!-- You can also require other files to run in this process -->
<script src="./renderer.js"></script>
</body>
</html>
// Modules to control application life and create native browser window
const { app, BrowserWindow, session, BrowserView, WebContentsView } = require('electron')
const path = require('node:path')
function createViews (session) {
// Create the browser window.
const mainWindow = new BrowserWindow({
width: 800,
height: 600,
webPreferences: {
preload: path.join(__dirname, 'preload.js'),
session
}
})
// and load the index.html of the app.
mainWindow.loadFile('index.html')
// Create a child view using the deprecated BrowserView (WebContentsView shim)
const childView1 = new BrowserView({
webPreferences: {
session,
}
})
// Set the bounds for the child view
childView1.setBounds({
width: 800,
height: 200,
x: 0,
y: 200,
})
// Add the child view to the main window
mainWindow.addBrowserView(childView1);
childView1.webContents.loadFile('childView1.html')
// Create another child view using WebContentsView
const childView2 = new WebContentsView({
webPreferences: {
session,
}
})
// Set the bounds for the child view
childView2.setBounds({
width: 800,
height: 200,
x: 0,
y: 400,
})
// Add the child view to the main window
mainWindow.contentView.addChildView(childView2);
childView2.webContents.loadFile('childView2.html')
// Open DevTools.
// childView1.webContents.openDevTools({ mode:'detach' });
// childView2.webContents.openDevTools({ mode:'detach' });
// mainWindow.webContents.openDevTools()
}
// This method will be called when Electron has finished
// initialization and is ready to create browser windows.
// Some APIs can only be used after this event occurs.
app.whenReady().then(() => {
try {
const defaultSession = session.defaultSession;
defaultSession.setPermissionRequestHandler((webContents, permission, callback, details) => {
console.log('setPermissionRequestHandler', permission, details)
callback(true);
});
createViews(defaultSession)
app.on('activate', function () {
// On macOS it's common to re-create a window in the app when the
// dock icon is clicked and there are no other windows open.
if (BrowserWindow.getAllWindows().length === 0) createViews(defaultSession)
})
} catch(error) {console.log(error)}
})
// Quit when all windows are closed, except on macOS. There, it's common
// for applications and their menu bar to stay active until the user quits
// explicitly with Cmd + Q.
app.on('window-all-closed', function () {
if (process.platform !== 'darwin') app.quit()
})
// In this file you can include the rest of your app's specific main process
// code. You can also put them in separate files and require them here.
{
"name": "selective-love-flood-in3fv",
"productName": "selective-love-flood-in3fv",
"description": "My Electron application description",
"keywords": [],
"main": "./main.js",
"version": "1.0.0",
"author": "aj3621",
"scripts": {
"start": "electron ."
},
"dependencies": {},
"devDependencies": {
"electron": "31.0.0-alpha.2"
}
}
/**
* The preload script runs before `index.html` is loaded
* in the renderer. It has access to web APIs as well as
* Electron's renderer process modules and some polyfilled
* Node.js functions.
*
* https://www.electronjs.org/docs/latest/tutorial/sandbox
*/
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 loaded via the <script> tag in 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 and
* `contextIsolation` is turned on. Use the contextBridge API in `preload.js`
* to expose Node.js functionality from the main process.
*/
window.addEventListener('DOMContentLoaded', () => {
document.getElementById('enterFullcreen').addEventListener('click', (e) => {
console.log(e.currentTarget)
e.currentTarget.parentElement.requestFullscreen();
});
document.getElementById('exitFullscreen').addEventListener('click', () => {
document.exitFullscreen();
});
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment