forked from meeeejin/srtmacro
-
Notifications
You must be signed in to change notification settings - Fork 1
/
options.js
44 lines (36 loc) · 1.53 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
42
43
44
var defaultBotToken = 'Set your telegram bot token';
var defaultChatId = 'Set your telegram chat id';
function save_options() {
var botToken = document.getElementById('bot_token').value;
var chatId = document.getElementById('chat_id').value;
chrome.storage.sync.set({'botToken': botToken, 'chatId': chatId}, function() {
console.log('Settings saved, botToken: ' + botToken + ', chatId: ' + chatId);
});
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);