forked from web-scrobbler/web-scrobbler
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ttnet.js
112 lines (91 loc) · 3.45 KB
/
ttnet.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
/*
*
* Chrome-Last.fm-Scrobbler TTnetMuzik.com.tr Connector by Yasin Okumus
* http://www.yasinokumus.com
* v0.2
* - Long song names problem fixed
* - "-" char in the song names problem fixed
* - "(Album Version)" in the song names will not scrobbling
*/
// track and artist will be stored
var track = '';
var artist = '';
var state = 'init';
// change element
var player = "#playingSong";
var time = "div.time";
$(function(){
$(player).bind('DOMSubtreeModified',function(e){
var songHtml = $(player).html();
if(songHtml.indexOf("playingSongLoading") > -1 && state != 'loading'){
state = 'loading';
}
// track starting here
else if(songHtml.indexOf("playingSongTitle") > -1 && state != 'playing'){
var title = $("div.playingSongTitle").attr("title");
separation = title.lastIndexOf(' - ');
var aTrack = title.substring(0, separation);
var anArtist = title.substring(separation + 3);
parsed = cleanArtistTrack(anArtist,aTrack);
artist = parsed['artist'];
track = parsed['track'];
state = 'playing';
//start listening duration loading
$(time).bind('DOMSubtreeModified',function(e){
// when get duration, try scrobbling
getDuration();
});
}
});
// bind page unload function to discard current "now listening"
$(window).unload(function() {
// reset the background scrobbler song data
chrome.extension.sendRequest({type: 'reset'});
return true;
});
});
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*\.(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*(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 getDuration(){
var durationDiv = $("div.time strong").text();
//if we have duration
if(durationDiv != ''){
//we have duration. stop listening (unbind)
$(time).unbind('DOMSubtreeModified');
// "now listening"
var duration = parseDuration(durationDiv);
chrome.extension.sendRequest({type: 'validate', artist: artist, track: track}, function(response) {
if (response != false){
chrome.extension.sendRequest({type: 'nowPlaying', artist: artist, track: track, duration: duration});
}
});
}
}
function parseDuration(match){
try{
mins = match.substring(0, match.indexOf(':'));
seconds = match.substring(match.indexOf(':')+1);
return parseInt(mins*60) + parseInt(seconds);
}catch(err){
return 0;
}
}