Skip to content

Instantly share code, notes, and snippets.

@pmadhur
Created May 8, 2024 06:35
Show Gist options
  • Save pmadhur/b60062b61ff9441e842d182f25ea13d8 to your computer and use it in GitHub Desktop.
Save pmadhur/b60062b61ff9441e842d182f25ea13d8 to your computer and use it in GitHub Desktop.
DevTools dock right with WCO enabled
<!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'">
</head>
<body>
<h1>Browser View 1</h1>
<button id="btnOpenDevTools">Open DevTools</button>
<!-- You can also require other files to run in this process -->
<script src="./browser-view-1.js"></script>
</body>
</html>
// Empty
document.addEventListener('DOMContentLoaded', () => {
document.getElementById('btnOpenDevTools').addEventListener('click', () => {
mainProcess.send('data-to-main', { action: 'open-dev-tools', id: 1 });
});
});
<!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'">
</head>
<body>
<h1>Browser View 2</h1>
<button id="btnOpenDevTools">Open DevTools</button>
<!-- You can also require other files to run in this process -->
<script src="./browser-view-2.js"></script>
</body>
</html>
// Empty
document.addEventListener('DOMContentLoaded', () => {
document.getElementById('btnOpenDevTools').addEventListener('click', () => {
mainProcess.send('data-to-main', { action: 'open-dev-tools', id: 2 });
});
});
<!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>
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>
<style>
html,
body {
padding: 0px;
margin: 0px;
width: calc(100% - 150px);
overflow: hidden;
}
</style>
</body>
</html>
// Modules to control application life and create native browser window
const { app, BrowserWindow, BrowserView, ipcMain } = require('electron')
const path = require('node:path')
function createWindow() {
const titleBarHeight = 40;
// Create the browser window.
const mainWindow = new BrowserWindow({
width: 800,
height: 600,
webPreferences: {
preload: path.join(__dirname, 'preload.js')
},
titleBarStyle: 'hidden',
titleBarOverlay: {
height: titleBarHeight
}
})
// and load the index.html of the app.
mainWindow.loadFile('index.html')
const windowRect = mainWindow.getContentBounds();
const browserViewHeight = Math.round((windowRect.height - titleBarHeight) / 2);
const browserView1 = new BrowserView({
webPreferences: {
preload: path.join(__dirname, 'preload.js'),
partition: `persist:browser-view-1`,
sandbox: true
}
});
browserView1.webContents.loadFile('browser-view-1.html');
browserView1.setAutoResize({ width: true, height: true });
browserView1.setBounds({ x: 0, y: titleBarHeight, width: windowRect.width, height: browserViewHeight });
mainWindow.addBrowserView(browserView1);
const browserView2 = new BrowserView({
webPreferences: {
preload: path.join(__dirname, 'preload.js'),
partition: `persist:browser-view-2`,
sandbox: true
}
});
browserView2.webContents.loadFile('browser-view-2.html');
browserView2.setAutoResize({ width: true, height: true });
browserView2.setBounds({ x: 0, y: titleBarHeight + browserViewHeight, width: windowRect.width, height: browserViewHeight });
mainWindow.addBrowserView(browserView2);
ipcMain.on('data-to-main', (event, eventData) => {
switch (eventData.action) {
case 'get-title-bar-height':
event.returnValue = titleBarHeight;
break;
case 'open-dev-tools':
const browserView = eventData.id === 1 ? browserView1 : browserView2;
browserView.webContents.openDevTools({ mode: 'right' });
break;
case 'get-versions':
event.returnValue = process.versions;
break;
}
});
// Open the DevTools.
// 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(() => {
createWindow()
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) createWindow()
})
})
// 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": "teeny-truth-induce-7qjc4",
"productName": "teeny-truth-induce-7qjc4",
"description": "My Electron application description",
"keywords": [],
"main": "./main.js",
"version": "1.0.0",
"author": "pmadh",
"scripts": {
"start": "electron ."
},
"dependencies": {},
"devDependencies": {
"electron": "30.0.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
*/
const { contextBridge, ipcRenderer } = require('electron')
const mainProcess = {
send: (channel, data) => {
ipcRenderer.send(channel, data);
},
sendSync: (channel, data) => {
return ipcRenderer.sendSync(channel, data);
},
receive: (channel, func) => {
ipcRenderer.on(channel, func);
}
}
contextBridge.exposeInMainWorld('mainProcess', mainProcess);
/**
* 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', () => {
const titleBarHeight = mainProcess.sendSync('data-to-main', { action: 'get-title-bar-height' });
document.body.style.height = `${titleBarHeight}px`;
const replaceText = (selector, text) => {
const element = document.getElementById(selector)
if (element) element.innerText = text
}
const versions = mainProcess.sendSync('data-to-main', { action: 'get-versions' });
for (const type of ['chrome', 'node', 'electron']) {
replaceText(`${type}-version`, versions[type])
}
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment