-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
400 lines (356 loc) · 12.4 KB
/
script.js
File metadata and controls
400 lines (356 loc) · 12.4 KB
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
// Color Toggle Stuff
function get_color() {
var todos = true;
var todos_str = localStorage.getItem('curr_color');
if (todos_str !== null) {
todos = JSON.parse(todos_str);
}
return todos;
}
document.addEventListener('DOMContentLoaded', function() {
pickColor();
document.getElementById('color-toggle').onclick = switchColor;
});
function colorSelector(curr_color) {
if (curr_color === true) {
color1 = "#787fa1";
color2 = "#718ec4";
color4 = "#dc8998";
color5 = "#fec5b9";
color3 = "#f1e0cd";
} else {
color1 = "#BFD8D2";
color2 = "#576490";
color4 = "#7796CB";
color5 = "#C9CAD9";
color3 = "#FEDCD2";
}
document.getElementById("weatherpad").style.backgroundColor = color1;
document.getElementById("topsitespad").style.backgroundColor = color2;
document.getElementById("songspad").style.backgroundColor = color3;
document.getElementById("listpad").style.backgroundColor = color4;
document.getElementById("myDIV").style.backgroundColor = color3;
document.getElementById("topstoriespad").style.backgroundColor = color5;
}
function pickColor() {
var curr_color = ! get_color();
colorSelector(curr_color);
}
function switchColor() {
var curr_color = get_color();
colorSelector(curr_color);
localStorage.setItem('curr_color', JSON.stringify(! curr_color));
}
// end Color Toggle Stuff
// TopSites stuff
function onAnchorClick(event) {
chrome.tabs.create({ url: event.srcElement.href });
return false;
}
chrome.topSites.get(function(mostVisitedURLs) {
var popupDiv = document.getElementById('mostVisited_div');
var ol = popupDiv.appendChild(document.createElement('ol'));
var numURLs = Math.min(mostVisitedURLs.length, 5);
for (var i = 0; i < numURLs; i++) {
var li = ol.appendChild(document.createElement('li'));
var a = li.appendChild(document.createElement('a'));
a.href = mostVisitedURLs[i].url;
a.appendChild(document.createTextNode(mostVisitedURLs[i].title));
a.addEventListener('click', onAnchorClick);
}
});
// end TopSites stuff
// Todo List Stuff
// Storing Tasks in Local DB
function get_todos() {
var todos = new Array;
var todos_str = localStorage.getItem('todo');
if (todos_str !== null) {
todos = JSON.parse(todos_str);
}
return todos;
}
function get_checked() {
var checked = new Array;
var checked_str = localStorage.getItem('checked');
if (checked_str != null) {
checked = JSON.parse(checked_str);
}
return checked;
}
function find_index(task) {
var todos = get_todos();
var i;
for (i = 0; i < todos.length; i++) {
if(todos[i] === task) {
return i;
}
}
return -1;
}
document.addEventListener('DOMContentLoaded', function() {
show_todos();
document.getElementById("newTask").addEventListener("click", newElement);
var checked = get_checked();
// Add a "checked" symbol when clicking on a list item
var list = document.querySelector('ul');
list.addEventListener('click', function(ev) {
if (ev.target.tagName === 'LI') {
var div = ev.target;
var index = find_index(div.textContent.slice(0,div.textContent.length - 1));
checked[index] = ! checked[index];
localStorage.setItem('checked', JSON.stringify(checked));
div.classList.toggle('checked');
}
}, false);
});
var close = document.getElementsByClassName("close");
function close_helper(li) {
var span = document.createElement("SPAN");
var txt = document.createTextNode("\u00D7");
span.className = "close";
span.appendChild(txt);
li.appendChild(span);
var todos = get_todos();
var checked = get_checked();
for (j = 0; j < close.length; j++) {
close[j].onclick = function() {
var div = this.parentElement;
var id = find_index(div.textContent.slice(0,div.textContent.length - 1));
todos.splice(id, 1);
checked.splice(id, 1);
localStorage.setItem('todo', JSON.stringify(todos));
localStorage.setItem('checked', JSON.stringify(checked));
div.style.display = "none";
var div2 = document.getElementById("myUL");
div2.removeChild(div2.childNodes[id]);
}
}
}
function show_todos() {
var todos = get_todos();
var checked = get_checked();
for (i = 0; i < todos.length; i++) {
var li = document.createElement("li");
li.id = i;
var inputValue = todos[i];
var t = document.createTextNode(inputValue);
li.appendChild(t);
if (checked[i] == true) {
li.classList.toggle('checked');
}
document.getElementById("myUL").appendChild(li);
close_helper(li);
}
}
//Create a new list item when clicking on the "Add" button
function newElement() {
var li = document.createElement("li");
var inputValue = document.getElementById("myInput").value;
var todos = get_todos();
todo_id = todos.length;
todos.push(inputValue);
localStorage.setItem('todo', JSON.stringify(todos));
var t = document.createTextNode(inputValue);
li.appendChild(t);
li.id = todo_id;
if (inputValue === '') {
alert("You must write something!");
} else {
var div = document.getElementById("lhidden_content");
document.getElementById("myUL").appendChild(li);
div.style.height = div.scrollHeight+"px";
}
document.getElementById("myInput").value = "";
close_helper(li);
}
// end Todo List Stuff
document.addEventListener('DOMContentLoaded', function(){
var xmlhttp = new XMLHttpRequest();
var url = "http://api.wunderground.com/api/0233c96d7f0837b9/conditions/q/CA/Berkeley.json";
xmlhttp.onreadystatechange = function() {
if (this.readyState == 4 && this.status == 200) {
var myArr = JSON.parse(this.responseText);
myFunction(myArr);
}
};
xmlhttp.open("GET", url, true);
xmlhttp.send();
function myFunction(arr) {
var out = "";
var blah1 = arr['current_observation']["display_location"]["city"]
var blah2 = arr['current_observation']["display_location"]["state"]
var blah3 = blah1 + ", " + blah2
document.getElementById("weather").innerHTML = blah3;
document.getElementById("weather2").innerHTML = arr['current_observation']["weather"];
var blah = arr['current_observation']["temp_f"] + "℉" + " " + " ";
blah += arr['current_observation']["temp_c"] + "℃";
document.getElementById("weather3").innerHTML = blah;
document.getElementById("weatherIMG").src = arr["current_observation"]["icon_url"];
}
});
function get_opened() {
var opened = new Array;
var opened_str = localStorage.getItem('open_pads');
if (opened_str !== null) {
opened = JSON.parse(opened_str);
} else {
for (var i = 0; i < 5; i++) {
opened.push(false);
}
}
console.log(opened_str);
return opened;
}
window.onload = function(){
document.getElementById('wbutton').onclick = function() {
var opened = get_opened();
this.__toggle = !this.__toggle;
var target = document.getElementById('whidden_content');
opened[0] = ! opened[0];
localStorage.setItem('open_pads', JSON.stringify(opened));
if( this.__toggle) {
target.style.height = target.scrollHeight+"px";
//this.firstChild.nodeValue = "Hide Weather";
} else {
target.style.height = 0;
//this.firstChild.nodeValue = "Show Weather";
}
}
document.getElementById('sbutton').onclick = function() {
var opened = get_opened();
this.__toggle = !this.__toggle;
var target = document.getElementById('shidden_content');
opened[1] = ! opened[1];
localStorage.setItem('open_pads', JSON.stringify(opened));
if( this.__toggle) {
target.style.height = target.scrollHeight+"px";
//this.firstChild.nodeValue = "Hide Top Sites";
} else {
target.style.height = 0;
//this.firstChild.nodeValue = "Show Top Sites";
}
}
document.getElementById('lbutton').onclick = function() {
var opened = get_opened();
this.__toggle = !this.__toggle;
var target = document.getElementById('lhidden_content');
opened[2] = ! opened[2];
localStorage.setItem('open_pads', JSON.stringify(opened));
if( this.__toggle) {
target.style.height = target.scrollHeight+"px";
//this.firstChild.nodeValue = "Hide To Do List";
} else {
target.style.height = 0;
//this.firstChild.nodeValue = "Show To Do List";
}
}
document.getElementById('nbutton').onclick = function() {
var opened = get_opened();
this.__toggle = !this.__toggle;
var target = document.getElementById('nhidden_content');
opened[3] = ! opened[3];
localStorage.setItem('open_pads', JSON.stringify(opened));
if( this.__toggle) {
target.style.height = target.scrollHeight+"px";
//this.firstChild.nodeValue = "Hide Latest News";
} else {
target.style.height = 0;
//this.firstChild.nodeValue = "Show Latest News";
}
}
document.getElementById('snbutton').onclick = function() {
var opened = get_opened();
this.__toggle = !this.__toggle;
var target = document.getElementById('snhidden_content');
opened[4] = ! opened[4];
localStorage.setItem('open_pads', JSON.stringify(opened));
if( this.__toggle) {
target.style.height = target.scrollHeight+"px";
//this.firstChild.nodeValue = "Hide Top Songs";
} else {
target.style.height = 0;
//this.firstChild.nodeValue = "Show Top Songs";
}
}
var list = document.getElementById("widgetlist");
Sortable.create(list, { });
var todolist = document.getElementById("myUL");
Sortable.create(todolist, { });
}
document.addEventListener('DOMContentLoaded', function(){
var popupDiv = document.getElementById('news');
var xmlhttp = new XMLHttpRequest();
var url = "https://api.nytimes.com/svc/topstories/v2/home.json?api-key=ece2c9475024433fa41ea3cf39e0f29d";
xmlhttp.onreadystatechange = function() {
if (this.readyState == 4 && this.status == 200) {
var myArr = JSON.parse(this.responseText);
myFunction(myArr);
}
};
xmlhttp.open("GET", url, true);
xmlhttp.send();
function myFunction(arr) {
var newsDiv = document.getElementById('news');
var ol = newsDiv.appendChild(document.createElement('ol'));
for (var i = 0; i < 3; i++) {
var li = ol.appendChild(document.createElement('li'));
var a = li.appendChild(document.createElement('a'));
a.href = arr["results"][i]["url"];
a.appendChild(document.createTextNode(arr["results"][i]["title"]));
a.addEventListener('click', onAnchorClick);
}
}
});
document.addEventListener('DOMContentLoaded', function(){
var xmlhttp = new XMLHttpRequest();
var url = "http://ws.audioscrobbler.com/2.0/?method=chart.getTopTracks&api_key=155c1541197e6602512b44c9a17a3dd7&format=json"
xmlhttp.onreadystatechange = function() {
if (this.readyState == 4 && this.status == 200) {
var myArr = JSON.parse(this.responseText);
myFunction(myArr);
}
};
xmlhttp.open("GET", url, true);
xmlhttp.send();
function myFunction(arr) {
var songsDiv = document.getElementById('songs');
var ol = songsDiv.appendChild(document.createElement('ol'));
for (var i = 0; i < 5; i++) {
var li = ol.appendChild(document.createElement('li'));
var a = li.appendChild(document.createElement('a'));
a.href = arr.tracks.track[i].url;
var textStuff = arr.tracks.track[i].name + " - " + arr.tracks.track[i].artist.name;
a.appendChild(document.createTextNode(textStuff));
// var DOM_img = document.createElement("img");
// console.log(arr.tracks.track[i].image[0]["#text"]);
// DOM_img.src = arr.tracks.track[i].image[0]["#text"];
// a.appendChild(DOM_img);
a.addEventListener('click', onAnchorClick);
}
}
});
setTimeout(function() {
var opened = get_opened();
var divs = document.getElementsByClassName("hidden");
for (var i = 0; i < divs.length; i++) {
if (opened[i] === true) {
if (i == 0) {
var thissle = document.getElementById('wbutton');
} else if (i == 1) {
var thissle = document.getElementById('sbutton');
} else if (i == 2) {
var thissle = document.getElementById('lbutton');
} else if (i == 3) {
var thissle = document.getElementById('nbutton');
} else {
var thissle = document.getElementById('snbutton');
}
var target = divs[i];
thissle.__toggle = !thissle.__toggle;
target.style.height = target.scrollHeight+"px";
}
}
}, 300);
// f18591ef19a34f3eb023911fbebffa16
// Songs Key: 155c1541197e6602512b44c9a17a3dd7