<!DOCTYPE html> | |
<html> | |
<head> | |
<meta charset="UTF-8"> | |
<title>Context Bridge Example</title> | |
<link rel="stylesheet" type="text/css" href="./styles.css"> | |
</head> | |
<body> | |
<main class="app"> | |
<button onclick="window.app.setFullscreen(true)">Enter fullscreen</button> | |
<button onclick="window.app.setFullscreen(false)">Exit fullscreen</button> | |
</main> | |
</body> | |
</html> |
// Modules to control application life and create native browser window | |
const {app, BrowserWindow, ipcMain} = require('electron') | |
const path = require('path') | |
function createWindow () { | |
// 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) | |
} | |
}) | |
} | |
// 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', createWindow) | |
// 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() | |
} | |
}) |
const { ipcRenderer, contextBridge } = require('electron') | |
contextBridge.exposeInMainWorld( | |
'app', | |
{ | |
setFullscreen: (flag) => ipcRenderer.invoke('setFullscreen', flag), | |
} | |
) |
// Empty |
.app { | |
text-align: center; | |
margin: 10rem; | |
} |
This comment has been minimized.
This comment has been minimized.
Oh sorry, that line and comment was added by default by Electron Fiddle and I didn't notice it. You can't use |
This comment has been minimized.
This comment has been minimized.
Well now that does make sense indeed, otherwise, why disabling nodeIntegration if it is to be able to require node extensions ^^' Anyway, thank you for coming back to me on this! I wanted to make sure this was a typo and not something I didn't understand! |
This comment has been minimized.
This comment has been minimized.
@matt-allan preload.js :
global.ts :
main.ts :
in the renderer process App.tsx :
); If I set in App.tsx :
I get the same error and the window is not rendered : What am I doing wrongly? How to solve the problem? |
This comment has been minimized.
Hi Matt,
Thank you very much for the example and your article "Safer Electron apps with ContextBridge" was of help for my current endeavour with electron
There is one thing though that I do not get yet, how do you successfully use "require('./renderer.js')" in the index.html script section?
For me it always fails at execution time with the following error message:
Uncaught ReferenceError: require is not defined
at index.html:21
What I am trying to understand is; how do you successfully use the nodejs directive "require" in the inline script tag?
Other than this little detail, very nice explanation,
Thanks,
Alex.