Created
September 11, 2014 12:32
-
-
Save chrisdone/dec52a5995290bab5d27 to your computer and use it in GitHub Desktop.
grooveshark broadcast tracks
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// ==UserScript== | |
// @name Grooveshark broadcast tracks | |
// @namespace chrisdone | |
// @description Grooveshark broadcast tracks | |
// @include http://grooveshark.com/* | |
// @require http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js | |
// @version 1 | |
// @grant none | |
// ==/UserScript== | |
function main(){ | |
function parseTime(t){ | |
var ps = t.split(/:/); | |
if (ps.length == 2) { | |
return (ps[0] * 60) + (ps[1] * 1); | |
} else return 0; | |
} | |
var $ = jQ; | |
// Submit listened tracks when they're played | |
var lastTitle = null; | |
var lastElapsed = 0; | |
setInterval(function(){ | |
var title = $('#now-playing-metadata a:first').text(); | |
var artist = $('#now-playing-metadata a:last').text(); | |
var release = ''; | |
var playing = $('#play-pause').hasClass('playing'); | |
var entry = { | |
artist: artist, | |
title: title, | |
release: release | |
}; | |
var elapsed = parseTime($('#time-elapsed').text()); | |
// console.log("title: %o, artist: %o, lastTitle: %o, playing: %o",title,artist,lastTitle,playing); | |
if (title != '' && artist != '' && playing) { | |
if ((title != lastTitle) || (elapsed < lastElapsed)) { | |
if (title != lastTitle) | |
console.log("New track, submitting: %o",entry); | |
else | |
console.log("Re-playing same tr, submitting: %o",entry); | |
lastTitle = title; | |
$.ajax({ | |
url: 'http://chrisdone.com:3939/submit', | |
type: 'GET', | |
dataType: 'jsonp', | |
data: entry | |
}); | |
} else { | |
console.log("Same track, same playback."); | |
} | |
} | |
lastElapsed = elapsed; | |
},10000); | |
// Submit loved tracks any time they're in the display | |
var lastLove = null; | |
setInterval(function(){ | |
var title = $('#now-playing-metadata a:first').text(); | |
var artist = $('#now-playing-metadata a:last').text(); | |
var release = ''; | |
var loved = $('#np-fav').hasClass('active'); | |
if (loved && title != '' && artist != '' && title + ' - ' + artist != lastLove) { | |
$.ajax({ | |
url: 'http://chrisdone.com:3939/love', | |
type: 'GET', | |
dataType: 'jsonp', | |
data: { | |
artist: artist, | |
title: title, | |
release: release | |
} | |
}); | |
lastLove = title + ' - ' + artist; | |
} | |
},500); | |
} | |
// a function that loads jQuery and calls a callback function when jQuery has finished loading | |
function addJQuery(callback) { | |
var script = document.createElement("script"); | |
script.setAttribute("src", "//ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"); | |
script.addEventListener('load', function() { | |
var script = document.createElement("script"); | |
script.textContent = "window.jQ=jQuery.noConflict(true);(" + callback.toString() + ")();"; | |
document.body.appendChild(script); | |
}, false); | |
document.body.appendChild(script); | |
} | |
// load jQuery and execute the main function | |
addJQuery(main); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment