Skip to content

Instantly share code, notes, and snippets.

@inverse
Last active February 10, 2016 06:02
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save inverse/4aed673c0b0d51dc2d3d to your computer and use it in GitHub Desktop.
Save inverse/4aed673c0b0d51dc2d3d to your computer and use it in GitHub Desktop.
Last.fm Extra
// ==UserScript==
// @name Last.fm Extra
// @namespace https://malachisoord.com/
// @version 0.1.3
// @description Provide missing extra functionality to Last.fm
// @author Malachi Soord
// @match http://www.last.fm/*
// @require https://code.jquery.com/jquery-2.2.0.min.js
// @run-at document-idle
// ==/UserScript==
'use strict';
$(document).on('ready', function() {
var url = window.location.href;
console.log('xx: ' + url);
if (!url.match(/^http:\/\/www\.last.fm\/([a-z]{2}\/)?music\/[^\/]+$/)) {
return;
}
console.log('xx: running');
var users = getUsers();
var libraryUrl = 'http://www.last.fm/user/{user}/library/music/{artist}';
var artist = url.substring(url.lastIndexOf('/') + 1);
libraryUrl = libraryUrl.replace('{artist}', artist);
var template = '<div id="listening" class="kerve widget widget_listeningnow"><div class="nowwrap noselect"></div></div>';
$(template).insertAfter('#mobile_pos_4');
var found = false;
$.each(users, function(index, user) {
var requestUrl = libraryUrl.replace('{user}', user);
$.ajax(requestUrl, {
success: function(data) {
var $data = $(data);
var scrobbles = $data.find('#mantle_skin > div.container.page-content > div > div.col-main > ul > li:nth-child(1) > p').html() || 0;
if (scrobbles !== 0) {
found = true;
var pictureUrl = $data.find('div.header-avatar > div > a > img').prop('src');
var userTemplate = $('<div style="padding-bottom:5px;"><a href="' + requestUrl + '"><img src="' + pictureUrl + '" class="avatar" style="width:40px;margin-right:10px;" title ="' + user +'"/>' + scrobbles + ' scrobbles</a></div>');
$('#listening').append(userTemplate);
} else if (index === (users.length-1) && !found) {
$('#listening').append('None of your friends listen to this artist :(');
}
}
});
});
function getUsers() {
var users = GM_getValue('users', []);
if (users.length === 0) {
var user = $('#mantle_skin > div.container.page-content > div > div.col-sidebar > div.kerve.widget.widget_trends').attr('data-user');
var followingUrl = 'http://www.last.fm/user/{user}/following?page='.replace('{user}', user);
loadUsers(followingUrl, users, 1);
GM_setValue('users', users);
}
return users;
}
function loadUsers(followingUrl, users, page) {
var response = $.ajax({
type: 'GET',
url: followingUrl + page++,
async: false
}).responseText;
var $response = $(response);
var $users = $(response).find('div.username > a');
$.each($users, function() {
users.push($.trim($(this).text()));
});
var $next = $response.find('li.next > a');
if ($next.length === 1) {
loadUsers(followingUrl, users, page);
}
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment