forked from rahimnathwani/pushover-for-chrome
-
Notifications
You must be signed in to change notification settings - Fork 24
/
Copy pathbackground.js
123 lines (110 loc) · 3.59 KB
/
background.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
var open_options = function() {
if (chrome.runtime.openOptionsPage) {
return chrome.runtime.openOptionsPage();
}
return chrome.tabs.create({
url: chrome.runtime.getURL('options.html')
});
},
combo_valid = async function() {
const {valid, token, userkey} = await chrome.storage.local.get(['valid', 'token', 'userkey']);
if (!valid || valid !== token + userkey) {
open_options();
return false;
}
return true;
},
show_badge_text = function(color, text, timeout){
chrome.action.setBadgeBackgroundColor({
'color': color
});
chrome.action.setBadgeText({
'text': text
});
setTimeout(function() {
chrome.action.setBadgeText({
'text': ''
});
}, timeout * 1000);
},
push_message = async function(source, tab, selection, device) {
if (!combo_valid()) {
return false;
}
if (selection) {
var text = encodeURIComponent(selection.substring(0, 512));
} else {
var text = encodeURIComponent(tab.url.substring(0, 500));
}
const {userkey, token} = await chrome.storage.local.get(['userkey', 'token']);
var url = 'https://api.telegram.org/bot' + token + '/sendMessage';
url += '?chat_id=' + encodeURIComponent(userkey);
url += '&text=' + text;
url += encodeURIComponent('\n\nFrom: \n' + tab.title + '\n' + tab.url)
fetch(url, {
method: 'GET',
headers: {
'Content-Type': 'application/json'
}
})
.then((response) => {
if (!response.ok) {
throw new Error("Network response was not ok.");
} else {
show_badge_text('#006400', '✓', 2);
}
})
.catch((error) => {
console.error(`Fetch Error: ${error}`);
alert('Error: ' + error);
show_badge_text('#ff0000', '✗', 2);
});
return false;
},
setup_context_menus = function() {
var devices = ['Telegram Bot'],
ctxs = ['page', 'link', 'image', 'selection'];
chrome.contextMenus.removeAll();
if (devices.length) {
for(var j = 0; j < ctxs.length; j++) {
for (var i = 0; i < devices.length; i++) {
chrome.contextMenus.create({
'title': 'Push this ' + ctxs[j] + ' to ' + devices[i],
'contexts': [ctxs[j]],
'id': 'ctx:' + ctxs[j] + ':' + devices[i]
});
}
}
}
};
chrome.action.onClicked.addListener(function(tab) {
chrome.tabs.sendMessage(tab.id, {
method: 'selection'
}, function(text) {
push_message('badge', tab, text);
});
});
chrome.runtime.onMessage.addListener(function(request) {
if (request && request.action == "reload-contextmenus") {
setup_context_menus();
}
});
chrome.contextMenus.onClicked.addListener(function(info, tab) {
var devices = ['Telegram Bot'];
if (devices.length) {
for (var i = 0; i < devices.length; i++) {
if (info.menuItemId === 'ctx:page:' + devices[i]) {
return push_message('menu', tab, '', devices[i]);
} else if (info.menuItemId === 'ctx:link:' + devices[i]) {
return push_message('menu', tab, info.linkUrl, devices[i]);
} else if (info.menuItemId === 'ctx:image:' + devices[i]) {
return push_message('menu', tab, info.srcUrl, devices[i]);
} else if (info.menuItemId === 'ctx:selection:' + devices[i]) {
return push_message('menu', tab, info.selectionText, devices[i]);
}
}
}
});
if (combo_valid()) {
setup_context_menus();
}