docs: enable contextIsolation in fiddles (#39613)

This commit is contained in:
Milan Burda 2023-08-29 21:52:16 +02:00 committed by GitHub
parent 2e79f34c84
commit 9280b79112
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
61 changed files with 206 additions and 233 deletions

View file

@ -83,9 +83,6 @@ ipcMain.on('save-dialog', (event) => {
</div>
</div>
<script>
// You can also require other files to run in this process
require("./renderer.js");
</script>
<script src="renderer.js"></script>
</body>
</html>

View file

@ -1,5 +1,6 @@
// Modules to control application life and create native browser window
const { app, BrowserWindow, ipcMain, dialog, shell } = require('electron/main')
const path = require('node:path')
// Keep a global reference of the window object, if you don't, the window will
// be closed automatically when the JavaScript object is garbage collected.
@ -11,8 +12,7 @@ function createWindow () {
width: 800,
height: 600,
webPreferences: {
contextIsolation: false,
nodeIntegration: true
preload: path.join(__dirname, 'preload.js')
}
})
@ -59,14 +59,12 @@ app.on('activate', function () {
}
})
ipcMain.on('save-dialog', event => {
ipcMain.handle('save-dialog', async () => {
const options = {
title: 'Save an Image',
filters: [{ name: 'Images', extensions: ['jpg', 'png', 'gif'] }]
}
dialog.showSaveDialog(options, filename => {
event.sender.send('saved-file', filename)
})
return (await dialog.showSaveDialog(options)).filePath
})
// In this file you can include the rest of your app's specific main process

View file

@ -0,0 +1,5 @@
const { contextBridge, ipcRenderer } = require('electron/renderer')
contextBridge.exposeInMainWorld('electronAPI', {
saveDialog: () => ipcRenderer.invoke('save-dialog')
})

View file

@ -1,12 +1,6 @@
const { ipcRenderer } = require('electron/renderer')
const saveBtn = document.getElementById('save-dialog')
saveBtn.addEventListener('click', event => {
ipcRenderer.send('save-dialog')
})
ipcRenderer.on('saved-file', (event, path) => {
if (!path) path = 'No path'
saveBtn.addEventListener('click', async () => {
const path = await window.electronAPI.saveDialog()
document.getElementById('file-saved').innerHTML = `Path selected: ${path}`
})