Revision 653137353662 () - Diff

Link to this snippet: https://friendpaste.com/5I4gCia8xh1fOH1oQ7c5JQ
Embed:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
// ==UserScript==
// @name Last.fm - Real Names
// @namespace none
// @include http://www.last.fm*
// @include http://www.lastfm.*
// @include http://cn.last.fm*
// ==/UserScript==

function trim(str) {
return str.replace(/^\s+/, '').replace(/\s+$/, '');
}

function getRealName(nick, elem, sliceBy, addRealName) {
GM_xmlhttpRequest({
method: 'GET',
url: 'http://ws.audioscrobbler.com/2.0/?method=user.getinfo&user='+nick+'&api_key=b25b959554ed76058ac220b7b2e0a026',
headers: {
'User-agent': 'Mozilla/4.0 (compatible) Greasemonkey/0.3',
'Accept': 'text/xml'
},
onload: function(response) {
if (!response.responseXML) {
var xml = (new DOMParser()).parseFromString(response.responseText, 'text/xml');
} else {
var xml = response.responseXML;
}
var realName = xml.getElementsByTagName('realname')[0].firstChild.nodeValue;
if (realName) {
if (addRealName == true) {
var newText = realName.slice(0,sliceBy)+' ('+nick+')';
} else {
var newText = realName.slice(0,sliceBy);
}
var replacedText = new RegExp(nick, 'gi');
if (!elem.hasAttribute('realNameAdded')) {
elem.innerHTML = elem.innerHTML.replace(replacedText, newText);
elem.setAttribute('realNameAdded', true);
}
}
}
});
}

function changeName(topElem, sliceBy, addRealName) {
var re = /^http:\/\/(.*\.|)(last\.fm|lastfm\.[^\/]+)\/user\/([^\?#]*)$/i;
var elems = topElem.getElementsByTagName('a');
for (var i = 0; i < elems.length; i++) {
var elem = elems[i];
if (!elem.hasAttribute('realNameAdded')) {
if (m = re.exec(elem.href)) {
parts = m[3].split('/');
if (!parts[1]) {
getRealName(parts[0], elem, sliceBy, addRealName);
}
}
}
}
}

var profileLinks = document.getElementById('profileLinks');
if (profileLinks)
changeName(profileLinks, 26, false);
var aboutMe = document.getElementById('aboutMe');
if (aboutMe)
changeName(aboutMe, 30, false);

var shoutList = document.getElementById('shoutList');
if (shoutList)
changeName(shoutList, 40, true);

var recentDiscussions = document.getElementById('recentDiscussions');
if (recentDiscussions)
changeName(recentDiscussions, 60, true);

changeName(document.body, 20, false);
document.addEventListener('DOMNodeInserted', function(ev){ changeName(ev.originalTarget, 15, true); }, true);