From fe192aee2da0e5f14275977927a09c9a7cf9b222 Mon Sep 17 00:00:00 2001 From: David Sanders Date: Wed, 10 Jan 2024 05:30:41 -0800 Subject: [PATCH] chore: update show me examples (#1527) --- static/show-me/app/main.js | 2 +- static/show-me/autoupdater/main.js | 2 +- static/show-me/browserview/main.js | 8 ++--- static/show-me/browserwindow/main.js | 2 +- static/show-me/clipboard/main.js | 4 +-- static/show-me/clipboard/preload.js | 2 +- static/show-me/contenttracing/main.js | 16 +++++----- static/show-me/cookies/main.js | 37 +++++++++++++---------- static/show-me/crashreporter/main.js | 12 ++++---- static/show-me/debugger/main.js | 2 +- static/show-me/desktopcapturer/main.js | 6 ++-- static/show-me/desktopcapturer/preload.js | 3 +- static/show-me/dialog/main.js | 19 ++++++------ static/show-me/globalshortcut/main.js | 2 +- static/show-me/inapppurchase/main.js | 2 +- static/show-me/ipc/main.js | 6 ++-- static/show-me/ipc/preload.js | 2 +- static/show-me/menu/main.js | 2 +- static/show-me/nativeimage/main.js | 2 +- static/show-me/net/main.js | 2 +- static/show-me/notification/main.js | 6 ++-- static/show-me/powermonitor/main.js | 6 +--- static/show-me/powersaveblocker/main.js | 2 +- static/show-me/screen/main.js | 10 ++---- static/show-me/screen/preload.js | 2 +- static/show-me/session/main.js | 4 +-- static/show-me/shell/main.js | 4 +-- static/show-me/shell/preload.js | 2 +- static/show-me/systempreferences/main.js | 2 +- static/show-me/touchbar/main.js | 2 +- static/show-me/tray/main.js | 2 +- static/show-me/webcontents/main.js | 2 +- static/show-me/webframe/main.js | 6 ++-- static/show-me/webframe/preload.js | 2 +- 34 files changed, 84 insertions(+), 101 deletions(-) diff --git a/static/show-me/app/main.js b/static/show-me/app/main.js index 01b0305468..0f253ba056 100644 --- a/static/show-me/app/main.js +++ b/static/show-me/app/main.js @@ -5,7 +5,7 @@ // For more info, see: // https://electronjs.org/docs/api/app -const { app } = require('electron') +const { app } = require('electron/main') app.whenReady().then(() => console.log('The app is now ready for action')) diff --git a/static/show-me/autoupdater/main.js b/static/show-me/autoupdater/main.js index bc1dc746b3..c01c6e9c8e 100644 --- a/static/show-me/autoupdater/main.js +++ b/static/show-me/autoupdater/main.js @@ -3,7 +3,7 @@ // For more info, see: // https://electronjs.org/docs/api/auto-updater -const { app, autoUpdater } = require('electron') +const { app, autoUpdater } = require('electron/main') app.whenReady().then(() => { const server = 'https://your-deployment-url.com' diff --git a/static/show-me/browserview/main.js b/static/show-me/browserview/main.js index 518af235a7..6b017dde40 100644 --- a/static/show-me/browserview/main.js +++ b/static/show-me/browserview/main.js @@ -6,7 +6,7 @@ // https://electronjs.org/docs/api/browser-view // In the main process. -const { BrowserView, BrowserWindow, app } = require('electron') +const { BrowserView, BrowserWindow, app } = require('electron/main') app.whenReady().then(() => { let win = new BrowserWindow({ width: 800, height: 600 }) @@ -14,11 +14,7 @@ app.whenReady().then(() => { win = null }) - const view = new BrowserView({ - webPreferences: { - nodeIntegration: false - } - }) + const view = new BrowserView() win.setBrowserView(view) view.setBounds({ x: 0, y: 0, width: 300, height: 300 }) diff --git a/static/show-me/browserwindow/main.js b/static/show-me/browserwindow/main.js index 4778bb804a..bcb413d5ca 100644 --- a/static/show-me/browserwindow/main.js +++ b/static/show-me/browserwindow/main.js @@ -4,7 +4,7 @@ // For more info, see: // https://electronjs.org/docs/api/browser-window -const { app, BrowserWindow } = require('electron') +const { app, BrowserWindow } = require('electron/main') const windows = [] diff --git a/static/show-me/clipboard/main.js b/static/show-me/clipboard/main.js index c821acce74..4481e44636 100644 --- a/static/show-me/clipboard/main.js +++ b/static/show-me/clipboard/main.js @@ -3,8 +3,8 @@ // For more info, see: // https://electronjs.org/docs/api/clipboard -const { app, BrowserWindow, ipcMain, clipboard } = require('electron') -const path = require('path') +const { app, BrowserWindow, ipcMain, clipboard } = require('electron/main') +const path = require('node:path') ipcMain.handle('clipboard:readText', () => { return clipboard.readText() diff --git a/static/show-me/clipboard/preload.js b/static/show-me/clipboard/preload.js index 7ea394192e..31ce721451 100644 --- a/static/show-me/clipboard/preload.js +++ b/static/show-me/clipboard/preload.js @@ -1,4 +1,4 @@ -const { contextBridge, ipcRenderer } = require('electron') +const { contextBridge, ipcRenderer } = require('electron/renderer') contextBridge.exposeInMainWorld('clipboard', { readText: () => ipcRenderer.invoke('clipboard:readText'), diff --git a/static/show-me/contenttracing/main.js b/static/show-me/contenttracing/main.js index 3b06594a2e..a0ded6be08 100644 --- a/static/show-me/contenttracing/main.js +++ b/static/show-me/contenttracing/main.js @@ -8,7 +8,7 @@ // For more info, see: // https://electronjs.org/docs/api/content-tracing -const { app, contentTracing } = require('electron') +const { app, contentTracing } = require('electron/main') app.whenReady().then(() => { const options = { @@ -16,13 +16,13 @@ app.whenReady().then(() => { traceOptions: 'record-until-full,enable-sampling' } - contentTracing.startRecording(options, () => { + contentTracing.startRecording(options).then(() => { console.log('Tracing started') - - setTimeout(() => { - contentTracing.stopRecording('', (path) => { - console.log('Tracing data recorded to ' + path) - }) - }, 5000) }) + + setTimeout(() => { + contentTracing.stopRecording().then((path) => { + console.log('Tracing data recorded to ' + path) + }) + }, 5000) }) diff --git a/static/show-me/cookies/main.js b/static/show-me/cookies/main.js index 649ca96bab..3f8bdaf9ae 100644 --- a/static/show-me/cookies/main.js +++ b/static/show-me/cookies/main.js @@ -3,36 +3,41 @@ // For more info, see: // https://electronjs.org/docs/api/cookies -const { app, BrowserWindow, session } = require('electron') +const { app, BrowserWindow, session } = require('electron/main') app.whenReady().then(() => { const mainWindow = new BrowserWindow({ height: 600, - width: 600, - webPreferences: { - nodeIntegration: false - } + width: 600 }) // Once the window has finished loading, let's check out // the cookies - mainWindow.webContents.on('did-finish-load', () => { + mainWindow.webContents.on('did-finish-load', async () => { // Query all cookies. - session.defaultSession.cookies.get({}, (error, cookies) => { - console.log(error, cookies) - }) + try { + const cookies = await session.defaultSession.cookies.get({}) + console.log(cookies) + } catch (error) { + console.error(error) + } // Query all cookies associated with a specific url. - session.defaultSession.cookies.get({ url: 'http://www.github.com' }, (error, cookies) => { - console.log(error, cookies) - }) + try { + const cookies = await session.defaultSession.cookies.get({ url: 'http://www.github.com' }) + console.log(cookies) + } catch (error) { + console.error(error) + } // Set a cookie with the given cookie data; // may overwrite equivalent cookies if they exist. - const cookie = { url: 'http://www.github.com', name: 'dummy_name', value: 'dummy' } - session.defaultSession.cookies.set(cookie, (error) => { - if (error) console.error(error) - }) + try { + const cookie = { url: 'http://www.github.com', name: 'dummy_name', value: 'dummy' } + await session.defaultSession.cookies.set(cookie) + } catch (error) { + console.error(error) + } }) mainWindow.loadURL('https://electronjs.org') diff --git a/static/show-me/crashreporter/main.js b/static/show-me/crashreporter/main.js index 653919d42b..a06e1239e8 100644 --- a/static/show-me/crashreporter/main.js +++ b/static/show-me/crashreporter/main.js @@ -3,12 +3,14 @@ // For more info, see: // https://electronjs.org/docs/api/crash-reporter -const { app, BrowserWindow, crashReporter } = require('electron') -const path = require('path') +const { app, BrowserWindow, crashReporter } = require('electron/main') +const path = require('node:path') crashReporter.start({ productName: 'YourName', - companyName: 'YourCompany', + globalExtra: { + _companyName: 'YourCompany' + }, submitURL: 'https://your-domain.com/url-to-submit', uploadToServer: true }) @@ -18,14 +20,12 @@ app.whenReady().then(() => { height: 600, width: 600, webPreferences: { - nodeIntegration: false, // default in Electron >= 5 - contextIsolation: true, // default in Electron >= 12 preload: path.join(__dirname, 'preload.js') } }) mainWindow.loadFile('index.html') - mainWindow.webContents.on('crashed', () => { + mainWindow.webContents.on('render-process-gone', () => { console.log('Window crashed!') }) }) diff --git a/static/show-me/debugger/main.js b/static/show-me/debugger/main.js index eca1082f81..bfe6fad0ee 100644 --- a/static/show-me/debugger/main.js +++ b/static/show-me/debugger/main.js @@ -6,7 +6,7 @@ // For more info, see: // https://electronjs.org/docs/api/debugger -const { app, BrowserWindow } = require('electron') +const { app, BrowserWindow } = require('electron/main') app.whenReady().then(() => { const mainWindow = new BrowserWindow({ height: 600, width: 600 }) diff --git a/static/show-me/desktopcapturer/main.js b/static/show-me/desktopcapturer/main.js index 88c0ee1c0c..c72963db29 100644 --- a/static/show-me/desktopcapturer/main.js +++ b/static/show-me/desktopcapturer/main.js @@ -4,16 +4,14 @@ // For more info, see: // https://electronjs.org/docs/api/desktop-capturer -const { app, BrowserWindow, desktopCapturer } = require('electron') -const path = require('path') +const { app, BrowserWindow, desktopCapturer } = require('electron/main') +const path = require('node:path') app.whenReady().then(() => { const mainWindow = new BrowserWindow({ height: 600, width: 600, webPreferences: { - nodeIntegration: false, // default in Electron >= 5 - contextIsolation: true, // default in Electron >= 12 preload: path.join(__dirname, 'preload.js') } }) diff --git a/static/show-me/desktopcapturer/preload.js b/static/show-me/desktopcapturer/preload.js index 5606388b42..1b321d0419 100644 --- a/static/show-me/desktopcapturer/preload.js +++ b/static/show-me/desktopcapturer/preload.js @@ -1,5 +1,4 @@ -const { desktopCapturer } = require('electron') -const { ipcRenderer } = require('electron') +const { desktopCapturer, ipcRenderer } = require('electron/renderer') // The following example shows how to capture video from // the screen. It also grabs each window, so you could diff --git a/static/show-me/dialog/main.js b/static/show-me/dialog/main.js index c0f15785d2..f306a9357a 100644 --- a/static/show-me/dialog/main.js +++ b/static/show-me/dialog/main.js @@ -3,23 +3,24 @@ // For more info, see: // https://electronjs.org/docs/api/dialog -const { app, BrowserWindow, dialog } = require('electron') +const { app, BrowserWindow, dialog } = require('electron/main') -app.whenReady().then(() => { +app.whenReady().then(async () => { const mainWindow = new BrowserWindow({ height: 600, width: 600 }) // Show an "Open File" dialog and attempt to open // the chosen file in our window. - dialog.showOpenDialog(mainWindow, { - properties: ['openFile'] - }).then(result => { - if (result.canceled) { + try { + const { filePaths, canceled } = await dialog.showOpenDialog(mainWindow, { + properties: ['openFile'] + }) + if (canceled) { console.log('Dialog was canceled') } else { - const file = result.filePaths[0] + const file = filePaths[0] mainWindow.loadURL(`file://${file}`) } - }).catch(err => { + } catch (err) { console.log(err) - }) + } }) diff --git a/static/show-me/globalshortcut/main.js b/static/show-me/globalshortcut/main.js index cde5213175..7c8206739a 100644 --- a/static/show-me/globalshortcut/main.js +++ b/static/show-me/globalshortcut/main.js @@ -13,7 +13,7 @@ // https://electronjs.org/docs/api/accelerator // https://electronjs.org/docs/api/global-shortcut -const { app, globalShortcut } = require('electron') +const { app, globalShortcut } = require('electron/main') app.whenReady().then(() => { // Register a 'CommandOrControl+Y' shortcut listener. diff --git a/static/show-me/inapppurchase/main.js b/static/show-me/inapppurchase/main.js index 162effb966..8249a1c148 100644 --- a/static/show-me/inapppurchase/main.js +++ b/static/show-me/inapppurchase/main.js @@ -6,7 +6,7 @@ // For more info, see: // https://electronjs.org/docs/api/in-app-purchase -const { app, inAppPurchase } = require('electron') +const { app, inAppPurchase } = require('electron/main') app.whenReady().then(() => { // Can the user can make a payment? diff --git a/static/show-me/ipc/main.js b/static/show-me/ipc/main.js index ccb61ae47e..71d52a0da2 100644 --- a/static/show-me/ipc/main.js +++ b/static/show-me/ipc/main.js @@ -5,16 +5,14 @@ // https://electronjs.org/docs/api/ipc-main // https://electronjs.org/docs/api/ipc-renderer -const { app, BrowserWindow, ipcMain } = require('electron') -const path = require('path') +const { app, BrowserWindow, ipcMain } = require('electron/main') +const path = require('node:path') app.whenReady().then(() => { const mainWindow = new BrowserWindow({ height: 600, width: 600, webPreferences: { - nodeIntegration: false, // default in Electron >= 5 - contextIsolation: true, // default in Electron >= 12 preload: path.join(__dirname, 'preload.js') } }) diff --git a/static/show-me/ipc/preload.js b/static/show-me/ipc/preload.js index 2009fd98c6..5109203a6a 100644 --- a/static/show-me/ipc/preload.js +++ b/static/show-me/ipc/preload.js @@ -1,4 +1,4 @@ -const { ipcRenderer } = require('electron') +const { ipcRenderer } = require('electron/renderer') // prints "pong" console.log(ipcRenderer.sendSync('synchronous-message', 'ping')) diff --git a/static/show-me/menu/main.js b/static/show-me/menu/main.js index 5386bf4429..b59274aa6b 100644 --- a/static/show-me/menu/main.js +++ b/static/show-me/menu/main.js @@ -3,7 +3,7 @@ // For more info, see: // https://electronjs.org/docs/api/menu -const { app, BrowserWindow, Menu } = require('electron') +const { app, BrowserWindow, Menu } = require('electron/main') app.whenReady().then(() => { const mainWindow = new BrowserWindow({ height: 600, width: 600 }) diff --git a/static/show-me/nativeimage/main.js b/static/show-me/nativeimage/main.js index 78877a1549..ca8a79479c 100644 --- a/static/show-me/nativeimage/main.js +++ b/static/show-me/nativeimage/main.js @@ -3,7 +3,7 @@ // For more info, see: // https://electronjs.org/docs/api/native-image -const { app, Tray, nativeImage } = require('electron') +const { app, Tray, nativeImage } = require('electron/main') let trayIcon = null diff --git a/static/show-me/net/main.js b/static/show-me/net/main.js index eca1ffec64..dfe3d75389 100644 --- a/static/show-me/net/main.js +++ b/static/show-me/net/main.js @@ -8,7 +8,7 @@ // For more info, see: // https://electronjs.org/docs/api/net -const { app, net } = require('electron') +const { app, net } = require('electron/main') app.whenReady().then(() => { const request = net.request('https://github.com') diff --git a/static/show-me/notification/main.js b/static/show-me/notification/main.js index 984756a56e..c7967fc467 100644 --- a/static/show-me/notification/main.js +++ b/static/show-me/notification/main.js @@ -4,16 +4,14 @@ // https://electronjs.org/docs/api/notification // https://electronjs.org/docs/tutorial/notifications -const { app, BrowserWindow, Notification } = require('electron') -const path = require('path') +const { app, BrowserWindow, Notification } = require('electron/main') +const path = require('node:path') app.whenReady().then(() => { const mainWindow = new BrowserWindow({ height: 600, width: 600, webPreferences: { - nodeIntegration: false, // default in Electron >= 5 - contextIsolation: true, // default in Electron >= 12 preload: path.join(__dirname, 'preload.js') } }) diff --git a/static/show-me/powermonitor/main.js b/static/show-me/powermonitor/main.js index ad342b5493..d54bfd4b1a 100644 --- a/static/show-me/powermonitor/main.js +++ b/static/show-me/powermonitor/main.js @@ -3,13 +3,9 @@ // For more info, see: // https://electronjs.org/docs/api/power-monitor -const { app } = require('electron') +const { app, powerMonitor } = require('electron/main') app.whenReady().then(() => { - // We cannot require the "ready" module until - // the app is ready - const { powerMonitor } = require('electron') - powerMonitor.on('suspend', () => { console.log('The system is going to sleep') }) diff --git a/static/show-me/powersaveblocker/main.js b/static/show-me/powersaveblocker/main.js index ac99c1b103..744a8b779b 100644 --- a/static/show-me/powersaveblocker/main.js +++ b/static/show-me/powersaveblocker/main.js @@ -3,7 +3,7 @@ // For more info, see: // https://electronjs.org/docs/api/power-save-blocker -const { app, powerSaveBlocker } = require('electron') +const { app, powerSaveBlocker } = require('electron/main') app.whenReady().then(() => { const id = powerSaveBlocker.start('prevent-display-sleep') diff --git a/static/show-me/screen/main.js b/static/show-me/screen/main.js index 2245dad41a..f87573f860 100644 --- a/static/show-me/screen/main.js +++ b/static/show-me/screen/main.js @@ -3,14 +3,10 @@ // For more info, see: // https://electronjs.org/docs/api/screen -const { app, BrowserWindow, ipcMain } = require('electron') -const path = require('path') +const { app, BrowserWindow, ipcMain, screen } = require('electron/main') +const path = require('node:path') app.whenReady().then(() => { - // We cannot require the screen module until the - // app is ready - const { screen } = require('electron') - // Create a window that fills the screen's available // work area. const primaryDisplay = screen.getPrimaryDisplay() @@ -20,8 +16,6 @@ app.whenReady().then(() => { width, height, webPreferences: { - nodeIntegration: false, // default in Electron >= 5 - contextIsolation: true, // default in Electron >= 12 preload: path.join(__dirname, 'preload.js') } }) diff --git a/static/show-me/screen/preload.js b/static/show-me/screen/preload.js index 3772ddc93e..a5689a67a5 100644 --- a/static/show-me/screen/preload.js +++ b/static/show-me/screen/preload.js @@ -1,4 +1,4 @@ -const { ipcRenderer } = require('electron') +const { ipcRenderer } = require('electron/renderer') window.addEventListener('DOMContentLoaded', () => { ipcRenderer diff --git a/static/show-me/session/main.js b/static/show-me/session/main.js index 28a3836174..f76e325a0e 100644 --- a/static/show-me/session/main.js +++ b/static/show-me/session/main.js @@ -3,7 +3,7 @@ // For more info, see: // https://electronjs.org/docs/api/session -const { app, session } = require('electron') +const { app, session } = require('electron/main') app.whenReady().then(() => { const { defaultSession } = session @@ -16,7 +16,7 @@ app.whenReady().then(() => { console.log(defaultSession.getUserAgent()) // Cache Size - defaultSession.getCacheSize((result) => { + defaultSession.getCacheSize().then((result) => { console.log(result) }) }) diff --git a/static/show-me/shell/main.js b/static/show-me/shell/main.js index 497b2e5afd..8f73975bff 100644 --- a/static/show-me/shell/main.js +++ b/static/show-me/shell/main.js @@ -3,8 +3,8 @@ // For more info, see: // https://electronjs.org/docs/api/shell -const { app, BrowserWindow, ipcMain, shell } = require('electron') -const path = require('path') +const { app, BrowserWindow, ipcMain, shell } = require('electron/main') +const path = require('node:path') app.whenReady().then(() => { const mainWindow = new BrowserWindow({ diff --git a/static/show-me/shell/preload.js b/static/show-me/shell/preload.js index 137a5860d6..7d5e8dffa1 100644 --- a/static/show-me/shell/preload.js +++ b/static/show-me/shell/preload.js @@ -1,4 +1,4 @@ -const { contextBridge, ipcRenderer } = require('electron') +const { contextBridge, ipcRenderer } = require('electron/renderer') contextBridge.exposeInMainWorld( 'electron', diff --git a/static/show-me/systempreferences/main.js b/static/show-me/systempreferences/main.js index 0f0109987c..6abc77462f 100644 --- a/static/show-me/systempreferences/main.js +++ b/static/show-me/systempreferences/main.js @@ -3,7 +3,7 @@ // For more info, see: // https://electronjs.org/docs/api/system-preferences#systempreferences -const { app, systemPreferences } = require('electron') +const { app, systemPreferences } = require('electron/main') app.whenReady().then(() => { // This module let's us access various system preferences. diff --git a/static/show-me/touchbar/main.js b/static/show-me/touchbar/main.js index ce7385021c..d2c20bd441 100644 --- a/static/show-me/touchbar/main.js +++ b/static/show-me/touchbar/main.js @@ -3,7 +3,7 @@ // For more info, see: // https://electronjs.org/docs/api/touch-bar -const { app, BrowserWindow, TouchBar } = require('electron') +const { app, BrowserWindow, TouchBar } = require('electron/main') const { TouchBarLabel, TouchBarButton, TouchBarSpacer } = TouchBar app.whenReady().then(() => { diff --git a/static/show-me/tray/main.js b/static/show-me/tray/main.js index e64a6a8b0f..283f675bd9 100644 --- a/static/show-me/tray/main.js +++ b/static/show-me/tray/main.js @@ -3,7 +3,7 @@ // For more info, see: // https://electronjs.org/docs/api/tray -const { app, Tray, Menu, nativeImage } = require('electron') +const { app, Tray, Menu, nativeImage } = require('electron.main') let tray diff --git a/static/show-me/webcontents/main.js b/static/show-me/webcontents/main.js index c6d2aee724..88cb037e76 100644 --- a/static/show-me/webcontents/main.js +++ b/static/show-me/webcontents/main.js @@ -3,7 +3,7 @@ // For more info, see: // https://electronjs.org/docs/api/web-contents -const { app, BrowserWindow, webContents } = require('electron') +const { app, BrowserWindow, webContents } = require('electron/main') app.whenReady().then(() => { const mainWindow = new BrowserWindow({ height: 600, width: 600 }) diff --git a/static/show-me/webframe/main.js b/static/show-me/webframe/main.js index 92ef81a365..630577d9e1 100644 --- a/static/show-me/webframe/main.js +++ b/static/show-me/webframe/main.js @@ -3,16 +3,14 @@ // For more info, see: // https://electronjs.org/docs/api/web-frame -const { app, BrowserWindow } = require('electron') -const path = require('path') +const { app, BrowserWindow } = require('electron/main') +const path = require('node:path') app.whenReady().then(() => { const mainWindow = new BrowserWindow({ width: 600, height: 600, webPreferences: { - nodeIntegration: false, // default in Electron >= 5 - contextIsolation: true, // default in Electron >= 12 preload: path.join(__dirname, 'preload.js') } }) diff --git a/static/show-me/webframe/preload.js b/static/show-me/webframe/preload.js index 0a78ab761c..d0c57d5686 100644 --- a/static/show-me/webframe/preload.js +++ b/static/show-me/webframe/preload.js @@ -1,4 +1,4 @@ -const { webFrame } = require('electron') +const { webFrame } = require('electron/renderer') setInterval(() => { // A random number