-
Notifications
You must be signed in to change notification settings - Fork 0
/
content.js
69 lines (56 loc) · 1.94 KB
/
content.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
console.log("Hello, I'm injected!");
function onAccessApproved(stream) {
let recorder = new MediaRecorder(stream);
recorder.start();
recorder.onstop = () => {
stream.getTracks().forEach(track => {
if (track.readyState === "live") {
track.stop();
}
});
};
recorder.ondataavailable = event => {
let recordedBlob = event.data;
let url = URL.createObjectURL(recordedBlob);
let a = document.createElement("a");
a.style.display = "none";
a.href = url;
a.download = "screen-recording.webm";
document.body.appendChild(a);
a.click();
document.body.removeChild(a);
URL.revokeObjectURL(url);
};
}
chrome.runtime.onMessage.addListener((message, sender, sendResponse) => {
try {
if (message.action === "request_recording") {
console.log("Requesting recording");
sendResponse(`Processed: ${message.action}`);
navigator.mediaDevices.getDisplayMedia({
video: {
width: 9999999999,
height: 9999999999
},
audio: true
}).then(stream => {
onAccessApproved(stream);
}).catch(error => {
console.error("Error getting display media:", error);
sendResponse(`Error: ${error.message}`);
});
}
if (message.action === "stopvideo") {
console.log("Stopping video");
sendResponse(`Processed: ${message.action}`);
if (!recorder) {
console.log("No recorder found");
return;
}
recorder.stop();
}
} catch (error) {
console.error("Unexpected error:", error);
sendResponse(`Error: ${error.message}`);
}
});