-
Notifications
You must be signed in to change notification settings - Fork 0
/
j.html
134 lines (130 loc) · 3.72 KB
/
j.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
<!doctype html>
<html>
<head>
<meta charset="utf-8" />
<title>J 48</title>
<script src="jquery-2.0.3.js"></script>
<style>
* {
margin: 0;
padding: 0;
font-family: arial;
}
#error {
font-size: 108px;
color: red;
}
#times {
font-size: 200px;
}
.J {
text-align: left;
-webkit-text-stroke: 10px darkorange;
-webkit-text-fill-color: gray;
display: flex;
align-items: center;
}
.jtime {
flex: 0 0 auto;
}
.weird {
-webkit-text-fill-color: initial;
-webkit-text-stroke: initial;
font-size: 40px;
flex: 0 1 auto;
}
.bus48 {
text-align: right;
-webkit-text-fill-color: yellow;
-webkit-text-stroke: 10px lime;
}
</style>
</head>
<body>
<div id="error"></div>
<div id="times"></div>
<script>
function add_leading_zero(seconds) {
if (seconds >= 10)
return seconds;
return "0" + seconds;
}
var url = "http://webservices.nextbus.com/service/publicXMLFeed?command=predictionsForMultiStops&a=sf-muni&stops=J|3996&stops=48|3461";
// url: "http://webservices.nextbus.com/service/publicXMLFeed?command=predictions&a=sf-muni&stopId=13996",
var debug = false;
if (location.search == "?debug") {
url = "dogs3.xml";
debug = true;
}
var for_each_fills = [];
var alignment;
// TODO: Change alignment from global to a parameter. Pass bound function to forEach.
function forEachPrediction(index, elem) {
if (elem.attributes['affectedByLayover'] == null ||
!elem.attributes['affectedByLayover'].value) {
var seconds = elem.attributes['seconds'].value;
let weird = "";
if (route == "J") {
let inbound = elem.parentNode["attributes"]["title"].value;
if (inbound != "Inbound to Embarcadero Station") {
weird = inbound;
}
}
for_each_fills.push({"seconds": seconds, "route": route, "weird": weird});
}
}
var success = function(e) {
$('#error').empty();
$('#times').empty();
for_each_fills = [];
console.log(e);
route = "J";
directions = $(e).find("predictions[routeTag='J'] > direction[title^='Inbound']");
predictions = directions.find('prediction');
predictions.each(forEachPrediction);
route = "bus48";
directions = $(e).find("predictions[routeTag='48'] > direction[title^='Inbound']");
predictions = directions.find('prediction');
predictions.each(forEachPrediction);
for_each_fills = for_each_fills.sort(function(a, b) {
return a.seconds - b.seconds;
});
for (var i = 0; i < for_each_fills.length; i++) {
var seconds = for_each_fills[i].seconds;
var to_display = Math.floor(seconds / 60) + "m " + add_leading_zero(seconds % 60) + "s";
var weird = for_each_fills[i].weird;
let route = for_each_fills[i].route;
if (route == "J") {
to_display = `<div class=jtime>${to_display}</div><div class=weird>${weird}</div>`
}
$('#times').append('<div class="' + for_each_fills[i].route + '">' + to_display + "</div>");
}
if (for_each_fills.length == 0) {
// console.log("nothing");
$('#times').append('<div>nothing</div>');
}
}
var error = function(e) {
// TODO: Instead of empty/append, change to replace?
$('#error').empty();
$('#error').append("<div>got error: " + JSON.stringify(e, null, 1) + "</div>");
}
function fetchNewData() {
var ajaxOptions = {
url: url,
dataType: "xml",
success: success,
error: error
};
// TODO: Use fetch instead of jquery.
$.ajax(ajaxOptions);
if (!debug) {
setInterval(function() {
$.ajax(ajaxOptions);
}, 10 * 1000)
}
}
fetchNewData();
</script>
</body>
</html>