Permalink
Browse files

Merge remote branch 'shamer/jango_connector'

Adds Jango.com support (scrobbling is little delayed but works just fine), closes #60
  • Loading branch information...
2 parents 8cbaa75 + 31cbe4c commit 564e4147d053c6710c8fc38559e58498c16eae6a @david-sabata committed Jun 13, 2012
Showing with 122 additions and 0 deletions.
  1. +54 −0 jango-dom-inject.js
  2. +61 −0 jango.js
  3. +5 −0 manifest.json
  4. +2 −0 options.html
View
@@ -0,0 +1,54 @@
+/*
+ * Chrome-Last.fm-Scrobbler jango.com connector
+ * (c) 2012 Stephen Hamer <stephen.hamer@gmail.com>
+ */
+
+// The attach_artist_pic method gets called in the evaluated javascript
+// returned when songs are switched to update the pictures associated with the
+// artist. This method is used as the trigger for scrobbling the song.
+var orig_attach_artist_pic;
+
+function chromeLastFMUpdateNowPlaying(skip_callback){
+ var sm = _jp.soundManager;
+
+ if (!sm) {
+ // The sound manager hasn't loaded yet, poll for it to load so we can get the song details.
+ setTimeout(function() { chromeLastFMUpdateNowPlaying(true) }, 1000);
+
+ } else {
+ var latest_sound = sm.sounds[sm.soundIDs[sm.soundIDs.length - 1]];
+
+ var duration = null;
+ if (latest_sound.bytesLoaded === null || latest_sound.bytesLoaded != latest_sound.bytesTotal) {
+ // The soung is still loading, continue to poll for it to load.
+ setTimeout(function() { chromeLastFMUpdateNowPlaying(true) }, 5000);
+ } else {
+ duration = latest_sound.duration / 1000;
+ }
+
+ // We can start updating the attached pics before the sound manager has
+ // loaded the track (and knows the duration).
+ if (duration) {
+ var songInfo = {'title': _jm.song_info.song, 'artist': _jm.song_info.artist, 'duration': duration};
+ var commDiv = document.getElementById('chromeLastFM');
+ commDiv.innerText = JSON.stringify(songInfo);
+ }
+ }
+
+ // Only call the original method once, don't call it each time we poll the
+ // sound manager for the song duration
+ if (!skip_callback) {
+ orig_attach_artist_pic();
+ }
+}
+
+function waitUi() {
+ //console.log("waiting for _jui");
+ if (window["_jui"] === undefined || _jui.jango_player === undefined) {
+ setTimeout(waitUi, 1000);
+ return;
+ }
+ orig_attach_artist_pic = _jui.jango_player.attach_artist_pic.bind(_jui.jango_player);
+ _jui.jango_player.attach_artist_pic = chromeLastFMUpdateNowPlaying;
+}
+waitUi();
View
@@ -0,0 +1,61 @@
+/*
+ * Chrome-Last.fm-Scrobbler jango.com connector
+ * (c) 2012 Stephen Hamer <stephen.hamer@gmail.com>
+ */
+
+// Used to cache the song title to prevent repeated calls to updateNowPlaying
+var lastSongTitle = '';
+
+function updateNowPlaying() {
+ var commDiv = document.getElementById('chromeLastFM');
+ try {
+ var songInfo = JSON.parse(commDiv.innerText);
+ } catch (e) {
+ // Skip malformed communication blobs
+ return;
+ }
+
+ if (songInfo['title'] == lastSongTitle) {
+ return;
+ }
+ lastSongTitle = songInfo['title'];
+
+ // Update scrobbler
+ console.log('submitting a now playing request. artist: ' + songInfo['artist'] + ', title: ' + songInfo['title'] + ', duration: ' + songInfo['duration']);
+ chrome.extension.sendRequest({'type': 'validate', 'artist': songInfo['artist'], 'track': songInfo['title']}, function(response) {
+ if (response != false) {
+ chrome.extension.sendRequest({type: 'nowPlaying', 'artist': songInfo['artist'], 'track': songInfo['title'], 'duration': songInfo['duration']});
+ } else {
+ // on validation failure send nowPlaying 'unknown song'
+ chrome.extension.sendRequest({'type': 'nowPlaying', 'duration': songInfo['duration']});
+ }
+ });
+}
+
+// Run at startup, inject hooks into Jango application javascript to catch song changes.
+$(document).ready(function(){
+ console.log('Jango module starting up');
+
+ // XXX(shamer): we can't directly access the page javascript from the
+ // extension. To get code running in the page context we have to add a script tag
+ // to the DOM. The script then is executed in the global context.
+ // To communicate between the injected JS and the extension a DOM node is updated with the text to send.
+
+ var comNode = $('<div id="chromeLastFM" style="display: none"><span id="title"></span><span id="artist"></span><span id="duration"></span></div>');
+ document.body.appendChild(comNode[0]);
+
+ $('body').append('<script type="text/javascript">(function(l) {\n' +
+" var injectScript = document.createElement('script');\n" +
+" injectScript.type = 'text/javascript';\n" +
+" injectScript.src = l;\n" +
+" document.getElementsByTagName('head')[0].appendChild(injectScript);\n" +
+" })('" + chrome.extension.getURL('jango-dom-inject.js') + "');</script>");
+
+ // Listen for 'messages' from the injected script
+ $('#chromeLastFM').live('DOMSubtreeModified', updateNowPlaying);
+
+ $(window).unload(function() {
+ chrome.extension.sendRequest({type: 'reset'});
+ return true;
+ });
+});
View
@@ -79,6 +79,11 @@
"run_at": "document_idle"
},
{
+ "matches": ["*://www.jango.com/*"],
+ "js": ["jquery-1.6.1.min.js", "jquery.dump.js", "jango.js", "jango-dom-inject.js"],
+ "run_at" : "document_start"
+ },
+ {
"matches": ["*://www.pandora.com/*"],
"js": ["jquery-1.6.1.min.js", "jquery.dump.js", "pandora.js"],
"run_at" : "document_start"
View
@@ -55,6 +55,7 @@
<li>Z-music.org</li>
<li>VK.com</li>
<li>Zvooq.ru</li>
+ <li>Jango.com</li>
</ul>
</p>
<p>
@@ -93,6 +94,7 @@
<li><a href="http://sheckel.net/">Jacob Tolar</a> - Amazon Cloud Player support</li>
<li><a href="http://github.com/vintitres">vintitres</a> - Pitchfork support</li>
<li><a href="http://github.com/nobodyzzz">nobodyzzz</a> - Z-music.org, VK.com, Zvooq.ru support</li>
+ <li><a href="https://github.com/shamer">Stephen Hamer</a> - Jango.com support</li>
</ul>
</p>
</div>

0 comments on commit 564e414

Please sign in to comment.