Last active
August 29, 2015 14:18
-
-
Save Zn4rK/5b4b28ec50c2114d1a89 to your computer and use it in GitHub Desktop.
Total Plays on Spotify.js
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
if(match = document.URL.match(/https:\/\/play\.spotify\.com\/artist\/(.+)/)) { | |
if (!window.jQuery) { | |
var script = document.createElement("script"); | |
script.src = "//ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"; | |
document.head.appendChild(script); | |
} | |
$(function() { | |
var iframe = $('iframe[id*=' + match[1] + ']').contents(); | |
var trackList = {}; | |
var total = 0; | |
var tracks = iframe.find('.tl-row'); | |
tracks.each(function(i, track) { | |
var track = $(track); | |
// Check if we have the popularity cell | |
var cell = track.find('.tl-listen-count'); | |
var count = 0; | |
if(cell.length == 0) { | |
// We should be looking at tl-listen-count instead | |
var possibleCount = track.find('.tl-popularity').data('tooltip').split(' '); | |
if(possibleCount.length > 2) { | |
count = possibleCount[1]; | |
} else { | |
count = possibleCount[0]; | |
} | |
var name = track.find('.tl-highlight').text().replace(/\s+/g, ' ').trim(); | |
var time = track.find('.tl-time').text().trim(); | |
var key = name + '|' + time + '|' + count; | |
// Push it to the total | |
trackList[key] = count; | |
} | |
}); | |
for (var prop in trackList) { | |
if(trackList.hasOwnProperty(prop)){ | |
total += parseInt(trackList[prop].replace(/,/g, '')); | |
} | |
} | |
alert('Total Plays: ' + total); | |
}); | |
} else { | |
alert('You need to be on an artist page on play.spotify.com to use this bookmarklet'); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment