-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpopup.js
248 lines (219 loc) · 7.58 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
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
function playRadio(radioItem) {
chrome.runtime.sendMessage({action: 'play', value: radioItem});
DisplayCurrentRadio(radioItem);
}
function getRadioList() {
chrome.runtime.getBackgroundPage(function(bg) {
var list = bg.radio.getRadioList();
DisplayRadioList(list);
});
}
function getFavoriteList() {
chrome.runtime.getBackgroundPage(function(bg) {
var list = bg.radio.getFavoriteList();
DisplayRadioList(list);
});
}
function getRadioListCategories() {
chrome.runtime.getBackgroundPage(function(bg) {
var list = bg.radio.getRadioListCategories();
DisplayCategoryList(list);
});
}
function getRadioListCategory(cat) {
chrome.runtime.getBackgroundPage(function(bg) {
var list = bg.radio.getRadioListByCategory(cat);
DisplayRadioList(list);
});
}
function getRadioListGenres() {
chrome.runtime.getBackgroundPage(function(bg) {
var list = bg.radio.getRadioListGenres();
DisplayGenreList(list);
});
}
function getRadioListGenre(cat) {
chrome.runtime.getBackgroundPage(function(bg) {
var list = bg.radio.getRadioListByGenre(cat);
DisplayRadioList(list);
});
}
function addFavoriteToList(radioItem) {
chrome.runtime.getBackgroundPage(function(bg) {
bg.radio.addFavoriteToList(radioItem);
getFavoriteList();
});
}
function setVolume(e) {
var value = document.querySelector('#player_volume').value;
value = value / 100;
chrome.runtime.sendMessage({action: 'player_volume',volume: value});
}
function doSearch(e) {
var value = document.querySelector('#player_search').value;
chrome.runtime.getBackgroundPage(function(bg) {
var list = bg.radio.getRadioListByName(value);
DisplayRadioList(list);
});
}
function getPlayerState() {
chrome.runtime.getBackgroundPage(function(bg) {
document.querySelector('#player_state').innerHTML = 'currently ' + bg.radio.player_state.msg;
});
}
function DisplayCurrentRadio(radioItem) {
var html = '';
var slug = 'nostream';
var title = 'Offline';
var description = 'currently no radio playing';
if (radioItem && radioItem.state == 1) {
slug = radioItem.slug;
title = radioItem.name;
description = radioItem.description;
SetButtonState(true);
}else{
SetButtonState(false);
}
html = html + '<div class="media-left">';
html = html + '<img class="media-object" src="https://api.kandru.de/tire/images/streams/'+slug+'.png" width="50px" alt="Stream Logo">';
html = html + '</div>';
html = html + '<div class="media-body">';
html = html + '<h4 class="media-heading">' + title + '</h4>';
html = html + description;
html = html + '</div>';
document.querySelector('#player_current').innerHTML = html;
}
function DisplayRadioList(list)
{
var html = "";
for (var value in list)
{
var value = list[value];
html = html + '<li class="list-group-item" id="radiolist-'+value.slug+'">';
html = html + '<div class="media">';
html = html + '<div class="media-left">';
html = html + '<img class="media-object" src="https://api.kandru.de/tire/images/streams/'+value.slug+'.png" width="50px" alt="Logo">';
html = html + '</div>';
html = html + '<div class="media-body">';
html = html + '<h4 class="media-heading">'+value.name+'</h4>';
html = html + value.description;
html = html + '</div>';
html = html + '</div>';
html = html + '</li>';
}
if (html == "")
{
html = html + '<li class="list-group-item">no radios listed</li>';
}
document.querySelector('#stream_list').innerHTML = html;
for (var value in list)
{
var value = list[value];
document.querySelector('#radiolist-'+value.slug).addEventListener('click',function(param1){param1.state = 1;playRadio(param1);}.bind(this,value));
}
}
function DisplayCategoryList(list)
{
var html = "";
for (var value in list)
{
var value = list[value];
html = html + '<li class="list-group-item" id="categorylist-'+value.category.replace(/\s+/g, '-')+'">';
html = html + '<div class="media">';
html = html + '<div class="media-body">';
html = html + value.category;
html = html + '</div>';
html = html + '</div>';
html = html + '</li>';
}
if (html == "")
{
html = html + '<li class="list-group-item">no categories found Oo. Check your internet connection!</li>';
}
document.querySelector('#stream_list').innerHTML = html;
for (var value in list)
{
var value = list[value];
document.querySelector('#categorylist-'+value.category.replace(/\s+/g, '-')).addEventListener('click',function(param1){getRadioListCategory(param1);}.bind(this,value.category));
}
}
function DisplayGenreList(list)
{
var html = "";
for (var value in list)
{
var value = list[value];
html = html + '<li class="list-group-item" id="genrelist-'+value.replace(/\s+/g, '-')+'">';
html = html + '<div class="media">';
html = html + '<div class="media-body">';
html = html + value;
html = html + '</div>';
html = html + '</div>';
html = html + '</li>';
}
if (html == "")
{
html = html + '<li class="list-group-item">no genres found -.- the world is going to destroy itself. Check your internet connection!</li>';
}
document.querySelector('#stream_list').innerHTML = html;
for (var value in list)
{
var value = list[value];
document.querySelector('#genrelist-'+value.replace(/\s+/g, '-')).addEventListener('click',function(param1){getRadioListGenre(param1);}.bind(this,value));
}
}
function SetButtonState(type) {
if (type) {
document.getElementById("player_play").disabled = true;
document.getElementById("player_stop").disabled = false;
document.getElementById("player_homepage").disabled = false;
document.getElementById("player_favorite").disabled = false;
}else{
document.getElementById("player_play").disabled = false;
document.getElementById("player_stop").disabled = true;
document.getElementById("player_homepage").disabled = true;
document.getElementById("player_favorite").disabled = true;
}
}
// after document is loaded
document.addEventListener('DOMContentLoaded', function () {
DisplayCurrentRadio(JSON.parse(localStorage.getItem('data_radio')));
document.querySelector('#player_volume').value = localStorage.getItem('player_volume') * 100;
document.querySelector('#player_volume').addEventListener('input', setVolume);
document.querySelector('#player_search').addEventListener('input', doSearch);
document.querySelector('#player_favorite').addEventListener('click', function(e){
var radioItem = JSON.parse(localStorage.getItem('data_radio'));
if (radioItem) {
addFavoriteToList(radioItem);
}
});
document.querySelector('#player_play').addEventListener('click', function(e){
var radioItem = JSON.parse(localStorage.getItem('data_radio'));
if (radioItem) {
radioItem.state = 1;
playRadio(radioItem);
}
});
document.querySelector('#player_category').addEventListener('click', function(e){getRadioListCategories();});
document.querySelector('#player_genre').addEventListener('click', function(e){getRadioListGenres();});
document.querySelector('#player_favorites').addEventListener('click', function(e){getFavoriteList();});
// update radiolist every hour
if(localStorage.getItem('online_lastupdate') <= (Math.round(new Date().getTime() / 1000) - 3600))
{
chrome.runtime.sendMessage({action: 'updateOnlineRadioList'});
}
// add eventlistener for link to homepage
document.querySelector('#player_homepage').addEventListener('click', function(e)
{
var radioItem = JSON.parse(localStorage.getItem('data_radio'));
chrome.tabs.create({ url: radioItem.homepage });;
});
// add eventlistener for stop button
document.querySelector('#player_stop').addEventListener('click', function(e){
chrome.runtime.sendMessage({action: 'stop'});
DisplayCurrentRadio(null);
});
// add polling for player state
//setInterval(getPlayerState, 100);
getFavoriteList();
});