docs: fix take-screenshot fiddle to use desktopCapturer in main.js (#39420)
This commit is contained in:
parent
c4d417b6f6
commit
1ce2fdd63d
2 changed files with 18 additions and 19 deletions
|
@ -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 () {
|
||||
|
|
Loading…
Reference in a new issue