-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.js
116 lines (90 loc) · 2.89 KB
/
main.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
const corsProxy = "https://api.allorigins.win/get?url=";
const rootUrl = encodeURIComponent("https://www.trentbarton.co.uk/RTILiveTimings.aspx");
const getAllStops = encodeURIComponent("?m=GetStopsForMap&isService=false");
const getStop = encodeURIComponent("?m=GetRtiFull&stop=");
function getStoreageItem(name, defaultValue) {
var result = JSON.parse(window.localStorage.getItem(name));
if (result) {
return result
} else {
return defaultValue;
}
};
function setStoreageItem(name, value) {
window.localStorage.setItem(name, JSON.stringify(value));
};
(function getStops() {
const urlParams = new URLSearchParams(window.location.search);
const lastStopData = getStoreageItem("lastStopData", {})
var stopCode = (urlParams.get('stop') || "").toLowerCase();
if (stopCode === lastStopData.C) {
updateDetails(lastStopData.Id);
return;
};
if (!stopCode && lastStopData.Id) {
updateDetails(lastStopData.Id);
return;
}
fetch(corsProxy + rootUrl + getAllStops,
{
method: "GET"
})
.then(resp => resp.json())
.then(data => {
data = JSON.parse(data.contents);
data = data.filter(stopData => stopData.C === stopCode);
if (data.length) {
var stopDetails = data[0];
} else {
var stopDetails = {C: "ntmatgjw", Id: 15896}
};
updateDetails(stopDetails.Id);
setStoreageItem("lastStopData", stopDetails);
})
})();
function updateDetails(forStopId) {
fetch(corsProxy + rootUrl + getStop + forStopId,
{
method: "GET"
})
.then((resp) => resp.json())
.then((data) => {
data = JSON.parse(data.contents);
let rows = document.querySelectorAll(".row")
if (rows) {
rows.forEach((row) => {
row.parentNode.removeChild(row);
})
}
if (data && data[0] && data[0].result) {
document.title = data[0].stopName
data[0].result.forEach((bus) => {
addRowTo("service", bus.serviceDisplayName)
addRowTo("destination", bus.destination)
addRowTo("duein", bus.dueIn)
})
}
setTimeout(() => {updateDetails(forStopId)}, 15000)
})
};
var showColon = true;
(function updateTime() {
var now = new Date();
function pad(int) {
return int.toString().padStart(2, '0')
}
var colon = ":";
if (!showColon) {
colon = " ";
};
showColon = !showColon;
document.querySelector(".time")
.textContent = `Time is ${pad(now.getHours())}${colon}${pad(now.getMinutes())}`;
setTimeout(updateTime, 500)
})();
function addRowTo(elementId, text) {
let newRow = document.createElement("div");
newRow.classList.add("large-text", "row");
newRow.appendChild(document.createTextNode(text));
document.getElementById(elementId).appendChild(newRow);
};