-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
83 lines (66 loc) · 2.42 KB
/
script.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
let mediaRecorder;
let screenStream;
let recordedChunks = [];
const startButton = document.getElementById('start');
const stopButton = document.getElementById('stop');
const downloadButton = document.getElementById('download');
const videoElement = document.getElementById('recording');
// Disable the stop and download buttons initially
stopButton.disabled = true;
downloadButton.disabled = true;
startButton.addEventListener('click', startRecording);
stopButton.addEventListener('click', stopRecording);
downloadButton.addEventListener('click', downloadRecording);
function startRecording() {
startButton.disabled = true;
stopButton.disabled = false;
recordedChunks = [];
// Check if the getDisplayMedia method is available
if (navigator.mediaDevices && navigator.mediaDevices.getDisplayMedia) {
navigator.mediaDevices.getDisplayMedia({ video: true, audio: true })
.then(stream => {
screenStream = stream;
mediaRecorder = new MediaRecorder(stream);
mediaRecorder.ondataavailable = function(event) {
if (event.data.size > 0) {
recordedChunks.push(event.data);
}
};
mediaRecorder.onstop = function() {
startButton.disabled = false;
stopButton.disabled = true;
downloadButton.disabled = false;
const videoBlob = new Blob(recordedChunks, { type: 'video/webm' });
videoElement.src = URL.createObjectURL(videoBlob);
// Stop both screen sharing and media recording streams
screenStream.getTracks().forEach(track => track.stop());
};
mediaRecorder.start();
})
.catch(err => {
console.error('Error accessing screen:', err);
});
} else {
alert('Screen recording is not supported on this device/browser.');
}
}
function stopRecording() {
startButton.disabled = false;
stopButton.disabled = true;
downloadButton.disabled = false;
mediaRecorder.stop();
}
function downloadRecording() {
const currentDate = new Date();
const dateString = currentDate.toISOString().replace(/:/g, "-").split(".")[0];
const fileName = `recordscreen.me-${dateString}.webm`;
const blob = new Blob(recordedChunks, { type: 'video/webm' });
const url = URL.createObjectURL(blob);
const a = document.createElement('a');
a.href = url;
a.download = fileName;
document.body.appendChild(a);
a.click();
document.body.removeChild(a);
URL.revokeObjectURL(url);
}