Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

docs: enable contextIsolation in fiddles #39613

Merged
merged 1 commit into from
Aug 29, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 2 additions & 2 deletions docs/fiddles/features/web-bluetooth/preload.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
const { contextBridge, ipcRenderer } = require('electron/renderer')

contextBridge.exposeInMainWorld('electronAPI', {
cancelBluetoothRequest: (callback) => ipcRenderer.send('cancel-bluetooth-request', callback),
bluetoothPairingRequest: (callback) => ipcRenderer.on('bluetooth-pairing-request', callback),
cancelBluetoothRequest: () => ipcRenderer.send('cancel-bluetooth-request'),
bluetoothPairingRequest: (callback) => ipcRenderer.on('bluetooth-pairing-request', () => callback()),
bluetoothPairingResponse: (response) => ipcRenderer.send('bluetooth-pairing-response', response)
})
2 changes: 1 addition & 1 deletion docs/fiddles/ipc/pattern-3/preload.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
const { contextBridge, ipcRenderer } = require('electron/renderer')

contextBridge.exposeInMainWorld('electronAPI', {
handleCounter: (callback) => ipcRenderer.on('update-counter', callback)
handleCounter: (callback) => ipcRenderer.on('update-counter', () => callback())
})
5 changes: 1 addition & 4 deletions docs/fiddles/media/screenshot/take-screenshot/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,6 @@ <h3>Take a Screenshot</h3>
<p>Clicking the demo button will take a screenshot of your current screen and open it in your default viewer.</p>
</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>
41 changes: 32 additions & 9 deletions docs/fiddles/media/screenshot/take-screenshot/main.js
Original file line number Diff line number Diff line change
@@ -1,23 +1,46 @@
const { BrowserWindow, app, screen, ipcMain, desktopCapturer } = require('electron/main')
const { BrowserWindow, app, screen, ipcMain, desktopCapturer, shell } = require('electron/main')
const fs = require('node:fs').promises
const os = require('node:os')
const path = require('node:path')

let mainWindow = null

ipcMain.handle('get-screen-size', () => {
return screen.getPrimaryDisplay().workAreaSize
})
function determineScreenShotSize (devicePixelRatio) {
const screenSize = screen.getPrimaryDisplay().workAreaSize
const maxDimension = Math.max(screenSize.width, screenSize.height)
return {
width: maxDimension * devicePixelRatio,
height: maxDimension * devicePixelRatio
}
}

ipcMain.handle('get-sources', (event, options) => {
return desktopCapturer.getSources(options)
})
async function takeScreenshot (devicePixelRatio) {
const thumbSize = determineScreenShotSize(devicePixelRatio)
const options = { types: ['screen'], thumbnailSize: thumbSize }

const sources = await desktopCapturer.getSources(options)
for (const source of sources) {
const sourceName = source.name.toLowerCase()
if (sourceName === 'entire screen' || sourceName === 'screen 1') {
const screenshotPath = path.join(os.tmpdir(), 'screenshot.png')

await fs.writeFile(screenshotPath, source.thumbnail.toPNG())
shell.openExternal(`file://${screenshotPath}`)

return `Saved screenshot to: ${screenshotPath}`
}
}
}

ipcMain.handle('take-screenshot', (event, devicePixelRatio) => takeScreenshot(devicePixelRatio))

function createWindow () {
const windowOptions = {
width: 600,
height: 300,
title: 'Take a Screenshot',
webPreferences: {
contextIsolation: false,
nodeIntegration: true
preload: path.join(__dirname, 'preload.js')
}
}

Expand Down
5 changes: 5 additions & 0 deletions docs/fiddles/media/screenshot/take-screenshot/preload.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
const { contextBridge, ipcRenderer } = require('electron/renderer')

contextBridge.exposeInMainWorld('electronAPI', {
takeScreenshot: () => ipcRenderer.invoke('take-screenshot', window.devicePixelRatio)
})
32 changes: 1 addition & 31 deletions docs/fiddles/media/screenshot/take-screenshot/renderer.js
Original file line number Diff line number Diff line change
@@ -1,37 +1,7 @@
const { shell, ipcRenderer } = require('electron/renderer')

const fs = require('node:fs').promises
const os = require('node:os')
const path = require('node:path')

const screenshot = document.getElementById('screen-shot')
const screenshotMsg = document.getElementById('screenshot-path')

screenshot.addEventListener('click', async (event) => {
screenshotMsg.textContent = 'Gathering screens...'
const thumbSize = await determineScreenShotSize()
const options = { types: ['screen'], thumbnailSize: thumbSize }

const sources = await ipcRenderer.invoke('get-sources', options)
for (const source of sources) {
const sourceName = source.name.toLowerCase()
if (sourceName === 'entire screen' || sourceName === 'screen 1') {
const screenshotPath = path.join(os.tmpdir(), 'screenshot.png')

await fs.writeFile(screenshotPath, source.thumbnail.toPNG())
shell.openExternal(`file://${screenshotPath}`)

const message = `Saved screenshot to: ${screenshotPath}`
screenshotMsg.textContent = message
}
}
screenshotMsg.textContent = await window.electronAPI.takeScreenshot()
})

async function determineScreenShotSize () {
const screenSize = await ipcRenderer.invoke('get-screen-size')
const maxDimension = Math.max(screenSize.width, screenSize.height)
return {
width: maxDimension * window.devicePixelRatio,
height: maxDimension * window.devicePixelRatio
}
}
6 changes: 1 addition & 5 deletions docs/fiddles/menus/customize-menus/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -119,10 +119,6 @@ <h2>Create a context menu</h2>
</div>
</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>
4 changes: 2 additions & 2 deletions docs/fiddles/menus/customize-menus/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ const {
dialog,
autoUpdater
} = require('electron/main')
const path = require('node:path')

const menu = new Menu()
menu.append(new MenuItem({ label: 'Hello' }))
Expand Down Expand Up @@ -295,8 +296,7 @@ function createWindow () {
width: 800,
height: 600,
webPreferences: {
contextIsolation: false,
nodeIntegration: true
preload: path.join(__dirname, 'preload.js')
}
})

Expand Down
5 changes: 5 additions & 0 deletions docs/fiddles/menus/customize-menus/preload.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
const { contextBridge, ipcRenderer } = require('electron/renderer')

contextBridge.exposeInMainWorld('electronAPI', {
showContextMenu: () => ipcRenderer.send('show-context-menu')
})
4 changes: 1 addition & 3 deletions docs/fiddles/menus/customize-menus/renderer.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
const { ipcRenderer } = require('electron/renderer')

// Tell main process to show the menu when demo button is clicked
const contextMenuBtn = document.getElementById('context-menu')

contextMenuBtn.addEventListener('click', () => {
ipcRenderer.send('show-context-menu')
window.electronAPI.showContextMenu()
})
6 changes: 1 addition & 5 deletions docs/fiddles/menus/shortcuts/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,7 @@ function createWindow () {
// Create the browser window.
mainWindow = new BrowserWindow({
width: 800,
height: 600,
webPreferences: {
contextIsolation: false,
nodeIntegration: true
}
height: 600
})

globalShortcut.register('CommandOrControl+Alt+K', () => {
Expand Down
5 changes: 1 addition & 4 deletions docs/fiddles/native-ui/dialogs/error-dialog/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -73,9 +73,6 @@ <h5>Main Process</h5>
</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>
4 changes: 2 additions & 2 deletions docs/fiddles/native-ui/dialogs/error-dialog/main.js
Original file line number Diff line number Diff line change
@@ -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.
Expand All @@ -11,8 +12,7 @@ function createWindow () {
width: 800,
height: 600,
webPreferences: {
contextIsolation: false,
nodeIntegration: true
preload: path.join(__dirname, 'preload.js')
}
})

Expand Down
5 changes: 5 additions & 0 deletions docs/fiddles/native-ui/dialogs/error-dialog/preload.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
const { contextBridge, ipcRenderer } = require('electron/renderer')

contextBridge.exposeInMainWorld('electronAPI', {
openErrorDialog: () => ipcRenderer.send('open-error-dialog')
})
6 changes: 2 additions & 4 deletions docs/fiddles/native-ui/dialogs/error-dialog/renderer.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
const { ipcRenderer } = require('electron/renderer')

const errorBtn = document.getElementById('error-dialog')

errorBtn.addEventListener('click', event => {
ipcRenderer.send('open-error-dialog')
errorBtn.addEventListener('click', () => {
window.electronAPI.openErrorDialog()
})
5 changes: 1 addition & 4 deletions docs/fiddles/native-ui/dialogs/information-dialog/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -96,9 +96,6 @@ <h5>Main Process</h5>
</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>
10 changes: 4 additions & 6 deletions docs/fiddles/native-ui/dialogs/information-dialog/main.js
Original file line number Diff line number Diff line change
@@ -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.
Expand All @@ -11,8 +12,7 @@ function createWindow () {
width: 800,
height: 600,
webPreferences: {
contextIsolation: false,
nodeIntegration: true
preload: path.join(__dirname, 'preload.js')
}
})

Expand Down Expand Up @@ -59,16 +59,14 @@ app.on('activate', function () {
}
})

ipcMain.on('open-information-dialog', event => {
ipcMain.handle('open-information-dialog', async () => {
const options = {
type: 'info',
title: 'Information',
message: "This is an information dialog. Isn't it nice?",
buttons: ['Yes', 'No']
}
dialog.showMessageBox(options, index => {
event.sender.send('information-dialog-selection', index)
})
return (await dialog.showMessageBox(options)).response
})

// In this file you can include the rest of your app's specific main process
Expand Down
5 changes: 5 additions & 0 deletions docs/fiddles/native-ui/dialogs/information-dialog/preload.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
const { contextBridge, ipcRenderer } = require('electron/renderer')

contextBridge.exposeInMainWorld('electronAPI', {
openInformationDialog: () => ipcRenderer.invoke('open-information-dialog')
})
13 changes: 3 additions & 10 deletions docs/fiddles/native-ui/dialogs/information-dialog/renderer.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,7 @@
const { ipcRenderer } = require('electron/renderer')

const informationBtn = document.getElementById('information-dialog')

informationBtn.addEventListener('click', event => {
ipcRenderer.send('open-information-dialog')
})

ipcRenderer.on('information-dialog-selection', (event, index) => {
let message = 'You selected '
if (index === 0) message += 'yes.'
else message += 'no.'
informationBtn.addEventListener('click', async () => {
const index = await window.electronAPI.openInformationDialog()
const message = `You selected: ${index === 0 ? 'yes' : 'no'}`
document.getElementById('info-selection').innerHTML = message
})
Original file line number Diff line number Diff line change
Expand Up @@ -100,9 +100,6 @@ <h2>ProTip</h2>
</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>
20 changes: 7 additions & 13 deletions docs/fiddles/native-ui/dialogs/open-file-or-directory/main.js
Original file line number Diff line number Diff line change
@@ -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.
Expand All @@ -11,8 +12,7 @@ function createWindow () {
width: 800,
height: 600,
webPreferences: {
contextIsolation: false,
nodeIntegration: true
preload: path.join(__dirname, 'preload.js')
}
})

Expand Down Expand Up @@ -59,17 +59,11 @@ app.on('activate', function () {
}
})

ipcMain.on('open-file-dialog', event => {
dialog.showOpenDialog(
{
properties: ['openFile', 'openDirectory']
},
files => {
if (files) {
event.sender.send('selected-directory', files)
}
}
)
ipcMain.handle('open-file-dialog', async () => {
const options = {
properties: ['openFile', 'openDirectory']
}
return (await dialog.showOpenDialog(options)).filePaths
})

// In this file you can include the rest of your app's specific main process
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
const { contextBridge, ipcRenderer } = require('electron/renderer')

contextBridge.exposeInMainWorld('electronAPI', {
openFileDialog: () => ipcRenderer.invoke('open-file-dialog')
})
Original file line number Diff line number Diff line change
@@ -1,11 +1,6 @@
const { ipcRenderer } = require('electron/renderer')

const selectDirBtn = document.getElementById('select-directory')

selectDirBtn.addEventListener('click', event => {
ipcRenderer.send('open-file-dialog')
})

ipcRenderer.on('selected-directory', (event, path) => {
selectDirBtn.addEventListener('click', async () => {
const path = await window.electronAPI.openFileDialog()
document.getElementById('selected-file').innerHTML = `You selected: ${path}`
})
5 changes: 1 addition & 4 deletions docs/fiddles/native-ui/dialogs/save-dialog/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -83,9 +83,6 @@ <h5>Main Process</h5>
</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>