const electron = require('electron')
const desktopCapturer = electron.desktopCapturer
const electronScreen = electron.screen
const shell = electron.shell

const fs = require('fs')
const os = require('os')
const path = require('path')

const screenshot = document.getElementById('screen-shot')

screenshot.addEventListener('click', function (event) {
  const thumbSize = determineScreenShotSize()
  desktopCapturer.getSources({types: ['screen'], thumbnailSize: thumbSize}, function (error, sources) {
    if (error) return console.log(error)

    sources.forEach(function (source) {
      if (source.name === "Entire screen") {
        var screenshotPath = path.join(os.tmpdir(), 'screenshot.png')

        fs.writeFile(screenshotPath, source.thumbnail.toPng(), function (error) {
          if (error) return console.log(error)
          shell.openItem(screenshotPath)
          var message = 'Wrote screenshot to: ' + screenshotPath
          document.getElementById('screenshot-path').innerHTML = message
        })
      }
    })
  })
})

function determineScreenShotSize () {
  var screenSize = electronScreen.getPrimaryDisplay().workAreaSize
  var maxDimension = Math.max(screenSize.width, screenSize.height)
  return {
    width: maxDimension * window.devicePixelRatio,
    height: maxDimension * window.devicePixelRatio
  }
}

@@ -395,36 +395,24 @@
 )%0A  
-desktopCapturer.getSources(%7B
+let options = %7B 
 type
@@ -450,17 +450,56 @@
 humbSize
-%7D
+ %7D%0A%0A  desktopCapturer.getSources(options
 , functi
@@ -628,17 +628,17 @@
 ame === 
-%22
+'
 Entire s
@@ -646,9 +646,9 @@
 reen
-%22
+'
 ) %7B%0A

Clean up