Created
September 20, 2021 23:56
-
-
Save VerteDinde/9f7f2f62959cec7bc01a98a0b00fa4fa to your computer and use it in GitHub Desktop.
Child Window Node Integration Test
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<html lang="en"> | |
<head> | |
<title>BrowserWindow</title> | |
</head> | |
<body> | |
<h1>Hello from your BrowserWindow!</h1> | |
Parent window. | |
<button onClick="window.open('', '', 'top=500,left=200,frame=false,transparent=true,nodeIntegration=true');">Open Child About Blank</button> | |
<p> | |
<button onClick="window.open('index.html', '', 'top=500,left=200,frame=false,transparent=true,nodeIntegration=true');">Open Child Load HTML (nodeIntegration: true)</button> | |
<button onClick="window.open('index.html', '', 'top=500,left=200,frame=false,transparent=true,nodeIntegration=false');">Open Child Load HTML (nodeIntegration: false)</button> | |
</p> | |
<!-- <main class="app"> | |
<button onclick="window.app.openChildWindow()">Open Child Window</button> --> | |
</main> | |
</body> | |
</html> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Modules to control application life and create native browser window | |
const {app, BrowserWindow, ipcMain} = require('electron') | |
const path = require('path') | |
function createMainWindow () { | |
// Create the browser window. | |
const mainWindow = new BrowserWindow({ | |
width: 800, | |
height: 600, | |
webPreferences: { | |
allowRunningInsecureContent: false, | |
contextIsolation: true, | |
enableRemoteModule: false, | |
nodeIntegration: false, | |
sandbox: true, | |
preload: path.join(app.getAppPath(), 'preload.js'), | |
} | |
}) | |
// and load the index.html of the app. | |
mainWindow.loadFile('index.html') | |
// Open the DevTools. | |
// mainWindow.webContents.openDevTools() | |
ipcMain.handle('setFullscreen', (event, flag) => { | |
if (mainWindow) { | |
mainWindow.setFullScreen(flag) | |
} | |
}) | |
ipcMain.handle('openChildWindow', (event) => { | |
const childWindow = new BrowserWindow({ | |
show: false, | |
frame: false, | |
transparent: true, | |
alwaysOnTop: true, | |
autoHideMenuBar: true, | |
resizable: false, | |
useContentSize: true, | |
fullscreenable: false, | |
enableLargerThanScreen: true, | |
hasShadow: true, | |
skipTaskbar: true, | |
}); | |
}) | |
} | |
// 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.on('ready', createMainWindow) | |
// Quit when all windows are closed. | |
app.on('window-all-closed', function () { | |
// On OS X it is common for applications and their menu bar | |
// to stay active until the user quits explicitly with Cmd + Q | |
if (process.platform !== 'darwin') { | |
app.quit() | |
} | |
}) | |
app.on('activate', function () { | |
// On OS X 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() | |
} | |
}) | |
function createNewWindow(options) { | |
// Create the browser window. | |
let newWindow = null; | |
if (options) { | |
newWindow = new BrowserWindow({options}); | |
} else { | |
newWindow = new BrowserWindow({ | |
show: false, | |
frame: false, | |
transparent: true, | |
alwaysOnTop: true, | |
autoHideMenuBar: true, | |
resizable: false, | |
useContentSize: true, | |
fullscreenable: false, | |
enableLargerThanScreen: true, | |
hasShadow: false, | |
skipTaskbar: true, | |
webPreferences: { | |
contextIsolation: true, | |
nodeIntegration: false, | |
sandbox: true, | |
preload: path.join(app.getAppPath(), 'preload.js'), | |
} | |
}) | |
} | |
// and load the index.html of the app. | |
newWindow.loadFile('index.html') | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"name": "relieved-record-evaluate-uxi73", | |
"productName": "relieved-record-evaluate-uxi73", | |
"description": "My Electron application description", | |
"keywords": [], | |
"main": "./main.js", | |
"version": "1.0.0", | |
"author": "khammond", | |
"scripts": { | |
"start": "electron ." | |
}, | |
"dependencies": {}, | |
"devDependencies": { | |
"electron": "15.0.0-beta.6" | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const { ipcRenderer, contextBridge } = require('electron') | |
contextBridge.exposeInMainWorld( | |
'app', | |
{ | |
setFullscreen: (flag) => ipcRenderer.invoke('setFullscreen', flag), | |
openChildWindow: () => ipcRenderer.invoke('openChildWindow'), | |
openNativeChildWindow: () => ipcRenderer.invoke('openNativeChildWindow'), | |
} | |
) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Empty |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* Empty */ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment