-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcapture.js
86 lines (75 loc) · 2.79 KB
/
capture.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
var videoElement = document.createElement("video");
videoElement.setAttribute("id", "video");
videoElement.style.position = "absolute";
videoElement.style.top = '10px';
videoElement.style.right = 0;
videoElement.style.height = '200px';
videoElement.style.width = '400px';
var canvasElement = document.createElement("canvas");
canvasElement.setAttribute("id", "canvas-element");
setInterval(()=>{
chrome.storage.sync.get(['tabs_count'], function(result) {
if(result.tabs_count > 1) {
alert("Multiple tabs detected. Only Assessment tab should be open!");
}
});
},5000)
window.addEventListener("load", function () {
const element = document.getElementsByTagName("body")[0];
element.appendChild(videoElement)
element.appendChild(canvasElement)
const videoObject = document.getElementById("video");
const canvas = document.getElementById("canvas-element");
canvas.style.display = "none";
const context = canvas.getContext("2d");
let interval = 3000;
navigator.mediaDevices
.getUserMedia({
video: true,
audio: true,
})
.then((stream) => {
videoObject.srcObject = stream;
videoObject.addEventListener("loadedmetadata", () => {
videoObject.play();
videoObject.muted = true;
var myFunction = function(){
clearInterval(interval_timer);
captureImage();
interval_timer = setInterval(myFunction, interval);
// console.log(interval,interval_timer)
}
var interval_timer = setInterval(myFunction,interval);
});
})
.catch(function(err) {
console.log("Audio and Video permission denied");
});
function captureImage() {
context.drawImage(videoObject, 0, 0, canvas.width, canvas.height);
const imageData = canvas.toDataURL("image/png");
chrome.storage.sync.get(['userid'], function(result) {
sendImageToServer(result.userid, imageData);
});
}
function sendImageToServer(userid, imageData) {
var xhr = new XMLHttpRequest();
xhr.open('POST', 'https://mr-proctor.onrender.com/upload-image');
xhr.setRequestHeader('Content-Type', 'application/json;charset=UTF-8');
xhr.onload = function () {
if (xhr.status === 200) {
var response = JSON.parse(xhr.responseText);
interval = response.interval;
} else {
console.log('Request failed. Returned status of ' + xhr.status);
}
};
xhr.onerror = function () {
console.log('Request failed. Please check your network connection.');
};
xhr.send(JSON.stringify({
userid: userid,
image: imageData,
}));
}
});