forked from web-scrobbler/web-scrobbler
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ghostly.js
106 lines (73 loc) · 2.74 KB
/
ghostly.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
/*
* Chrome-Last.fm-Scrobbler Ghostly Discovery Connector by Peter McEvoy
* 2011-08-28
*/
var track, artist, album, duration
var scrobbleTimeout = null;
var durationTimeout = null;
$(function(){
//setup unload handler
$(window).unload(function() {
// reset the background scrobbler song data
chrome.extension.sendRequest({type: 'reset'});
return true;
});
//Start when user has picked a mood to listen to.
$("div#submit input").click(function(){
//Listen for change in result div
var c = 0;
$('div#result').bind('DOMNodeInserted', function(e) {
//HTML changed, NEW SONG!!!
//The jquery dom bind gets triggerd two times, this is to avoid the code from being executed twice.
if (c == 0) {c++; return false;}
// cancel any previous timeout
if (scrobbleTimeout != null)clearTimeout(scrobbleTimeout);
if (durationTimeout != null)clearTimeout(durationTimeout);
displayMsg()
//set vars
artist = $("dd.artist").text();
track = $("dd.track").text();
album = $("dd.album").text();
//Song duration on page updates as the music gets loaded by the browser. Might take a while before you get a fixed duration for the track. waitForDuration checks if the track duration has not changed in a while (the track should then be fully loaded)
waitForDuration(updateNowPlaying);
c=0;
})
})
});
function updateNowPlaying() {
if (artist == '' || track == '' || duration == 0) {return;}
//validate track info
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});
console.log({type: 'nowPlaying', artist: artist, track: track, duration: duration});
displayMsg("Scrobbling");
}
});
}
function waitForDuration(callback) {
function getDuration() {
var duration_el = $("li#info_position").text();
if (duration_el == "loading..." || duration_el == "0:00")return false;
var total_length = duration_el.split(" / ")[1];
return parseInt(total_length.split(":")[0]*60)+parseInt(total_length.split(":")[1]); // turn something like 5:50 into seconds
}
var check_1 = getDuration();
durationTimeout = setTimeout(function(){
var check_2 = getDuration();
if (check_1 == check_2 && check_1 != false && check_1 != 0 ){
//not loading anymore
duration = check_1;
callback();
}
else {
waitForDuration(callback);
}
}, 1000);
}
function displayMsg(msg) {
$('#chrome-scrobbler-status').remove();
if (msg) {
$('#playButton').append('<span id="chrome-scrobbler-status" title="">'+msg+'</span>');
}
}