-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathWILT.js
383 lines (335 loc) · 8.96 KB
/
WILT.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
/*
* If you ain't first, you're Last(.fm).
*
* Supports:
* Pandora
* GooglePlay
* Bandcamp
* Spotify
* YouTube(soontm)
*
*/
/////////////////////////////////////////
// Credentials / extension util
/////////////////////////////////////////
let ENV = 'live';
let PLEXURL = 'plex_ip';
let API;
let drfHeader;
chrome.storage.sync.get(['drfHeader'], (items) => {
drfHeader = items.drfHeader;
});
switch (ENV) {
case 'dev':
API = 'http://localhost:8000/api/';
break;
case 'live':
API = 'https://wilt.fm/api/';
}
let isPandora;
let isBandcamp;
let isGoogle;
let isYouTube;
let isPlex;
let isSpotify;
let isMutant;
let lastSong;
let isAuth = false;
if (window.location.href.indexOf('pandora') > -1) {
isPandora = true;
} else {
isPandora = false;
}
if (window.location.href.indexOf('mutantradio') > -1) {
isMutant = true;
} else {
isMutant = false;
}
if (window.location.href.indexOf('play.google') > -1) {
isGoogle = true;
} else {
isGoogle = false;
}
if (window.location.href.indexOf('watch?v=') > -1) {
isYouTube = true;
} else {
isYouTube = false;
}
if (window.location.href.indexOf('bandcamp') > -1) {
isBandcamp = true;
} else {
isBandcamp = false;
}
if (window.location.href.indexOf(PLEXURL) > -1) {
isPlex = true;
} else {
isPlex = false;
}
if (window.location.href.indexOf('open.spotify') > -1) {
isSpotify = true;
} else {
isSpotify = false;
}
console.log(`
YouTube: ${isYouTube}\n
Pandora: ${isPandora}\n
GooglePlay: ${isGoogle}\n
BandCamp: ${isBandcamp}\n
PleX: ${isPlex}\n
Spotify: ${isSpotify}\n
MutantRadio: ${isMutant}\n
`);
/////////////////////////////////////////
// What I Listen To
/////////////////////////////////////////
function isAuthenticated() {
if (isAuth) {
return true;
} else {
return false;
}
}
function scrobble(song, artist, album) {
if (!song.length || !artist.length) {
console.log('Prevented empty scrobble.');
} else {
chrome.runtime.sendMessage({
method: 'POST',
action: 'xhttp',
url: `${API}scrobbles/`,
data: `song=${song}&artist=${artist}&album=${album}`,
drf: drfHeader
}, function(responseText) {
console.log(`Scrobbled ${song} by ${artist} on the album ${album}`);
});
}
}
/////////////////////////////////////////
// YouTube
/////////////////////////////////////////
let tag;
function getContentTag() {
return new Promise((resolve, reject) => {
let content = document.getElementsByClassName('watch-info-tag-list')[0];
resolve(content.getElementsByTagName('a')[0].innerHTML);
});
}
// getContentTag().then(r => tag = r);
/////////////////////////////////////////
// Mutant Radio (Whoever wrote this site fucking sucks)
/////////////////////////////////////////
function mutantRadio() {
return new Promise((resolve, reject) => {
let jumbo = document.getElementsByClassName('jumbotron container')[0];
let track = jumbo.getElementsByTagName('p')[0].innerHTML;
resolve({
'song': track.split('<br>')[1],
'artist': track.split('<b>')[1].split('</b>')[0]
});
});
}
function mutantLoop() {
mutantRadio().then((track) => {
if (lastSong != track.song) {
lastSong = track.song;
scrobble(track.song, track.artist);
}
});
}
/////////////////////////////////////////
// Google Play
/////////////////////////////////////////
function getGooglePlayer() {
return new Promise((resolve, reject) => {
resolve(document.getElementsByClassName('currently-playing-details')[0]);
});
}
// temp (lol jk)
let googlePlayer;
function getGoogleAlbum() {
return new Promise((resolve, reject) => {
resolve(googlePlayer.getElementsByClassName('player-album')[0].innerHTML);
});
}
function getGoogleArtist() {
return new Promise((resolve, reject) => {
resolve(googlePlayer.getElementsByClassName('player-artist')[0].innerHTML);
});
}
function getGoogleSong() {
return new Promise((resolve, reject) => {
resolve(document.getElementById('currently-playing-title').innerHTML);
});
}
function googleLoop() {
getGooglePlayer().then((_player) => {
googlePlayer = _player;
getGoogleAlbum().then((_album) => {
getGoogleSong().then((_song) => {
getGoogleArtist().then((_artist) => {
if (lastSong != _song) {
lastSong = _song;
scrobble(_song, _artist, _album);
}
});
});
});
});
}
/////////////////////////////////////////
// Pandora
/////////////////////////////////////////
let ELEMENTS = {
'SONG': 'Marquee__wrapper__content',
'SONG_LONG': 'Marquee__wrapper__content__child',
'ALBUM': 'nowPlayingTopInfo__current__albumName nowPlayingTopInfo__current__link',
'ARTIST': 'nowPlayingTopInfo__current__artistName nowPlayingTopInfo__current__link'
}
function getSong() {
return new Promise((resolve, reject) => {
let song;
try {
song = document.getElementsByClassName(ELEMENTS.SONG)[0].innerHTML;
} catch(err) {
song = document.getElementsByClassName(ELEMENTS.SONG_LONG)[0].innerHTML;
}
resolve(song);
});
}
function getAlbum() {
return new Promise((resolve, reject) => {
let album = document.getElementsByClassName(ELEMENTS.ALBUM)[0].innerText;
resolve(album);
});
}
function getArtist() {
return new Promise((resolve, reject) => {
let artist = document.getElementsByClassName(ELEMENTS.ARTIST)[0].innerHTML;
resolve(artist);
});
}
function pandoraLoop() {
getSong().then((_song) => {
getAlbum().then((_album) => {
getArtist().then((_artist) => {
if (lastSong != _song) {
lastSong = _song;
scrobble(_song, _artist, _album);
}
});
});
});
}
/////////////////////////////////////////
// Bandcamp
/////////////////////////////////////////
function getBandCampAlbum() {
return new Promise((resolve, reject) => {
let album = document.getElementsByClassName('trackTitle')[0].innerHTML;
resolve(album.trim());
});
}
function getBandCampSong() {
return new Promise((resolve, reject) => {
var song = document.getElementsByClassName('title')[0].innerHTML;
resolve(song.trim());
});
}
function getBandCampArtist() {
return new Promise((resolve, reject) => {
resolve(document.getElementById('name-section').getElementsByTagName('a')[0].innerHTML);
});
}
function bandCampLoop() {
getBandCampAlbum().then((_album) => {
getBandCampSong().then((_song) => {
getBandCampArtist().then((_artist) => {
if (lastSong != _song) {
lastSong = _song;
scrobble(_song, _artist, _album);
}
});
});
});
}
/////////////////////////////////////////
// PleX (Web player)
/////////////////////////////////////////
function getPlexArtist() {
return new Promise((resolve, reject) => {
let player = document.getElementsByClassName('grandparent-title-container')[0];
resolve(player.getElementsByTagName('button')[0].innerHTML);
});
}
function getPlexSong() {
return new Promise((resolve, reject) => {
resolve(document.getElementsByClassName('item-title btn-link')[0].innerHTML);
});
}
function getPlexAlbum() {
return new Promise((resolve, reject) => {
resolve(document.getElementsByClassName(
'media-poster loaded')[1].getAttribute('data-parent-title'));
});
}
function plexLoop() {
getPlexArtist().then(_artist => {
getPlexSong().then(_song => {
getPlexAlbum().then(_album => {
if (lastSong != _song) {
lastSong = _song;
scrobble(_song, _artist, _album);
}
});
});
});
}
/////////////////////////////////////////
// Spotify
////////////////////////////////////////
function getSpotify() {
return new Promise((resolve, reject) => {
let song = document.getElementsByClassName('track-info__name')[0].textContent;
let artist = document.getElementsByClassName('track-info__artists')[0].textContent;
resolve({song, artist});
});
}
function spotifyLoop() {
getSpotify().then(track => {
if (lastSong != track.song) {
lastSong = track.song;
scrobble(track.song, track.artist);
}
});
}
/////////////////////////////////////////
// Main function (loop)
/////////////////////////////////////////
function main() {
setInterval(() => {
if (isPandora) {
pandoraLoop();
}
if (isBandcamp) {
bandCampLoop();
}
if (isGoogle) {
googleLoop();
}
if (isPlex) {
plexLoop();
}
if (isSpotify) {
spotifyLoop();
}
if (isMutant) {
mutantLoop();
}
}, 5000);
}
/* wait 10 seconds before starting loop.
this is for single paged web apps that
take time loading dom elements after
the navigation has already technically
ended. */
main();