-
-
Notifications
You must be signed in to change notification settings - Fork 127
/
options.js
41 lines (33 loc) · 1.4 KB
/
options.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
var defaultBotToken = 'Set your telegram bot token';
var defaultChatId = 'Set your telegram chat id';
function save_options() {
localStorage['botToken'] = document.getElementById('bot_token').value;
localStorage['chatId'] = document.getElementById('chat_id').value;
var url = 'https://api.telegram.org/bot' + document.getElementById('bot_token').value + '/sendMessage?chat_id=' + document.getElementById('chat_id').value + '&text=' + encodeURI('Bot connected.');
var xmlhttp = new XMLHttpRequest();
xmlhttp.onreadystatechange=function() {
if (xmlhttp.readyState==4 && xmlhttp.status==200) {
var response = xmlhttp.responseText; //if you need to do something with the returned value
}
}
xmlhttp.open('GET', url, true);
xmlhttp.send();
var status = document.getElementById('status');
status.textContent = 'Options saved.';
setTimeout(function() {
status.textContent = '';
}, 750);
}
function restore_options() {
var botToken = localStorage['botToken'];
var chatId = localStorage['chatId'];
if (botToken == undefined)
botToken = defaultBotToken;
if (chatId == undefined)
chatId = defaultChatId;
document.getElementById('bot_token').value = botToken;
document.getElementById('chat_id').value = chatId;
}
document.addEventListener('DOMContentLoaded', restore_options);
document.getElementById('save').addEventListener('click',
save_options);