-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathbackground.html
385 lines (302 loc) · 10.9 KB
/
background.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
<html>
<script src="jquery.js"></script>
<script>
/*
Collect data
*/
var myStorageInsert = function(key, object) { // Store array in LocalStorage
window.localStorage.setItem(key, JSON.stringify(object));
};
var myStorageGet = function(key) { // Parse array from LocalStorage
return JSON.parse(window.localStorage.getItem(key));
};
var titles = myStorageGet("titles"); // Parse all video titles
var urls = myStorageGet("urls"); // Parse all video urls
if(titles=== null){ // Make empty array if LocalStorage is empty
titles =[];
urls =[];
};
var newVideo = "";
var newTitle ="";
var newUrl = "";
var playingVideo = "";
var playingTitle = "";
var playingUrl = "";
var status = "PAUSED";
chrome.extension.onRequest.addListener(function(request, sender, sendResponse) { // Listen to inject.js
if (request.greeting == "hello"){
if (urls.length === 0){
newVideo = request.currentVideo; // Set new video object
newTitle = request.currentTitle; // Set new video title
newUrl = request.currentUrl; // Set new video url
}
else{
for(i=0;i<urls.length;i++){
if (request.currentUrl === urls[i]){
playingVideo = request.currentVideo; // Set playing video object
playingTitle = request.currentTitle; // Set playing video title
playingUrl = request.currentUrl; // Set playing video url
}
else {
newVideo = request.currentVideo; // Set new video object
newTitle = request.currentTitle; // Set new video title
newUrl = request.currentUrl; // Set new video url
}
}
}
}
else if (request.afgelopen == "afgelopen"){
status = request.status; // Set video status of playing tab
if (status === "ENDED"){ // If video ended, play next video in playlist
playNextVideo();
}
updatePopup();
}
else{
sendResponse({}); // close request.
}
});
/*
Control video
*/
function updateTab(link){ // Change tab url of tab playing video from playlist
chrome.tabs.query({url: "http://www.youtube.com/watch*"}, function (tabs) {
if (tabs[0] === undefined){
chrome.tabs.update(null, {url: link});
}
else{
for(j=0;j<tabs.length;j++){
if (inPlaylist(tabs[j]) === true){
chrome.tabs.update(tabs[j].id, {url: link});
break;
}
else {
chrome.tabs.update(null, {url: link});
}
}
}
updatePopup();
});
};
var playNextVideo = function(){ // Play next video in playlist
updateTab(nextVideo());
};
var playPreviousVideo = function(){ // Play previous video in playlist
updateTab(previousVideo());
};
var playVideo = function() { // Play playing video
chrome.tabs.query({url: "http://www.youtube.com/watch*"}, function (tabs) {
if (tabs[0] === undefined){
chrome.tabs.update(null, {url: urls[0]});
}
else{
for(j=0;j<tabs.length;j++){
if (inPlaylist(tabs[j]) === true){
chrome.tabs.sendRequest(tabs[j].id, {action: "play"}); // pass counter:1 to content script to play video
break;
}
else {
chrome.tabs.sendRequest(null, {action: "play"}); // pass counter:1 to content script to play video
}
}
}
updatePopup();
});
};
var pauseVideo = function() { // Pause playing video
chrome.tabs.query({url: "http://www.youtube.com/watch*"}, function (tabs) {
for(j=0;j<tabs.length;j++){
if (inPlaylist(tabs[j]) === true){
chrome.tabs.sendRequest(tabs[j].id, {action: "pause"}); // send command to inject.js
break;
}
else {
chrome.tabs.sendRequest(null, {action: "pause"}); // send command to inject.js
}
}
updatePopup();
});
};
var nextVideo = function(){ // Get URL of next video in playlist
for (i=0;i<titles.length;i++){
if (playingTitle===titles[titles.length-1]){
return urls[0];
}
else if (playingTitle===titles[i]){
return urls[i+=1];
}
}
};
var previousVideo = function(){ // Get URL of previous video in playlist
for (i=0;i<titles.length;i++){
if (playingTitle===titles[0]){
return urls[titles.length-1];
}
else if (playingTitle===titles[i]){
return urls[i-=1];
}
}
};
var inPlaylist = function(tabs){ // Check if any tab is playing a video from the playlist
var count = 0;
for(i=0;i<urls.length;i++){
if (tabs.url === urls[i]){
count+=1;
}
}
if(count===0){
return false;
}
else{
return true;
}
};
/*
Control Playlist
*/
var buildPlaylist = function(){ // Build playlist
var samen ="";
for(i=0;i<titles.length;i++){
if (playingUrl === urls[i]){ // playingly playing
samen += "<li id='playing'>"
+"<div id='remove' onClick='bkg.removeSong("+i+")'></div>"
+"<div id='title' onClick='bkg.updateTab("+JSON.stringify(urls[i])+")'><a href='' >"+strip(titles[i])+" </a></div>"
+"</li>";
}
else if(i%2 === 0){ // even numbers
samen += "<li id='even'>"
+"<div id='remove' onClick='bkg.removeSong("+i+")'></div>"
+"<div id='title' onClick='bkg.updateTab("+JSON.stringify(urls[i])+")'><a href='' >"+strip(titles[i])+" </a></div>"
+"</li>";
}
else{
samen += "<li id='uneven'>" // uneven numbers
+"<div id='remove' onClick='bkg.removeSong("+i+")'></div>"
+"<div id='title' onClick='bkg.updateTab("+JSON.stringify(urls[i])+")'><a href='' >"+strip(titles[i])+" </a></div>"
+"</li>";
}
}
var totaal = "<div id='playlist'><ol>" + samen + "</ol></div>";
return totaal;
}
function addToPlaylist(){ // Add video to playlist
if(duplicates(newTitle,titles)!==true){
titles.push(newTitle);
urls.push(newUrl);
storeAll();
updatePopup();
}
};
function removeSong(nummer){ // remove video from playlist
titles.splice(nummer,1);
urls.splice(nummer,1);
storeAll();
updatePopup();
};
var duplicates = function(temporary,stored){ //check if video exists in playlist
for (i=0;i<stored.length;i++){
if (temporary==stored[i]){
return true;
break;
}
}
};
var storeAll = function(){ // Store all arrays in localstorage
myStorageInsert("titles",titles);
myStorageInsert("urls",urls);
};
var strip = function(string){ // Shorten a string to 32 characters
if(string.length > 32){
return string.substring(0,32)+"...";
}
else{
return string;
}
};
/*
Show data
*/
var updatePopup = function(){ // Update popup.html
var popups = chrome.extension.getViews({type: "popup"});
if (popups.length != 0) {
var popup = popups[0];
addAction();
if(titles.length === 0){ // If playlist is empty, show tutorial
popup.document.getElementById("bottom").innerHTML= showTutorial();
popup.document.getElementById("center").innerHTML= "";
}
else{
if (status === "PLAYING"){ // If a video is playing, show video controls with pause button
popup.document.getElementById("center").innerHTML= showPauseControls();
}
else if (status === "PAUSED"){ // If a video is paused, show video controls with play button
popup.document.getElementById("center").innerHTML= showPlayControls();
}
else{ // If none of the tabs is showing a youtube video, show video controls with play button
popup.document.getElementById("center").innerHTML= showPlayControls();
}
popup.document.getElementById("bottom").innerHTML= buildPlaylist(); // show playlist output
}
}
};
var showPlayControls = function(){ // Show video controls with play button
var inzetten = "<div id='controls'>"
+ "<div id='previous' onClick='bkg.playPreviousVideo()'></div>"
+ "<div id='play' onclick='bkg.playVideo()'></div>"
+ "<div id='next' onClick='bkg.playNextVideo()'></div>"
+ "</div>";
return inzetten;
}
var showPauseControls = function(){ // Show video controls with pause button
var inzetten = "<div id='controls'>"
+ "<div id='previous' onClick='bkg.playPreviousVideo()'></div>"
+ "<div id='pause' onclick='bkg.pauseVideo()'></div>"
+ "<div id='next' onClick='bkg.playNextVideo()'></div>"
+ "</div>";
return inzetten;
}
var showTutorial = function(){ // Show tutorial
var youtubeUrl = "http://www.youtube.com";
var tutorial = "<div id='tutorial'>"
+ "<h1>Hey!</h1>"
+ "<h2>Your playlist seems to have no videos in it. Let change that!</h2>"
+ "<ol>"
+ "<li>Go to <a href='' onClick='bkg.updateTab("+JSON.stringify(youtubeUrl)+")'>Youtube</a></li>"
+ "<li>Choose one of your favorite videos</li>"
+ "<li>Press the blue bar to add it to your playlist</li>"
+ "</ol></div>";
return tutorial;
}
var showAdd = function(){ // Show "add to playlist"-link on top of the playlist
var inzetten = "<div id='add'><a href='' onClick='bkg.addToPlaylist()'>Add ""+newTitle.substring(0,23)+"..."</a></div>";
var popups = chrome.extension.getViews({type: "popup"});
if (popups.length != 0) {
var popup = popups[0];
popup.document.getElementById("top").innerHTML= inzetten;
}
}
var hideAdd = function(){ // Hide "add to playlist"-link on top of the playlist
var inzetten = "";
var popups = chrome.extension.getViews({type: "popup"});
if (popups.length != 0) {
var popup = popups[0];
popup.document.getElementById("top").innerHTML= inzetten;
}
}
var addAction = function(){ // Decide to show or hide "add to playlist"-link on top of the playlist
chrome.tabs.query({'url': "http://www.youtube.com/watch*"}, function (tabs) {
for(j=0;j<tabs.length;j++){
if (inPlaylist(tabs[j]) === false){
showAdd();
}
else {
hideAdd();
}
}
});
};
chrome.tabs.onUpdated.addListener(function(){ // listen to updated tabs
updatePopup();
});
</script>
</html>