-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
158 lines (145 loc) · 4.6 KB
/
script.js
File metadata and controls
158 lines (145 loc) · 4.6 KB
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
(function() {
// ----- Options -----
var transitionTime = '.3s';
var controllerSize = 80;
var trackSize = 68;
var trackWidth = 2;
var ballSize = 12;
var storageKey = 'tvc-volume';
// -------------------
var soundButton = document.querySelector('.sound-button');
if(!soundButton) return;
var volume = localStorage.getItem(storageKey);
if(volume === null) volume = 1;
volume = Math.min(Math.max(+volume, 0), 1);
var trackPos = (controllerSize - trackSize) / 2;
soundButton.style.position = 'relative';
soundButton.style.marginRight = '15px';
soundButton.style.marginTop = '0';
soundButton.style.transition = 'margin-right '+transitionTime;
var wrapper = document.createElement('div');
wrapper.style.position = 'absolute';
wrapper.style.top = '0';
wrapper.style.bottom = '5px';
wrapper.style.left = '100%';
wrapper.style.width = controllerSize + 'px';
wrapper.style.overflow = 'hidden';
wrapper.style.visibility = 'hidden';
wrapper.style.transition = 'visibility '+transitionTime;
var bar = document.createElement('div');
bar.style.position = 'absolute';
bar.style.width = '100%';
bar.style.height = '100%';
// bar.style.backgroundColor = 'rgba(0,0,0,.6)';
bar.style.backgroundImage = 'linear-gradient(white, white)';
bar.style.backgroundRepeat = 'no-repeat';
bar.style.backgroundSize = trackSize + 'px ' + trackWidth + 'px';
bar.style.backgroundPosition = 'center';
bar.style.borderRadius = '5px 5px 0 0';
bar.style.transform = 'translateX(-100%)';
bar.style.transition = 'transform '+transitionTime;
var ball = document.createElement('div');
ball.style.position = 'absolute';
ball.style.width = ballSize+'px';
ball.style.height = ballSize+'px';
ball.style.borderRadius = '50%';
ball.style.background = 'white';
ball.style.left = trackPos + 'px';
ball.style.top = '50%';
var hbs = ballSize / 2;
ball.style.margin = '-'+hbs+'px 0 0 -'+hbs+'px';
// Move it to the left
var leftSide = document.querySelector('.player-controls-bottom-left');
if(leftSide) leftSide.appendChild(soundButton);
var nativeSlider = document.querySelector('.volume-control-container');
if(nativeSlider) nativeSlider.style.display = 'none';
var timeDisplayStyle = document.createElement('style');
timeDisplayStyle.type = 'text/css';
timeDisplayStyle.appendChild(document.createTextNode('.time-display { margin-right: 15px; }'));
document.head.appendChild(timeDisplayStyle);
var video = null;
function waitForVideo() {
video = document.querySelector('video');
if(!video) requestAnimationFrame(waitForVideo);
else videoReady();
}
updateVolume(volume);
waitForVideo();
function videoReady() {
setVolume(volume);
video.addEventListener('volumechange', function() {
updateVolume(video.volume, video.muted);
});
}
function setVolume(vol) {
volume = vol;
if(video) {
// video.muted = false;
video.volume = vol;
}
}
function updateVolume(vol, muted) {
// var pos = muted ? 0 : vol * trackSize;
var pos = vol * trackSize;
ball.style.transform = 'translateX('+pos+'px)';
localStorage.setItem(storageKey, vol);
}
var hovered = false;
var mouseDown = false, preventClick = false;
var barScreenPos = null;
function showBar() {
if(!wrapper || !bar) return;
soundButton.style.marginRight = (15 + controllerSize) + 'px';
wrapper.style.visibility = 'visible';
bar.style.transform = 'translateX(0)';
}
function hideBar() {
if(!wrapper || !bar) return;
soundButton.style.marginRight = '15px';
wrapper.style.visibility = 'hidden';
bar.style.transform = 'translateX(-100%)';
}
soundButton.addEventListener('mouseenter', function() {
hovered = true;
showBar();
});
soundButton.addEventListener('mouseleave', function mouseLeave() {
hovered = false;
if(!mouseDown) hideBar();
});
function checkMouse(e) {
if(!barScreenPos) return;
var posIn = e.clientX - barScreenPos.left - trackPos;
var result = Math.min(Math.max(posIn / trackSize, 0), 1);
setVolume(result);
}
bar.addEventListener('mousedown', function(e) {
e.stopPropagation();
barScreenPos = wrapper.getBoundingClientRect();
mouseDown = true;
preventClick = true;
checkMouse(e);
}, true);
document.addEventListener('mousemove', function(e) {
if(!mouseDown) return;
checkMouse(e);
});
document.addEventListener('mouseup', function(e) {
if(mouseDown) {
e.stopPropagation();
e.preventDefault();
} else {
preventClick = false;
}
mouseDown = false;
if(!hovered) hideBar();
}, true);
bar.addEventListener('click', function(e) {
if(preventClick)
e.stopPropagation();
preventClick = false;
}, true);
bar.appendChild(ball);
wrapper.appendChild(bar);
soundButton.appendChild(wrapper);
})();