-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path_old_index.html
390 lines (376 loc) · 22.1 KB
/
_old_index.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
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8"/>
<title>Live Election Data Table</title>
<style type="text/css">
body {
background-color: black;
color: white;
font-family: "Consolas, monaco, monospace";
width: 100%;
}
table {
margin-left: auto;
margin-right: auto;
}
</style>
<script type="application/javascript">
function makeDataTable(){
var h1 = document.createElement("h1");
h1.style.cssText = "text-align: center;"
h1.innerText = "This webpage pulls a json file containing election stats once per second";
document.body.appendChild(h1);
var h2 = document.createElement("h2");
h2.style.cssText = "text-align: center;"
h2.innerText = "Though, the remote json file is updated much less frequently";
document.body.appendChild(h2);
var h3 = document.createElement("h3");
h3.style.cssText = "text-align: center;"
h3.innerText = "Data is pulled from The New York Times";
document.body.appendChild(h3);
var topTable = document.createElement("table");
topTable.style.cssText = "padding: 5px;"
topTable.appendChild(document.createElement("colgroup"));
var count = 0;
[0,1].forEach(function(){
var col = document.createElement("col");
switch(count){
case 0:{
col.style.cssText = "width: 110px;"
break;
}
case 1:{
col.style.cssText = "width: 715px;"
break;
}
}
count++;
topTable.firstChild.appendChild(col);
});
var srcRow = document.createElement("tr");
var srcLabel = document.createElement("td");
var srcSpan1 = document.createElement("span");
srcSpan1.style.cssText = "color: yellow; white-space: nowrap;"
srcSpan1.innerText = "Data source:";
srcLabel.appendChild(srcSpan1);
var srcUri = document.createElement("td");
var srcA = document.createElement("a");
srcA.style.cssText = "color: chartreuse; white-space: nowrap; padding-left: 10px;"
srcA.href = "https://static01.nyt.com/elections-assets/2020/data/api/2020-11-03/national-map-page/national/president.json";
srcA.innerText = "https://static01.nyt.com/elections-assets/2020/data/api/2020-11-03/national-map-page/national/president.json";
srcA.target = "_blank";
srcUri.appendChild(srcA);
srcRow.appendChild(srcLabel);
srcRow.appendChild(srcUri);
topTable.appendChild(srcRow);
var j;
var dt2 = document.createElement("table");
dt2.style.cssText = "border: 1px solid white; border-collapse: collapse; padding: 5px;"
dt2.appendChild(document.createElement("colgroup"));
[0,1].forEach(function(){
var col = document.createElement("col");
col.style.cssText = "width: 210px;"
dt2.firstChild.appendChild(col);
});
dt2.appendChild(document.createElement("tr"));
['Joseph R. Biden Jr.','Donald J. Trump'].forEach((i)=>{
var th = document.createElement("th");
if(i.match('Biden')){
th.style.cssText = "border: 1px solid white; border-collapse: collapse; padding: 5px; color: blue;"
}
if(i.match('Trump')){
th.style.cssText = "border: 1px solid white; border-collapse: collapse; padding: 5px; color: red;"
}
th.innerText = i;
dt2.children[1].appendChild(th);
});
var dt = document.createElement("table");
dt.style.cssText = "border: 1px solid white; border-collapse: collapse; padding: 5px;"
dt.appendChild(document.createElement("colgroup"));
var count = 0;
[0,1,2].forEach(function(){
var col = document.createElement("col");
switch(count){
case 0: {
col.style.cssText = "width: 120px;";
break;
}
case 1: {
col.style.cssText = "width: 145px;";
break;
}
case 2: {
col.style.cssText = "width: 155px;";
break;
}
}
count++;
dt.firstChild.appendChild(col);
});
dt.appendChild(document.createElement("tr"));
["Electoral Votes","State","Winner / Vote Count"].forEach((i)=>{
var th = document.createElement("th");
th.style.cssText = "border: 1px solid white; border-collapse: collapse; padding: 5px;"
th.innerText = i;
dt.children[1].appendChild(th);
});
var bra = [];
var tra = [];
fetch("https://static01.nyt.com/elections-assets/2020/data/api/2020-11-03/national-map-page/national/president.json").then(function(resolve,reject){
resolve.text().then(function(result){
j = JSON.parse(result);
var lastModified = new Date(j.meta.timestamp);
var versionRow = document.createElement("tr");
var verLabel = document.createElement("td");
var verSpan = document.createElement("span");
verSpan.style.cssText = "display: inline; color: yellow; white-space: nowrap;"
verSpan.innerText = "Json version:";
verLabel.appendChild(verSpan);
var verNo = document.createElement("td");
var verSpan2 = document.createElement("span");
verSpan2.style.cssText = "display: inline; color: chartreuse; white-space: nowrap; padding-left: 10px;"
verSpan2.id = "version";
verSpan2.innerText = j.meta.version;
verNo.appendChild(verSpan2);
versionRow.appendChild(verLabel);
versionRow.appendChild(verNo);
topTable.appendChild(versionRow);
var tsRow = document.createElement("tr");
var tsLabel = document.createElement("td");
var tsSpan = document.createElement("span");
tsSpan.style.cssText = "display: inline; color: yellow; white-space: nowrap;"
tsSpan.innerText = "Last modified:";
tsLabel.appendChild(tsSpan);
var tsDate = document.createElement("td");
var tsSpan2 = document.createElement("span");
tsSpan2.style.cssText = "display: inline; color: chartreuse; white-space: nowrap; padding-left: 10px;"
tsSpan2.id = "version";
tsSpan2.innerText = lastModified.toString();
tsDate.appendChild(tsSpan2);
tsRow.appendChild(tsLabel);
tsRow.appendChild(tsDate);
topTable.appendChild(tsRow);
document.body.appendChild(topTable);
var sorted = j.data.races.sort(function(a,b){
return new Date(b.last_updated) - new Date(a.last_updated);
});
sorted.forEach((i)=>{
if(i.result.match('winner')){
i.candidates.filter((a)=>{
if(a.winner){
if(a.name_display == 'Joseph R. Biden Jr.'){
bra.push(i.electoral_votes);
}
if(a.name_display == 'Donald J. Trump'){
tra.push(i.electoral_votes);
}
}
});
}
});
var bsum = bra.reduce(function(a, b){
return a + b;
}, 0);
var tsum = tra.reduce(function(a,b){
return a + b;
}, 0);
var row1 = document.createElement("tr");
var jb0 = document.createElement("td")
jb0.id = 'joebiden';
jb0.style.cssText = "border: 1px solid white; border-collapse: collapse; padding: 5px;"
jb0.innerText = bsum;
var dt1 = document.createElement("td");
dt1.id = 'donaldtrump';
dt1.style.cssText = "border: 1px solid white; border-collapse: collapse; padding: 5px;"
dt1.innerText = tsum;
[jb0,dt1].forEach((a)=>{
row1.appendChild(a);
});
dt2.appendChild(row1);
document.body.appendChild(dt2);
var pending = [];
var determined = [];
j.data.races.forEach((i)=>{
var row = document.createElement("tr")
if(i.result == 'winner'){
var ev = document.createElement("td");
ev.id = i.state_name + "_ev";
ev.innerText = i.electoral_votes;
var st = document.createElement("td");
st.id = i.state_name;
st.innerText = i.state_name;
var wi = document.createElement("td");
wi.id = i.state_name + "_wi";
wi.innerText = i.candidates.filter(function(a){if(a.winner){ return a }})[0].name_display;
if(wi.innerText.match('Biden')){
wi.style.cssText = "border: 1px solid white; border-collapse: collapse; padding: 5px; color: blue;"
ev.style.cssText = "border: 1px solid white; border-collapse: collapse; padding: 5px; color: blue;"
st.style.cssText = "border: 1px solid white; border-collapse: collapse; padding: 5px; color: blue;"
}
if(wi.innerText.match('Trump')){
wi.style.cssText = "border: 1px solid white; border-collapse: collapse; padding: 5px; color: red;"
ev.style.cssText = "border: 1px solid white; border-collapse: collapse; padding: 5px; color: red;"
st.style.cssText = "border: 1px solid white; border-collapse: collapse; padding: 5px; color: red;"
}
[ev,st,wi].forEach((a)=>{
row.appendChild(a);
});
determined.push(row);
} else {
function getBidenVotes(biden){
return biden.candidates.filter(function(a){if(a.name_display.match('Biden')){ return a}})[0].votes;
}
function getTrumpVotes(trump){
return trump.candidates.filter(function(a){if(a.name_display.match('Trump')){ return a}})[0].votes;
}
var ev = document.createElement("td");
ev.id = i.state_name + "_ev";
ev.style.cssText = "border: 1px solid white; border-collapse: collapse; padding: 5px;"
ev.innerText = i.electoral_votes;
var st = document.createElement("td");
st.id = i.state_name;
st.style.cssText = "border: 1px solid white; border-collapse: collapse; padding: 5px;"
st.innerText = i.state_name;
var wi = document.createElement("td");
var vcTable = document.createElement("table");
vcTable.style.cssText = "border: 1px solid white; border-collapse: collapse;";
var vcColgroup = document.createElement("colgroup");
[0,1].forEach((a)=>{
var vcCol = document.createElement("col");
vcCol.style.cssText = "width: 77.5px;"
vcColgroup.appendChild(vcCol);
})
vcTable.appendChild(vcColgroup);
var vcRow = document.createElement("tr");
var vcOne = document.createElement("td");
vcOne.style.cssText = "border: 1px solid white; border-collapse: collapse; padding: 5px;";
var vcTwo = document.createElement("td");
vcTwo.style.cssText = "border: 1px solid white; border-collapse: collapse;padding: 5px;";
wi.id = i.state_name + "_wi";
wi.style.cssText = "border: 1px solid white; border-collapse: collapse;"
var sp1 = document.createElement("span");
sp1.id = i.state_name + "_span1";
var sp2 = document.createElement("span");
sp2.id = i.state_name + "_span2";
var bvotes = i.candidates.filter(function(a){if(a.name_display.match('Biden')){ return a}})[0].votes;
var tvotes = i.candidates.filter(function(a){if(a.name_display.match('Trump')){ return a}})[0].votes;
if(getBidenVotes(i) > getTrumpVotes(i)){
sp1.innerText = getBidenVotes(i);
sp1.style.cssText = "display: inline; color: blue;";
sp2.innerText = getTrumpVotes(i);
sp2.style.cssText = "display: inline; color: red;";
}
if(getTrumpVotes(i) > getBidenVotes(i)){
sp1.innerText = getTrumpVotes(i);
sp1.style.cssText = "display: inline; color: red;";
sp2.innerText = getBidenVotes(i);
sp2.style.cssText = "display: inline; color: blue;";
}
vcOne.appendChild(sp1);
vcTwo.appendChild(sp2);
vcRow.appendChild(vcOne);
vcRow.appendChild(vcTwo);
vcTable.appendChild(vcRow);
wi.appendChild(vcTable);
[ev,st,wi].forEach((a)=>{
row.appendChild(a);
});
pending.push(row);
}
});
pending.forEach((row)=>{
dt.appendChild(row);
})
determined.forEach((row)=>{
dt.appendChild(row);
})
document.body.appendChild(dt);
});
});
function updateTable(){
var bra = [];
var tra = [];
fetch("https://static01.nyt.com/elections-assets/2020/data/api/2020-11-03/national-map-page/national/president.json").then(function(resolve,reject){
resolve.text().then(function(result){
j = JSON.parse(result);
var lastModified = new Date(j.meta.timestamp);
document.getElementById("version").innerText = j.meta.version;
document.getElementById("timestamp").innerText = new Date(j.meta.timestamp).toString();
var sorted = j.data.races.sort(function(a,b){
return new Date(b.last_updated) - new Date(a.last_updated);
});
j.data.races.forEach((i)=>{
if(i.result.match('winner')){
i.candidates.filter((a)=>{
if(a.winner){
if(a.name_display == 'Joseph R. Biden Jr.'){
bra.push(i.electoral_votes);
}
if(a.name_display == 'Donald J. Trump'){
tra.push(i.electoral_votes);
}
}
});
}
});
var bsum = bra.reduce(function(a, b){
return a + b;
}, 0);
var tsum = tra.reduce(function(a,b){
return a + b;
}, 0);
document.getElementById('joebiden').innerText = bsum;
document.getElementById('donaldtrump').innerText = tsum;
sorted.forEach((i)=>{
if(i.result == 'winner'){
document.getElementById(i.state_name + "_ev").innerText = i.electoral_votes;
document.getElementById(i.state_name + "_wi").innerText = i.candidates.filter(function(a){if(a.winner){ return a }})[0].name_display;
if(document.getElementById(i.state_name + "_wi").innerText.match('Biden')){
document.getElementById(i.state_name).style.cssText = "border: 1px solid white; border-collapse: collapse; padding: 5px; color: blue;";
document.getElementById(i.state_name + "_ev").style.cssText = "border: 1px solid white; border-collapse: collapse; padding: 5px; color: blue;";
document.getElementById(i.state_name + "_wi").style.cssText = "border: 1px solid white; border-collapse: collapse; padding: 5px; color: blue;";
}
if(document.getElementById(i.state_name + "_wi").innerText.match('Trump')){
document.getElementById(i.state_name).style.cssText = "border: 1px solid white; border-collapse: collapse; padding: 5px; color: red;";
document.getElementById(i.state_name + "_ev").style.cssText = "border: 1px solid white; border-collapse: collapse; padding: 5px; color: red;";
document.getElementById(i.state_name + "_wi").style.cssText = "border: 1px solid white; border-collapse: collapse; padding: 5px; color: red;";
}
} else {
document.getElementById(i.state_name + "_ev").innerText = i.electoral_votes;
function getBidenVotes(biden){
return biden.candidates.filter(function(a){if(a.name_display.match('Biden')){ return a}})[0].votes;
}
function getTrumpVotes(trump){
return trump.candidates.filter(function(a){if(a.name_display.match('Trump')){ return a}})[0].votes;
}
if(getBidenVotes(i) > getTrumpVotes(i)){
document.getElementById(i.state_name + "_span1").style.cssText = "display: inline; color: blue;";
document.getElementById(i.state_name + "_span1").innerText = getBidenVotes(i);
document.getElementById(i.state_name + "_span2").style.cssText = "display: inline; color: red;";
document.getElementById(i.state_name + "_span2").innerText = getTrumpVotes(i);
}
if(getTrumpVotes(i) > getBidenVotes(i)){
document.getElementById(i.state_name + "_span1").style.cssText = "display: inline; color: red;";
document.getElementById(i.state_name + "_span1").innerText = getTrumpVotes(i);
document.getElementById(i.state_name + "_span2").style.cssText = "display: inline; color: blue;";
document.getElementById(i.state_name + "_span2").innerText = getBidenVotes(i);
}
}
});
});
});
}
setInterval(updateTable,1000);
}
if(document.readyState == "complete"){
makeDataTable();
} else {
console.log("loading script via EventListener");
document.addEventListener("DOMContentLoaded", makeDataTable);
}
</script>
</head>
<body>
</body>
</html>