forked from web-scrobbler/web-scrobbler
-
Notifications
You must be signed in to change notification settings - Fork 0
/
slacker2.js
94 lines (79 loc) · 2.6 KB
/
slacker2.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
/*
* Chrome-Last.fm-Scrobbler Slacker.com Connector
*
* Uses Jordan Perr's pandora connector as a template --- http://jperr.com --- jordan[at]jperr[dot]com
*
* Joe Crawford [email protected]
*/
/********* Configuration: ***********/
// changes to the DOM in this container will trigger an update.
LFM_WATCHED_CONTAINER = "div#track-metadata";
function LFM_IS_A_SONG() {
return !$("#player-track-name").hasClass("noClick");
}
// function that returns title of current song
function LFM_TRACK_TITLE() {
return $("#player-track-name").text();
}
// function that returns artist of current song
function LFM_TRACK_ARTIST() {
return $("#player-artist-name").text();
}
// function that returns artist of current song
function LFM_TRACK_ALBUM() {
return $("#player-album-name").text();
}
// function that returns duration of current song in seconds
// called at begining of song
function LFM_TRACK_DURATION() {
durationArr = $('#total-length').text().split(":");
return parseInt(durationArr[0], 10)*60 + parseInt(durationArr[1], 10);
}
/********* Connector: ***********/
var LFM_lastTrack = "";
var LFM_isWaiting = 0;
function LFM_updateNowPlaying(){
// Acquire data from page
title = LFM_TRACK_TITLE();
artist = LFM_TRACK_ARTIST();
duration = LFM_TRACK_DURATION();
album = LFM_TRACK_ALBUM();
newTrack = title + " " + artist;
isASong = LFM_IS_A_SONG();
// Update scrobbler if necessary
if (isASong && newTrack != " " && newTrack != LFM_lastTrack){
if (duration === 0) {
// Nasty workaround for delayed duration visiblity with skipped tracks.
setTimeout(LFM_updateNowPlaying, 5000);
return 0;
}
console.log("submitting a now playing request. artist: "+artist+", title: "+title+", duration: "+duration + ", album: " + album);
LFM_lastTrack = newTrack;
chrome.extension.sendRequest({type: 'validate', artist: artist, track: title}, function(response) {
if (response !== false) {
chrome.extension.sendRequest({type: 'nowPlaying', artist: artist, track: title, duration: duration, album: album});
} else { // on failure send nowPlaying 'unknown song'
chrome.extension.sendRequest({type: 'nowPlaying', duration: duration});
}
});
}
LFM_isWaiting = 0;
}
// Run at startup
$(function(){
console.log("Slacker module starting up");
$(LFM_WATCHED_CONTAINER).live('DOMSubtreeModified', function(e) {
//console.log("Live watcher called");
if ($(LFM_WATCHED_CONTAINER).length > 0) {
if(LFM_isWaiting === 0){
LFM_isWaiting = 1;
setTimeout(LFM_updateNowPlaying, 10000);
}
return;
}
});
$(window).unload(function() {
chrome.extension.sendRequest({type: 'reset'});
return true;
});
});