docs: fix take-screenshot fiddle to use desktopCapturer in main.js (#39420)

This commit is contained in:
Milan Burda 2023-08-10 10:53:23 +02:00 committed by GitHub
parent c4d417b6f6
commit 1ce2fdd63d
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 18 additions and 19 deletions

View file

@ -1,4 +1,4 @@
const { BrowserWindow, app, screen, ipcMain } = require('electron')
const { BrowserWindow, app, screen, ipcMain, desktopCapturer } = require('electron')
let mainWindow = null
@ -6,6 +6,10 @@ ipcMain.handle('get-screen-size', () => {
return screen.getPrimaryDisplay().workAreaSize
})
ipcMain.handle('get-sources', (event, options) => {
return desktopCapturer.getSources(options)
})
function createWindow () {
const windowOptions = {
width: 600,

View file

@ -1,6 +1,6 @@
const { desktopCapturer, shell, ipcRenderer } = require('electron')
const { shell, ipcRenderer } = require('electron')
const fs = require('node:fs')
const fs = require('node:fs').promises
const os = require('node:os')
const path = require('node:path')
@ -12,24 +12,19 @@ screenshot.addEventListener('click', async (event) => {
const thumbSize = await determineScreenShotSize()
const options = { types: ['screen'], thumbnailSize: thumbSize }
desktopCapturer.getSources(options, (error, sources) => {
if (error) return console.log(error)
sources.forEach((source) => {
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')
fs.writeFile(screenshotPath, source.thumbnail.toPNG(), (error) => {
if (error) return console.log(error)
await fs.writeFile(screenshotPath, source.thumbnail.toPNG())
shell.openExternal(`file://${screenshotPath}`)
const message = `Saved screenshot to: ${screenshotPath}`
screenshotMsg.textContent = message
})
}
})
})
}
})
async function determineScreenShotSize () {