forked from web-scrobbler/web-scrobbler
-
Notifications
You must be signed in to change notification settings - Fork 0
/
grooveshark.js
151 lines (127 loc) · 4.87 KB
/
grooveshark.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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
/*
* Chrome-Last.fm-Scrobbler Grooveshark Connector
*
* Matt Runkle - matt[at]mrunkle[dot]com
*
* Inspired by Pandora connector, written by Jordan Perr, jordan[at]jperr[dot]com
*
* Note, this connector will only scrobble when no user is authenticated. If you
* maintain a Grooveshark account, configure the built-in Last.fm scrobbling to
* keep track of song plays while logged-in to the site.
*/
/*** Configuration ***/
// Determines whether this plugin will scrobble
GS_SCROBBLE = true;
// Changes to this div may toggle scrobbling
GS_LOGIN_CONTAINER = "div#header-user-assets";
// Additions to this div will trigger update
GS_WATCHED_CONTAINER = "div#now-playing-metadata";
// Returns the currently playing artist name
function GS_getArtist( ) {
// Grab title element, since text portion can be truncated
var artist = $("div#now-playing-metadata a.artist").attr('title');
if( artist == undefined ) {
// Fall back to text portion if title attr is empty
artist = $("div#now-playing-metadata a.artist").text();
}
return GS_cleanLabel( artist );
}
// Returns the currently playing track title
// Grab title element, since text portion can be truncated
function GS_getTrack( ) {
var track = $("div#now-playing-metadata a.song").attr('title');
if( track == undefined ) {
// Fall back to text portion if title attr is empty
track = $("div#now-playing-metadata a.song").text();
}
return GS_cleanLabel( track );
}
// Returns the song length in seconds
function GS_getDuration( ) {
var timeArr = $("span#time-total").text().split(":");
return parseInt( timeArr[0] ) * 60 + parseInt( timeArr[1] );
}
// Perform some common cleanup of artist/song strings
function GS_cleanLabel( label ) {
label = label.replace( '&', '&' );
return $.trim( label );
}
/*** Scrobbler Interface ***/
GS_LAST_TRACK = "";
GS_TIMEOUT = "";
// Validates and scrobbles the currently playing song
function GS_updateNowPlaying() {
if( GS_SCROBBLE != true ) {
// Abort scrobbling
return;
}
var title = GS_getTrack();
var artist = GS_getArtist();
var duration = GS_getDuration();
var newTrack = title + " " + artist;
// Update scrobbler if necessary
if ( newTrack != " " && newTrack != GS_LAST_TRACK ) {
//console.log("Submitting a now playing request. artist: "+artist+", title: "+title+", duration: "+duration);
GS_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});
}
});
}
}
// Turns scrobbling on or off depending on user login state
function GS_updateScrobbling() {
var loginButton = $("a#header-login-btn")[0];
if( loginButton != undefined && !GS_SCROBBLE ) {
// "Sign In" button is present, non-logged in user
// OK to scrobble
console.log("User logout detected. Enabling Chrome Last.fm scrobbler on Grooveshark.");
GS_SCROBBLE = true;
} else if ( loginButton == undefined && GS_SCROBBLE ) {
// "Sign In" button is gone, assume user is logged in
// Use built-in Grookshark scrobbling
console.log("User login detected. Disabling Chrome Last.fm scrobbler on Grooveshark.");
GS_resetScrobbling();
GS_SCROBBLE = false;
}
}
// Resets the plugin, cancelling any queued scrobbles
function GS_resetScrobbling() {
console.log("Resetting scrobbler.");
chrome.extension.sendRequest({type: 'reset'});
}
// Attach listener for song changes, filter out duplicate event firings
$(function(){
console.log("Grooveshark scrobbling module starting up");
// Attach listener to login container
$(GS_LOGIN_CONTAINER).live('DOMSubtreeModified', function(e) {
GS_updateScrobbling();
});
// Attach listener to "recently played" song list
$(GS_WATCHED_CONTAINER).live('DOMSubtreeModified', function(e) {
//console.log( "Update triggered" );
var nowPlaying = GS_getTrack() + " " + GS_getArtist();
if ( nowPlaying != " " && nowPlaying != GS_LAST_TRACK ) {
// Stop any non-scrobbled songs (i.e. song skipped)
if( GS_TIMEOUT != "" ) {
clearTimeout( GS_TIMEOUT );
}
// Schedule the next scrobble
GS_TIMEOUT = setTimeout( GS_updateNowPlaying, 10000 );
return;
}
});
// Turn on scrobbling if user is logged out on page load
GS_updateScrobbling();
$(window).unload(function() {
GS_resetScrobbling();
return true;
});
});