Skip to content

Instantly share code, notes, and snippets.

@lukasbach
Last active October 11, 2022 10:04
Show Gist options
  • Save lukasbach/cffea4ec536b82d4b1d68585548ef19f to your computer and use it in GitHub Desktop.
Save lukasbach/cffea4ec536b82d4b1d68585548ef19f to your computer and use it in GitHub Desktop.
Electron desktopCapturer.getSources demo. To reproduce, install Electron Fiddle (https://www.electronjs.org/fiddle) and load the gist there.
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<!-- https://developer.mozilla.org/en-US/docs/Web/HTTP/CSP -->
<meta http-equiv="Content-Security-Policy" content="default-src 'self'; script-src 'self'; style-src 'self' 'unsafe-inline'">
<link href="./styles.css" rel="stylesheet">
<title>Hello World!</title>
</head>
<body>
Run this app from the terminal, and see available windows printed there.
<script src="./renderer.js"></script>
</body>
</html>
const {app, BrowserWindow, desktopCapturer} = require('electron')
const path = require('path')
function createWindow () {
const mainWindow = new BrowserWindow({
width: 800,
height: 600,
webPreferences: {
preload: path.join(__dirname, 'preload.js')
}
})
mainWindow.loadFile('index.html')
}
app.whenReady().then(() => {
createWindow()
app.on('activate', function () {
if (BrowserWindow.getAllWindows().length === 0) createWindow()
})
})
app.on('window-all-closed', function () {
if (process.platform !== 'darwin') app.quit()
})
try {
desktopCapturer.getSources({ types: ['window'] }).then(sources => {
console.log(sources.map(s => s.name))
})
}catch(e) {
console.log(e)
}
{
"name": "vast-cobweb-drip-opzuw",
"productName": "vast-cobweb-drip-opzuw",
"description": "My Electron application description",
"keywords": [],
"main": "./main.js",
"version": "1.0.0",
"author": "lbach",
"scripts": {
"start": "electron ."
},
"dependencies": {},
"devDependencies": {
"electron": "22.0.0-alpha.3"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment