-
Notifications
You must be signed in to change notification settings - Fork 0
/
content.js
113 lines (94 loc) · 3.17 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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
console.log("Hi, I have been injected");
var recorder = null;
var videoId = null; // Variable to store the video ID
var recordedChunks = []; // Array to store the data chunks
function onAccessApproved(stream) {
// Make an initial API request to start recording and get the video ID
fetch('https://ovidotvideo.onrender.com/start_video', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({}),
})
.then((response) => response.json())
.then((data) => {
// console.log(data)
videoId = data.video_id; // Store the received video ID from the json response
console.log('Video recording started with ID:', videoId);
recorder = new MediaRecorder(stream);
recorder.start();
recorder.onstop = function () {
stream.getTracks().forEach(function (track) {
if (track.readyState === "live") {
track.stop();
}
});
// Create a blob from the recorded data chunks
const recordedBlob = new Blob(recordedChunks, { type: 'video/webm' });
// Save the recorded video to the API endpoint
saveRecordedVideo(recordedBlob, videoId);
recordedChunks = []; // Clear the array
// Redirect to a localhost URL for rendering or perform other actions
// redirectToLocalhost();
};
recorder.ondataavailable = function (event) {
if (event.data.size > 0) {
recordedChunks.push(event.data);
}
};
})
.catch((error) => {
console.log('Error starting recording:', error);
});
}
// Save the video blob to the API endpoint
function saveRecordedVideo(blob, videoId) {
const formData = new FormData();
formData.append('video', blob, 'screen-recording.webm'); // Video is the field name
fetch(`https://ovidotvideo.onrender.com/update_video/${videoId}`, {
method: 'POST',
body: formData,
})
.then((response) => {
if (response.ok) {
console.log('Video successfully sent to API');
} else {
console.error('Failed to send video to API:', response.statusText);
}
})
.catch((error) => {
console.log('Error sending video to API', error);
});
}
// Redirect to a localhost URL for rendering
function redirectToLocalhost() {
const localhostURL = 'http://localhost:3000/videoPlayback';
// Change the window location to the localhost URL
window.location.href = localhostURL;
}
chrome.runtime.onMessage.addListener((message, sender, sendResponse) => {
if (message.action === 'request_user_permission') {
console.log('Requesting recording permission');
sendResponse(`processed: ${message.action}`);
// Request the browser to grant permission for recording feature
navigator.mediaDevices.getDisplayMedia({
audio: true,
video: {
width: 9999999999,
height: 9999999999,
},
}).then((stream) => {
onAccessApproved(stream);
});
}
if (message.action === 'stopvideo') {
console.log('Stopping video');
sendResponse(`processed: ${message.action}`);
if (!recorder) {
return console.log('No recorder');
} else {
recorder.stop();
}
}
});