-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathspeedcontrol.js
408 lines (372 loc) · 11.2 KB
/
speedcontrol.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
/**
* @filedescription This is a simple script for adding HTML5 speed controls to
* video elements.
* @author Benedict Chen (benedict@benedictchen.com)
*/
var sophis = sophis || {};
var increment = 0.1;
var keyCombo = 'udar';
var blackListedSites = ['vine.com'];
var isCurrentSiteBlackListed = function() {
blackListedSites.forEach((blackListedSite) => {
if (blackListedSite && sophis.VideoControl.isLocation(blackListedSite)) {
sophis.VideoControl.killAll()
}
});
};
chrome.storage.sync.get(null, function(items) {
this.increment = items.increment;
this.keyCombo = items.keyCombo;
blackListedSites = (items.blackListedSites &&
items.blackListedSites.split(/[\t\n\r,\s]/g)
.filter((item) => !!item))|| [];
// NOTE: The issue here is that the settings are retreived after the component
// is loaded, so we need to retroactively remove the items.
if (isCurrentSiteBlackListed()) {
sophis.VideoControl.killAll()
}
});
/**
* Keyboard character mappings from their numeric values.
* @type {Object.<String:Number>}
* @enum
*/
var KeyCodes = {
UP: 38,
DOWN: 40,
LEFT: 37,
RIGHT: 39,
PAGEUP: 33,
PAGEDOWN: 34
};
/**
* A mapping of actions to keyboard character values.
* @type {Object.<String>}
* @enum
*/
var KeyMapping = {
DECREASE_SPEED: 'A',
INCREASE_SPEED: 'S'
};
/**
* Controls an HTML video with playback speed.
* @param {Element} targetEl The target element to inject a video control into.
*/
sophis.VideoControl = function(targetEl) {
/**
* The html body that stores the controls.
* @private {Element}
*/
this.el_ = null;
/**
* The shadow container that stores the controls.
* @private {Element}
*/
this.bgEl_ = null;
/**
* The video element.
* @private {Element}
*/
this.videoEl_ = targetEl;
/**
* @private {Element}
*/
this.speedIndicator_ = null;
/**
* The button that destroys the component
* @private {Element}
*/
this.closeButton_ = null;
if (!isCurrentSiteBlackListed()) {
this.createDom();
this.enterDocument();
sophis.VideoControl.instances.push(this);
} else {
console.warn('Current site is blacklisted from speed controller.');
}
};
/** @const */
sophis.VideoControl.CLASS_NAME = 'sophis-video-control';
/**
* Keeps track of all current instances of current class.
* @type {Array.<sophis.VideoControl>}
*/
sophis.VideoControl.instances = [];
/**
* Removes all instances of the current class.
*/
sophis.VideoControl.killAll = function() {
sophis.VideoControl.instances.forEach((instance) => instance.dispose());
sophis.VideoControl.instances = [];
}
/**
* Creates the HTML body of the controls.
*/
sophis.VideoControl.prototype.createDom = function() {
var container = document.createElement('div');
var shadow = container.createShadowRoot();
var bg = document.createElement('div');
var speedIndicator = document.createElement('span');
var minusButton = document.createElement('button');
var plusButton = document.createElement('button');
var closeButton = document.createElement('a');
shadow.appendChild(bg);
bg.appendChild(minusButton);
bg.appendChild(speedIndicator);
bg.appendChild(plusButton);
bg.appendChild(closeButton);
bg.classList.add('sophis-bg');
speedIndicator.classList.add('speed-indicator');
minusButton.textContent = '-';
minusButton.classList.add('sophis-btn', 'decrease');
plusButton.textContent = '+';
plusButton.classList.add('sophis-btn', 'increase');
closeButton.classList.add('sophis-btn', 'sophis-close-button');
closeButton.textContent = 'close';
this.videoEl_.parentElement.insertBefore(container, this.videoEl_);
this.videoEl_.classList.add('sophis-video');
this.el_ = container;
this.el_.classList.add(sophis.VideoControl.CLASS_NAME);
this.bgEl_ = bg;
this.speedIndicator_ = speedIndicator;
this.minusButton_ = minusButton;
this.plusButton_ = plusButton;
this.closeButton_ = closeButton;
// Vimeo iframe hack. They are intercepting all our events with a hidden
// element.
if (this.isLocationVimeo_()) {
var clickInterceptingScum = document.querySelector('.player .target');
if (clickInterceptingScum) {
clickInterceptingScum.parentElement.removeChild(clickInterceptingScum);
}
}
};
/**
* Post-dom creation actions such as adding event listeners.
*/
sophis.VideoControl.prototype.enterDocument = function() {
var self = this;
var clickHandler = this.handleClick_.bind(this);
var dblClickHandler = this.handleDblClick_.bind(this);
var keydownHandler = this.handleKeyDown_.bind(this);
var keyPressHandler = this.handleKeyPress_.bind(this);
var dragHandler = this.handleDragEndEvent_.bind(this);
this.bgEl_.addEventListener('click', clickHandler, true);
this.bgEl_.addEventListener('dblclick', dblClickHandler, true);
document.body.addEventListener('keydown', keydownHandler, true);
document.body.addEventListener('keypress', keyPressHandler, true);
document.body.addEventListener('dragend', dragHandler, true);
this.el_.setAttribute('draggable', true);
// Set speed indicator to correct amount.
this.speedIndicator_.textContent = this.getSpeed();
this.videoEl_.addEventListener('ratechange', function() {
self.speedIndicator_.textContent = self.getSpeed();
});
};
/**
* Increases the current video's playback rate.
*/
sophis.VideoControl.prototype.decreaseSpeed = function () {
this.videoEl_.playbackRate -= increment;
};
/**
* Decreases the current video's playback rate.
*/
sophis.VideoControl.prototype.increaseSpeed = function () {
this.videoEl_.playbackRate += increment;
};
/**
* Determines if the current video element is playing.
* @return {Boolean} Whether or not the video is playing.
* @private
*/
sophis.VideoControl.prototype.isPlaying_ = function() {
var videoEl = this.videoEl_;
return videoEl.currentTime > 0 && !videoEl.paused && !videoEl.ended;
};
sophis.VideoControl.prototype.hasFocus = function() {
var activeEl = document.activeElement;
if (activeEl.nodeName === 'BODY') {
return false;
}
return (activeEl && activeEl.querySelector('video, .sophis-video-control'));
};
/**
* Handles the native `keyPress` events.
* @param {Event} e The native key press event.
*/
sophis.VideoControl.prototype.handleKeyPress_ = function(e) {
if (!this.isPlaying_() || !this.hasFocus() || !e.keyCode) {
return;
}
var characterValue = String.fromCharCode(e.keyCode).toUpperCase();
if (characterValue) {
switch(characterValue) {
case KeyMapping.INCREASE_SPEED:
this.increaseSpeed();
break;
case KeyMapping.DECREASE_SPEED:
this.decreaseSpeed();
break;
}
}
}
/**
* Handles native `keyDown` events.
* @param {Event} e The native keyDown event.
* @private
*/
sophis.VideoControl.prototype.handleKeyDown_ = function(e) {
if (!this.isPlaying_() || !this.hasFocus()) {
return;
}
var keyCode = e.keyCode;
if (keyCode && keyCombo === 'pgud') {
switch (keyCode) {
case KeyCodes.PAGEDOWN:
this.decreaseSpeed();
break;
case KeyCodes.PAGEUP:
this.increaseSpeed();
break;
default:
this.videoEl_.focus();
return false;
}
}
else if (keyCode && keyCombo === 'lrar') {
switch (keyCode) {
case KeyCodes.LEFT:
this.decreaseSpeed();
break;
case KeyCodes.RIGHT:
this.increaseSpeed();
break;
default:
this.videoEl_.focus();
return false;
}
}
else if (keyCode && keyCombo === 'udar') {
switch (keyCode) {
case KeyCodes.DOWN:
this.decreaseSpeed();
break;
case KeyCodes.UP:
this.increaseSpeed();
break;
default:
this.videoEl_.focus();
return false;
}
}
};
/**
* Handles a user clicking on the video controls.
* @param {Event} e The native click event.
* @private
*/
sophis.VideoControl.prototype.handleClick_ = function(e) {
if (!e.target.classList.contains('sophis-btn')) {
return;
}
e.preventDefault();
e.stopPropagation();
if (e.target === this.minusButton_) {
this.decreaseSpeed();
} else if (e.target === this.plusButton_) {
this.increaseSpeed();
} else if (e.target === this.closeButton_) {
this.dispose();
}
// Redundant if we listen for 'ratechange', but do it anyway
this.speedIndicator_.textContent = this.getSpeed();
return false;
};
/**
* Handles a double-click event on the video controls.
* @param {Event} e The native click event.
* @private
*/
sophis.VideoControl.prototype.handleDblClick_ = function(e) {
if (!e.target.classList.contains('sophis-btn')) {
return;
}
e.preventDefault();
e.stopPropagation();
};
/**
* Handles when the user drags the control node.
* @param {Event} e The native drag event.
* @private
*/
sophis.VideoControl.prototype.handleDragEndEvent_ = function(e) {
let leftPosition = Math.max(0, e.clientX);
// BUG: For whatever reason, the drag offset height is wonky and
// is arbitrarily approximately 80 pixels pushed downward.
let topPosition = Math.max(0, e.clientY - this.el_.offsetHeight - 80);
this.el_.style.left = `${leftPosition}px`;
this.el_.style.top = `${topPosition}px`;
};
/**
* Determines whether or not the current page is being executed within
* the boundaries of an iframe.
* @return {Boolean} Whether or not the current page is an iframe.
* @private
*/
sophis.VideoControl.prototype.isEmbeddedInIframe_ = function() {
return window.self !== window.top;
};
/**
* Determines whether we are coming from a particular URL.
* @param {String|RegExp} url The URL patten to match against.
* @return {Boolean} Whether or not the current window is from a URL.
*/
sophis.VideoControl.isLocation = function(url) {
return !!window.location.href.match(url);
};
/**
* Determines whether we are coming from a Vimeo URL.
* @return {Boolean} Whether or not the current window is from Vimeo.
* @private
*/
sophis.VideoControl.prototype.isLocationVimeo_ = function() {
return sophis.VideoControl.isLocation('vimeo');
};
/**
* Gets the current speed of the player.
* @return {String} The playback speed/rate of the video.
*/
sophis.VideoControl.prototype.getSpeed = function() {
return parseFloat(this.videoEl_.playbackRate).toFixed(2);
};
/**
* Destroys and removes the component from page.
*/
sophis.VideoControl.prototype.dispose = function() {
this.el_.parentNode.removeChild(this.el_);
};
/**
* Finds all video elements that have no video control yet and
* adds a new one.
*/
sophis.VideoControl.insertAll = function () {
var videoTags = document.getElementsByTagName('video');
Array.prototype.forEach.call(videoTags, function(videoTag) {
if (!videoTag.getAttribute('sophis-video-control')) {
videoTag.setAttribute('sophis-video-control', true);
new sophis.VideoControl(videoTag);
}
});
};
// Listen for new video elements and inject into it.
document.addEventListener('DOMNodeInserted', function(event) {
var node = event.target || null;
if (node && node.nodeName === 'VIDEO') {
new sophis.VideoControl(node);
}
});
sophis.VideoControl.insertAll();
// Ghetto polling for new video elements being added to the page.
// Necessary for Tuts+ and many non-standard implementations.
setInterval(sophis.VideoControl.insertAll, 1000);