(function ($) { var username; // How many entries to show per category before expanding var initialCount = 3; var tasteometer = ""; var lastPlayedTrack = ""; var sharedArtists = ""; var topArtists = ""; var topAlbums = ""; var topTracks = ""; var expanded = false; // Failed request flag. var flag = 0; $(document).ready(function () { // Avoid conflicting with other jQuery instances (userscripts et al). $.noConflict(); // Fetch the username (appended from php) to base all get requests on. username = $('#lastfm_username').text(); var div = $('#lastfm_stats'); // Fetch the required data. // If data isn't cached, delays are issued in the class to avoid too many parallel requests to Last.fm getTasteometer(div); getLastPlayedTrack(div); getTopArtists(div); getTopAlbums(div); getTopTracks(div); // Allow expanding the show information to more than three entries. // Attach to document as lastfm_expand links are added dynamically when fetching the data. $(document).on('click', "#lastfm_expand", function () { // Make hidden entries visible and remove the expand button. if ( $(this).attr("href") == "#sharedartists" ){ sharedArtists = sharedArtists.replace(/\ class="hidden"/g,""); sharedArtists = sharedArtists.replace(/
  • \[/,""); } else if ( $(this).attr("href") == "#topartists" ){ topArtists = topArtists.replace(/\ class="hidden"/g,""); topArtists = topArtists.replace(/
  • \[/,""); } else if ( $(this).attr("href") == "#topalbums" ){ topAlbums = topAlbums.replace(/\ class="hidden"/g,""); topAlbums = topAlbums.replace(/
  • \[/,""); } else if ( $(this).attr("href") == "#toptracks" ){ topTracks = topTracks.replace(/\ class="hidden"/g,""); topTracks = topTracks.replace(/
  • \[/,""); } updateDivContents(div); }); // Allow expanding or collapsing the Last.fm data. $("#lastfm_expand").on('click', function () { if(expanded == false){ expanded = true; $(this).html("Show less info"); } else { expanded = false; $(this).html("Show more info"); } updateDivContents(div); }); // Hide the reload button until data is expanded. $("#lastfm_reload_container").addClass("hidden"); // Allow reloading the data manually. $.urlParam = function(name){ var results = new RegExp('[\\?&]' + name + '=([^&#]*)').exec(window.location.href); return results[1] || 0; } $("#lastfm_reload").on('click', function () { // Clear the cache and the necessary variables. $.get('user.php?action=lastfm_clear_cache&username=' + username + '&uid=' + $.urlParam('id'), function (response) { }); tasteometer = ""; lastPlayedTrack = ""; sharedArtists = ""; topArtists = ""; topAlbums = ""; topTracks = ""; // Revert the sidebar box to its initial state. $("#lastfm_stats").html(""); //$(".box_lastfm").children("ul").append('
  • Loading...
  • '); $("#lastfm_stats").append('
  • Loading...
  • '); // Remove the stats reload button. $("#lastfm_reload_container").remove(); getTasteometer(div); getLastPlayedTrack(div); getTopArtists(div); getTopAlbums(div); getTopTracks(div); }); }); // Allow updating the sidebar element contents as get requests are completed. function updateDivContents(div) { var html = ""; // Pass all data vars, gets that haven't completed yet append empty strings. html += tasteometer; html += lastPlayedTrack; html += sharedArtists; html += topArtists; html += topAlbums; html += topTracks; html += '
  • Loading...
  • '; div.html(html); // If the data isn't expanded hide most of the info. if( expanded == false ){ $("#lastfm_stats").children(":not(.lastfm_essential)").addClass("hidden"); $("#lastfm_reload_container").addClass("hidden"); } else { $("#lastfm_reload_container").removeClass("hidden"); } // Once all requests are completed, remove the loading message. if(tasteometer && lastPlayedTrack && sharedArtists && topArtists && topAlbums && topTracks){ $('#lastfm_loading').remove(); } } // Escape ampersands with html code to avoid breaking the search links function escapeAmp(input){ return input.replace(/&/g,"%26"); } // Functions for fetching the required data are as follows. // Also gets the data for shared artists as they're bundled. function getTasteometer(div) { if ($("#lastfm_stats").attr("data-uid")){ //Own profile, don't show tasteometer and shared artists. tasteometer = " "; sharedArtists = " "; } else { $.get('user.php?action=lastfm_compare&username=' + username, function (response) { // Two separate elements are received from one Last.fm API call. var tasteometerHtml = ""; var sharedArtistsHtml = ""; if ( response ){ json = JSON.parse(response); if (json != null && json['error']) { console.log("Tasteometer: " + json['message']); // Specified non-existant username for Last.fm, remove Last.fm box from page. if (json['error'] == "7" ){ tasteometer = " "; sharedArtists = " "; } } else if (json == null) { // No Last.fm compare possible. tasteometer = " "; sharedArtists = " "; } else { var j = json['comparison']['result']; var a = j['artists']['artist']; tasteometerHtml += '
  • Compatibility: '; var compatibility = Math.round(j['score'] * 100); var background; if (compatibility < 50){ background = 'rgb(255, '+Math.floor(255*compatibility/50)+', 0)' } else { background = 'rgb('+Math.floor((1-(compatibility-50)/50)*255)+', 255, 0)' } tasteometerHtml += compatibility + '%\r\
  • \r\
    \n\
    \n\
    \r\
    \r\
  • '; // Only print shared artists if there are any if (j['artists']['matches'] != 0){ sharedArtistsHtml += '
  • Shared artists:
  • '; sharedArtists = sharedArtistsHtml; } else { // Allow removing loading message regardless. sharedArtists = " "; sharedArtistsHtml += '
  • [Expand]
  • ' } tasteometerHtml += ""; tasteometer = tasteometerHtml; } } else { sharedArtists = " "; tasteometer = " "; } updateDivContents(div); }); } } function getLastPlayedTrack(div) { $.get('user.php?action=lastfm_last_played_track&username=' + username, function (response) { var html = ""; if ( response ){ var json = JSON.parse(response); if (json != null && json['error']) { console.log("Last played track: " + json['message']); lastPlayedTrack = " "; } else if (json == null) { // No last played track available. // Allow removing the loading message regardless. lastPlayedTrack = " "; } else { // Fix Last.fm API returning more than one entry despite limit on certain conditions. if ( typeof(json[0]) === "object" ) json = json[0]; html += '
  • Last played: '; html += '' + json['artist']['#text'] + ' - ' + json['name'] + ''; html += "
  • "; lastPlayedTrack = html; } } else { lastPlayedTrack = " "; } updateDivContents(div); }); } function getTopArtists(div) { $.get('user.php?action=lastfm_top_artists&username=' + username, function (response) { var html; if ( response ){ var json = JSON.parse(response); if (json != null && json['error']) { console.log("Top artists: " + json['message']); topArtists = " "; } else if (json == null) { console.log("Error: json == null"); topArtists = " "; } else if ( json['topartists']['total']==0 ) { // No top artists for the specified user, possibly a new Last.fm account. // Allow removing the loading message regardless. topArtists = " "; } else { html = "
  • Top Artists:
  • "; html += "
  • "; var j = json['topartists']['artist']; html += ''; html += "
  • "; topArtists = html; } } else { topArtists = " "; } updateDivContents(div); }); } function getTopAlbums(div) { $.get('user.php?action=lastfm_top_albums&username=' + username, function (response) { var html; if ( response ){ var json = JSON.parse(response); if (json != null && json['error']) { console.log("Top albums: " + json['message']); topAlbums = " "; } else if (json == null) { console.log("Error: json == null"); topAlbums = " "; } else if ( json['topalbums']['total']==0 ) { // No top artists for the specified user, possibly a new Last.fm account. // Allow removing the loading message regardless. topAlbums = " "; } else { var j = json['topalbums']['album']; html = "
  • Top Albums:
  • "; html += "
  • "; html += ''; html += "
  • "; topAlbums = html; } } else { topAlbums = " "; } updateDivContents(div); }); } function getTopTracks(div) { $.get('user.php?action=lastfm_top_tracks&username=' + username, function (response) { var html; if ( response ){ var json = JSON.parse(response); if (json != null && json['error']) { console.log("Toptracks: " + json['message']); topTracks = " "; } else if (json == null) { console.log("Error: json == null"); topTracks = " "; } else if ( json['toptracks']['total']==0 ) { // No top artists for the specified user, possibly a new Last.fm account. // Allow removing the loading message regardless. topTracks = " "; } else { html = "
  • Top Tracks:
  • "; html += "
  • "; var j = json['toptracks']['track']; html += ''; html += "
  • "; topTracks = html; } } else { topTracks = " "; } updateDivContents(div); }); } })(jQuery);