-
Notifications
You must be signed in to change notification settings - Fork 0
/
background.js
251 lines (219 loc) · 7.43 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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
window.chrome = (function () {
return window.msBrowser ||
window.browser ||
window.chrome;
})();
//var runtimeOrExtension = chrome.runtime && chrome.runtime.sendMessage ? 'runtime' : 'extension';
var base_url = 'http://jp.inoreader.com/';
var base_url_nossl = 'http://jp.inoreader.com/';
var inoreader_url = /inoreader\.com/;
var ignored_patterns = [/forum\.inoreader\.com/, /translate\.inoreader\.com/, /inoreader\.com\/developers/, /inoreader\.com\/changelog/, /inoreader\.com\/login/, ]
var server_timeout = 30 * 1000; // 30 seconds
var get_server_interval = 300 * 1000; // 5 minutes
var old_data = {};
var old_badge_type;
var connect_timer;
var app_id = '1000001524';
var app_key = 'uxILTlPQilTphfcf4xwBEspL_0EDaUZL';
function init_icon(){
var icon = (localStorage['icon']) ? localStorage['icon'] : 1;
chrome.browserAction.setIcon({
path: {
"19": "icons/icon"+icon+'.png',
"38": "icons/icon"+icon+"_r.png"
}
});
console.log('Starting Inoreader Companion...');
if(localStorage['badge_type'] != '3'){
chrome.browserAction.setBadgeText({text: '...'});
chrome.browserAction.setBadgeBackgroundColor({color: '#EEEEEE'});
chrome.browserAction.setTitle({title: 'Initializing...'});
}else{
chrome.browserAction.setBadgeText({text: ''});
}
localStorage['badge_type'] = localStorage['badge_type'] || 1;
}
function tab_scan(){
find_tab(function(tab){
if(tab === null){
console.log('Fetching data from servers');
get_server_data();
}else{
chrome.tabs.sendMessage(tab.id, {action: "force_counter_refresh"}, function(response) {
console.log("Forced tab to reload counters", response);
});
}
});
}
function process_data(data){
badge_update = false;
//console.log('Processing data ',data);
if(localStorage['badge_type'] == '3'){
chrome.browserAction.setBadgeText({text: ''});
return true;
}
if(data == null || data.error != undefined){
chrome.browserAction.setBadgeText({text: 'X'});
chrome.browserAction.setBadgeBackgroundColor({color: '#DF0101'});
var err_text = ' ';
if(data != null && data.error){
err_text = ' ' + data.error;
}
chrome.browserAction.setTitle({title: 'Cannot fetch data! ' + err_text});
return;
}
if(data == 0){
chrome.browserAction.setBadgeText({text: ''});
return true;
}
if (localStorage['badge_type'] == 2 && data.unseen_cnt != old_data.unseen_cn){
badge_update = true;
}
else if (data.unread_cnt != old_data.unread_cnt){
badge_update = true;
}
else if (old_badge_type != localStorage['badge_type']){
badge_update = true;
}
if(badge_update || old_data.error != undefined){
console.log('Data changed. Updating badge... ');
var cnt = localStorage['badge_type'] == 2 ? parseInt(data.unseen_cnt) : parseInt(data.unread_cnt);
var badge_bgcolor = (localStorage['badge_bgcolor']) ? localStorage['badge_bgcolor'] : '#0069D6';
if(data.max_cnt > 1000){
if(cnt >= 9999) cnt = "9999";
}else{
if(cnt >= 1000) cnt = "999+";
}
if(cnt != 0){
chrome.browserAction.setBadgeText({text: cnt+'' || ''});
}else{
chrome.browserAction.setBadgeText({text: ''});
}
//chrome.browserAction.setBadgeText({text: cnt+'' || ''});
chrome.browserAction.setBadgeBackgroundColor({color: badge_bgcolor});
if(localStorage['badge_type'] == 2){
chrome.browserAction.setTitle({title: 'You have ' + cnt + ' unseen articles in Inoreader'});
}else{
chrome.browserAction.setTitle({title: 'You have ' + cnt + ' unread articles in Inoreader'});
}
}
old_data = data;
old_badge_type = localStorage['badge_type'];
}
function get_server_data() {
var data = null;
if(localStorage['badge_type'] == '3'){
process_data(data);
return true;
}
connect_timer = setTimeout(function(){
httpRequest.abort();
chrome.browserAction.setBadgeText({text: ''});
}, server_timeout);
var httpRequest = new XMLHttpRequest();
httpRequest.onerror = function(err){ console.log(err); }
httpRequest.onreadystatechange = function() {
if(httpRequest.readyState == 4){
if(httpRequest.status >= 400){
console.log('Err: ' + httpRequest.status + ' (' + httpRequest.statusText + ')');
}else if(httpRequest.responseText){
try{
if(connect_timer) clearTimeout(connect_timer);
data = JSON.parse(httpRequest.responseText);
process_data(data);
}catch(e){
console.log(e);
}
}else{
console.log('No data');
}
}
}
try{
var session_key = '';
var url = base_url;
// if(localStorage['session_key']){
// session_key = '&session_key=' + localStorage['session_key'];
// url = base_url;
// }
var badge_type = (localStorage['badge_type']) ? localStorage['badge_type'] : 1;
console.log('Requesting ' + url + 'api/browser_extensions.php?counters&type=' + badge_type + session_key);
httpRequest.open('GET', url + 'api/browser_extensions.php?counters=1&AppId=' + app_id + '&AppKey=' + app_key + '&type=' + badge_type + session_key, true);
httpRequest.send(null);
}catch(e) {
console.log(e);
}
}
function find_tab(callback_fn){
chrome.windows.getAll({populate: true}, function(w){
for(var i in w){
var TabsCount = w[i].tabs.length;
while(TabsCount){
if(inoreader_url.exec(w[i].tabs[TabsCount-1].url)){
for(var c in ignored_patterns){
if(ignored_patterns[c].exec(w[i].tabs[TabsCount-1].url)){
console.log(w[i].tabs[TabsCount-1].url);
continue;
}
}
callback_fn(w[i].tabs[TabsCount-1]);
return;
}
--TabsCount;
}
}
callback_fn(null);
});
}
chrome.browserAction.onClicked.addListener(function(tab) {
find_tab(function(tab){
if(tab === null){
localStorage['use_ssl'] = localStorage['use_ssl'] || 0;
var this_url = (localStorage['use_ssl'] == 1) ? base_url : base_url_nossl;
chrome.tabs.create({url: this_url});
}else{
chrome.tabs.update(tab.id, {active: true});
}
});
});
if(chrome.runtime && chrome.runtime.sendMessage) {
chrome.runtime.onMessage.addListener(function (msg) {
if (msg.option_update) {
chrome.browserAction.setIcon({
path: {
"19": "icons/icon" + (localStorage['icon'] || 1) + '.png',
"38": "icons/icon" + (localStorage['icon'] || 1) + "_r.png"
}
});
get_server_data();
return;
} else if (msg.url) {
chrome.tabs.query({active: true, currentWindow: true}, function (active) {
chrome.tabs.query({currentWindow: true}, function (tabs) {
var idx = 0;
var ino_tab = (active && active.length) ? active[0].id : 0;
for (var i in tabs) {
var t = tabs[i];
if (t.active) {
idx = t.index;
}
if (t.openerTabId == ino_tab) {
idx = t.index;
}
}
console.log('Creating new tab ' + (idx + 1) + ' with URL ' + msg.url);
try{
chrome.tabs.create({url: msg.url, active: false, openerTabId: ino_tab, index: ++idx});
}catch(e){
chrome.tabs.create({url: msg.url, active: false, index: idx});
}
});
});
return;
}
process_data(msg);
});
}
setInterval(function(){ tab_scan(); },get_server_interval);
init_icon();
tab_scan();