-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
418 lines (393 loc) · 12.7 KB
/
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
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<link rel="manifest" href="manifest.json" />
<title>一鍵公車</title>
<script>
const LAST_MODIFIED_DATE = '2020091701';
//prevent css cache not update
document.getElementsByTagName("head")[0].insertAdjacentHTML(
'afterBegin',
'<link rel="stylesheet" href="nav.css?v='+LAST_MODIFIED_DATE+'" />');
const REFRESH_PERIOD = 60*1000;
const URL = 'https://script.google.com/macros/s/AKfycbxVH6wsfiRs_u-GSLVKVoENVX_SfJ7wUSFEqTB_hDFleA4NzEY/exec?key=7dKw1ubRxz4EtL2EuN5ZWKZPT9GBeL6T';
const URL_DATA = URL + '&action=get_bus';
const URL_INFO = URL + '&action=get_info';
const CACHE_DATA_TIME = 'cache_data_time';
const CACHE_DATA = 'cache_data';
const CACHE_INFO = 'cache_info';
const CACHE_INFO_TIME = 'cache_info_time';
let info = {title:''};
//for share API
const shareData = {
url: window.location.href
};
window.addEventListener('load', function(){
loadInfo();
loadData();
if ('serviceWorker' in navigator) {
//console.log("Will the service worker register?");
navigator.serviceWorker.register('service-worker.js')
.then(function(reg){
//console.log("Yes, it did.");
}).catch(function(err) {
//console.log("No it didn't. This happened: ", err)
});
}
let btnAddHomescreen = document.getElementById('add_homescreen');
btnAddHomescreen.addEventListener('click', (e) => {
// hide our user interface that shows our A2HS button
btnAddHomescreen.style.display = 'none';
// Show the prompt
deferredPrompt.prompt();
// Wait for the user to respond to the prompt
deferredPrompt.userChoice
.then((choiceResult) => {
if (choiceResult.outcome === 'accepted') {
console.log('User accepted the A2HS prompt');
} else {
console.log('User dismissed the A2HS prompt');
}
deferredPrompt = null;
});
});
//stop load when not visible,and restart when visible
document.addEventListener('visibilitychange', function(){
clearTimeout(loadTimeout);
if(document['hidden']){
}else{
loadData()
}
}, false);
//intial nav
const jsMenuNode = document.querySelector('.menu');
const demoMenu = new Menu ({
menuNode: jsMenuNode
});
function callMenuToggle(event) {
demoMenu.toggleMenuState('menu_activated');
}
jsMenuNode.querySelector('.menu__hamburger').addEventListener('click', callMenuToggle);
});
let deferredPrompt;
window.addEventListener('beforeinstallprompt', (e) => {
e.preventDefault();
deferredPrompt = e;
// Update UI notify the user they can add to home screen
btnAddHomescreen.style.display = 'block';
});
function loadInfo(){
let now = new Date();
//if less than 300s use cache
if(localStorage[CACHE_INFO] &&
localStorage[CACHE_INFO_TIME] &&
(now.getTime() - parseInt(localStorage[CACHE_INFO_TIME], 10) < 300000)){
info = JSON.parse(localStorage[CACHE_INFO]);
updateInfo();
return;
}
fetch(URL_INFO,{
method:'GET',
headers: {
'Content-Type': 'application/x-www-form-urlencoded; charset=utf-8'
}
})
.then(res => {
return res.json();
}).then(result => {
info = result;
updateInfo();
localStorage[CACHE_INFO] = JSON.stringify(info);
localStorage[CACHE_INFO_TIME] = now.getTime();
})
.catch(function(err) {
console.log(err);
//loadInfo();
});
}
function updateInfo(){
document.querySelector('#title').innerText = info['title'];
}
let loadTimeout;
function loadData(){
let now = new Date();
//if less than 60s use cache
if(localStorage[CACHE_DATA] &&
localStorage[CACHE_DATA_TIME] &&
(now.getTime() - parseInt(localStorage[CACHE_DATA_TIME], 10) < 60000)){
parseData(JSON.parse(localStorage[CACHE_DATA]));
return;
}
document.querySelector('.loading').classList.toggle('hidden');
fetch(URL_DATA,{
method:'GET',
headers: {
'Content-Type': 'application/x-www-form-urlencoded; charset=utf-8'
}
})
.then(res => {
return res.json();
}).then(result => {
parseData(result);
localStorage[CACHE_DATA] = JSON.stringify(result);
localStorage[CACHE_DATA_TIME] = now.getTime();
document.querySelector('.loading').classList.toggle('hidden');
loadTimeout = setTimeout(function(){loadData()}, REFRESH_PERIOD);
})
.catch(function(err) {
alert(err);
document.querySelector('.loading').classList.toggle('hidden');
loadTimeout = setTimeout(function(){loadData()}, REFRESH_PERIOD);
});
}
function parseData(result){
let estimate = [];
for(let i=0;i<result.length;i++){
let text = '';
if(result[i]['PlateNumb'] == '-1' || result[i]['PlateNumb'] == ''){
text = getStatus(result[i]['StopStatus'], result[i]['NextBusTime']);
}else{
if(result[i]['EstimateTime'] > 60){
text = Math.ceil(result[i]['EstimateTime']/60)+'分後';
}else{
text = '1分鐘內';
}
}
estimate.push({
route: result[i]['SubRouteName']['Zh_tw'],
no: result[i]['PlateNumb'],
text: text,
stop: result[i]['StopName']['Zh_tw']
});
}
showResult(estimate);
}
function showResult(estimate){
let content = '';
for(let i=0;i<estimate.length;i++){
content += '<div class="info_row">'+
'<div class="info_route">'+estimate[i]['route'] + '</div>'+
'<div class="info_no">'+estimate[i]['no']+'</div>'+
'<div class="info_text">' + estimate[i]['text']+'</div>'+
'<div class="info_stop">' + estimate[i]['stop']+'</div>'+
'</div>';
}
document.getElementById('estimate_info').innerHTML = content;
}
function getStatus(status, next_time){
if(status === 1){
if(next_time == undefined){ return '尚未發車';}
let time = new Date(next_time);
let hour = time.getHours() < 10 ? '0'+time.getHours() : time.getHours();
let minute = time.getMinutes() < 10 ? '0'+time.getMinutes() : time.getMinutes();
return hour + ':' + minute + '發車';
}else if(status === 2){
return '交管不停靠';
}else if(status === 3){
return '末班車已過';
}else if(status === 4){
return '今日未營運';
}
}
function assignSvgUrl(svg){
let use = svg.getElementsByTagName("use")[0];
use.href.baseVal = 'icon.svg?v='+LAST_MODIFIED_DATE+'#' + svg.dataset.svgId;
}
function shareUrl(){
if (navigator.share) {
try {
navigator.share(shareData);
} catch (err) {
const { name, message } = err;
if (name === 'AbortError') {
console.log('您已取消分享此訊息');
} else {
console.log('發生錯誤', err);
}
}
} else {
copy(window.location.href);
showToast('網址複製完成');
}
document.querySelector('.menu__hamburger').dispatchEvent(new Event('click'));
}
function copy(text){
const input = document.createElement('input');
input.value = text;
document.body.appendChild(input);
input.select();
if (document.execCommand('Copy')) {
document.execCommand('Copy');
}
document.body.removeChild(input);
}
function showToast(text) {
let x = document.getElementById("toast");
x.innerText = text;
x.classList.toggle('show');
setTimeout(function(){ x.classList.toggle('show'); }, 3000);
}
//from https://codepen.io/melnik909/pen/JpJPYp
class Menu {
constructor(settings) {
this.menuNode = settings.menuNode;
}
toggleMenuState(className) {
if (typeof className !== 'string' || className.length === 0) {
return console.log('you did not give the class for toggleState function');
}
return this.menuNode.classList.toggle(className);
}
}
</script>
<style>
html{
height: 100%;
background-image: linear-gradient(35deg, rgba(253, 253, 253, 0.03) 0%, rgba(253, 253, 253, 0.03) 53%,rgba(109, 109, 109, 0.03) 53%, rgba(109, 109, 109, 0.03) 59%,rgba(228, 228, 228, 0.03) 59%, rgba(228, 228, 228, 0.03) 66%,rgba(42, 42, 42, 0.03) 66%, rgba(42, 42, 42, 0.03) 95%,rgba(165, 165, 165, 0.03) 95%, rgba(165, 165, 165, 0.03) 100%),linear-gradient(205deg, rgba(62, 62, 62, 0.03) 0%, rgba(62, 62, 62, 0.03) 31%,rgba(200, 200, 200, 0.03) 31%, rgba(200, 200, 200, 0.03) 41%,rgba(30, 30, 30, 0.03) 41%, rgba(30, 30, 30, 0.03) 47%,rgba(151, 151, 151, 0.03) 47%, rgba(151, 151, 151, 0.03) 60%,rgba(95, 95, 95, 0.03) 60%, rgba(95, 95, 95, 0.03) 100%),linear-gradient(30deg, rgba(7, 7, 7, 0.03) 0%, rgba(7, 7, 7, 0.03) 19%,rgba(63, 63, 63, 0.03) 19%, rgba(63, 63, 63, 0.03) 33%,rgba(175, 175, 175, 0.03) 33%, rgba(175, 175, 175, 0.03) 37%,rgba(244, 244, 244, 0.03) 37%, rgba(244, 244, 244, 0.03) 60%,rgba(177, 177, 177, 0.03) 60%, rgba(177, 177, 177, 0.03) 100%),linear-gradient(90deg, rgb(162, 162, 162),rgb(229, 229, 229));
}
body{
padding: 0;
}
body *{
text-align: center;
}
h1{
margin: 0.2em 0;
}
#add_homescreen{
display: none;
}
.info_row{
padding: .5em 0;
}
.info_route{
display: inline-block;
width: 15%;
}
.info_no{
display: inline-block;
width: 25%;
}
.info_text{
display: inline-block;
width: 30%;
}
.info_stop{
display: inline-block;
width: 30%;
}
.svg-icon {
fill: #fff;
width: .8em;
height: .8em;
margin: .2em;
}
.loading{
width: 1.5em;
height: 1.5em;
position: absolute;
bottom: .3em;
right: .3em;
fill: black;
animation: spin 1.4s infinite linear;
}
.hidden { display: none; }
@keyframes spin{
0% {
transform: rotate(0deg);
}
100% {
transform: rotate(-360deg);
}
}
#toast {
min-width: 250px;
background-color: #333;
color: #fff;
text-align: center;
border-radius: 2px;
padding: 16px;
position: fixed;
z-index: 1000; /* need to higher than .menu in nav.css */
left: 50%; /* Center the toast */
transform: translatex(-50%);
opacity: 0;
bottom: calc(let(--toastBottom) * -1 );
transition: bottom .5s, opacity .5s;/* move with animation */
}
#toast.show {
opacity: 1;
bottom: let(--toastBottom);
transition: bottom .5s, opacity .5s;/* move with animation */
}
:root{
--toastBottom: 3.2em;
}
</style>
</head>
<body>
<div class="page">
<center>
<h1 id="title"> </h1>
</center>
<div id="estimate_info">讀取中...</div>
<div>
<input id="add_homescreen" type="button" value="新增到桌面"/>
</div>
<svg class="svg-icon loading hidden" xmlns="http://www.w3.org/2000/svg" onload="">
<use xlink:href="icon.svg#loading"></use>
</svg>
</div>
<div class="menu">
<nav class="menu__nav">
<ul class="r-list menu__list">
<li class="menu__group">
<a href="https://ptx.transportdata.tw/PTX" class="r-link menu__link">
<svg class="svg-icon" xmlns="http://www.w3.org/2000/svg" data-svg-id="bus" onload="assignSvgUrl(this);">
<use xlink:href=""></use>
</svg>交通部PTX
</a>
</li>
<li class="menu__group">
<a href="https://woodloch.blog/aboutme/" class="r-link menu__link">
<svg class="svg-icon" xmlns="http://www.w3.org/2000/svg" data-svg-id="user" onload="assignSvgUrl(this);">
<use xlink:href=""></use>
</svg>木澤
</a>
</li>
<li class="menu__group">
<a href="https://woodlochhome.wordpress.com/2020/05/30/oneclickbus/" class="r-link menu__link">
<svg class="svg-icon" xmlns="http://www.w3.org/2000/svg" data-svg-id="link" onload="assignSvgUrl(this);">
<use xlink:href=""></use>
</svg>部落格
</a>
</li>
<li class="menu__group">
<a href="mailto:[email protected]" class="r-link menu__link">
<svg class="svg-icon" xmlns="http://www.w3.org/2000/svg" data-svg-id="mail" onload="assignSvgUrl(this);">
<use xlink:href=""></use>
</svg>聯繫我
</a>
</li>
<li class="menu__group">
<a href="javascript:void(0);" onclick="shareUrl();" class="r-link menu__link">
<svg class="svg-icon" xmlns="http://www.w3.org/2000/svg" data-svg-id="share" onload="assignSvgUrl(this);">
<use xlink:href=""></use>
</svg>分享給朋友
</a>
</li>
</ul>
</nav>
<div class="menu__toggle">
<button class="r-button menu__hamburger">
<span class="m-hamburger">
<span class="m-hamburger__label">Open menu</span>
</span>
</button>
</div>
</div>
<div id="toast">Some text some message..</div>
</body>
</html>