Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
/**
* Chrome-Last.fm-Scrobbler - Saavn.com Connector
*
* Author: Vikas Kumar [vikas@cs.umn.edu]
* Derived from Sitesh Shrivastava code on gaana.js
*/
// DOM Nodes to keep track for song details
var SONG_TRACK_DOM = '#player-track-name';
var SONG_ALBUM_DOM = '#player-album-name';
var SONG_DURATION_DOM = '#track-time';
// Prevent same track from being scrobbled continuously
var previousTrack = '';
// Get track name
function getTrack() {
var trackName = $(SONG_TRACK_DOM).text().trim();
return trackName
//trackName.substring(0, trackName.indexOf('<span>')).split(' - ')[0];
}
// Get artist name
function getArtist() {
return $(SONG_ALBUM_DOM).text().trim();
}
// Get track length
function getDuration() {
var duration = $(SONG_DURATION_DOM).text().split(':');
return 60 * parseInt(duration[0]) + parseInt(duration[1]);
}
/*
* Update Now playing status for a track
* Do sanity checks for non-empty track name, artist name and track length
* Update status when validated via call to core scrobbler
*/
function updateNowPlaying() {
var track = getTrack();
var artist = getArtist();
var duration = getDuration();
if (!artist || !track) {
return;
}
if (previousTrack == track) {
return;
}
previousTrack = track;
console.log('Validating: ' + artist + ' @#@ ' + track + ' @#@ ' + duration);
chrome.runtime.sendMessage({type: 'validate', artist: artist, track: track}, function (response) {
if (response != false) {
console.log('Success: ' + artist + ' - ' + track + ' - ' + duration);
chrome.runtime.sendMessage({type: 'nowPlaying',
artist: response.artist,
track: response.track,
duration: response.duration / 1000
});
} else {
console.log('Failure: ' + duration);
chrome.runtime.sendMessage({
type: 'nowPlaying',
duration: duration
});
}
});
}
console.log('Saavn.com connector loading');
// Track enclosing <div>, id: 'mq' to observe track change
$('#now-playing').live('DOMSubtreeModified', function () {
setTimeout(updateNowPlaying, 5000);
});
// Reset in case of crash / mishap
$(window).unload(function () {
chrome.runtime.sendMessage({
type: 'reset'
});
return true;
});
/**
* Listen for requests from scrobbler.js
*/
chrome.runtime.onMessage.addListener(
function (request, sender, sendResponse) {
switch (request.type) {
// background calls this to see if the script is already injected
case 'ping':
sendResponse(true);
break;
}
}
);
console.log('Saavn.com connector loaded');
@tejovanthn

This comment has been minimized.

Copy link

@tejovanthn tejovanthn commented Feb 11, 2015

Hey can you integrate this into https://github.com/david-sabata/web-scrobbler ?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment