-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
177 lines (156 loc) · 4.49 KB
/
script.js
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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
var artistMap = {};
var userMap = {};
//TODO: https://github.com/lloyd/JSONSelect
function artistJSONToHtml(artist)
{
var infoHtml = $("<div />");
var similar = '<div id="similar" align="center"><table><tr colspan="5"><b style="font-size:120%">Similar Artists</b></tr><tr>'
infoHtml.append('<b style="font-size:150%">{0}</b><br />'.fmt(artist.name))
if(artist.image[3]["#text"].length)
{
infoHtml.append('<img src="{0}" align="left"/>'.fmt(artist.image[3]["#text"]));
}
if(typeof(artist.tags.tag) != "undefined")
{
infoHtml.append('<p><b>Tagged as:</b> {0}</p>'.fmt(
artist.tags.tag.length ? map(artist.tags.tag, function(tag){return tag.name;}).join(", ") :
artist.tags.tag.name));
}
var stats = '<p><div>{0} plays({1} listeners)</div>'.fmt(
formatInteger(artist.stats.playcount, "### ### ### ###"),
formatInteger(artist.stats.listeners, "### ### ### ###"));
if(artist.stats.userplaycount)
{
stats += '<div><small>{0} plays in your library</small></div>'.fmt(
formatInteger(artist.stats.userplaycount, "### ### ### ###"));
}
stats += '</p>';
infoHtml.append(stats);
infoHtml.append('{0}</div>'.fmt(artist.bio.summary));
for(var i = 0, l = artist.similar.artist.length; i < l; i += 1){
var sa = artist.similar.artist[i];
similar += '<td><img src="{0}" /><small>{1}</small></td>'
.fmt(sa.image[1]["#text"].replace("/64/", "/64s/"), sa.name);
}
similar += "</tr></table></div>";
infoHtml.append(similar);
return infoHtml.html();
}
function userJSONToHtml(user)
{
var infoHtml = $("<div />");
var info = [];
var xhr = new XMLHttpRequest();
var table = "<table><tr><td>";
console.log(user);
infoHtml.append('<b style="font-size:150%">{0}</b><br />'.fmt(user.name))
if(user.image.length)
{
table += '<img src="{0}"/></td><td>'.fmt(user.image[2]["#text"]);
}
if(user.realname.length)
{
info.push("<b>" + user.realname + "</b>");
}
if(user.age.length)
{
info.push(user.age);
}
if(user.country.length)
{
info.push(countryNameByCode(user.country));
}
if(user.gender != "n")
{
info.push(user.gender == "m" ? "Male" : "Female");
}
table += "<p>{0}</p>".fmt(info.join(", "));
table += "<p>{0} plays</p>".fmt(user.playcount);
xhr.open("GET", encodeURI("http://www.last.fm/user/{0}/tasteomatic".fmt(user.name)), false);
xhr.send();
if(xhr.status == 200)
{
table +=xhr.responseText;
}
table += "</td></tr></table>";
infoHtml.append(table);
return infoHtml.html();
}
function getArtistInfo(artist_, el)
{
var port = chrome.extension.connect({name : "artist.getinfo"})
port.onMessage.addListener(function(data){
console.log(data);
artistMap[artist_] = artistJSONToHtml(data.artist);
$("div#tooltip > div").html(artistMap[artist_]);
});
console.log(artist_);
port.postMessage({artist: artist_, username: currentUser})
}
function getUserInfo(username, el)
{
var port = chrome.extension.connect({name : "user.getinfo"});
port.onMessage.addListener(function(data){
userMap[username] = userJSONToHtml(data.user);
$("div#tooltip > div").html(userMap[username]);
});
port.postMessage({user: username})
}
var currentUser = $('a#idBadgerUser').text();
$(document).ready(function () {
if(window.location.hostname !== "funkysouls.com"){
$('a[href*="\\/music\\/"]')
.filter(function(i){ return this.href.match("http://www.last.fm/music/[^/]*$"); })
.tooltip({
bodyHandler: function() {
var artist = $(this).attr("href").replace(/(http)?.*music\//, "");
if(!artistMap[artist])
{
getArtistInfo(artist, $(this));
return '<b> Loading... <b>';
}
else
{
return artistMap[artist];
}
},
showURL: false,
});
$('a[href*="\\/user\\/"]')
.filter(function(i){ return this.href.match("http://www.last.fm/user/[^/]*$"); })
.tooltip({
bodyHandler: function() {
var user = $(this).attr("href").replace(/(http)?.*user\//, "")
if(!userMap[user])
{
getUserInfo(user, $(this));
return '<b> Loading... <b>';
}
else
{
return userMap[user];
}
},
showURL: false,
});
} else {
$('div.txt a')
.filter(function(){ return this.href.indexOf("/2/") !== -1; })
.tooltip({
bodyHandler: function() {
var artist = $(this).text().split(" - ")[0];
artist = encodeURI(artist);
if(!artistMap[artist])
{
getArtistInfo(artist, $(this));
return '<b> Loading... <b>';
}
else
{
return artistMap[artist];
}
},
showURL: false
});
}
});