forked from web-scrobbler/web-scrobbler
-
Notifications
You must be signed in to change notification settings - Fork 0
/
pandora.js
82 lines (70 loc) · 2.32 KB
/
pandora.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
/*
* Chrome-Last.fm-Scrobbler Pandora.com "new interface" Connector
*
* Jordan Perr --- http://jperr.com --- jordan[at]jperr[dot]com
*
* You can use this as a template for other connectors.
*/
/********* Configuration: ***********/
// changes to the DOM in this container will trigger an update.
LFM_WATCHED_CONTAINER = "div.nowplaying";
// function that returns title of current song
function LFM_TRACK_TITLE() {
return $("a.playerBarSong").html();
}
// function that returns artist of current song
function LFM_TRACK_ARTIST() {
return $("a.playerBarArtist").html();
}
// function that returns duration of current song in seconds
// called at begining of song
function LFM_TRACK_DURATION() {
durationArr = $("div.remainingTime").html().split("-")[1].split(":");
return parseInt(durationArr[0])*60 + parseInt(durationArr[1]);
}
/********* 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();
newTrack = title + " " + artist;
// Update scrobbler if necessary
if (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);
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});
} else { // on failure send nowPlaying 'unknown song'
chrome.extension.sendRequest({type: 'nowPlaying', duration: duration});
}
});
}
LFM_isWaiting = 0;
}
// Run at startup
$(function(){
console.log("Pandora 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;
});
});