-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
487 lines (429 loc) · 17.5 KB
/
script.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
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
(function() {
/**
* Decimal adjustment of a number.
*
* @param {String} type The type of adjustment.
* @param {Number} value The number.
* @param {Integer} exp The exponent (the 10 logarithm of the adjustment base).
* @returns {Number} The adjusted value.
*/
function decimalAdjust(type, value, exp) {
// If the exp is undefined or zero...
if (typeof exp === 'undefined' || +exp === 0) {
return Math[type](value);
}
value = +value;
exp = +exp;
// If the value is not a number or the exp is not an integer...
if (isNaN(value) || !(typeof exp === 'number' && exp % 1 === 0)) {
return NaN;
}
// Shift
value = value.toString().split('e');
value = Math[type](+(value[0] + 'e' + (value[1] ? (+value[1] - exp) : -exp)));
// Shift back
value = value.toString().split('e');
return +(value[0] + 'e' + (value[1] ? (+value[1] + exp) : exp));
}
// Decimal round
if (!Math.round10) {
Math.round10 = function(value, exp) {
return decimalAdjust('round', value, exp);
};
}
// Decimal floor
if (!Math.floor10) {
Math.floor10 = function(value, exp) {
return decimalAdjust('floor', value, exp);
};
}
// Decimal ceil
if (!Math.ceil10) {
Math.ceil10 = function(value, exp) {
return decimalAdjust('ceil', value, exp);
};
}
})();
//document.getElementById('player-menu-track-settings').addEventListener('onmouseenter', function() { console.log('clicked'); })
//$('.player-control-bar').append('<div class="player-control-button player-fill-screen icon-player-full-screen" tabindex="0" role="button"></div>');
$(window).load(function() {
'use strict';
var DEBUG = 1;
function LOG(msg) { if (DEBUG) console.log(msg); }
function WARN(msg) { if (DEBUG) console.warn(msg); }
function ERR(msg) { console.error(msg); }
var customSubtitles = null;
watchSettingsMenu(function(settingsMenu) {
if (customSubtitles === null) {
customSubtitles = new CustomSubtitlesInjector();
}
customSubtitles.injectIntoSettings(settingsMenu);
}, function() {
customSubtitles.uninject();
});
function watchSettingsMenu(addedCb, removedCb) {
if (!$('#playerContainer').length) {
WARN('No player container, won\'t look for settings');
return;
}
var settingsMenuId = 'player-menu-track-settings';
var settingsFinder = new MutationObserver(function (mutations) {
mutations.forEach(function (mutation) {
var node = null;
var i = 0;
for (i = 0; i < mutation.addedNodes.length; ++i) {
node = mutation.addedNodes[i];
if (node.id === settingsMenuId) {
LOG('Menu settings has been added');
addedCb($(node));
return;
}
}
for (i = 0; i < mutation.removedNodes.length; ++i) {
node = mutation.removedNodes[i];
if (node.id === settingsMenuId) {
LOG('Menu settings has been removed');
removedCb($(node));
return;
}
}
});
});
settingsFinder.observe(document, {childList: true, subtree: true});
}
// This functions is used to hide subtitles so user preference is saved instead of overridden to NONE
function hideNetflixSubtitles() {
$('.player-timedtext-text-container').hide();
if (!document.netflixSubtitleObserver) {
document.netflixSubtitleObserver = new MutationObserver(function (mutations) {
mutations.forEach(function (mutation) {
for (var i = 0; i < mutation.addedNodes.length; ++i) {
var node = $(mutation.addedNodes[i]);
if (node.hasClass('player-timedtext-text-container')) {
node.hide();
}
}
});
});
}
findSubtitleContainer().then(function (subtitleContainer) {
document.netflixSubtitleObserver.observe(subtitleContainer[0], {childList: true});
});
}
function restoreNetflixSubtitles() {
if (document.netflixSubtitleObserver) {
document.netflixSubtitleObserver.disconnect();
$('.player-timedtext-text-container').show();
}
}
function findVideoTag() {
return new Promise(function(resolve, reject) {
var videoTag = $('video');
if (videoTag.length != 1) {
ERR('[NetflixSubtitle] Could not find video tag.');
reject();
} else {
resolve(videoTag);
}
});
}
function findSubtitleContainer() {
return new Promise(function(resolve, reject) {
var element = $('.player-timedtext');
if (element.length != 1) {
ERR('[NetflixSubtitle] Could not find subtitle container.');
reject();
} else {
resolve(element);
}
});
}
function getUserSubtitle() {
return new Promise(function(resolve, reject) {
var input = $('<input type="file"/>');
input.change(function () {
var files = input[0].files;
if (files.length < 1) {
LOG('No files picked');
reject();
} else {
if (files.length > 1) {
WARN('Only one file is supported, picking first one');
}
var reader = new FileReader();
reader.addEventListener('loadend', function () {
if (reader.error) {
WARN('Failed loading file: ' + reader.error);
reject();
} else {
resolve(parser.fromSrt(reader.result, true));
}
});
reader.readAsText(files[0]);
}
});
input.click();
});
}
function observeNetflixVisibility(element, visible, invisible) {
var isVisible = !element.hasClass('display-none');
var settingsObserver = new MutationObserver(function(mutations) {
mutations.forEach(function(mutation) {
if (mutation.attributeName === 'class') {
if (!element.hasClass('display-none')) {
if (!isVisible) {
isVisible = true;
visible(element);
}
} else if (isVisible) {
isVisible = false;
invisible(element);
}
}
});
});
settingsObserver.observe(element[0], {attributes: true});
if (isVisible) {
visible(element);
} else {
invisible(element);
}
return { disconnect: settingsObserver.disconnect.bind(settingsObserver) };
}
function htmlForTextWithEmbeddedNewlines(text) {
var htmls = [];
var lines = text.split(/\n/);
// The temporary <div/> is to perform HTML entity encoding reliably.
//
// document.createElement() is *much* faster than jQuery('<div></div>')
// http://stackoverflow.com/questions/268490/
//
// You don't need jQuery but then you need to struggle with browser
// differences in innerText/textContent yourself
var tmpDiv = jQuery(document.createElement('div'));
for (var i = 0 ; i < lines.length ; i++) {
htmls.push(tmpDiv.text(lines[i]).html());
}
return htmls.join("<br/>");
}
function CustomSubtitlesInjector() {
// Custom subtitles settings menu
var customSubtitlesEmbedder = null;
var wasEmbedderActive = false;
var subtitles = null;
var customSubtitlesSettings = $('<ol class="player-timed-text-tracks player-visible custom-netflix-subtitles">' +
'<lh>Subtitles settings</lh></ol>');
var customSubtitlesDelayLabel = $('<div class="netflix-subtitles-delay-label"></div>');
var customSubtitlesDelayIncrease = $('<i class="fa fa-plus-circle"></i>');
var customSubtitlesDelayDecrease = $('<i class="fa fa-minus-circle"></i>');
var delay;
var delayStep = 0.25;
var setDelay = function (newDelay) {
if ($.isNumeric(newDelay)) {
delay = Math.round10(newDelay, -2);
customSubtitlesDelayLabel.text('Delay: ' + delay + ' s');
if (customSubtitlesEmbedder) {
customSubtitlesEmbedder.delay = delay;
customSubtitlesEmbedder.displaySubtitlesForCurrentTime();
}
}
};
setDelay(0);
customSubtitlesDelayIncrease.click(function () {
setDelay(delay + delayStep);
return false;
});
customSubtitlesDelayDecrease.click(function () {
setDelay(delay - delayStep);
return false;
});
customSubtitlesSettings.append(
$('<li></li>')
.append(customSubtitlesDelayLabel)
.append($('<div class="settings-right"></div>')
.append(customSubtitlesDelayIncrease)
.append(customSubtitlesDelayDecrease)));
var selectedClass = 'player-track-selected';
var customSubtitles = $('<li>Custom</li>');
var settingsMenu = null;
var originalTextSettings = null;
var substitutedTextSettings = null;
var selectedSubtitle = null;
var settingsMenuClickHandler = function () {
var element = $(this);
if (customSubtitlesEmbedder) {
customSubtitlesEmbedder.deactivate();
customSubtitlesEmbedder = null;
subtitles = null;
}
customSubtitlesSettings.detach();
setDelay(0);
if (this == customSubtitles[0]) {
var previouslySelected = selectedSubtitle;
getUserSubtitle().then(function (newSubs) {
LOG('Subtitles loaded');
if (selectedSubtitle[0] === customSubtitles[0]) {
subtitles = newSubs;
hideNetflixSubtitles();
customSubtitlesSettings.appendTo(settingsMenu);
customSubtitlesEmbedder = new SubtitlesEmbedder(subtitles);
customSubtitlesEmbedder.delay = delay;
customSubtitlesEmbedder.activate();
previouslySelected.removeClass(selectedClass);
element.addClass(selectedClass);
}
// else someone switched subtitles while we were loading
});
} else {
restoreNetflixSubtitles();
selectedSubtitle.removeClass(selectedClass);
element.addClass(selectedClass);
}
selectedSubtitle = element;
originalTextSettings.children().eq(element.index()).click();
};
this.injectIntoSettings = function(settingsNode) {
if (settingsMenu !== null) {
this.uninject();
}
// rewire subtitle settings
settingsMenu = settingsNode;
originalTextSettings = settingsMenu.find('.player-timed-text-tracks');
substitutedTextSettings = originalTextSettings.clone();
selectedSubtitle = substitutedTextSettings.find('.' + selectedClass);
substitutedTextSettings.append(customSubtitles);
substitutedTextSettings.on('click', 'li', settingsMenuClickHandler);
originalTextSettings.hide();
settingsMenu.append(substitutedTextSettings);
if (wasEmbedderActive && subtitles !== null) {
LOG('Restoring subtitles');
selectedSubtitle.removeClass(selectedClass);
customSubtitles.addClass(selectedClass);
selectedSubtitle = customSubtitles;
hideNetflixSubtitles();
customSubtitlesSettings.appendTo(settingsMenu);
customSubtitlesEmbedder = new SubtitlesEmbedder(subtitles);
customSubtitlesEmbedder.delay = delay;
customSubtitlesEmbedder.activate();
}
};
this.uninject = function() {
if (customSubtitlesEmbedder) {
wasEmbedderActive = true;
customSubtitlesEmbedder.deactivate();
customSubtitlesEmbedder = null;
restoreNetflixSubtitles();
}
substitutedTextSettings.remove();
originalTextSettings.show();
originalTextSettings = null;
substitutedTextSettings = null;
selectedSubtitle = null;
settingsMenu = null;
};
}
function SubtitlesEmbedder(subtitles) {
this.subtitles = subtitles;
this.delay = 0;
var video = null;
var netflixSubtitleContainer = null;
var subtitleContainer = $('<div id="custom-subtitles" style="white-space: nowrap; text-align: center; position: absolute;"></div>');
var subtitleText =
$('<span style="line-height:normal;font-weight:normal;color:#ffffff;text-shadow:#000000 0px 0px 13px;' +
'font-family:Arial,Helvetica;font-weight:bolder"></span>');
var progressBarVisible = true;
var progressBarObserver = null;
var progressBar = null;
subtitleContainer.append(subtitleText);
var subtitleResize = function() {
if (subtitleContainer.is(":visible")) {
// Try to do the same thing as netflix ie.
// Place subtitles inside video element (which can be scaled down)
// In the 10% from bottom or right above progress bar whichever is higher
// Center of text in the center of video element
var containerWidth = netflixSubtitleContainer.width();
var containerHeight = netflixSubtitleContainer.height();
var containerPosition = netflixSubtitleContainer.position();
var videoWidth = video.videoWidth;
var videoHeight = video.videoHeight;
var insideWidth = containerWidth;
var insideHeight = containerHeight;
if (containerHeight / containerWidth > videoHeight / videoWidth) {
insideHeight = videoHeight * (containerWidth / videoWidth);
} else {
insideWidth = videoWidth * (containerHeight / videoHeight);
}
LOG('containerWidth ' + containerWidth + ' videoWidth ' + videoWidth + ' insideWidth ' + insideWidth);
LOG('containerHeight ' + containerHeight + ' videoHeight ' + videoHeight + ' insideHeight ' + insideHeight);
var fontSize = Math.round(insideWidth / 40);
subtitleText.css('fontSize', fontSize);
// assume video is always centered
var textWidth = subtitleContainer.width();
var leftRatio = (1 - Math.max(textWidth / containerWidth, 0)) * 50; // half the value and change to % == multiply by 50
var progressTop = progressBar.position().top;
var heightOffset = Math.max((containerHeight - insideHeight) / 2, 0);
var progressBarTransposition = progressBarVisible ? heightOffset + insideHeight - progressTop : 0;
var bottomRatio = heightOffset + Math.max(0.10 * insideHeight, progressBarTransposition);
LOG('bottomRatio: ' + bottomRatio + ' heightOffset: ' + heightOffset + ' progressBarTransposition: ' + progressBarTransposition + ' progress top: ' + progressTop);
bottomRatio /= containerHeight;
bottomRatio *= 100; // percentify
subtitleContainer.css({left: (leftRatio + '%'), bottom: (bottomRatio + '%')});
LOG('Resize to font: ' + fontSize + ', bottom: ' + bottomRatio + ' left: ' + leftRatio);
}
};
this.displaySubtitlesForCurrentTime = function() {
var time = (video.currentTime - this.delay) * 1000; // get time in milliseconds
// TODO: ain't this a little too heavy?
var subtitlesLength = this.subtitles.length;
for (var i = 0; i < subtitlesLength; ++i) {
if (this.subtitles[i].startTime > time) {
subtitleContainer.hide();
break;
}
if (this.subtitles[i].endTime > time) {
subtitleText.html(htmlForTextWithEmbeddedNewlines(this.subtitles[i].text));
subtitleContainer.show();
subtitleResize();
break;
}
}
if (i == subtitlesLength) {
subtitleContainer.hide();
}
};
var subtitlesDisplayCallback = this.displaySubtitlesForCurrentTime;
this.activate = function() {
LOG('Activating custom subtitles');
netflixSubtitleContainer = $('.player-video-wrapper').children().first();
progressBar = $('.player-controls-wrapper');
progressBarObserver = observeNetflixVisibility(progressBar,
function() { progressBarVisible = true; subtitleResize(); },
function() { progressBarVisible = false; subtitleResize(); });
$(window).resize(subtitleResize);
netflixSubtitleContainer.resize(subtitleResize);
video = $('video')[0];
subtitlesDisplayCallback = this.displaySubtitlesForCurrentTime.bind(this);
video.addEventListener('timeupdate', subtitlesDisplayCallback);
netflixSubtitleContainer.append(subtitleContainer);
subtitlesDisplayCallback();
};
this.deactivate = function() {
LOG('Deactivating custom subtitles');
if (netflixSubtitleContainer) {
subtitleContainer.remove();
netflixSubtitleContainer.off('resize', subtitleResize);
netflixSubtitleContainer = null;
}
$(window).off('resize', subtitleResize);
if (progressBarObserver) {
progressBarObserver.disconnect();
progressBarObserver = null;
}
progressBar = null;
if (video) {
video.removeEventListener('timeupdate', subtitlesDisplayCallback);
video = null;
}
};
}
});