last.fm profile

By slosd Last update Jun 3, 2009 — Installed 1,734 times. Daily Installs: 2, 2, 2, 1, 0, 0, 3, 1, 1, 0, 5, 3, 2, 2, 4, 0, 2, 2, 1, 0, 2, 2, 3, 1, 0, 2, 0, 2, 5, 4, 2, 2

There are 11 previous versions of this script.

// ==UserScript==
// @name           last.fm profile
// @namespace      slosd@freedig.org
// @description    Replaces the playlist player with the user information on profile pages
// @include        http://www.last.fm/user/*
// @include        http://www.lastfm.*/user/*
// ==/UserScript==

function XPath(xpath, parent) {
	this.elements = new Array;
	if(typeof xpath == "object" && typeof xpath.length != "undefined")
		for(var i = 0; i < xpath.length; i++)
			this.evaluate(xpath[i], parent);
	else if(typeof xpath == "string")
		this.evaluate(xpath, parent);
	else
		return this;
};
XPath.prototype.evaluate = function(xpath, parent) {
	if(!parent) var parent = document;
	var snapshot = document.evaluate(xpath, parent, null, XPathResult.ORDERED_NODE_SNAPSHOT_TYPE, null);
	for(var i = 0; i < snapshot.snapshotLength; i++)
		this.elements[this.elements.length] = snapshot.snapshotItem(i);
};
XPath.prototype.getElement = function(n) {
	if(!n) var n = 0;
	return this.elements[n];
};
XPath.prototype.getElements = function() {
	return this.elements;
};
XPath.prototype.empty = function() {
	return (this.elements.length == 0);
}

function walk(element, walk, start){
	if(!element) return null;
	var el = element[start || walk];
	while(el && el.nodeType != 1) el = el[walk];
	return el;
};

function getFirst(element){
	return walk(element, 'nextSibling', 'firstChild');
};

function getNext(element){
	return walk(element, 'nextSibling', null);
};

(function() {

	var profile_part, element;
	var player = document.getElementById("player");
	var right_col = new XPath("//div[@class='rightCol']").getElement();
	
	// get the first header	
	var first_header = document.getElementById("recentTracks");
	
	if(player) {
		// remove player
		player.removeChild(document.getElementById("flashContainer"));
		player.style.height = "auto";
		var container = player;
	}
	
	// if there is no player...
	else {
		// ...create a container
		var container = document.createElement("div");
		right_col.insertBefore(container, right_col.firstChild);
	}
	
	// get all profile related elements...
	var profile = document.getElementById("userBadge");

	// ...and append them to the container
	container.appendChild(profile);
	
	// move tasteometer and buttons
	var tasteometer = document.getElementById("tasteometer");
	if(tasteometer) {

		var user_avatar = new XPath("//div[@class='badgeAvatar']", profile).getElement();
		var user_options = new XPath("//div[@class='profileOptions clearit']").getElement();
		var buttons = new XPath("//ul[@class='buttons']", user_options).getElement();
		
		user_avatar.appendChild(buttons);
		buttons.style.cssFloat = "left";
		buttons.style.margin = "13px 0 3px";
		
		container.appendChild(tasteometer);
		
		user_options.parentNode.removeChild(user_options);
		
		var friend_button = document.getElementById("button1");
		var friend_ignore_button = document.getElementById("button2");
		var button_classes = new RegExp("(lfmButton|lfmBigButton|lfmBefriendButton)", "g");
		
		if(friend_button) {
			friend_button.className = friend_button.className.replace(button_classes, "");
			var friend_strong = getFirst(friend_button);
			var friend_img = getFirst(friend_strong);
			
			friend_strong.style.fontWeight = "normal";
			friend_img.style.margin = "-6px 5px 0 0";
		}
		if(friend_ignore_button) {
			friend_ignore_button.className = friend_ignore_button.className.replace(button_classes, "");
			getFirst(friend_ignore_button).style.fontWeight = "normal";
		}
		
		var friend_indicator = new XPath("//span[@class='friendIndicator pendingIndicator']");
		var is_friend_img = new XPath("//img[@class='icon isfriend_icon']");
		
		// pending
		if(!friend_indicator.empty()) {
			with(friend_indicator.getElement().style) {
				display = "block";
				width = "130px";
				textAlign = "center";
			}
		}
		// friend
		else if(!is_friend_img.empty()) {
			is_friend_img.getElement().style.margin = "0 5px 0 4px";
		}
		// not a friend
		else {}
		
		var mail_img = new XPath("//img[@class='icon mailuser_icon']").getElement();
		mail_img.style.margin = "0 5px 0 4px";
		
		var shout_img = new XPath("//img[@class='icon comment_icon']");
		if(!shout_img.empty())
			shout_img.getElement().style.margin = "0 5px -4px 4px";

	}

	// change playcount style
	var user_plays = new XPath("//span[@class='userPlays']").getElement();
	with(getNext(getFirst(user_plays)).style) {
		display = "block";
	}
	
	// other style adjustments
	container.style.height = "auto";
	first_header.style.marginTop = "16px";

})();