-
Notifications
You must be signed in to change notification settings - Fork 40
/
Copy pathmap_kakao.js
419 lines (357 loc) · 13.1 KB
/
map_kakao.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
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
let sButtonText = "";
let sButtonReload = undefined;
let _tick;
const RELOAD_INTERVAL_MILLISECONDS = 500;
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;";
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.");
// }
sButtonReload.classList.add("refresh_animation_step1");
setTimeout(function () {
sButtonReload.classList.add("refresh_animation_step2");
}, 200);
chrome.storage.local.get(function (value) {
let body = value.kakao_body;
if (!body) {
alert("left_count_by_coords api호출의 request body 데이터가 필요합니다.");
macroStop('data_error');
return;
}
fetch("https://vaccine-map.kakao.com/api/v3/vaccine/left_count_by_coords", {
"headers": {
"accept": "application/json, text/plain, */*",
"accept-language": "ko-KR,ko;q=0.9,en-US;q=0.8,en;q=0.7",
"content-type": "application/json;charset=UTF-8",
"sec-ch-ua": "\" Not;A Brand\";v=\"99\", \"Google Chrome\";v=\"91\", \"Chromium\";v=\"91\"",
"sec-ch-ua-mobile": "?0",
"sec-fetch-dest": "empty",
"sec-fetch-mode": "cors",
"sec-fetch-site": "same-origin"
},
"referrer": "https://vaccine-map.kakao.com/map2",
"referrerPolicy": "strict-origin-when-cross-origin",
"body": body,
"method": "POST",
"mode": "cors",
"credentials": "include"
}).then(function (response) {
sButtonReload.classList.remove("refresh_animation_step1");
sButtonReload.classList.remove("refresh_animation_step2");
afterReload(response);
});
});
};
const afterReload = (response) => {
chrome.storage.local.get(function (value) {
const isStarted = value.macro === "on";
if (isStarted) {
macro(value, response);
}
if (!sButtonReload) {
console.log("can't found reload button, ignored macro.");
return;
}
});
};
function _findWithSelectedName(selected, data) {
const length = data === undefined || data.organizations === undefined ? 0 : data.organizations.length;
const selOnMenu = selected;
let loadedCount = 0;
let loadCount = 0;
for (let i = 0; i < length; i++) {
const curr = data.organizations[i];
const enable = curr.leftCounts > 0;
if (!enable) {
continue;
}
console.log("curr.leftCounts > 0, " + curr.orgName);
let info = getHospitalByCode(curr.orgCode);
if (info == null) {
console.log("found by orgCode is null : " + curr);
info = getHospital(curr.orgName);
}
++loadCount;
const countUrl = '/api/v3/org/org_code/' + curr.orgCode;
fetch(countUrl, {
"headers": {
"accept": "application/json, text/plain, */*",
// "accept-language": "ko-KR,ko;q=0.9,en-US;q=0.8,en;q=0.7",
"content-type": "application/json;charset=UTF-8",
// "sec-ch-ua": "\" Not;A Brand\";v=\"99\", \"Google Chrome\";v=\"91\", \"Chromium\";v=\"91\"",
// "sec-ch-ua-mobile": "?0",
// "sec-fetch-dest": "empty",
// "sec-fetch-mode": "cors",
// "sec-fetch-site": "same-origin"
},
// "referrer": "https://vaccine-map.kakao.com/map2",
// "referrerPolicy": "strict-origin-when-cross-origin",
"method": "GET",
// "mode": "cors",
// "credentials": "include"
}).then(function (response) {
if (response && response.status === 200) {
response.json().then(function (data) {
if (data.leftCount > 0) {
for (let i = 0; i < data.lefts.length; ++i) {
if (data.lefts[i].vaccineName == selOnMenu
&& data.lefts[i].leftCount > 0) {
if (openReservePage(curr.orgName, info))
return;
}
}
}
if (++loadedCount == loadCount)
{
console.log("선택 백신 종류:", selOnMenu, ",현재 확인 병원 수 :", loadedCount);
if (!movingToReserve)
{
// keep going
_reload();
}
}
});
}
else
{
if (++loadedCount == loadCount)
{
console.log("선택 백신 종류:", selOnMenu, ",현재 확인 병원 수 :", loadedCount);
if (!movingToReserve)
{
// keep going
_reload();
}
}
}
});
}
if (loadCount == 0)
_reload();
}
const macro = (value, response) => {
const hospitalListStr = value.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 (value.selected_vaccine != null)
selectedName = value.selected_vaccine;
if (response && response.status === 200) {
response.json().then(function (data) {
// console.log(data);
if (selectedName != null && selectedName != undefined && selectedName.length > 0) {
_findWithSelectedName(selectedName, data);
return;
}
const length = data.organizations.length;
for (let i = 0; i < length; i++) {
const curr = data.organizations[i];
const enable = curr.leftCounts > 0;
// console.log(curr.orgCode, curr.leftCounts, curr.orgName);
if (enable) {
console.log("curr.leftCounts > 0, " + curr.orgName);
let info = getHospitalByCode(curr.orgCode);
if (info == null) {
console.log("found by orgCode is null : " + curr);
info = getHospital(curr.orgName);
}
if (openReservePage(curr.orgName, info))
return;
}
}
// keep going
_reload();
});
return;
}
// keep going
_reload();
};
const RESERVE_URL = 'https://v-search.nid.naver.com/reservation?orgCd=';
const RESERVER_URL_ADD_PARM = '&sid=';
let movingToReserve = false;
const openReservePage = (name, obj) => {
if (movingToReserve) {
console.log("already moving..!");
return false;
}
if (obj && obj.vaccineQuantity && obj.vaccineQuantity.vaccineOrganizationCode) {
movingToReserve = true;
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;
};
const getHospitalByCode = (code) => {
if (!hospitalList)
return undefined;
for (let i = 0; i < hospitalList.length; ++i) {
const org = hospitalList[i];
if (org && org.vaccineQuantity && org.vaccineQuantity.vaccineOrganizationCode == code) {
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 = () => {
if (!sButtonReload)
sButtonReload = document.querySelector(".btn_refresh");
};
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();
})();