Skip to content

Instantly share code, notes, and snippets.

@nornagon
Created July 9, 2021 17:37
Show Gist options
  • Save nornagon/925744c7c67347d67c5b29e91c31e37e to your computer and use it in GitHub Desktop.
Save nornagon/925744c7c67347d67c5b29e91c31e37e to your computer and use it in GitHub Desktop.
Electron Fiddle Gist
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Hello World!</title>
</head>
<body>
<h1>Hello World!</h1>
<video id="video" autoplay playsinline></video>
<script>
document.addEventListener('DOMContentLoaded', async () => {
const { ipcRenderer } = require('electron');
const sourceId = ipcRenderer.sendSync('get-media-source-id')
const stream = await navigator.mediaDevices.getUserMedia({
video: {
mandatory: {
chromeMediaSource: 'desktop',
chromeMediaSourceId: sourceId,
maxWidth: Math.min(screen.width, 1280),
maxHeight: Math.min(screen.height, 720),
},
},
audio: false,
});
const video = document.getElementById('video');
video.srcObject = stream;
})
</script>
</body>
</html>
// Modules to control application life and create native browser window
const {app, BrowserWindow, ipcMain} = require('electron')
ipcMain.on('get-media-source-id', (e) => {
e.returnValue = BrowserWindow.fromWebContents(e.sender).getMediaSourceId();
})
function createWindow () {
// Create the browser window.
const mainWindow = new BrowserWindow({
width: 800,
height: 600,
webPreferences: {
nodeIntegration: true,
contextIsolation: false,
}
})
mainWindow.loadFile('index.html')
}
app.whenReady().then(createWindow)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment