-
Notifications
You must be signed in to change notification settings - Fork 40
/
Copy pathmap.js
343 lines (283 loc) · 10.2 KB
/
map.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
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
let sButtonText = "";
let sButtonReload = undefined;
let _tick;
const RELOAD_INTERVAL_MILLISECONDS = 200;
let hospitalList;
let hospitalListStrCache;
const _className = "_" + Math.random().toString(36).substring(2, 7);
const _buttonStyle = "width: 175px;height: 38px;line-height: 38px;text-align: center;background: linear-gradient(to bottom, #17e677, #19d1d1);color: #ffffff;font-size: 13px;border: 1px solid #149393;cursor: pointer;float:right;";
let nums = new Array();
const injectButton = () => {
if (!sButtonReload)
return;
sButtonReload.parentNode.insertAdjacentHTML(
"beforeend",
`
<span><button type="button" class="${_className}" style="${_buttonStyle}">${sButtonText}</button></span>
`
);
document
.querySelector(`.${_className}`)
.addEventListener("click", onMacroClick);
}
const setEscapeEvent = () => {
window.addEventListener("keydown", e => {
if (e.key === "Escape") {
macroStop();
}
});
};
const onMacroClick = () => {
chrome.storage.local.get(function (data) {
const isStarted = (data.macro === "on");
if (isStarted)
macroStop();
else
macroStart();
});
// const isStarted = localStorage.getItem("macro") === "on";
// if (isStarted)
// macroStop();
// else macroStart();
};
const macroStart = () => {
alert(
"자동 새로고침을 시작합니다.\n" +
"예약 시도 시 예약 신청 페이지로 이동합니다.\n" +
"탐지할 지역의 graphql result을 입력해두어야 예약 신청 페이지로 이동 가능합니다.\n" +
"자동 새로고침 종료는 '자동 새로고침 정지' 혹은 esc키를 눌러주세요."
);
chrome.storage.local.set({macro: "on"}, function () {
reload();
});
let button = document.querySelector(`.${_className}`);
if (button) {
button.innerText = "자동 새로고침 정지(ESC)";
}
setEscapeEvent();
// localStorage.setItem("macro", "on");
};
const macroStop = (result) => {
if (!result)
alert("자동 새로고침을 종료합니다.");
chrome.storage.local.set({macro: ""});
clearTimeout(_tick);
let button = document.querySelector(`.${_className}`);
if (button) {
button.innerText = "자동 새로고침 다시 시작";
}
};
const _reload = () => {
let settingInterval = RELOAD_INTERVAL_MILLISECONDS;
let modify = ((new Date().getTime()) % 20);
// console.log("setting interval:" + settingInterval + ", modify:" + modify);
if (settingInterval > modify)
settingInterval -= modify;
// console.log("curr interval:" + settingInterval);
_tick = setTimeout(reload, settingInterval);
};
const reload = () => {
// recover numbers color
if (nums.length > 0) {
for (let i = 0; i < nums.length; i++) {
const num = nums[i];
num.style.backgroundColor = "";
}
nums.length = 0;
}
// console.log(sButtonReload);
// console.log(sButtonReload.classList);
// console.log(sButtonReload.classList.length);
if (sButtonReload)
sButtonReload.click();
else {
// console.log("will reloading ignore current tick.");
}
};
const afterReload = () => {
chrome.storage.local.get(function (value) {
const isStarted = value.macro === "on";
if (isStarted) {
macro(value);
}
if (!sButtonReload) {
console.log("can't found reload button, ignored macro.");
return;
}
});
};
function _findWithSelectedName(selected) {
const selOnMenu = selected;
if (selected == '아스트라제네카')
selected = 'AZ';
let listContainer = document.getElementById('_list_scroll_container');
let placesForSelect = listContainer.querySelectorAll('.place_blind');
// console.log(placesForSelect);
let checkHospitalCount = 0;
for (let i = 0; i < placesForSelect.length; i++) {
if (placesForSelect[i].innerHTML == '잔여백신종류') {
let next = placesForSelect[i].parentNode.nextSibling;
if (!next) continue;
while (next) {
let curr = next;
next = curr.nextSibling;
let vaccineName = curr.firstChild.nodeValue;
if (selected != vaccineName)
continue;
let targetName = curr.parentNode.parentNode.childNodes[0].querySelector('span').innerText;
let em = curr.querySelector('em');
let num = parseInt(em.firstChild.nodeValue);
if (isNaN(num))
num = 0;
// console.log(targetName, num, curr ? curr.innerText : '','/', curr ? curr.innerHTML : '','/', curr ? curr.firstChild : '','/', em,'/', em ? em.firstChild : '');
let enable = num > 0;
if (enable) {
console.log(targetName, '/', vaccineName, '/', num, enable);
let info = getHospital(targetName);
if (openReservePage(targetName, info))
return;
}
else
++checkHospitalCount;
}
}
}
console.log("선택 백신 종류:", selOnMenu, ",현재 확인 병원 수 :", checkHospitalCount);
// keep going
_reload();
}
const macro = (data) => {
const hospitalListStr = data.hospital_list;
// console.log("execute macro." + hospitalListStr);
if (hospitalListStr && hospitalListStrCache != hospitalListStr) {
hospitalList = JSON.parse(hospitalListStr);
hospitalListStrCache = hospitalListStr;
// console.log("parsed list. hospital list string changed.");
}
// console.log(hospitalList);
if (!hospitalList || hospitalList.length == 0) {
console.log("hospitalList is empty. cannot macro execute.");
macroStop('empty');
alert("병원 목록 데이터가 없습니다. 현재 지도에서 새로고침을 수행한 graphql result를 매크로 페이지에 입력하여 병원 목록을 생성한 뒤 동작 시켜주세요.");
return;
}
let selectedName = null;
if (data.selected_vaccine != null)
selectedName = data.selected_vaccine;
if (selectedName != null && selectedName != undefined && selectedName.length > 0) {
_findWithSelectedName(selectedName);
return;
}
let places = document.querySelectorAll(".place_blind");
// console.log(places);
let arr = new Array();
for (let i = 0; i < places.length; i++) {
// console.log(places[i].parentNode);
if (places[i].parentNode.querySelectorAll(".place_blind").length >= 3) {
arr.push(places[i].parentNode);
}
}
if (arr.length > 0) {
for (let i = 0; i < arr.length; i++) {
// console.log(arr[i]);
let targetName = arr[i].childNodes[0];
let targetNum = arr[i].childNodes[arr[i].childNodes.length - 1];
// console.log(targetNum);
// console.log(targetNum.innerText);
const name = targetName.innerText;
const num = parseInt(targetNum.innerText);
let info = getHospital(name);
// console.log(name + ", " + num);
// console.log(info);
let enable = num > 0;
// targetName.style.backgroundColor = enable ? "#3ef03e" : "#f03e3e";
targetNum.style.backgroundColor = enable ? "#3ef03e" : "#f03e3e";
nums.push(targetNum);
if (enable) {
if (openReservePage(name, info))
return;
}
}
}
// keep going
_reload();
};
const RESERVE_URL = 'https://v-search.nid.naver.com/reservation?orgCd=';
const RESERVER_URL_ADD_PARM = '&sid=';
const openReservePage = (name, obj) => {
if (obj && obj.vaccineQuantity && obj.vaccineQuantity.vaccineOrganizationCode) {
const orgCd = obj.vaccineQuantity.vaccineOrganizationCode;
const sid = obj.id;
const url = RESERVE_URL + orgCd + RESERVER_URL_ADD_PARM + sid;
console.log("try open url : " + url);
document.location.href = url;
return true;
} else {
console.log('cannot found open url for reserve. ignored : ' + name);
return false;
}
}
const getHospital = (name) => {
if (!hospitalList)
return undefined;
for (let i = 0; i < hospitalList.length; ++i) {
if (hospitalList[i].name == name) {
return hospitalList[i];
}
}
return undefined;
};
let reloading = false;
const observer = new MutationObserver(function (mutations) {
mutations.forEach(function (mutation) {
if (mutation.type == "attributes") {
let prevReloading = reloading;
if (sButtonReload)
reloading = sButtonReload.classList.length == 2;
if (prevReloading && !reloading) {
// console.log("reload done. try after reload.");
// setTimeout(afterReload, 0);
afterReload();
}
}
});
});
const _prepare = () => {
chrome.storage.local.get(function (value) {
observer.observe(sButtonReload, {
attributes: true //configure it to listen to attribute changes
});
const isStarted = value.macro === "on";
if (isStarted) {
macro(value);
setEscapeEvent();
}
sButtonText = isStarted ? "자동 새로고침 정지(ESC)" : "자동 새로고침 시작";
setTimeout(injectButton, 100);
});
};
const _assureReloadButton = () => {
let places = document.querySelectorAll("span");
// console.log(places);
for (let i = 0; i < places.length; i++) {
if (!sButtonReload && places[i].innerHTML == "새로고침") {
sButtonReload = places[i].parentNode;
break;
}
}
};
let tryCount = 0;
const _prepareIf = () => {
_assureReloadButton();
if (!sButtonReload) {
if (++tryCount > 5)
console.log(`can't found reload button ${tryCount}times, try again after 500ms.`);
setTimeout(_prepareIf, 500);
return;
}
_prepare();
};
(() => {
// console.log("map.js loaded.");
_prepareIf();
})();