Skip to content

Commit f7885d9

Browse files
Colin TroisemaineColin Troisemaine
Colin Troisemaine
authored and
Colin Troisemaine
committed
Added basic screen capture
1 parent 9345414 commit f7885d9

File tree

4 files changed

+76
-22
lines changed

4 files changed

+76
-22
lines changed

electron_app/control_menu.html

+21-8
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,28 @@
11
<!DOCTYPE html>
2-
<html lang="en">
2+
<html>
33
<head>
44
<meta charset="UTF-8">
55
<!-- https://developer.mozilla.org/en-US/docs/Web/HTTP/CSP -->
6-
<meta http-equiv="Content-Security-Policy" content="default-src 'self'; script-src 'self'">
7-
<title>Hello World!</title>
6+
<meta http-equiv="Content-Security-Policy"
7+
content="img-src 'self' data: filesystem:; default-src 'self'; script-src 'self'; style-src 'self' 'unsafe-inline'; script-src-elem 'self' 'unsafe-eval' https://unpkg.com">
8+
<title>Live Desktop Translator</title>
89
</head>
10+
911
<body>
10-
<h1>Hello World!</h1>
11-
We are using Node.js <span id="node-version"></span>,
12-
Chromium <span id="chrome-version"></span>,
13-
and Electron <span id="electron-version"></span>.
12+
<div style="text-align: center;">
13+
<button id="startButton" class="button">Start</button>
14+
<button id="stopButton" class="button">Stop</button>
15+
16+
<br />
17+
<br />
18+
19+
<video width="800" height="450" autoplay></video>
20+
21+
<hr />
22+
23+
<button id="videoSelectButton" class="button">Choose a Video Source</button>
24+
25+
<script src="renderer.js"></script>
26+
</div>
1427
</body>
15-
</html>
28+
</html>

electron_app/main.js

+17-4
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,29 @@
1-
const { app, BrowserWindow } = require('electron')
1+
const { app, BrowserWindow, desktopCapturer, session } = require('electron')
22

33
const path = require('node:path')
44

55
const createWindow = () => {
66
const win = new BrowserWindow({
7-
width: 800,
8-
height: 600,
7+
width: 850,
8+
height: 700,
99
webPreferences: {
10-
preload: path.join(__dirname, 'preload.js')
10+
preload: path.join(__dirname, 'preload.js'),
11+
nodeIntegration: true,
12+
contextIsolation: false
1113
}
1214
})
1315

16+
session.defaultSession.setDisplayMediaRequestHandler((request, callback) => {
17+
desktopCapturer.getSources({ types: ['screen'] }).then((sources) => {
18+
// Grant access to the first screen found.
19+
callback({ video: sources[0] })
20+
})
21+
// If true, use the system picker if available.
22+
// Note: this is currently experimental. If the system picker
23+
// is available, it will be used and the media request handler
24+
// will not be invoked.
25+
}, { useSystemPicker: true })
26+
1427
win.loadFile('electron_app/control_menu.html')
1528
}
1629

electron_app/preload.js

-10
Original file line numberDiff line numberDiff line change
@@ -1,10 +0,0 @@
1-
window.addEventListener('DOMContentLoaded', () => {
2-
const replaceText = (selector, text) => {
3-
const element = document.getElementById(selector)
4-
if (element) element.innerText = text
5-
}
6-
7-
for (const dependency of ['chrome', 'node', 'electron']) {
8-
replaceText(`${dependency}-version`, process.versions[dependency])
9-
}
10-
})

electron_app/renderer.js

+38
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
const startButton = document.getElementById('startButton')
2+
const stopButton = document.getElementById('stopButton')
3+
const videoSelectButton = document.getElementById('videoSelectButton')
4+
5+
const video = document.querySelector('video')
6+
7+
startButton.addEventListener('click', () => {
8+
console.log("startButton")
9+
navigator.mediaDevices.getDisplayMedia({
10+
audio: true,
11+
video: {
12+
width: 800,
13+
height: 450,
14+
frameRate: 30
15+
}
16+
}).then(stream => {
17+
video.srcObject = stream
18+
video.onloadedmetadata = (e) => video.play()
19+
}).catch(e => console.log(e))
20+
})
21+
22+
stopButton.addEventListener('click', () => {
23+
console.log("stopButton")
24+
video.pause()
25+
})
26+
27+
// Get the available video sources
28+
const { desktopCapturer } = require('electron')
29+
videoSelectButton.addEventListener('click', () => {
30+
console.log("videoSelectButton")
31+
32+
const inputSources = desktopCapturer.getSources({
33+
types: ['window', 'screen']
34+
});
35+
36+
console.log(inputSources)
37+
38+
})

0 commit comments

Comments
 (0)