From 708f25ce83e3ee42ef5ac9da81eb9b29006fe43b Mon Sep 17 00:00:00 2001 From: Dan Cojocaru Date: Fri, 17 Mar 2023 03:16:51 +0100 Subject: [PATCH] Manipulate history when viewing yesterday's train --- sw.js | 2 +- view-train.js | 18 +++++++++++++++++- 2 files changed, 18 insertions(+), 2 deletions(-) diff --git a/sw.js b/sw.js index 7b9b09c..a0f4573 100755 --- a/sw.js +++ b/sw.js @@ -1,4 +1,4 @@ -const VERSION = 'v33' +const VERSION = 'v34' const API_ORIGIN = 'https://scraper.infotren.dcdev.ro/' const API_TRAINS = `${API_ORIGIN}v3/trains` const API_STATIONS = `${API_ORIGIN}v3/stations` diff --git a/view-train.js b/view-train.js index b68da97..f476658 100755 --- a/view-train.js +++ b/view-train.js @@ -89,6 +89,16 @@ function onTrainData(data, fetchDate) { e.preventDefault() // Implement date switcher yesterday = !yesterday + if (!yesterday) { + history.back() + } + else { + const yesterdayUrl = new URL(location.href) + const newDate = new Date(date.getTime()) + newDate.setDate(newDate.getDate() - 1) + yesterdayUrl.searchParams.set('date', newDate.toISOString()) + history.pushState('', '', yesterdayUrl) + } refresh() }) @@ -495,7 +505,13 @@ function rsk() { } window.addEventListener('popstate', function (e) { - groupIndex = null + if (yesterday) { + yesterday = false + trainData = null + } + else { + groupIndex = null + } if (trainData) { onTrainData(trainData) }