diff --git a/index.html b/index.html index 04b658c..c6b0134 100644 --- a/index.html +++ b/index.html @@ -12,6 +12,8 @@ + +

InfoTren

@@ -19,8 +21,8 @@
diff --git a/index.js b/index.js new file mode 100644 index 0000000..91b41d4 --- /dev/null +++ b/index.js @@ -0,0 +1,58 @@ +window.addEventListener('load', function (e) { + if (window.localStorage) { + var recentViewTrain = localStorage.getItem('recent/view-train') + if (recentViewTrain) { + /** + * @property {string} trainNumber + * @property {string} date + * @property {string} $addDate + * @property {string | undefined} groupIndex + */ + recentViewTrain = JSON.parse(recentViewTrain) + var addDate = new Date(recentViewTrain.$addDate) + addDate.setHours(addDate.getHours() + 2) // store recents for 2 hours + if (addDate.getTime() > Date.now()) { + var recentViewTrainLi = document.createElement('li') + var recentViewTrainLink = document.createElement('a') + recentViewTrainLi.appendChild(recentViewTrainLink) + var recentViewTrainLinkUrl = new URL('/view-train.html', window.location.origin) + recentViewTrainLinkUrl.searchParams.append('train', recentViewTrain.trainNumber) + recentViewTrainLinkUrl.searchParams.append('date', recentViewTrain.date) + if (recentViewTrain.groupIndex) { + recentViewTrainLinkUrl.searchParams.append('groupIndex', recentViewTrain.groupIndex) + } + recentViewTrainLink.href = recentViewTrainLinkUrl.toString() + recentViewTrainLink.classList.add('items') + recentViewTrainLink.innerText = `Recent train: ${recentViewTrain.trainNumber}` + + fetch(`https://scraper.infotren.dcdev.ro/v3/trains/${recentViewTrain.trainNumber}?date=${recentViewTrain.date}`) + .then(function (result) { + if (result.ok) { + return result.json() + } + }) + .then(function (result) { + recentViewTrainLink.innerText = 'Recent train: ' + trainIdSpan(result.rank, result.number, recentViewTrainLink) + if (recentViewTrain.groupIndex !== undefined || result.groups.length === 1) { + var group = result.groups[recentViewTrain.groupIndex || 0] + if (group.status) { + if (group.status.delay === 0) { + recentViewTrainLink.appendChild(document.createTextNode(" (on time)")) + } + else if (group.status.delay > 0) { + recentViewTrainLink.appendChild(document.createTextNode(` (${group.status.delay} min late)`)) + } + else { + recentViewTrainLink.appendChild(document.createTextNode(` (${-group.status.delay} min early)`)) + } + } + } + }) + + var myTrainLi = document.getElementById("my-train-li") + myTrainLi.parentNode.insertBefore(recentViewTrainLi, myTrainLi.nextSibling) + } + } + } +}) diff --git a/sw.js b/sw.js index ee57a69..d4993e1 100755 --- a/sw.js +++ b/sw.js @@ -1,4 +1,4 @@ -const VERSION = 'v23' +const VERSION = 'v24' const API_ORIGIN = 'https://scraper.infotren.dcdev.ro/' const API_TRAINS = `${API_ORIGIN}v3/trains` const API_STATIONS = `${API_ORIGIN}v3/stations` @@ -22,8 +22,10 @@ const CACHE_FIRST = [ // Pages '/index.html', + '/index.js', '/about.html', + '/about.js', '/train.html', '/train.js', diff --git a/view-train.js b/view-train.js index bcc7b4a..b68da97 100755 --- a/view-train.js +++ b/view-train.js @@ -129,6 +129,14 @@ function onTrainData(data, fetchDate) { function onAction(e) { var url = new URL(window.location.toString()) groupIndex = i + if (window.localStorage) { + localStorage.setItem('recent/view-train', JSON.stringify({ + trainNumber: trainNumber, + date: date.toISOString(), + groupIndex: groupIndex, + $addDate: new Date().toISOString(), + })) + } url.searchParams.append('groupIndex', groupIndex) window.history.pushState({'groupIndex': groupIndex}, '', url.toString( )) onTrainData(data) @@ -512,6 +520,19 @@ window.addEventListener('load', function (e) { date = sp.has('date') ? new Date(sp.get('date')) : new Date() groupIndex = sp.has('groupIndex') ? parseInt(sp.get('groupIndex')) : null + if (window.localStorage) { + var oldRecent = localStorage.getItem('recent/view-train') + if (oldRecent) { + oldRecent = JSON.parse(oldRecent) + } + localStorage.setItem('recent/view-train', JSON.stringify({ + trainNumber: trainNumber, + date: date.toISOString(), + groupIndex: oldRecent && oldRecent.trainNumber === trainNumber ? oldRecent.groupIndex : undefined, + $addDate: new Date().toISOString(), + })) + } + document.querySelectorAll('.rsk').forEach(function (rskElem) { rskElem.addEventListener('click', function (e) { rsk()