-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.js
250 lines (230 loc) · 8.81 KB
/
index.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
module.exports = {
template: require('./templates/controls.tpl'),
audioEvents: {
ended: () => Player.config.autoplayNext && Player.next(),
pause: 'controls.handleMediaEvent',
play: 'controls.handleMediaEvent',
seeked: 'controls.handleMediaEvent',
waiting: 'controls.handleMediaEvent',
ratechange: 'controls.handleMediaEvent',
timeupdate: 'controls.updateDuration',
loadedmetadata: [ 'controls.updateDuration', 'controls' ],
durationchange: 'controls.updateDuration',
volumechange: 'controls.updateVolume',
loadstart: 'controls.pollForLoading',
error: 'controls.handleAudioError'
},
actions: {
previous: 'previous({ force: true })',
playPause: 'togglePlay',
next: 'next({ force: true })',
seek: 'controls.handleSeek($event, "main")',
mute: 'toggleMute',
volume: 'controls.handleVolume($event, "main")',
fullscreen: 'display.toggleFullScreen'
},
async initialize() {
// Apply the previous volume
GM.getValue('volume').then(volume => volume >= 0 && volume <= 1 && (Player.audio.volume = volume));
// Only poll for the loaded data when the player is open.
Player.on('show', () => Player._hiddenWhilePolling && Player.controls.pollForLoading());
Player.on('hide', () => {
Player._hiddenWhilePolling = !!Player._loadingPoll;
Player.controls.stopPollingForLoading();
});
Player.on('rendered', () => {
Player.video = Player.$(`.${ns}-video`);
Player.video.dataset.id = 'main';
// Keep track of heavily updated elements.
Player.audio.volumeBar = Player.video.volumeBar = Player.$(`.${ns}-volume-bar .${ns}-current-bar`);
Player.audio.currentTimeBar = Player.video.currentTimeBar = Player.$(`.${ns}-seek-bar .${ns}-current-bar`);
Player.audio.loadedBar = Player.video.loadedBar = Player.$(`.${ns}-seek-bar .${ns}-loaded-bar`);
// Set the initial volume/seek bar positions and hidden controls.
Player.controls.updateDuration({ currentTarget: Player.audio });
Player.controls.updateVolume({ currentTarget: Player.audio });
Player.controls.preventWrapping();
});
// Show all the controls when wrapping prevention is disabled.
Player.on('config:preventControlsWrapping', newValue => !newValue && Player.controls.showAllControls());
// Reset the hidden controls when the hide order is changed.
Player.on('config:controlsHideOrder', () => {
Player.controls.setHideOrder();
Player.controls.preventWrapping();
});
// Sync audio/video when the tab is focused.
document.addEventListener('visibilitychange', () => {
if (document.visibilityState === 'visible') {
Player.controls.sync(Player.audio);
}
});
},
/**
* Handle audio errors
*/
handleAudioError(err) {
if (Player.playing) {
Player.logError(`Failed to play ${Player.playing.title}. Please check the console for details.`, err, 'warning');
Player.playing.error = err;
setTimeout(() => Player.next({ paused: true }), 3000);
}
},
/**
* Handle audio events. Sync the video up, and update the controls.
*/
handleMediaEvent(e) {
const audio = e.currentTarget._inlineAudio || e.currentTarget;
Player.controls.sync(e.currentTarget);
Player.controls.updateDuration(e);
document.querySelectorAll(`.${ns}-play-button[data-audio="${audio.dataset.id}"]`).forEach(el => {
el.classList[audio.paused ? 'add' : 'remove'](`${ns}-play`);
});
},
/**
* Sync the webm to the audio. Matches the videos time and play state to the audios.
*/
sync(from) {
const to = from._linked;
if (from && from.readyState > 3 && to && to.readyState > 3) {
to.currentTime = from.currentTime % to.duration;
to[from.paused ? 'pause' : 'play']();
to.playbackRate = from.playbackRate;
to.defaultPlaybackRate = from.defaultPlaybackRate;
}
},
/**
* Poll for how much has loaded. I know there's the progress event but it unreliable.
*/
pollForLoading() {
Player._loadingPoll = Player._loadingPoll || setInterval(Player.controls.updateLoaded, 1000);
},
/**
* Stop polling for how much has loaded.
*/
stopPollingForLoading() {
Player._loadingPoll && clearInterval(Player._loadingPoll);
Player._loadingPoll = null;
},
/**
* Update the loading bar.
*/
updateLoaded() {
const length = Player.audio.buffered.length;
const size = length > 0
? (Player.audio.buffered.end(length - 1) / Player.audio.duration) * 100
: 0;
// If it's fully loaded then stop polling.
size === 100 && Player.controls.stopPollingForLoading();
Player.audio.loadedBar.style.width = size + '%';
},
/**
* Update the seek bar and the duration labels.
*/
updateDuration(e) {
const media = e.currentTarget;
const audio = media._inlineAudio || media;
const controls = media._inlinePlayer ? media._inlinePlayer.controls : document;
const currentTime = _.toDuration(media.currentTime);
const duration = _.toDuration(media.duration);
const audioId = audio.dataset.id;
// Gross use of childNodes to avoid textContent triggering mutation observers of other scripts.
controls && controls.querySelectorAll(`.${ns}-current-time[data-audio="${audioId}"]`).forEach(el => el.childNodes[0].textContent = currentTime);
controls && controls.querySelectorAll(`.${ns}-duration[data-audio="${audioId}"]`).forEach(el => el.childNodes[0].textContent = duration);
Player.controls.updateProgressBarPosition(audio.currentTimeBar, media.currentTime, media.duration);
},
/**
* Update the volume bar.
*/
updateVolume(e) {
const audio = e.currentTarget._inlineAudio || e.currentTarget;
const controls = audio._inlinePlayer ? audio._inlinePlayer.controls : Player.container;
const vol = audio.volume;
// Store volume of the main player.
if (audio === Player.audio) {
vol > 0 && (Player._lastVolume = vol);
GM.setValue('volume', vol);
}
controls && controls.querySelectorAll(`.${ns}-volume-button[data-audio="${audio.dataset.id}"]`).forEach(el => {
el.classList[vol === 0 ? 'add' : 'remove']('mute');
el.classList[vol > 0 ? 'add' : 'remove']('up');
});
Player.controls.updateProgressBarPosition(audio.volumeBar, audio.volume, 1);
},
/**
* Update a progress bar width. Adjust the margin of the circle so it's contained within the bar at both ends.
*/
updateProgressBarPosition(bar, current, total) {
if (!bar) {
return;
}
current || (current = 0);
total || (total = 0);
const ratio = !total ? 0 : Math.max(0, Math.min(((current || 0) / total), 1));
bar.style.width = `calc(${ratio * 100}% - ${(0.8 * ratio) - 0.4}rem)`;
},
/**
* Handle the user interacting with the seek bar.
*/
handleSeek(e, audioId) {
const media = audioId === 'main'
? Player.audio
: Player.inline.audio[audioId]._inlinePlayer.master;
if (media && media.duration && media.duration !== Infinity) {
media.currentTime = media.duration * Player.controls._getBarXRatio(e);
}
},
/**
* Handle the user interacting with the volume bar.
*/
handleVolume(e, audioId) {
const audio = audioId === 'main' ? Player.audio : Player.inline.audio[audioId];
audio.volume = Player.controls._getBarXRatio(e);
},
_getBarXRatio(e) {
const offset = 0.4 * Player.remSize;
const offsetX = e.offsetX || (e.targetTouches[0].pageX - e.currentTarget.getBoundingClientRect().left);
return Math.max(0, Math.min(1, (offsetX - offset) / (parseInt(getComputedStyle(e.currentTarget).width, 10) - (2 * offset))));
},
/**
* Set all controls visible.
*/
showAllControls() {
Player.$all(`.${ns}-controls [data-hide-id]`).forEach(el => el.style.display = null);
},
/**
* Hide elements in the controls instead of wrapping
*/
preventWrapping() {
if (!Player.config.preventControlWrapping) {
return;
}
const controls = Player.$(`.${ns}-controls`);
// If the offset top of the last visible child than this value it indicates wrapping.
const expectedOffsetTop = parseFloat(window.getComputedStyle(controls).paddingTop);
const hideElements = Player.controls.hideOrder || Player.controls.setHideOrder();
let visibleChildren = Array.prototype.slice.call(controls.children);
let lastChild = visibleChildren.pop();
let hidden = 0;
// Show everything to check what has wrapped.
Player.controls.showAllControls();
// Keep hiding elements until the last visible child has not wrapped, or there's nothing left to hide.
while (lastChild.offsetTop > expectedOffsetTop && hidden < hideElements.length) {
const hide = hideElements[hidden++];
hide.style.display = 'none';
visibleChildren = visibleChildren.filter(el => el !== hide);
hide === lastChild && (lastChild = visibleChildren.pop());
}
},
/**
* Set the hide order from the user config.
*/
setHideOrder() {
if (!Array.isArray(Player.config.controlsHideOrder)) {
Player.settings.reset('controlsHideOrder');
}
const controls = Player.$(`.${ns}-controls`);
return Player.controls.hideOrder = Player.config.controlsHideOrder
.map(id => controls.querySelector(`[data-hide-id="${id}"]`))
.filter(el => el)
.sort((a, b) => a.dataset.hideOrder - b.dataset.hideOrder);
}
};