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 коммит произвёл GitHub
Родитель c4d417b6f6
Коммит 1ce2fdd63d
Не найден ключ, соответствующий данной подписи
Идентификатор ключа GPG: 4AEE18F83AFDEB23
2 изменённых файлов: 18 добавлений и 19 удалений

Просмотреть файл

@ -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,

Просмотреть файл

@ -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)
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')
sources.forEach((source) => {
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}`)
fs.writeFile(screenshotPath, source.thumbnail.toPNG(), (error) => {
if (error) return console.log(error)
shell.openExternal(`file://${screenshotPath}`)
const message = `Saved screenshot to: ${screenshotPath}`
screenshotMsg.textContent = message
})
}
})
})
const message = `Saved screenshot to: ${screenshotPath}`
screenshotMsg.textContent = message
}
}
})
async function determineScreenShotSize () {