-
Notifications
You must be signed in to change notification settings - Fork 0
/
insider.js
176 lines (148 loc) · 4.25 KB
/
insider.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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
console.log("insider.js installed")
// The socket connection
const ws = new WebSocket("ws://localhost:3000")
// HTML elements
const movie_player = document.getElementById("movie_player");
const video = movie_player.querySelector("video");
// Audio objects
const audioCtx = new AudioContext();
const elementSource = audioCtx.createMediaElementSource(video);
const streamDest = audioCtx.createMediaStreamDestination();
// The Recorder
const recorder = new MediaRecorder(streamDest.stream, { mimeType: "audio/webm;codecs=opus" });
// A custom timer
const timer = (() => {
/**
* @type Timer
*/
let interval = null;
return {
start() {
interval ??= setInterval(() => recorder.requestData(), 1000)
},
stop() {
clearInterval(interval)
interval = null
}
}
})()
// connect the video to the recorder
elementSource.connect(streamDest);
// Reset video in case it played too soon
setTimeout(() => {
video.pause()
video.currentTime = 0
attachEvents()
video.play()
}, 0);
ws.addEventListener("open", () => {
console.log("Websocket open");
});
// Globals
window.recorder = recorder
window.video = video
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Event handlers
function attachEvents() {
/**
* @param {BlobEvent} event
*/
recorder.ondataavailable = onRecordDataAvailable;
recorder.onstart = onRecordStart
recorder.onpause = onRecordPause;
recorder.onresume = onRecordResume
recorder.onstop = onRecordStop;
video.addEventListener("play", videoPlayHandler);
video.addEventListener("playing", videoPlayingHandler);
video.addEventListener("pause", videoPauseHandler);
video.addEventListener("ended", videoEndedHandler);
video.addEventListener("timeupdate", videoTimeUpdateHandler)
}
function dettachEvents() {
recorder.onstart = null
recorder.onresume = null
recorder.ondataavailable = null
recorder.onpause = null
video.removeEventListener("play", videoPlayHandler);
video.removeEventListener("playing", videoPlayingHandler);
video.removeEventListener("pause", videoPauseHandler);
video.removeEventListener("ended", videoEndedHandler);
video.removeEventListener("timeupdate", videoTimeUpdateHandler)
}
////////////////////////////////////
// Recorder handlers
function onRecordDataAvailable(event) {
const blob = event.data;
ws.send(blob);
}
function onRecordPause() {
SIGNAL("pause");
timer.stop();
recorder.requestData();
showDebugInfo()
}
function onRecordStart() {
SIGNAL("recording")
timer.start();
showDebugInfo()
}
function onRecordResume() {
timer.start()
showDebugInfo()
}
function onRecordStop() {
timer.stop();
showDebugInfo()
dettachEvents()
SIGNAL("end");
}
/////////////////////////////////
// Video handlers
function videoTimeUpdateHandler(_event) {
if (showingAds()) return;
SIGNAL("current_time", video.currentTime);
}
function videoPlayingHandler() {
// do not remove
if (showingAds()) {
// IMPORTANT: Do not add another skip_ad signal, this will trigger another click and pause the video
SIGNAL("skip_ad")
return;
}
SIGNAL("duration", video.duration)
recorder.state === "inactive" && recorder.start();
recorder.state === "paused" && recorder.resume();
}
function videoPlayHandler() {
if (showingAds()) return; // do not remove
recorder.state === "inactive" && recorder.start();
recorder.state === "paused" && recorder.resume();
}
function videoPauseHandler() {
if (showingAds()) return // do not remove
recorder.state !== "inactive" && recorder.pause();
};
function videoEndedHandler() {
if (showingAds()) return; // do not remove
recorder.state !== "inactive" && (recorder.stop());
};
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Utils
function showDebugInfo() {
const debug = {
ads: showingAds(),
recorder: recorder.state,
video: {
paused: video.paused,
ended: video.ended,
},
websocket: {
state: ws.readyState,
}
}
console.log(debug)
}
function showingAds() {
return movie_player.classList.contains("ad-showing");
}
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////