Skip to content

Instantly share code, notes, and snippets.

@VerteDinde
Created September 17, 2024 21:49
Show Gist options
  • Save VerteDinde/19563b207541c160bf88eca58137cd29 to your computer and use it in GitHub Desktop.
Save VerteDinde/19563b207541c160bf88eca58137cd29 to your computer and use it in GitHub Desktop.
System Picker Test
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<meta http-equiv="Content-Security-Policy" content="default-src 'self'; script-src 'self'">
<title>Hello World!</title>
</head>
<body>
<h1>Hello World!</h1>
<button id="startButton" class="button">Start</button>
<button id="stopButton" class="button">Stop</button>
<button id="pauseButton" class="button">Pause</button>
<video width="320" height="240" autoplay></video>
<script src="./renderer.js"></script>
</body>
</html>
// Modules to control application life and create native browser window
const { app, BrowserWindow, desktopCapturer, session } = require('electron')
const path = require('node: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()
session.defaultSession.setDisplayMediaRequestHandler((req, callback) => {
desktopCapturer.getSources({ types: ['screen', 'window'] }).then((sources) => {
callback({ video: sources[0] })
}).catch((err) => {
console.log('Error: ', err);
})
// If we should use the system picker
// Note: this is currently experimental
}, { useSystemPicker: true })
app.on('activate', function () {
if (BrowserWindow.getAllWindows().length === 0) createWindow()
})
})
app.on('window-all-closed', function () {
if (process.platform !== 'darwin') app.quit()
})
{
"name": "useSystemPicker",
"productName": "useSystemPicker",
"description": "My Electron application description",
"keywords": [],
"main": "./main.js",
"version": "1.0.0",
"author": "khammond",
"scripts": {
"start": "electron ."
},
"dependencies": {},
"devDependencies": {
"electron": "32.1.0"
}
}
window.addEventListener('DOMContentLoaded', () => {
const replaceText = (selector, text) => {
const element = document.getElementById(selector)
if (element) element.innerText = text
}
for (const type of ['chrome', 'node', 'electron']) {
replaceText(`${type}-version`, process.versions[type])
}
})
// renderer.js
const startButton = document.getElementById('startButton')
const stopButton = document.getElementById('stopButton')
const video = document.querySelector('video')
startButton.addEventListener('click', () => {
console.log('inside displayMedia')
navigator.mediaDevices.getDisplayMedia({
audio: true,
video: {
width: 320,
height: 240,
frameRate: 30,
// displaySurface: 'no_preference'
}
}).then(stream => {
video.srcObject = stream;
video.onloadedmetadata = (e) => video.play();
}).catch(e => console.log("Failed in getDisplayMedia", e))
})
pauseButton.addEventListener('click', () => {
video.pause()
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment