forked from web-scrobbler/web-scrobbler
-
Notifications
You must be signed in to change notification settings - Fork 0
/
turntable.js
95 lines (77 loc) · 2.96 KB
/
turntable.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
/*
* Chrome-Last.fm-Scrobbler TurnTable.FM Connector
*
* Matt Runkle - matt[at]mrunkle[dot]com
*
* Inspired by Pandora connector, written by Jordan Perr, jordan[at]jperr[dot]com
*/
/*** Configuration ***/
// Additions to this div will trigger update
TT_WATCHED_CONTAINER = "div#song-log";
// Returns the currently playing artist name
function TT_getArtist( ) {
var details = $("div#song-log div.details span:first").html();
return TT_cleanLabel( details.replace(/^|<span.*\d+:\d+$/g, '') );
}
// Returns the currently playing track title
function TT_getTrack( ) {
return TT_cleanLabel( $("div#song-log div.title:first").text() );
}
// Returns the number of seconds left in the song
function TT_getTimeLeft( ) {
var remainingArr = $("div#time-left").text().split(":");
return parseInt( remainingArr[0] ) * 60 + parseInt( remainingArr[1] );
}
// Perform some common cleanup of artist/song strings
function TT_cleanLabel( label ) {
label = label.replace( '&', '&' );
return $.trim( label );
}
/*** Scrobbler Interface ***/
TT_LAST_TRACK = "";
TT_TIMEOUT = "";
// Validates and scrobbles the currently playing song
function TT_updateNowPlaying() {
var title = TT_getTrack();
var artist = TT_getArtist();
var duration = TT_getTimeLeft();
var newTrack = title + " " + artist;
// Update scrobbler if necessary
if ( newTrack != " " && newTrack != TT_LAST_TRACK ) {
//console.log("Submitting a now playing request. artist: "+artist+", title: "+title+", duration: "+duration);
TT_LAST_TRACK = newTrack;
chrome.extension.sendRequest({type: 'validate', artist: artist, track: title}, function(response) {
if (response != false) {
// submit the validated song for scrobbling
var song = response;
chrome.extension.sendRequest({type: 'nowPlaying', artist: song.artist, track: song.track, duration: duration});
} else {
// on failure send nowPlaying 'unknown song'
console.log( "Song validation failed!" );
chrome.extension.sendRequest({type: 'nowPlaying', duration: duration});
}
});
}
}
// Attach listener for song changes, filter out duplicate event firings
$(function(){
console.log("TurnTable.fm module starting up");
// Attach listener to "recently played" song list
$(TT_WATCHED_CONTAINER).live('DOMNodeInserted', function(e) {
//console.log( "Update triggered" );
var nowPlaying = TT_getTrack() + " " + TT_getArtist();
if ( nowPlaying != " " && nowPlaying != TT_LAST_TRACK ) {
// Stop any non-scrobbled songs (i.e. song skipped)
if( TT_TIMEOUT != "" ) {
clearTimeout( TT_TIMEOUT );
}
// Schedule the next scrobble
TT_TIMEOUT = setTimeout( TT_updateNowPlaying, 10000 );
return;
}
});
$(window).unload(function() {
chrome.extension.sendRequest({type: 'reset'});
return true;
});
});