Skip to content

Instantly share code, notes, and snippets.

@beeblebrox3
Created July 5, 2013 00:12
Show Gist options
  • Save beeblebrox3/5930910 to your computer and use it in GitHub Desktop.
Save beeblebrox3/5930910 to your computer and use it in GitHub Desktop.
xbox music connector for Chrome-Last.fm-Scrobbler
(function xboxConnector() {
// listen to changes in player
// wait for the duration appears
// validate
// if valid, scrobble
var working = false;
$(function() {
var $nowPlaying = $('.playerNowPlaying'),
$nowPlayingDuration = $('.playerDurationText');
function getCurrentTrack() {
var current_track = {
artist: $nowPlaying.find('[data-bind^="text: primaryArtis"]').attr('title'),
track: $nowPlaying.find('.primaryMetadata').attr('title'),
album: $nowPlaying.find('[data-bind^="text: parentAlbum.Name"]').attr('title'),
duration: $('[data-bind^="text: duration"]').text()
};
// current_track.duration = current_track.duration.split(':');
// current_track.duration = parseInt(current_track.duration[0])*60 + parseInt(current_track.duration[1]);
return current_track;
}
// listen to changes in player
$nowPlaying.bind('DOMSubtreeModified', function xpto() {
if (working) {
console.log ('working');
return;
} else {
working = true;
}
console.log('xpto');
// $nowPlaying.unbind('DOMSubtreeModified');
// wait for duration
$nowPlayingDuration.bind('DOMSubtreeModified', function(e) {
$nowPlayingDuration.unbind('DOMSubtreeModified');
setTimeout(function() {
console.log('xpto2');
var current_track = getCurrentTrack();
console.log(current_track);
//validate
current_track.type = 'validate';
chrome.runtime.sendMessage(current_track, function(song) {
console.log('validated');
if (song) {
// send
song.type = 'nowPlaying';
chrome.runtime.sendMessage(song);
working = false;
console.log('played');
// bind alterações player novamente
setTimeout(function() {
// $nowPlaying.bind('DOMSubtreeModified', xpto);
}, 3000);
} else {
console.log('Error identifying song');
}
});
}, 3000);
});
});
});
}());
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment