-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTwitchVODEnhancer.user.js
292 lines (262 loc) · 9.95 KB
/
TwitchVODEnhancer.user.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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
// ==UserScript==
// @name TwitchVODEnhancer
// @author sooqua
// @namespace https://github.com/sooqua/
// @downloadURL https://github.com/sooqua/TwitchVODEnhancer/raw/master/TwitchVODEnhancer.user.js
// @version 0.4
// @match *://*.twitch.tv/*
// @run-at document-start
// @grant GM_addStyle
// ==/UserScript==
(function() {
'use strict';
const client_id = 'ENTER_YOUR_CLIENT_ID',
canvas_width = 2500,
canvas_height = 1,
slider_height = 2.6,
slider_height_unit = 'em',
step = 60000, // msec.
auto_zoom = 1; // width of one step (%), non-zero values override the 'zoom' value
let zoom = 3;
const gradient = [
[
0,
[0, 0, 0]
],
[
25,
[60, 100, 90]
],
[
30,
[132, 220, 198]
],
[
33,
[165, 255, 214]
],
[
35,
[255, 222, 158]
],
[
85,
[255, 166, 158]
],
[
100,
[255, 104, 107]
]
];
const slider_half_height = slider_height / 2;
let steps_data_mc = [],
steps_data_ts = [];
let observer;
async function init() {
await initOn(document);
observer = new MutationObserver(function(mutations) {
mutations.forEach(function(mutation) {
mutation.addedNodes.forEach(async function(node) {
if (node instanceof HTMLElement) {
await initOn(node);
}
});
});
});
observer.observe(document.body, {childList: true, subtree: true});
}
async function initOn(base) {
let slider = base.querySelector('.js-player-slider');
if (!slider) return;
let slider_handle = base.querySelector('.ui-slider-handle');
if (!slider_handle) return;
observer.disconnect();
let vid_id = /twitch.tv\/videos\/(\d+)/.exec(window.location.href)[1];
let r = await getJson('https://api.twitch.tv/kraken/videos/' + vid_id + '?client_id=' + client_id),
vid_start = new Date(r.recorded_at).getTime(),
vid_length = r.length * 1000,
vid_end = vid_start + vid_length,
step_width = Math.round(step / vid_length * canvas_width);
if (auto_zoom) {
zoom = (auto_zoom / (step_width / canvas_width * 100)).clamp(1, 100);
}
GM_addStyle(`
.player-seek {
top: 0px !important;
}
.canvasWrapper {
transform: translateZ(0) !important;
overflow: hidden !important;
}
.js-player-slider:before {
display: none !important;
}
.js-player-slider > .ui-slider-range {
pointer-events: none !important;
z-index: 1 !important;
background: rgba(169, 145, 212, .5) !important;
height: ${slider_height + slider_height_unit} !important;
top: 0px !important;
transition: initial !important;
}
.js-player-slider > .ui-slider-handle {
pointer-events: none !important;
width: .1em !important;
height: ${slider_height + slider_height_unit} !important;
background: black !important;
border: .1em dotted white !important;
margin-left: 0em !important;
top: 0em !important;
border-radius: initial !important;
transition: initial !important;
}
.player-slider--roundhandle .ui-slider-handle:before {
display: none !important;
}
.player-slider__popup-container {
box-shadow: none !important;
background: hsla(0,0%,0%,.5) !important;
}
.player-slider__muted-segments {
pointer-events: none !important;
height: ${slider_half_height + slider_height_unit} !important;
top: ${slider_half_height + slider_height_unit} !important;
}
.player-slider__muted {
pointer-events: none !important;
height: ${slider_half_height + slider_height_unit} !important;
}
.sliderCanvas:hover {
transform: scale(${zoom}, 1) !important;
}`);
let wrapper = document.createElement('div');
wrapper.className = 'canvasWrapper';
let c = document.createElement('canvas');
c.className = 'sliderCanvas';
c.width = canvas_width;
c.height = canvas_height;
c.style.width = '100%';
c.style.height = slider_height + slider_height_unit;
wrapper.appendChild(c);
slider.appendChild(wrapper);
let sheet;
c.addEventListener('mousemove', function(e) {
let r = wrapper.getBoundingClientRect(),
m = (e.pageX - r.left) / r.width * 100;
c.style.transformOrigin = m + '% center 0px';
let m_h = (parseFloat(slider_handle.style.left) * zoom - m * zoom + m).clamp(0, 100);
let s = `
.ui-slider-handle {
left: ${m_h}% !important;
}
.ui-slider-range {
width: ${m_h}% !important;
}`;
let muted_bars = document.querySelectorAll('.player-slider__muted');
for (let i = 0, l = muted_bars.length; i < l; ++i) {
let m_b = (parseFloat(muted_bars[i].style.left) * zoom - m * zoom + m).clamp(0, 100);
s += `
.js-muted-segments-container > span:nth-child(${i + 1}) {
left: ${m_b}% !important;
transform: scale(${zoom}, 1) !important;
transform-origin: left !important;
}`;
}
sheet = setStyle(s, sheet);
});
c.addEventListener('mouseout', function() {
sheet = setStyle('', sheet);
});
let last_step_ts = vid_start,
curr_step_mc = 0,
ctx = c.getContext('2d');
ctx.fillStyle = 'rgba(0, 0, 0, .5)';
ctx.fillRect(0, 0, canvas_width, canvas_height);
for (let ts = vid_start; ts < vid_end; ts += 30000) {
r = await getJson('https://rechat.twitch.tv/rechat-messages?video_id=v' + vid_id + '&start=' + Math.round(ts / 1000));
if (r.data.length === 0) {
continue;
}
for (let i = 0; i < r.data.length; i++) {
curr_step_mc++;
let curr_msg_ts = r.data[i].attributes.timestamp;
if (curr_msg_ts - last_step_ts >= step) {
steps_data_ts.push(curr_msg_ts);
steps_data_mc.push(curr_step_mc);
curr_step_mc = 0;
let steps_data_mc_max = Math.max(...steps_data_mc);
if (steps_data_mc_max <= 0) continue;
for (let i = 0, l = steps_data_mc.length; i < l; ++i) {
let pos = ((steps_data_ts[i] - vid_start) / (vid_end - vid_start)).clamp(0, 1),
int = (steps_data_mc[i] / steps_data_mc_max * 100).clamp(1, 100),
col = pickGradientColor(int, gradient);
ctx.fillStyle = 'rgb(' + col.join() + ')';
ctx.fillRect(Math.round(pos * canvas_width) - step_width, 0, step_width, canvas_height);
}
last_step_ts = curr_msg_ts;
}
}
}
}
function getJson(url) {
return new Promise(function(resolve) {
let xhr = new XMLHttpRequest();
xhr.addEventListener('load', function() { resolve(JSON.parse(this.responseText)); });
xhr.open('GET', url,);
xhr.send();
});
}
function pickGradientColor(position, gradient) {
let color_range = [];
for (let i = 0; i < gradient.length; i++) {
if (position<=gradient[i][0]) {
color_range = [i-1,i];
break;
}
}
//Get the two closest colors
let first_color = gradient[color_range[0]][1],
second_color = gradient[color_range[1]][1];
//Calculate ratio between the two closest colors
let first_color_x = gradient[color_range[0]][0]/100,
second_color_x = gradient[color_range[1]][0]/100-first_color_x,
slider_x = position/100-first_color_x,
ratio = slider_x/second_color_x;
return pickHex( second_color,first_color, ratio );
}
function pickHex(color1, color2, weight) {
let w = weight * 2 - 1,
w1 = (w+1) / 2,
w2 = 1 - w1;
return [Math.round(color1[0] * w1 + color2[0] * w2),
Math.round(color1[1] * w1 + color2[1] * w2),
Math.round(color1[2] * w1 + color2[2] * w2)];
}
function setStyle(cssText) {
let sheet = document.createElement('style');
sheet.type = 'text/css';
/* Optional */ window.customSheet = sheet;
(document.head || document.getElementsByTagName('head')[0]).appendChild(sheet);
return (setStyle = function(cssText, node) {
if(!node || node.parentNode !== sheet)
return sheet.appendChild(document.createTextNode(cssText));
node.nodeValue = cssText;
return node;
})(cssText);
}
/**
* Returns a number whose value is limited to the given range.
*
* Example: limit the output of this computation to between 0 and 255
* (x * 255).clamp(0, 255)
*
* @param {Number} min The lower boundary of the output range
* @param {Number} max The upper boundary of the output range
* @returns A number in the range [min, max]
* @type Number
*/
Number.prototype.clamp = function(min, max) {
return Math.min(Math.max(this, min), max);
};
document.addEventListener('DOMContentLoaded', init);
})();