From 086fff906e43f5ef5536d909a496694422321273 Mon Sep 17 00:00:00 2001 From: pistashe Date: Wed, 8 Dec 2021 11:48:01 +0100 Subject: [PATCH 1/3] add max date for appointment --- browser_action/index.html | 9 ++++++++ browser_action/index.js | 9 ++++++++ commons/AppStatus.js | 26 +++++++++++++++++++++ content_scripts/doctolib/book.js | 39 ++++++++++++++++++++++++++++---- 4 files changed, 78 insertions(+), 5 deletions(-) diff --git a/browser_action/index.html b/browser_action/index.html index 3af5c50..36506c4 100644 --- a/browser_action/index.html +++ b/browser_action/index.html @@ -95,6 +95,15 @@
+
+
+ + +
+
+ +
+
diff --git a/browser_action/index.js b/browser_action/index.js index 5a0a21d..651fe5f 100644 --- a/browser_action/index.js +++ b/browser_action/index.js @@ -104,6 +104,11 @@ document.getElementById(injectionVaccine).checked = true; } + function displayDateMaxSearch(dateMaxSearch) { + const date = new Date(dateMaxSearch); + document.getElementById('dateMax').value = date.toISOString().split('T')[0] + } + // Preparation des données const appStatus = new AppStatus(); const vCLStorage = new VCLocalStorage({ @@ -118,6 +123,7 @@ appStatus.onAutoBookChange(displayAutoBook); appStatus.onInjectionTypeChange(displayInjectionType); appStatus.onInjectionVaccineChange(displayInjectionVaccine); + appStatus.onDateMaxSearchChange(displayDateMaxSearch); // Initialisation donnée appStatus.init(); @@ -150,6 +156,8 @@ appStatus.setInjectionVaccine.bind(appStatus, "modernaInjection"); document.getElementById("pfizerInjection").onclick = appStatus.setInjectionVaccine.bind(appStatus, "pfizerInjection"); + document.getElementById("dateMax").onblur = + appStatus.setDateMaxSearch.bind(appStatus, document.getElementById('dateMax')); document.getElementById("reset").onclick = () => { if ( @@ -169,5 +177,6 @@ displayAutoBook(appStatus.getAutoBook()); displayInjectionType(appStatus.getInjectionType()); displayInjectionVaccine(appStatus.getInjectionVaccine()); + displayDateMaxSearch(appStatus.getDateMaxSearch()); displayLocations(); })(); diff --git a/commons/AppStatus.js b/commons/AppStatus.js index 06d3440..4a26955 100644 --- a/commons/AppStatus.js +++ b/commons/AppStatus.js @@ -22,6 +22,7 @@ class AppStatus { this.injectionType = "fullServiceInjection"; /** @type {'modernaInjection' | 'pfizerInjection'} vaccin d'injection souhaité par le user */ this.injectionVaccine = "pfizerInjection"; + this.dateMaxSearch = new Date(2022, 1, 15); /** @type {(string) => void} callback quand une {@link VaccineLocation} a été ajouté */ this.onLocationAddedCb = (job) => {}; /** @type {(string) => void} callback quand une {@link VaccineLocation} a été supprimée */ @@ -34,6 +35,7 @@ class AppStatus { this.onInjectionTypeCb = (newValue) => {}; /** @type {'modernaInjection' | 'pfizerInjection'} callback quand injectionVaccine change de valeur */ this.onInjectionVaccineCb = (newValue) => {}; + this.onDateMaxSearchCb = (newValue) => {}; this.onStorageChange = this.onStorageChange.bind(this); browser.storage.onChanged.addListener(this.onStorageChange); @@ -49,6 +51,7 @@ class AppStatus { autoBook: false, injectionType: "fullServiceInjection", injectionVaccine: "pfizerInjection", + dateMaxSearch: new Date(2022, 1, 15), }); Object.keys(result.locations).forEach((url) => { @@ -67,6 +70,9 @@ class AppStatus { this.injectionVaccine = result.injectionVaccine; this.onInjectionVaccineCb(this.injectionVaccine); + + this.dateMaxSearch = result.dateMaxSearch; + this.onDateMaxSearchCb(this.dateMaxSearch); } getLocations() { @@ -105,6 +111,10 @@ class AppStatus { return this.injectionVaccine; } + getDateMaxSearch() { + return this.dateMaxSearch; + } + /** * @param {(string) => void} cbAdd callback quand une {@link VaccineLocation} a été ajouté * @param {(string) => void} cbDelete callback quand une {@link VaccineLocation} a été supprimée @@ -141,6 +151,9 @@ class AppStatus { onInjectionVaccineChange(callback) { this.onInjectionVaccineCb = callback; } + onDateMaxSearchChange(callback) { + this.onDateMaxSearchCb = callback; + } start() { this.stopped = false; @@ -168,6 +181,11 @@ class AppStatus { browser.storage.sync.set({ injectionType: this.injectionType }); } + setDateMaxSearch(value) { + this.dateMaxSearch = new Date(value.value); + browser.storage.sync.set({ dateMaxSearch: this.dateMaxSearch }); + } + /** * @param {'modernaInjection' | 'pfizerInjection'} value The new injectionVaccine value */ @@ -193,6 +211,8 @@ class AppStatus { this.onInjectionTypeCb(this.injectionType); this.injectionVaccine = "pfizerInjection"; this.onInjectionTypeCb(this.injectionVaccine); + this.dateMaxSearch = new Date(2022, 1, 15); + this.onDateMaxSearchCb(this.dateMaxSearch); } /** @@ -209,6 +229,7 @@ class AppStatus { this.onAutoBookChangeCb = null; this.onInjectionTypeCb = null; this.onInjectionVaccineCb = null; + this.onDateMaxSearchCb = null; } /** @@ -262,5 +283,10 @@ class AppStatus { this.onInjectionVaccineCb(this.injectionVaccine); } + if (change.dateMaxSearch) { + this.dateMaxSearch = change.dateMaxSearch.newValue; + + this.onDateMaxSearchCb(this.dateMaxSearch); + } } } diff --git a/content_scripts/doctolib/book.js b/content_scripts/doctolib/book.js index 9bf02c2..8195d49 100644 --- a/content_scripts/doctolib/book.js +++ b/content_scripts/doctolib/book.js @@ -27,6 +27,20 @@ if (currentMonth > selectedMonth) return new Date().getFullYear() + 1; return new Date().getFullYear(); } + const MONTHS = { + 'janv.': 1, + 'févr.': 2, + 'mars': 3, + 'avr.': 4, + 'mai': 5, + 'juin': 6, + 'juil.': 7, + 'aout': 8, + 'sept.': 9, + 'oct.': 10, + 'nov.': 11, + 'déc.': 12, + }; async function waitTimeout(timeout) { await new Promise((r) => setTimeout(r, timeout)); @@ -295,15 +309,19 @@ let running = false; async function checkAvailability() { - const { locations, stopped, autoBook, injectionType, injectionVaccine } = + const { locations, stopped, autoBook, injectionType, injectionVaccine, dateMaxSearch } = await browser.storage.sync.get({ locations: {}, stopped: false, autoBook: false, injectionType: "fullServiceInjection", injectionVaccine: "pfizerInjection", + dateMaxSearch: new Date(2022, 1, 15) }); + const dateMaxSearchDate = typeof(dateMaxSearch) === 'string' ? new Date(dateMaxSearch) : dateMaxSearch; + console.log(dateMaxSearchDate) + if (stopped || !locations[url]) { running = false; return; @@ -421,6 +439,8 @@ const parts = slot.title.match( /([0-9]+)\.? ([\p{Letter}]+)\.? ([0-9]+:[0-9]+)/u ); + // /([0-9]+) [\p{Letter}]+\.? ([0-9]+:[0-9]+)/gu + // )[0].split(' '); if (!parts) { throw new Error( browser.i18n.getMessage("slotDateFormatNotFound", slot.title) @@ -434,18 +454,27 @@ const selectedTime = parts[3]; const date = new Date( - `${selectedMonth} ${selectedDay} ${selectedYear} ${selectedTime}` - ); + `${selectedMonth} ${selectedDay} ${selectedYear} ${selectedTime}`); + // console.log(parts) + // const year = parts[1] === 'decembre' ? 2021 : 2022; + // const date = new Date( + // `${MONTHS[parts[1]]} ${parts[0]} ${year} ${ + // parts[2] + // }` + // ); const tomorrow = new Date(); tomorrow.setHours(23); tomorrow.setMinutes(59); tomorrow.setDate(tomorrow.getDate() + 1); - if (date > tomorrow && date < new Date("2021-05-31T00:20:00")) + + if (date > dateMaxSearchDate) { + const formatedDate = dateMaxSearchDate.toLocaleDateString(); throw new Error( - "Pas de créneau dispo d'ici demain soir ou après le 31 mai" + `Pas de créneau dispo d'ici demain soir ou avant le ${formatedDate}` ); + } if (!autoBook) { browser.runtime.sendMessage({ From 9d202878d1ce59965503e2b2c762fd0c07a01747 Mon Sep 17 00:00:00 2001 From: pistashe Date: Sun, 12 Dec 2021 18:20:54 +0100 Subject: [PATCH 2/3] use relative dates & add doc --- browser_action/index.html | 2 +- browser_action/index.js | 1 + commons/AppStatus.js | 16 +++++++++++++--- content_scripts/doctolib/book.js | 16 +--------------- 4 files changed, 16 insertions(+), 19 deletions(-) diff --git a/browser_action/index.html b/browser_action/index.html index 36506c4..e1ea499 100644 --- a/browser_action/index.html +++ b/browser_action/index.html @@ -98,7 +98,7 @@
- +
diff --git a/browser_action/index.js b/browser_action/index.js index 651fe5f..dec278a 100644 --- a/browser_action/index.js +++ b/browser_action/index.js @@ -104,6 +104,7 @@ document.getElementById(injectionVaccine).checked = true; } + /** @param {Date} dateMaxSearch La date maximale de recherche de rdv */ function displayDateMaxSearch(dateMaxSearch) { const date = new Date(dateMaxSearch); document.getElementById('dateMax').value = date.toISOString().split('T')[0] diff --git a/commons/AppStatus.js b/commons/AppStatus.js index 4a26955..01b91f5 100644 --- a/commons/AppStatus.js +++ b/commons/AppStatus.js @@ -22,7 +22,8 @@ class AppStatus { this.injectionType = "fullServiceInjection"; /** @type {'modernaInjection' | 'pfizerInjection'} vaccin d'injection souhaité par le user */ this.injectionVaccine = "pfizerInjection"; - this.dateMaxSearch = new Date(2022, 1, 15); + /** @type {Date} Date max de recherche de rdv (par défaut aujd + 1 mois) */ + this.dateMaxSearch = new Date((new Date()).getFullYear()+1, (new Date()).getMonth(), (new Date()).getDate()); /** @type {(string) => void} callback quand une {@link VaccineLocation} a été ajouté */ this.onLocationAddedCb = (job) => {}; /** @type {(string) => void} callback quand une {@link VaccineLocation} a été supprimée */ @@ -35,6 +36,7 @@ class AppStatus { this.onInjectionTypeCb = (newValue) => {}; /** @type {'modernaInjection' | 'pfizerInjection'} callback quand injectionVaccine change de valeur */ this.onInjectionVaccineCb = (newValue) => {}; + /** @type {Date} callback quand maxDateSearch change de valeur */ this.onDateMaxSearchCb = (newValue) => {}; this.onStorageChange = this.onStorageChange.bind(this); @@ -51,7 +53,7 @@ class AppStatus { autoBook: false, injectionType: "fullServiceInjection", injectionVaccine: "pfizerInjection", - dateMaxSearch: new Date(2022, 1, 15), + dateMaxSearch: new Date((new Date()).getFullYear()+1, (new Date()).getMonth(), (new Date()).getDate()), }); Object.keys(result.locations).forEach((url) => { @@ -151,6 +153,10 @@ class AppStatus { onInjectionVaccineChange(callback) { this.onInjectionVaccineCb = callback; } + + /** + * @param {(Date) => void} callback quand dateMaxSearch change de valeur + */ onDateMaxSearchChange(callback) { this.onDateMaxSearchCb = callback; } @@ -181,6 +187,10 @@ class AppStatus { browser.storage.sync.set({ injectionType: this.injectionType }); } + + /** + * @param {Date} value The new dateMaxSearch value + */ setDateMaxSearch(value) { this.dateMaxSearch = new Date(value.value); browser.storage.sync.set({ dateMaxSearch: this.dateMaxSearch }); @@ -211,7 +221,7 @@ class AppStatus { this.onInjectionTypeCb(this.injectionType); this.injectionVaccine = "pfizerInjection"; this.onInjectionTypeCb(this.injectionVaccine); - this.dateMaxSearch = new Date(2022, 1, 15); + this.dateMaxSearch = new Date((new Date()).getFullYear(), (new Date()).getMonth()+1, (new Date()).getDate()); this.onDateMaxSearchCb(this.dateMaxSearch); } diff --git a/content_scripts/doctolib/book.js b/content_scripts/doctolib/book.js index 8195d49..c183c4d 100644 --- a/content_scripts/doctolib/book.js +++ b/content_scripts/doctolib/book.js @@ -316,11 +316,10 @@ autoBook: false, injectionType: "fullServiceInjection", injectionVaccine: "pfizerInjection", - dateMaxSearch: new Date(2022, 1, 15) + dateMaxSearch: new Date((new Date()).getFullYear()+1, (new Date()).getMonth(), (new Date()).getDate()), }); const dateMaxSearchDate = typeof(dateMaxSearch) === 'string' ? new Date(dateMaxSearch) : dateMaxSearch; - console.log(dateMaxSearchDate) if (stopped || !locations[url]) { running = false; @@ -455,19 +454,6 @@ const date = new Date( `${selectedMonth} ${selectedDay} ${selectedYear} ${selectedTime}`); - // console.log(parts) - // const year = parts[1] === 'decembre' ? 2021 : 2022; - // const date = new Date( - // `${MONTHS[parts[1]]} ${parts[0]} ${year} ${ - // parts[2] - // }` - // ); - - const tomorrow = new Date(); - tomorrow.setHours(23); - tomorrow.setMinutes(59); - tomorrow.setDate(tomorrow.getDate() + 1); - if (date > dateMaxSearchDate) { const formatedDate = dateMaxSearchDate.toLocaleDateString(); From d30b6475a0052dcbcf96a9bfedf837207efa19c8 Mon Sep 17 00:00:00 2001 From: pistashe Date: Thu, 16 Dec 2021 11:24:24 +0100 Subject: [PATCH 3/3] smells fix & prettier --- _locales/de/messages.json | 4 +++ _locales/fr/messages.json | 4 +++ browser_action/index.html | 4 ++- browser_action/index.js | 8 +++-- commons/AppStatus.js | 19 ++++++++--- content_scripts/doctolib/book.js | 54 +++++++++++++++----------------- 6 files changed, 57 insertions(+), 36 deletions(-) diff --git a/_locales/de/messages.json b/_locales/de/messages.json index a33f342..d109a78 100644 --- a/_locales/de/messages.json +++ b/_locales/de/messages.json @@ -35,6 +35,10 @@ "message": "Auffrischimpfung", "description": "Text der alle Auffrischimpfungen beschreibt." }, + "maxDateLabel": { + "message": "Termin finden bis zum (DD. MM. YYYY)", + "description": "Bis zu diesem Datum wird nach einem Termin gesucht" + }, "modernaVaccine": { "message": "Moderna Impfstoff", "description": "Beschreibung des Moderna Impfstoffes." diff --git a/_locales/fr/messages.json b/_locales/fr/messages.json index ba30b22..810a093 100644 --- a/_locales/fr/messages.json +++ b/_locales/fr/messages.json @@ -35,6 +35,10 @@ "message": "Troisième dose", "description": "Text qui décrit une injection de rappel au dela de la deuxième dose." }, + " maxDateLabel": { + "message": "Date maximale de recherche (MM/DD/YYYY)", + "description": "Date jusqu'à laquelle un rdv sera cherché" + }, "modernaVaccine": { "message": "Vaccin Moderna", "description": "Text qui décrit un vaccin Moderna." diff --git a/browser_action/index.html b/browser_action/index.html index e1ea499..aa6493e 100644 --- a/browser_action/index.html +++ b/browser_action/index.html @@ -98,7 +98,9 @@
- +
diff --git a/browser_action/index.js b/browser_action/index.js index dec278a..20e7152 100644 --- a/browser_action/index.js +++ b/browser_action/index.js @@ -107,7 +107,7 @@ /** @param {Date} dateMaxSearch La date maximale de recherche de rdv */ function displayDateMaxSearch(dateMaxSearch) { const date = new Date(dateMaxSearch); - document.getElementById('dateMax').value = date.toISOString().split('T')[0] + document.getElementById("dateMax").value = date.toISOString().split("T")[0]; } // Preparation des données @@ -157,8 +157,10 @@ appStatus.setInjectionVaccine.bind(appStatus, "modernaInjection"); document.getElementById("pfizerInjection").onclick = appStatus.setInjectionVaccine.bind(appStatus, "pfizerInjection"); - document.getElementById("dateMax").onblur = - appStatus.setDateMaxSearch.bind(appStatus, document.getElementById('dateMax')); + document.getElementById("dateMax").onblur = appStatus.setDateMaxSearch.bind( + appStatus, + document.getElementById("dateMax") + ); document.getElementById("reset").onclick = () => { if ( diff --git a/commons/AppStatus.js b/commons/AppStatus.js index 01b91f5..379e123 100644 --- a/commons/AppStatus.js +++ b/commons/AppStatus.js @@ -23,7 +23,11 @@ class AppStatus { /** @type {'modernaInjection' | 'pfizerInjection'} vaccin d'injection souhaité par le user */ this.injectionVaccine = "pfizerInjection"; /** @type {Date} Date max de recherche de rdv (par défaut aujd + 1 mois) */ - this.dateMaxSearch = new Date((new Date()).getFullYear()+1, (new Date()).getMonth(), (new Date()).getDate()); + this.dateMaxSearch = new Date( + new Date().getFullYear() + 1, + new Date().getMonth(), + new Date().getDate() + ); /** @type {(string) => void} callback quand une {@link VaccineLocation} a été ajouté */ this.onLocationAddedCb = (job) => {}; /** @type {(string) => void} callback quand une {@link VaccineLocation} a été supprimée */ @@ -53,7 +57,11 @@ class AppStatus { autoBook: false, injectionType: "fullServiceInjection", injectionVaccine: "pfizerInjection", - dateMaxSearch: new Date((new Date()).getFullYear()+1, (new Date()).getMonth(), (new Date()).getDate()), + dateMaxSearch: new Date( + new Date().getFullYear() + 1, + new Date().getMonth(), + new Date().getDate() + ), }); Object.keys(result.locations).forEach((url) => { @@ -187,7 +195,6 @@ class AppStatus { browser.storage.sync.set({ injectionType: this.injectionType }); } - /** * @param {Date} value The new dateMaxSearch value */ @@ -221,7 +228,11 @@ class AppStatus { this.onInjectionTypeCb(this.injectionType); this.injectionVaccine = "pfizerInjection"; this.onInjectionTypeCb(this.injectionVaccine); - this.dateMaxSearch = new Date((new Date()).getFullYear(), (new Date()).getMonth()+1, (new Date()).getDate()); + this.dateMaxSearch = new Date( + new Date().getFullYear() + 1, + new Date().getMonth(), + new Date().getDate() + ); this.onDateMaxSearchCb(this.dateMaxSearch); } diff --git a/content_scripts/doctolib/book.js b/content_scripts/doctolib/book.js index c183c4d..9d36ecc 100644 --- a/content_scripts/doctolib/book.js +++ b/content_scripts/doctolib/book.js @@ -27,20 +27,6 @@ if (currentMonth > selectedMonth) return new Date().getFullYear() + 1; return new Date().getFullYear(); } - const MONTHS = { - 'janv.': 1, - 'févr.': 2, - 'mars': 3, - 'avr.': 4, - 'mai': 5, - 'juin': 6, - 'juil.': 7, - 'aout': 8, - 'sept.': 9, - 'oct.': 10, - 'nov.': 11, - 'déc.': 12, - }; async function waitTimeout(timeout) { await new Promise((r) => setTimeout(r, timeout)); @@ -309,17 +295,30 @@ let running = false; async function checkAvailability() { - const { locations, stopped, autoBook, injectionType, injectionVaccine, dateMaxSearch } = - await browser.storage.sync.get({ - locations: {}, - stopped: false, - autoBook: false, - injectionType: "fullServiceInjection", - injectionVaccine: "pfizerInjection", - dateMaxSearch: new Date((new Date()).getFullYear()+1, (new Date()).getMonth(), (new Date()).getDate()), - }); + const { + locations, + stopped, + autoBook, + injectionType, + injectionVaccine, + dateMaxSearch, + } = await browser.storage.sync.get({ + locations: {}, + stopped: false, + autoBook: false, + injectionType: "fullServiceInjection", + injectionVaccine: "pfizerInjection", + dateMaxSearch: new Date( + new Date().getFullYear() + 1, + new Date().getMonth(), + new Date().getDate() + ), + }); - const dateMaxSearchDate = typeof(dateMaxSearch) === 'string' ? new Date(dateMaxSearch) : dateMaxSearch; + const dateMaxSearchDate = + typeof dateMaxSearch === "string" + ? new Date(dateMaxSearch) + : dateMaxSearch; if (stopped || !locations[url]) { running = false; @@ -438,8 +437,6 @@ const parts = slot.title.match( /([0-9]+)\.? ([\p{Letter}]+)\.? ([0-9]+:[0-9]+)/u ); - // /([0-9]+) [\p{Letter}]+\.? ([0-9]+:[0-9]+)/gu - // )[0].split(' '); if (!parts) { throw new Error( browser.i18n.getMessage("slotDateFormatNotFound", slot.title) @@ -453,14 +450,15 @@ const selectedTime = parts[3]; const date = new Date( - `${selectedMonth} ${selectedDay} ${selectedYear} ${selectedTime}`); + `${selectedMonth} ${selectedDay} ${selectedYear} ${selectedTime}` + ); if (date > dateMaxSearchDate) { const formatedDate = dateMaxSearchDate.toLocaleDateString(); throw new Error( `Pas de créneau dispo d'ici demain soir ou avant le ${formatedDate}` ); - } + } if (!autoBook) { browser.runtime.sendMessage({