-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
189 lines (172 loc) · 5.02 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
// if you have better idea try #qa @ irc.lolicore.org
// This code was originaly created by someone from lolicore.org
// It doesn't belong to me.
var playarrow = "▶";
function do_sort(t, col) {
setTimeout(hook_player, 0);
}
function toggle_sort(t,col) {
var hdr = t.getElementsByTagName("th");
hdr[col].addEventListener('click', function() { do_sort(t, col); });
}
function hook_sort(t) {
var i;
for (i = 0; i < 3; i++)
toggle_sort(t,i);
}
var tracks = [];
var current = 0;
var haveplayer = false;
function jump_dir() {
if (document.location.pathname == "/") {
localStorage.removeItem('autoplay');
localStorage.removeItem('lastdir');
return;
}
var url = document.location.href.split('#')[0];
localStorage.lastdir = url.substr(url.substr(0,url.length-1).lastIndexOf('/') + 1);
document.location.href = "..";
}
var lastdir;
function hook_player() {
var playnow = false;
var i, links = document.querySelectorAll('tr td a');
var havemp3 = false;
tracks = [];
for (i = 0; i < links.length; i++) {
var l = links[i];
if (l.href.match(/\.(mp3|ogg|wav|flac|m4a)$/i)) {
havemp3 = true;
l.insertAdjacentHTML('afterbegin', playarrow);
l.parentNode.className = "playable";
(function(l) {
l.parentNode.addEventListener('click', function (e) {
e.preventDefault();
set_current(tracks.indexOf(l));
})
})(l);
var hv = '/' + document.location.hash.substr(1);
try {
hv = decodeURIComponent(hv);
} catch (e) {};
var sv = l.href;
try {
sv = decodeURIComponent(sv);
} catch (e) {};
sv = sv.substr(sv.length - hv.length);
if (hv == sv) {
current = tracks.length;
playnow = true;
}
tracks.push(l);
}
if (l.href.match(/cover\.(jpg|png)$/i)) {
var img = document.createElement("img");
img.src = l.href;
img.alt = "cover"
img.id = "cover"
var listing = document.getElementsByClassName("listing")[0];
listing.appendChild(img);
}
}
var pl = document.getElementById('player');
if (!haveplayer) {
pl.innerHTML = "<div id='player'><a href='/random'><button>random</button></a><button id='back'><<</button><button id='forward'>>></button><input type=checkbox id=gonext>Repeat<br><audio id='audio' controls></audio></div>";
}
var gonext = document.getElementById('gonext');
var audio = document.getElementById('audio');
var forward = document.getElementById('forward');
var back = document.getElementById('back');
if (!haveplayer) {
var bc
if (window.BroadcastChannel) {
bc = new BroadcastChannel('play_notify');
bc.onmessage = function(ev) {
audio.pause();
}
}
gonext.checked = !JSON.parse(localStorage.gonext || "false");
gonext.addEventListener('click', function() {
localStorage.gonext = !gonext.checked;
})
audio.addEventListener('play', function() {
if (bc) bc.postMessage('playing');
var e = tracks[current].parentNode;
e.className = 'current';
if (e.scrollIntoViewIfNeeded)
e.scrollIntoViewIfNeeded(true);
})
audio.addEventListener('error', function(e) {
setTimeout(fwd, 5000);
});
function fwd() {
var ntr = (current + 1) % tracks.length;
if (ntr == 0 && !gonext.checked) {
jump_dir();
return;
}
set_current(ntr);
}
audio.addEventListener('ended', function() {
fwd();
})
forward.addEventListener('click', function() {
fwd();
})
back.addEventListener('click', function() {
set_current((tracks.length + current - 1) % tracks.length);
})
audio.src = tracks[current].href;
if (localStorage.autoplay || (playnow)) {
localStorage.removeItem('autoplay');
audio.play();
}
}
haveplayer = true;
function set_current(n) {
tracks[current].parentNode.className = 'playable';
current = n;
var hr = tracks[current].href;
audio.src = hr;
var hv = hr.substr(hr.lastIndexOf('/') + 1);
history.replaceState(undefined, undefined, "#" + hv)
audio.play();
}
}
var gcse_spawned = false;
function spawn_gcse() {
//console.log('gcse is', localStorage.search);
var elm = document.getElementById('___gcse_0');
if (!JSON.parse(localStorage.search || 'false')) {
//console.log(elm);
if (elm) elm.style.display = 'none';
return;
}
if (gcse_spawned) {
elm.style.display = 'block';
return;
}
gcse_spawned = true;
var cx = '016548008962335046113:f5uta2tifn4';
var gcse = document.createElement('script');
gcse.type = 'text/javascript';
gcse.async = true;
gcse.src = 'https://cse.google.com/cse.js?cx=' + cx;
var s = document.getElementsByTagName('script')[0];
s.parentNode.insertBefore(gcse, s);
}
document.addEventListener('DOMContentLoaded', function () {
var tab = document.getElementsByTagName('table')[0];
lastdir = localStorage.lastdir;
localStorage.removeItem('lastdir');
hook_sort(tab);
do_sort(tab);
var last = tab.getElementsByTagName('th')[3]
last.innerHTML="<span id='srch'>🔍</span>";
var srch = document.getElementById('srch');
srch.addEventListener('click', function(e) {
localStorage.search = !JSON.parse(localStorage.search || 'false');
spawn_gcse();
});
spawn_gcse();
});