forked from web-scrobbler/web-scrobbler
-
Notifications
You must be signed in to change notification settings - Fork 0
/
soundcloud.js
145 lines (122 loc) · 4.79 KB
/
soundcloud.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
/**
* Find first occurence of possible separator in given string
* and return separator's position and size in chars or null
*/
function findSeparator(str) {
// care - minus vs hyphen
var separators = [' - ', ' – ', '-', '–', ':'];
for (i in separators) {
var sep = separators[i];
var index = str.indexOf(sep);
if (index > -1)
return { index: index, length: sep.length };
}
return null;
}
/**
* Parse given string into artist and track, assume common order Art - Ttl
* @return {artist, track}
*/
function parseInfo(artistTitle) {
var artist = '';
var track = '';
var separator = findSeparator(artistTitle);
if (separator == null)
return { artist: '', track: '' };
artist = artistTitle.substr(0, separator.index);
track = artistTitle.substr(separator.index + separator.length);
return cleanArtistTrack(artist, track);
}
/**
* Clean non-informative garbage from title
*/
function cleanArtistTrack(artist, track) {
// Do some cleanup
artist = artist.replace(/^\s+|\s+$/g,'');
track = track.replace(/^\s+|\s+$/g,'');
// Strip crap
track = track.replace(/\s*\*+\s?\S+\s?\*+$/, ''); // **NEW**
track = track.replace(/\s*\[[^\]]+\]$/, ''); // [whatever]
track = track.replace(/\s*\([^\)]*version\)$/i, ''); // (whatever version)
track = track.replace(/\s*\.(avi|wmv|mpg|mpeg|flv)$/i, ''); // video extensions
track = track.replace(/\s*(of+icial\s*)?(music\s*)?video/i, ''); // (official)? (music)? video
track = track.replace(/\s*\(\s*of+icial\s*\)/i, ''); // (official)
track = track.replace(/\s*\(\s*[0-9]{4}\s*\)/i, ''); // (1999)
track = track.replace(/\s+\(\s*(HD|HQ)\s*\)$/, ''); // HD (HQ)
track = track.replace(/\s+(HD|HQ)\s*$/, ''); // HD (HQ)
track = track.replace(/\s*video\s*clip/i, ''); // video clip
track = track.replace(/\s+\(?live\)?$/i, ''); // live
track = track.replace(/\(\s*\)/, ''); // Leftovers after e.g. (official video)
track = track.replace(/^(|.*\s)"(.*)"(\s.*|)$/, '$2'); // Artist - The new "Track title" featuring someone
track = track.replace(/^(|.*\s)'(.*)'(\s.*|)$/, '$2'); // 'Track title'
track = track.replace(/^[\/\s,:;~-]+/, ''); // trim starting white chars and dash
track = track.replace(/[\/\s,:;~-]+$/, ''); // trim trailing white chars and dash
return {artist: artist, track: track};
}
(function() {
var options;
var current = {};
var song = function(ptitle) {
var title = ptitle.split(' by ');
var parsedInfo = parseInfo(title[0]);
if (parsedInfo.artist === '') {
parsedInfo.track = title[0];
parsedInfo.artist = title[1];
}
return {
artist: parsedInfo.artist,
track: parsedInfo.track,
duration: duration(title[0])
};
};
var duration = function(title) {
node = $('.sound.playing').not('.playlist').find('.timeIndicator__total');
if (node.length) {
// time is given as h.m.s
var digits = node[0].innerHTML.split(/\D/),
seconds = 0, length, digit;
length = digits.length;
if (length > 3)
return;
while (digits.length) {
d = digits.pop();
seconds += parseInt(d, 10) * Math.pow(60, length - digits.length - 1);
}
return seconds;
}
// if unknown duration, assume 2 minutes
return 120;
};
$(document).ready(function() {
var current_title = '';
$('title').live('DOMSubtreeModified', function() {
var title = $(this).text();
if (title[0] !== '▶' || current_title === title)
return;
current_title = title;
setTimeout(function () {
var s = song(title.substr(2));
chrome.extension.sendRequest({type: 'validate',
artist: s.artist,
track: s.track},
function(response) {
current.validated = response;
if (response !== false) {
chrome.extension.sendRequest({type: 'nowPlaying',
artist: response.artist,
track: response.track,
duration: s.duration});
}
else {
chrome.extension.sendRequest({type: 'nowPlaying',
duration: s.duration});
}
});
}, 500);
});
});
$(window).unload(function() {
chrome.extension.sendRequest({type: 'reset'});
return true;
});
})();