-
Notifications
You must be signed in to change notification settings - Fork 2
/
popup.js
121 lines (103 loc) · 3.64 KB
/
popup.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
var backgroundPage = chrome.extension.getBackgroundPage();
function t(messageName) {
return chrome.i18n.getMessage(messageName);
}
function getTranslationMap(messageNames) {
var map = {};
messageNames.forEach(function(messageName) {
map[messageName] = t(messageName);
});
return map;
}
function renderPopup() {
var table = JSON.parse(localStorage.lastScrobbled);
var nowPlaying = JSON.parse(localStorage.nowPlaying);
var bodyHtml = T.popup.render({
username: localStorage.username,
now: moment(),
date: function() {
if (this.timestamp) {
return moment.unix(this.timestamp).from(this.now);
} else {
return t('playingNow');
}
},
songData: function() {
return JSON.stringify(this);
},
filename: function() {
return this.artist.replace(/\./g, '') + ' - ' + this.track.replace(/\./g, '');
},
isNothingToShow: !nowPlaying && table.length == 0,
nowPlaying: nowPlaying,
lastScrobbled: table.slice(nowPlaying ? -9 : -10).reverse(),
i18n: getTranslationMap(['isNothingToShow', 'hello', 'from',
'dontScrobble', 'unscrobble'])
}, {
song: T.song
});
$(document.body).html(bodyHtml);
$('#now-playing.song .unscrobble').on('click', function() {
backgroundPage.scrobbler.cancelScrobbling();
$(this).parents('.song').addClass('hide')
.bind('webkitTransitionEnd', function() { $(this).remove(); }, false);
return false;
});
$('.scrobbled.song .unscrobble').on('click', function() {
var songEl = $(this).parents('.song');
var songData = JSON.parse(songEl.data('song'));
songEl.addClass('loading');
backgroundPage.storage.removeFromScrobbled(songData, function() {
songEl.addClass('hide').bind('webkitTransitionEnd', function() {
$(this).remove();
}, false);
});
return false;
});
$('.song .love').on('click', function() {
var songEl = $(this).parents('.song');
var songData = JSON.parse(songEl.data('song'));
songEl.addClass('loading');
backgroundPage.storage.triggerLove(songData, function(id) {
var button = $('#' + id).find('.love');
if (!button.hasClass('pressed')) {
button.addClass('pressed');
} else {
button.removeClass('pressed');
}
songEl.removeClass('loading');
});
return false;
});
}
function renderUnauthorizedPopup() {
var a = $('<a href="#" id="authorize"></a>');
a.html(t('authorize') + ' Lastique').on('click', function() {
backgroundPage.auth.obtainSessionId(true);
return false;
});
var note = $('<p></p>').html(t('authNote'));
$(document.body).empty().append(a).append(note);
}
Zepto(function($) {
moment.lang('en');
var locale = t('@@ui_locale');
if (locale.lastIndexOf('ru', 0) === 0) {
moment.lang('ru');
}
var sessionId = localStorage.sessionId;
if (localStorage.token && !sessionId) {
backgroundPage.auth.obtainSessionIdFromToken(localStorage.token, function successCallback(username, sId) {
sessionId = sId;
}, function errorCallback(error, message) {
sessionId = undefined;
});
}
if (sessionId) {
$(document.body).addClass('authorized');
renderPopup();
} else {
$(document.body).addClass('unauthorized');
renderUnauthorizedPopup();
}
});