forked from youngjin-k/ktx-macro
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpopup.js
47 lines (40 loc) · 1.85 KB
/
popup.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
(() => {
const MESSAGE_RESET = '초기화 되었습니다.';
const MESSAGE_CONNECTION_SUCCESS = '연동되었습니다.';
const MESSAGE_CONNECTION_FAIL = '연동에 실패하였습니다.<br>입력하신 정보를 다시 확인해주세요.';
const init = () => {
document.getElementById('bot-token').value = localStorage.getItem('KTX_MACRO::bot-token');
document.getElementById('chat-id').value = localStorage.getItem('KTX_MACRO::chat-id');
}
const save = () => {
const botToken = document.getElementById('bot-token').value;
const chatId = document.getElementById('chat-id').value;
const msg = encodeURI('KTX Macro: 예약 알림이 연동되었습니다.');
const url = `https://api.telegram.org/bot${botToken}/sendMessage?chat_id=${chatId}&text=${msg}`;
fetch(url).then(response => {
if (response.status === 200) {
localStorage.setItem('KTX_MACRO::bot-token', botToken);
localStorage.setItem('KTX_MACRO::chat-id', chatId);
setMessage(MESSAGE_CONNECTION_SUCCESS);
} else {
setMessage(MESSAGE_CONNECTION_FAIL);
}
}).catch(err => {
setMessage(MESSAGE_CONNECTION_FAIL);
console.error(err)
});
}
const reset = () => {
document.getElementById('bot-token').value = '';
document.getElementById('chat-id').value = '';
localStorage.removeItem('KTX_MACRO::bot-token');
localStorage.removeItem('KTX_MACRO::chat-id');
setMessage(MESSAGE_RESET);
}
const setMessage = message => {
document.getElementById('message').innerHTML = message;
}
init();
document.getElementById('button-save').addEventListener('click', save);
document.getElementById('button-reset').addEventListener('click', reset);
})();