forked from rahimnathwani/pushover-for-chrome
-
Notifications
You must be signed in to change notification settings - Fork 24
/
Copy pathoptions.js
62 lines (53 loc) · 1.74 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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
var $ = function(id) {
return document.getElementById(id);
},
show_message = function(message, hide_in_seconds) {
$('message').innerHTML = message;
if (hide_in_seconds) {
setTimeout(function() {
$('message').innerHTML = ' ';
}, hide_in_seconds * 1000);
}
},
validate = async function() {
const {userkey, token} = await chrome.storage.local.get(['userkey', 'token']);
if (!userkey || !token) {
show_message('Please fill both fields!');
return;
}
var url = 'https://api.telegram.org/bot' + token + '/sendMessage';
url += '?chat_id=' + encodeURIComponent(userkey);
url += '&text=' + encodeURIComponent('"Send to Telegram" configured successfully!');
fetch(url, {
method: 'GET',
headers: {
'Content-Type': 'application/json'
}
})
.then(async (response) => {
if (!response.ok) {
throw new Error("Wrong API Token or User ID.");
} else {
await chrome.storage.local.set({'valid': token + userkey});
show_message('"Send to Telegram" configured successfully!');
}
})
.catch((error) => {
console.error(`Fetch Error: ${error}`);
alert(error);
show_message(error);
});
},
save = async function() {
await chrome.storage.local.set({'userkey': $('userkey').value});
await chrome.storage.local.set({'token': $('token').value});
show_message('Saved!');
validate();
},
load = async function() {
const {userkey, token} = await chrome.storage.local.get(['userkey', 'token']);
$('token').value = token || '';
$('userkey').value = userkey || '';
};
$('save').addEventListener('click', save);
window.addEventListener("load", load);