forked from web-scrobbler/web-scrobbler
-
Notifications
You must be signed in to change notification settings - Fork 0
/
fizy.js
90 lines (78 loc) · 3.29 KB
/
fizy.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
/*
* Chrome-Last.fm-Scrobbler fizy.com / fizy.org Connector by Yasin Okumus
* http://www.yasinokumus.com
*/
var player = "#nowplaying";
var playingText = "#nowPlayingText";
$(function(){
//if opened by song
process();
$(player).bind('DOMSubtreeModified', function(e){
process();
});
$(window).unload(function() {
// reset the background scrobbler song data
chrome.extension.sendRequest({type: 'reset'});
return true;
});
});
function process(){
var title = $(playingText).text().replace(/^\s*\[[^\]]+\]/, ''); // [minute-second] etc. ignored
var info = parseTitle(title);
var artist = info['artist'];
var track = info['track'];
if(track != undefined && artist != undefined){
chrome.extension.sendRequest({type: 'validate', artist: artist, track: track}, function(response) {
if (response != false){
var duration = (response['duration']/1000);
chrome.extension.sendRequest({type: 'nowPlaying', artist: artist, track: track, duration: duration});
}
});
}
}
function parseTitle(artistTitle){
var artist = '';
var track = '';
// Figure out where to split; use " - " rather than "-"
if (artistTitle.indexOf(' - ') > -1) {
artist = artistTitle.substring(0, artistTitle.indexOf(' - '));
track = artistTitle.substring(artistTitle.indexOf(' - ') + 3);
} else if (artistTitle.indexOf('-') > -1) {
artist = artistTitle.substring(0, artistTitle.indexOf('-'));
track = artistTitle.substring(artistTitle.indexOf('-') + 1);
} else if (artistTitle.indexOf(':') > -1) {
artist = artistTitle.substring(0, artistTitle.indexOf(':'));
track = artistTitle.substring(artistTitle.indexOf(':') + 1);
} else {
// can't parse
return false;
}
return cleanArtistTrack(artist, track);
}
/**
* Clean non-informative garbage from title
* (copied from youtube.js because some musics on fizy directly come from youtube)
*/
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};
}