-
Notifications
You must be signed in to change notification settings - Fork 0
/
background.js
150 lines (140 loc) · 3.72 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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
// defaults
//
// domain = "rss.example.com"
// domain_path = "/exmple/"
// protocol = "https"
// api_unread_path = "public.php?op=getUnread&login="
// tt_rss_username = "admin"
// port = "443"
// interval = 60*5
var domain;
var domain_path;
var protocol;
var api_unread_path = "public.php?op=getUnread&login=";
var tt_rss_username;
var port;
var interval = "5";
var task_timer;
// get settings from chrome storage into global parameters for updating feed data
function loadSettings(callback){
chrome.storage.sync.get(['domain',
'domain_path',
'protocol',
'tt_rss_username',
'port',
'interval']
,result => {
domain = result.domain;
domain_path = result.domain_path;
protocol = result.protocol;
tt_rss_username = result.tt_rss_username;
port = result.port;
interval = result.interval;
console.log('Loaded settings from storage');
callback();
})
}
// checks if all settings are set
function settingsExists(){
if ( // if one of the settings required is undefined return false
domain == undefined ||
domain_path == undefined ||
protocol == undefined ||
tt_rss_username == undefined ||
port == undefined ||
interval == undefined
) { return false }
else { return true }
}
function updateUnread(callback){
if (!settingsExists()) {
console.log('updateUnread stopped, missing settings');
if(callback){
callback();
}
return;
} // if user didnt finish setup dont make a request
url = protocol +"://"+ domain +":"+ port + domain_path + api_unread_path + tt_rss_username;
fetch(url)
.then(response => {
if(!response.ok){
throw Error("Http request failed");
}else{
return response
}
})
.then(response => response.text())
.then(validate => validate.replace(/\D/g,''))
.then(count => {
chrome.action.setBadgeText({text: count});
if(callback){
callback();
}
})
.catch(error => {
console.log(error)
if(callback){
callback();
}
});
}
// when clicked take me to my RSS
chrome.action.onClicked.addListener(clicked_me)
function clicked_me(){
// if user didnt finish setup, ask to complete
if (!settingsExists()) {
chrome.tabs.create({url: "options.html"});
}else{
// if setup is done, sends user to the RSS feed
// console.log(protocol +"://"+ domain +":"+ port + domain_path);
chrome.tabs.create({url: protocol +"://"+ domain +":"+ port + domain_path});
}
}
function update_interval(period) {
if (period != undefined){
//clearInterval(task_timer);
//task_timer = setInterval(updateUnread, 1000*60*period);
chrome.alarms.clear("rss_update_alarm");
chrome.alarms.create("rss_update_alarm",{periodInMinutes:period});
console.log('updated alarm rss_update_alarm interval:', period, 'm');
}
};
function onAlarm(alarm) {
console.log('alarm fired is:', alarm);
if (alarm && alarm.name == 'rss_update_alarm') {
loadSettings(() => {
updateUnread();
});
} else {
console.log('Unkown alarm was called:',alarm);
}
}
chrome.alarms.onAlarm.addListener(onAlarm);
chrome.runtime.onMessage.addListener(function(msg, sender, resp) {
switch (msg.message) {
case 'update_count':
loadSettings(function() {
updateUnread(function() {
update_interval(parseInt(interval));
});
});
break;
default:
console.log('Warrning: wrong message recived: ' + msg.message);
break;
}
resp({'message':'done'});
});
chrome.runtime.onStartup.addListener(() => {
loadSettings(function() {
updateUnread(function() {
update_interval(parseInt(interval));
});
});
});
// after install direct user to change settings
chrome.runtime.onInstalled.addListener(details => {
if(details.reason == chrome.runtime.OnInstalledReason.INSTALL) {
chrome.tabs.create({url: "options.html"});
}
});