-
Notifications
You must be signed in to change notification settings - Fork 1
/
bigbro.js
151 lines (134 loc) · 5.16 KB
/
bigbro.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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
let BigBro = {
// This should not be modified outside of the init method.
data: {
user: "",
server: "",
events: ["click", "dblclick", "mousedown", "mouseup",
"mouseenter", "mouseout", "wheel", "loadstart", "loadend", "load",
"unload", "reset", "submit", "scroll", "resize",
"cut", "copy", "paste", "select", "keydown", "keyup",
"ontouchstart", "ontouchmove", "ontouchend", "ontouchcancel"
],
},
eventQueue: [],
captureQueue: [],
// State associated with recording user.
startedRecording: false,
captureStream: null,
captureInterval: 1000,
// init must be called with the user and the server, and optionally a list of
// events to listen to globally.
init: function (user, server, events) {
this.data.user = user;
this.data.server = server;
this.data.events = events || this.data.events;
let protocol = 'ws://';
if (window.location.protocol === 'https:') {
protocol = 'wss://';
}
this.wsEvent = new WebSocket(protocol + this.data.server + "/event");
this.wsRecord = new WebSocket(protocol + this.data.server + "/capture");
let self = this;
this.wsEvent.onopen = function () {
for (let i = 0; i < self.data.events.length; i++) {
window.addEventListener(self.data.events[i], function (e) {
self.log(e, self.data.events[i]);
})
}
};
this.wsRecord.onopen = function () {
window.setInterval(self.capture, self.captureInterval, self)
};
return this
},
// log logs an event with a specified method name (normally the actual event name).
log: function (e, method, comment) {
let event = {
target: e.target.tagName,
name: e.target.name,
id: e.target.id,
method: method,
location: window.location.href,
time: new Date().toISOString(),
x: e.x,
y: e.y,
screenWidth: window.innerWidth,
screenHeight: window.innerHeight,
actor: this.data.user
};
if (method === "keydown" || method === "keyup") {
// Which key was actually pressed?
event.comment = e.code;
}
if (method === "paste" || method === "cut" || method === "copy") {
// Seems like we can only get data for paste events.
event.comment = e.clipboardData.getData("text/plain")
}
if (method === "wheel") {
// Strength of the wheel rotation.
event.comment = e.deltaY.toString();
}
if (comment != null) {
event.comment = comment;
}
if (this.wsEvent.readyState !== 1) {
this.eventQueue.push(event);
return false;
}
while (this.eventQueue.length > 0) {
this.wsEvent.send(JSON.stringify(this.eventQueue.pop()))
}
this.wsEvent.send(JSON.stringify(event));
},
startCaptureWhenClicked: async function (elementId, interval) {
let self = this;
document.getElementById(elementId).addEventListener("click", function () {
try {
if (!self.startedRecording) {
self.startedRecording = true;
self.captureStream = navigator.mediaDevices.getDisplayMedia({video: {cursor: "always"}, displaySurface: "browser", browserWindow: false});
}
} catch (err) {
console.error("Error: " + err);
}
});
},
capture: function (self) {
if (self.captureStream === null || self.captureStream === undefined) {
return
}
if (!self.startedRecording) {
return
}
const canvas = document.createElement("canvas");
const context = canvas.getContext("2d");
const video = document.createElement("video");
try {
self.captureStream.then((stream) => {
video.srcObject = stream;
video.play().then(() => {
canvas.setAttribute("height", window.innerHeight);
canvas.setAttribute("width", window.innerWidth);
context.drawImage(video, 0, 0, window.innerWidth, window.innerHeight);
let blob = canvas.toDataURL("image/png");
console.log(blob);
let capture = {
actor: self.data.user,
time: new Date().toISOString(),
data: blob,
};
if (self.wsRecord.readyState !== 1) {
self.captureQueue.push(capture);
return false;
}
while (self.captureQueue.length > 0) {
self.captureQueue.send(JSON.stringify(self.captureQueue.pop()))
}
self.wsRecord.send(JSON.stringify(capture))
});
})
} catch (err) {
console.error("Error: " + err);
}
}
};