// ==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 = '
'; $(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 = $(''); $('#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); } } });