renderer.js 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637
  1. const { shell, ipcRenderer } = require('electron/renderer')
  2. const fs = require('node:fs').promises
  3. const os = require('node:os')
  4. const path = require('node:path')
  5. const screenshot = document.getElementById('screen-shot')
  6. const screenshotMsg = document.getElementById('screenshot-path')
  7. screenshot.addEventListener('click', async (event) => {
  8. screenshotMsg.textContent = 'Gathering screens...'
  9. const thumbSize = await determineScreenShotSize()
  10. const options = { types: ['screen'], thumbnailSize: thumbSize }
  11. const sources = await ipcRenderer.invoke('get-sources', options)
  12. for (const source of sources) {
  13. const sourceName = source.name.toLowerCase()
  14. if (sourceName === 'entire screen' || sourceName === 'screen 1') {
  15. const screenshotPath = path.join(os.tmpdir(), 'screenshot.png')
  16. await fs.writeFile(screenshotPath, source.thumbnail.toPNG())
  17. shell.openExternal(`file://${screenshotPath}`)
  18. const message = `Saved screenshot to: ${screenshotPath}`
  19. screenshotMsg.textContent = message
  20. }
  21. }
  22. })
  23. async function determineScreenShotSize () {
  24. const screenSize = await ipcRenderer.invoke('get-screen-size')
  25. const maxDimension = Math.max(screenSize.width, screenSize.height)
  26. return {
  27. width: maxDimension * window.devicePixelRatio,
  28. height: maxDimension * window.devicePixelRatio
  29. }
  30. }