Skip to content

Instantly share code, notes, and snippets.

@inverse
Last active February 10, 2016 06:02
Show Gist options
  • Select an option

  • Save inverse/4aed673c0b0d51dc2d3d to your computer and use it in GitHub Desktop.

Select an option

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
// @description Provide missing extra functionality to Last.fm
// @author Malachi Soord
// @match http://www.last.fm/music/*
// @require https://code.jquery.com/jquery-2.2.0.min.js
// @run-at document-idle
// ==/UserScript==
'use strict';
$(document).ready(function() {
var user = $('#mantle_skin > div.container.page-content > div > div.col-sidebar > div.kerve.widget.widget_trends').attr('data-user');
var users = getUsers();
var libraryUrl = 'http://www.last.fm/user/{user}/library/music/{artist}';
var url = window.location.href;
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');
$.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) {
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);
}
}
});
});
function getUsers() {
var users = GM_getValue('users', []);
if (users.length === 0) {
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