|
| 1 | +const electron = require('electron') |
| 2 | +const desktopCapturer = electron.desktopCapturer |
| 3 | +const electronScreen = electron.screen |
| 4 | +const shell = electron.shell |
| 5 | + |
| 6 | +const fs = require('fs') |
| 7 | +const os = require('os') |
| 8 | +const path = require('path') |
| 9 | + |
| 10 | +const screenshot = document.getElementById('screen-shot') |
| 11 | + |
| 12 | +screenshot.addEventListener('click', function (event) { |
| 13 | + const thumbSize = determineScreenShotSize() |
| 14 | + desktopCapturer.getSources({types: ['screen'], thumbnailSize: thumbSize}, function (error, sources) { |
| 15 | + if (error) return console.log(error) |
| 16 | + |
| 17 | + sources.forEach(function (source) { |
| 18 | + if (source.name === "Entire screen") { |
| 19 | + var screenshotPath = path.join(os.tmpdir(), 'screenshot.png') |
| 20 | + |
| 21 | + fs.writeFile(screenshotPath, source.thumbnail.toPng(), function (error) { |
| 22 | + if (error) return console.log(error) |
| 23 | + shell.openItem(screenshotPath) |
| 24 | + }) |
| 25 | + } |
| 26 | + }) |
| 27 | + }) |
| 28 | +}) |
| 29 | + |
| 30 | +function determineScreenShotSize () { |
| 31 | + var screenSize = electronScreen.getPrimaryDisplay().workAreaSize |
| 32 | + var maxDimension = Math.max(screenSize.width, screenSize.height) |
| 33 | + return { |
| 34 | + width: maxDimension * window.devicePixelRatio, |
| 35 | + height: maxDimension * window.devicePixelRatio |
| 36 | + } |
| 37 | +} |
0 commit comments