This repository has been archived by the owner on Mar 4, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdose_covid.js
259 lines (226 loc) · 9.46 KB
/
dose_covid.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
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
class Search {
constructor() {
this.today = new Date();
}
/**
* Parse a date string to a date object
* @param {String} date Date to give to the object
* @returns {Date} Date object set to the needed time
*/
parse_date_string(date) {
let day, month;
let year = this.today.getFullYear();
let months = ["janv.", "févr.", "mars", "avril", "mai", "juin", "juil.", "août", "sept.", "oct.", "nov.", "déc."];
[ day, month ] = date.split(" ");
let m = `${months.indexOf(month) + 1}`;
// Error if format incorrect
if (m === '-1') {
console.error(`parse_date_string: date '${date}' is of invalid format`);
}
// Day styling
if (day.length === 1) {
day = `0${day}`;
}
// Month styling
if (m.length === 1) {
m = `0${m}`;
}
// Set to next year if found month < current month
if (m < this.today.getMonth()) {
year++;
}
return new Date(`${year}-${m}-${day}T00:00:00`);
}
/**
* Parse a date string to a date object
* @param {Date} date Date object to parse into a string
* @return {String} Date string in the format yyyy/mm/dd
*/
parse_date_object(date) {
let day = date.getDate();
let month = date.getMonth();
let year = date.getFullYear();
return `${year}-${month +1}-${day}`;
}
/**
* Create a notification to inform a dose has been found
* @param {String} day Day of the found dose
* @param {String} time Time of the found dose
*/
notify(day, time) {
let found = new Notification('Dose trouvée !', {
body: `Trouvé une dose le ${day} à ${time}`,
icon: 'https://cdn.jsdelivr.net/gh/user038418/prestomadose/img/icon-vaccine.png'
});
setTimeout(() => {
found.close()
}, 10000);
let alerte = new Audio("https://ozna.me/Metal-Gear-Alert_-Sound-Effect.mp3");
alerte.play();
document.title = "⬤ " + document.title;
document.addEventListener("visibilitychange", () => {
if (document.title[0] === "⬤") {
document.title = document.title.slice(2);
}
});
}
/**
* Refresh the results list
*/
refresh() {
console.log("refresh");
let changeEvent = new Event("change", {"bubbles": true});
this.motive.selectedIndex = 0;
this.motive.dispatchEvent(changeEvent);
setTimeout(() => {
this.motive.selectedIndex = this.doseNumber;
this.motive.dispatchEvent(changeEvent);
}, 500);
}
/**
* Load the next set of dates
* @returns {boolean} always true
*/
get next_dates() {
console.log("next set of dates");
let clickEvent = new MouseEvent("click", {"bubbles": true});
document.querySelectorAll('svg.dl-icon.availabilities-pagination-arrow.dl-icon-large')[1].dispatchEvent(clickEvent);
return true;
}
/**
* Controls the appointment search
*/
desktop() {
document.removeEventListener("visibilitychange", () => document.title = document.title.slice(2));
let loop = setInterval(() => {
// If the availability is for later ("Prochain rendez-vous le ...")
let later = document.querySelector('#booking-content > div.booking.booking-compact-layout > div:nth-child(5) > div > div.dl-layout-container.dl-layout-spacing-xs-0 > div.dl-step-children.dl-layout-item.dl-layout-size-xs-12.dl-layout-size-sm-12 > div > div > div:nth-child(1) > div > div > div:nth-child(2) > div > div > div > div > div > div > div > button > span');
let dateButtons = document.querySelectorAll("div.availabilities-slot");
if (later) {
later.click()
} else if (dateButtons[0]) {
let dateIndex = 0;
let wait = false;
let dayColumn = dateButtons[dateIndex].parentElement.parentElement;
let day = dayColumn.querySelector("div.availabilities-day-date").innerText;
let date = this.parse_date_string(day);
if (this.filterType === 1 || this.filterType === 2) {
if (this.targetDate - date > 259200000) {
// Show the next set of dates
wait = this.next_dates;
} else {
while (((this.targetDate - date > 0) && this.filterType === 1) || ((this.targetDate - date >= 0) && this.filterType === 2)) {
// Look at the next day
dateIndex += 1;
try {
dayColumn = dateButtons[dateIndex].parentElement.parentElement;
day = dayColumn.querySelector("div.availabilities-day-date").innerText;
date = this.parse_date_string(day);
} catch (err) {
wait = this.next_dates;
throw err;
}
}
}
}
let checkBefore = (this.filterType === 0 && date < this.targetDate);
let checkOn = (this.filterType === 1 && !(date > this.targetDate || date < this.targetDate));
let checkAfter = (this.filterType === 2 && date > this.targetDate);
console.log("if-test: " + (checkBefore || checkOn || checkAfter));
if (checkBefore || checkOn || checkAfter) {
dateButtons[dateIndex].click();
this.notify(day, dateButtons[dateIndex].innerText);
clearInterval(loop);
let restart = setTimeout(() => {
this.desktop();
}, 60000);
document.body.addEventListener('click', () => {
clearTimeout(restart);
}, {once: true});
} else if (!wait) {
this.refresh();
}
} else {
this.refresh();
}
}, 3000);
document.querySelector('div#prestoMaDose button#stop').addEventListener('click', () => {
clearInterval(loop);
}, {once: true});
}
/**
* Initialise the bottom control bar
*/
init() {
let link = document.createElement('link');
link.rel = 'stylesheet';
link.type = 'text/css';
link.href = 'https://cdn.jsdelivr.net/gh/user038418/[email protected]/style.css';
document.head.appendChild(link);
let label = document.createElement('label');
label.for = "type";
label.innerText = "Trouver une ... ";
let selectDose = document.createElement('select');
selectDose.classList.add("dl-select");
selectDose.classList.add('form-control');
selectDose.classList.add('booking-compact-select');
let booking_motive = document.querySelectorAll('select#booking_motive option');
booking_motive.forEach((type) => {
let option = document.createElement('option')
option.value = type.value;
option.innerText = type.textContent;
selectDose.appendChild(option);
});
let selectDate = document.createElement('select');
selectDate.classList.add("dl-select");
selectDate.classList.add('form-control');
selectDate.classList.add('booking-compact-select');
let optionsDate = ["avant", "pour", "apres"];
optionsDate.forEach((type) => {
let option = document.createElement('option')
option.value = type;
option.innerText = `${type} le`;
selectDate.appendChild(option);
});
let inputDate = document.createElement('input');
inputDate.type = "date";
inputDate.name = "date";
inputDate.value = this.parse_date_object(this.today);
inputDate.min = this.parse_date_object(this.today);
let startButton = document.createElement('button');
startButton.onclick = () => {
if (Notification.permission !== "granted") {
Notification.requestPermission();
}
setTimeout(() => {
this.motive = document.getElementById('booking_motive');
this.targetDate = new Date(`${inputDate.value}T00:00:00`);
this.filterType = selectDate.selectedIndex;
this.doseNumber = selectDose.selectedIndex;
this.desktop();
}, 500);
startButton.classList.add("check");
setTimeout(() => {
startButton.classList.remove("check");
}, 3500);
};
startButton.innerText = "Lancer la recherche";
let stopButton = document.createElement('button');
stopButton.innerHTML = "✗"
stopButton.id = "stop";
stopButton.title = "Arrêter la recherche";
let div = document.createElement('div');
div.id = "prestoMaDose";
div.appendChild(label);
div.appendChild(selectDose);
div.appendChild(selectDate);
div.appendChild(inputDate);
div.appendChild(startButton);
div.appendChild(stopButton);
document.body.appendChild(div);
}
}
setTimeout(() => {
let search = new Search();
search.init();
}, 250);