-
Notifications
You must be signed in to change notification settings - Fork 0
/
popup.html
488 lines (451 loc) · 13.4 KB
/
popup.html
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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
<html>
<head>
<style type="text/css">
body {
min-width:420px;
overflow-x:hidden;
font-family: helvetica, arial, sans-serif;
font-size: 11px;
color: #122C4B;
}
a {
text-decoration: none;
color: #666;
}
a:hover {
text-decoration: underline;
}
form {
padding: 0;
margin: 0;
}
input {
-moz-border-radius: 4px;
-webkit-border-radius: 4px;
border: 1px solid #001a39;
}
select {
-moz-border-radius: 4px;
-webkit-border-radius: 4px;
border: 1px solid #001a39;
}
#header {
border-bottom: 1px solid #001a39;
height: 50px;
}
#logo {
float: left;
margin-top: 3px;
}
#search {
float: right;
margin-right: 18px;
}
#searchIn {
float: left;
padding-right: 5px;
padding-top: 5px;
font-size: 11px;
}
#searchForm {
float: left;
}
#searchBox {
background: url("search.png") 1px 1px no-repeat;
padding-left: 17px;
width: 120px;
font-size: 11px;
}
#sortBy {
width: 104px;
font-size: 11px;
}
#mixPlayingInBg {
display: none;
padding: 5px;
margin-top: 10px;
border: 1px solid black;
width: 356px;
background: #f1f2a0;
}
#results {
padding-top: 10px;
clear: both;
}
#refresh {
width: 109px;
height: 20px;
background: url("refresh.gif") no-repeat;
float: right;
padding-left: 22px;
padding-top: 2px;
margin-top: 5px;
font-size: 12px;
}
#heading {
font-size: 22px;
font-weight: bold;
}
#heading img {
margin-right: 5px;
}
#mixes {
margin-top: 10px;
}
.mix {
clear: left;
width: 389px;
-moz-border-radius: 6px;
-webkit-border-radius: 6px;
margin-bottom: 10px;
padding: 5px;
border: 1px solid #d5d5d5;
}
.mixThumbnail {
float: left;
}
.mixName {
font-size: 15px;
padding-bottom: 5px;
font-weight: bold;
margin-left: 62px;
}
.mixPub {
color: #999;
padding-bottom: 0px;
margin-left: 62px;
}
.mixDesc {
padding-bottom: 5px;
margin-left: 62px;
}
.mixTags {
padding-bottom: 5px;
font-size: 10px;
color: #999;
margin-left: 62px;
}
.playbackOptions {
margin-left: 62px;
clear: left;
padding-left: 22px;
padding-top: 1px;
background: url("listen.png") no-repeat;
}
.open8tracksPage {
margin-left: 62px;
margin-bottom: 2px;
clear: left;
padding-left: 22px;
background: url("newtab.png") no-repeat;
}
.mixPlayer {
padding-top: 5px;
}
.carryOnDiv{
padding: 5px;
padding-bottom: 0px;
display: none;
}
#more {
font-size: 12px;
}
#footer {
float: right;
margin-right: 18px;
padding-bottom: 5px;
font-size: 10px;
padding-top: 15px;
}
</style>
<script type="text/javascript" src="swfobject.js"></script>
<script type="text/javascript" src="jquery-1.4.1.min.js"></script>
<script type="text/javascript">
var bg = chrome.extension.getBackgroundPage();
var req = new XMLHttpRequest();
var searchDefaultText = "by tag, artist or DJ";
var headingLatest = "Latest mixes from <a href=\"#\" title=\"Open 8tracks.com in a new tab\">8tracks</a>";
var headingSearchResults = "<img src=\"search.png\"/>Results from <a href=\"#\" title=\"Open 8tracks.com in a new tab\">8tracks</a>";
var spinningPic = $("<img src=\"working.gif\"/>");
var seeMoreDiv = $("<div id=\"more\"><a href=\"\" title=\"See more...\">See more...</a></div>");
/**
* State info
*/
var currentPage = 1;
var currentSearchParams = "";
var numberOfMixesDisplayed = 0;
var currentFgMixId = null;
/**
* To call on load
*/
function init() {
$("#searchBox").val(searchDefaultText);
$("#searchBox").focus(function() {
if ($(this).val() == searchDefaultText) {
$(this).val("");
}
});
$("#searchBox").blur(function() {
if ($(this).val() == "") {
$(this).val(searchDefaultText);
}
});
$("#searchForm form").submit(function(e) {
var searchString = $("#searchBox").val();
if (searchString != "" && searchString != searchDefaultText) {
clearResults();
var sortBy = $("#sortBy").val();
bg.saveState({"searchString": searchString, "sortBy": sortBy});
getMixes("q=" + encodeURIComponent(searchString) + "&sort=" + sortBy, 1);
}
e.preventDefault();
});
$("#sortBy").change(function() {
$("#searchForm form").submit();
});
$("#refresh a").click(function(e) {
resetSearchPanel();
clearResults();
bg.saveState({"searchString": null, "sortBy": null});
getMixes("", 1);
e.preventDefault();
});
$("a#myTwitter").click(function(e) {
openUrlInBgTab("http://twitter.com/seewahcheng");
e.preventDefault();
});
$("a#my8tracks").click(function(e) {
openUrlInBgTab("http://8tracks.com/seewah");
e.preventDefault();
});
$("#mixPlayingInBg a#stopMix").click(function(e) {
stopMixInBg();
e.preventDefault();
});
$("#mixPlayingInBg a#openMixPage").click(function(e) {
openUrlInBgTab(bg.mixPageUrl);
e.preventDefault();
});
if (bg.mixPlaying) {
$("#mixPlayingInBg").show();
} else {
chrome.browserAction.setBadgeText({"text": ""});
}
if (bg.searchString == null || bg.sortBy == null) {
getMixes("", 1);
} else {
$("#searchBox").val(bg.searchString);
$("#sortBy").val(bg.sortBy);
$("#searchForm form").submit();
}
}
/**
* Opens url in a new unselected tab.
*/
function openUrlInBgTab(url) {
chrome.tabs.create({"url": url, "selected": false});
}
/**
* Opens url in a new browser window.
*/
function openUrlInNewWindow(url) {
chrome.windows.create({"url": url});
}
/**
* Resets search panel display.
*/
function resetSearchPanel() {
$("#searchBox").val(searchDefaultText);
$("#sortBy").val("recent");
}
/**
* Clear results (removing currently displayed mixes)
*/
function clearResults() {
$("#mixes").html("");
numberOfMixesDisplayed = 0;
currentFgMixId = null;
}
/**
* Gets mix info from 8tracks API. Calls showResults to display actual mix info.
*/
function getMixes(searchParams, page) {
currentPage = page;
currentSearchParams = searchParams;
$("#mixes").append(spinningPic);
$(seeMoreDiv).remove();
$("#heading").html(searchParams == "" ? headingLatest : headingSearchResults);
$("#heading a").click(function(e) {
openUrlInBgTab("http://8tracks.com");
e.preventDefault();
});
req.open("GET", "http://api.8tracks.com/mixes.xml?page=" + page + "&" + searchParams, true);
req.setRequestHeader("Authorization", "Basic S1BGUmZuQmJYQ0FKWUlhOmZlNDI2MzZjZThhMDkxNjYwN2QxYTg0MDc1YTNiZjc1MmFkNTZjMjU=");
req.onload = showResults;
// following a hack to get around the chrome extension async xhr bug
setTimeout(function() {
req.send(null);
}, 0);
}
/**
* Displays the actual mix info as returned by the AJAX call.
*/
function showResults() {
$(spinningPic).remove();
var totalEntries = req.responseXML.getElementsByTagName("total-entries")[0].childNodes[0].nodeValue;
var mixes = req.responseXML.getElementsByTagName("mix");
for (var i = 0, mix; mix = mixes[i]; i++) {
var id = mix.getElementsByTagName("id")[0].childNodes[0].nodeValue;
var url = mix.getElementsByTagName("restful-url")[0].childNodes[0].nodeValue;
var mixDiv = $("<div/>", {"class": "mix"});
var mixName = $("<div/>", {"class": "mixName"});
var mixPub = $("<div/>", {"class": "mixPub"});
var mixDesc = $("<div/>", {"class": "mixDesc"});
var mixTags = $("<div/>", {"class": "mixTags"});
var open8tracksPage = $("<div/>", {"class": "open8tracksPage"});
var playbackOptions = $("<div/>", {"class": "playbackOptions"});
var playerDiv = $("<div/>", {"id": "mixPlayer_" + id, "class": "mixPlayer"});
var carryOnDiv= $("<div/>", {"id": "carryOnDiv_" + id, "class": "carryOnDiv"});
mixName.text(getElementText(mix, "name"));
open8tracksPage.text("Open 8tracks page");
mixPub.text(getElementText(mix, "first-published-at").replace("T", " ").replace("Z", " GMT") + " by " + getElementText(mix, "login"));
mixDesc.text(getElementText(mix, "description"));
mixTags.text(getElementText(mix, "tag-list-cache"));
open8tracksPage.html("Open page <a id=\"open8tracksPageLink_" + id + "\" href=\"" + url + "\" title=\"Open page in a new tab\">in a new tab</a>");
playbackOptions.html("Play mix <a href=\"#\" id=\"playHere_" + id + "\" title=\"Play mix in this popup\">here</a> or <a id=\"playInBg_" + id + "\" href=\"#\" title=\"Play mix in the background (so that you can carry on listening while working!)\">in the background</a>");
carryOnDiv.html("Note: Playback will stop as soon as you click away from this popup. To carry on listening to the mix while you are working, play mix <a id=\"carryOn_" + id + "\" href=\"#\" title=\"Play mix in the background (so that you can carry on listening while working!)\">in the background</a>.");
var imgSrc = getElementText(mix, "sq56");
if (imgSrc.indexOf("http") != 0) {
imgSrc = "http://8tracks.com" + imgSrc;
}
var img = $("<img src=\"" + imgSrc + "\" class=\"mixThumbnail\"/>");
$("#mixes").append(mixDiv);
mixDiv.append(img);
mixDiv.append(mixName);
mixDiv.append(mixDesc);
mixDiv.append(mixPub);
mixDiv.append(mixTags);
mixDiv.append(open8tracksPage);
mixDiv.append(playbackOptions);
mixDiv.append(playerDiv);
mixDiv.append(carryOnDiv);
$("a#open8tracksPageLink_" + id).click(function(e) {
openUrlInBgTab($(this).attr("href"));
e.preventDefault();
});
$("a#playHere_" + id).click(function(e) {
var idFromLink = this.id.substring(this.id.indexOf("_") + 1);
displayMixPlayer(idFromLink);
e.preventDefault();
});
$("a#playInBg_" + id).click(function(e) {
var idFromLink = this.id.substring(this.id.indexOf("_") + 1);
playMixInBg(idFromLink);
e.preventDefault();
});
$("a#carryOn_" + id).click(function(e) {
var idFromLink = this.id.substring(this.id.indexOf("_") + 1);
playMixInBg(idFromLink);
e.preventDefault();
});
numberOfMixesDisplayed++;
}
if (numberOfMixesDisplayed < totalEntries) {
$("#mixes").append(seeMoreDiv);
seeMoreDiv.click(function(e) {
getMixes(currentSearchParams, currentPage + 1);
e.preventDefault();
});
}
}
/**
* Convenient method for getting XML text node values.
*/
function getElementText(element, tagName) {
var matches = element.getElementsByTagName(tagName);
if (matches.length > 0) {
var nodes = matches[0].childNodes;
if (nodes.length > 0) {
return nodes[0].nodeValue;
}
}
return "";
}
/**
* Displays the 8tracks player in the popup itself.
*/
function displayMixPlayer(id) {
if (currentFgMixId == id) {
return;
}
stopMixInBg();
hideCurrentFgMixPlayer();
var so = new SWFObject("http://8tracks.com/mixes/" + id + "/player_v2", "mixplayer_" + id, "100%", "120", "8", "#000000");
so.addVariable("auto_play", "true");
so.write("mixPlayer_" + id);
$("#carryOnDiv_" + id).show();
currentFgMixId = id;
}
/**
* Hides the current 8tracks player in the popup, destroying the flash object.
*/
function hideCurrentFgMixPlayer() {
if (currentFgMixId != null) {
$("#mixPlayer_" + currentFgMixId).html("");
$("#carryOnDiv_" + currentFgMixId).hide();
}
}
/**
* Plays mix in background.html, showing mix playing message.
*/
function playMixInBg(id) {
hideCurrentFgMixPlayer();
bg.playMix(id);
chrome.browserAction.setBadgeText({"text": "."});
$("#mixPlayingInBg").fadeIn("slow");
}
/**
* Stops mix in the background.html, hiding mix playing message.
*/
function stopMixInBg() {
bg.stopMix();
chrome.browserAction.setBadgeText({"text": ""});
$("#mixPlayingInBg").fadeOut("slow");
}
$(document).ready(init);
</script>
</head>
<body>
<div id="header">
<div id="logo"><img src="logo.gif" width="167" height="37" alt="logo"/></div>
<div id="search">
<div id="searchIn">Search for mixes:</div>
<div id="searchForm">
<form>
<div>
<input type="text" id="searchBox" value="" />
</div>
<div>
<select id="sortBy">
<option value="recent">sort by Recent</option>
<option value="popular">sort by Popular</option>
<option value="random">sort by Random</option>
</select>
</div>
</form>
</div>
</div>
</div>
<div id="mixPlayingInBg">A mix is currently playing in the background: <a id="stopMix" href="#" title="Stop mix in the background">STOP MIX</a> | <a id="openMixPage" href="#" title="Open mix page in a new tab">SEE MIX DETAILS</a></div>
<div id="results">
<div id="refresh"><a href="#" title="See latest mixes">See latest mixes</a></div>
<div id="heading"></div>
<div id="mixes"></div>
</div>
<div id="footer">
Created by See Wah Cheng |
Twitter: <a id="myTwitter" href="#" title="My Twitter profile">@seewahcheng</a> |
8tracks: <a id="my8tracks" href="#" title="My 8tracks profile">seewah</a>
</div>
</body>
</html>