-
Notifications
You must be signed in to change notification settings - Fork 8
/
background.js
503 lines (453 loc) · 16.6 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
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
// Copyright (c) 2013 https://bigC.at. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
var animationFrames = 36;
var animationSpeed = 10; // ms
var canvas = document.getElementById('canvas');
var loggedInImage = document.getElementById('logged_in');
var canvasContext = canvas.getContext('2d');
var pollIntervalMin = 1; // 1 minute
var pollIntervalMax = 60; // 1 hour
var requestTimeout = 1000 * 2; // 2 seconds
var rotation = 0;
var loadingAnimation = new LoadingAnimation();
var permission = 7; //options 1 2 4
// Legacy support for pre-event-pages.
var oldChromeVersion = !chrome.runtime;
var requestTimerId;
function getZhihuUrl() {
return "https://www.zhihu.com";
}
function getMsgUrl() {
return getZhihuUrl() + '/api/v4/notifications/v2/vote_thank?limit=100';
}
function getVoteThankUrl() {
var timeStamp = +new Date();
return getZhihuUrl() + '/api/v4/notifications/v2/vote_thank?limit=100&offset=' + timeStamp;
}
function getDefaultUrl() {
return getZhihuUrl() + '/api/v4/notifications/v2/default?limit=100';
}
function getFollowUrl() {
return getZhihuUrl() + '/api/v4/notifications/v2/follow?limit=100';
}
function isZhihuUrl(url) {
// Return whether the URL starts with the Zhihu prefix.
return url.indexOf(getZhihuUrl()) == 0;
}
// A "loading" animation displayed while we wait for the first response from
// Zhihu. This animates the badge text with a dot that cycles from left to
// right.
function LoadingAnimation() {
this.timerId_ = 0;
this.maxCount_ = 8; // Total number of states in animation
this.current_ = 0; // Current state
this.maxDot_ = 4; // Max number of dots in animation
}
LoadingAnimation.prototype.paintFrame = function () {
var text = "";
for (var i = 0; i < this.maxDot_; i++) {
text += (i == this.current_) ? "." : " ";
}
if (this.current_ >= this.maxDot_)
text += "";
chrome.browserAction.setBadgeText({ text: text });
this.current_++;
if (this.current_ == this.maxCount_)
this.current_ = 0;
};
LoadingAnimation.prototype.start = function () {
if (this.timerId_)
return;
var self = this;
this.timerId_ = window.setInterval(function () {
self.paintFrame();
}, 100);
};
LoadingAnimation.prototype.stop = function () {
if (!this.timerId_)
return;
window.clearInterval(this.timerId_);
this.timerId_ = 0;
};
function updateIcon() {
if (permission == 0) {
chrome.browserAction.setIcon({ path: "img/zhihu_logged_in.png" });
chrome.browserAction.setBadgeBackgroundColor({ color: [190, 190, 190, 230] });
chrome.browserAction.setBadgeText({ text: "" });
return;
}
if (!localStorage.hasOwnProperty('unreadCount')) {
chrome.browserAction.setIcon({ path: "img/zhihu_not_logged_in.png" });
chrome.browserAction.setBadgeBackgroundColor({ color: [190, 190, 190, 230] });
chrome.browserAction.setBadgeText({ text: "?" });
} else if (localStorage.unreadCount == "0") {
chrome.browserAction.setIcon({ path: "img/zhihu_not_logged_in.png" });
chrome.browserAction.setBadgeBackgroundColor({ color: [190, 190, 190, 230] });
chrome.browserAction.setBadgeText({ text: "" });
} else {
chrome.browserAction.setIcon({ path: "img/zhihu_logged_in.png" });
chrome.browserAction.setBadgeBackgroundColor({ color: [208, 0, 24, 255] });
chrome.browserAction.setBadgeText({
text: localStorage.unreadCount != "0" ? localStorage.unreadCount : ""
});
}
}
function scheduleRequest() {
console.log('scheduleRequest');
var randomness = Math.random() * 2;
var exponent = Math.pow(2, localStorage.requestFailureCount || 0);
var multiplier = Math.max(randomness * exponent, 1);
var delay = Math.min(multiplier * pollIntervalMin, pollIntervalMax);
delay = Math.round(delay);
console.log('Scheduling for: ' + delay);
if (oldChromeVersion) {
if (requestTimerId) {
window.clearTimeout(requestTimerId);
}
requestTimerId = window.setTimeout(onAlarm, delay * 60 * 1000);
} else {
console.log('Creating alarm');
// Use a repeating alarm so that it fires again if there was a problem
// setting the next alarm.
chrome.alarms.create('refresh', { periodInMinutes: delay });
}
}
// ajax stuff
function startRequest(params) {
// Schedule request immediately. We want to be sure to reschedule, even in the
// case where the extension process shuts down while this request is
// outstanding.
if (params && params.scheduleRequest) scheduleRequest();
function stopLoadingAnimation() {
if (params && params.showLoadingAnimation) loadingAnimation.stop();
}
if (params && params.showLoadingAnimation)
loadingAnimation.start();
getInboxCount(
function (count) {
stopLoadingAnimation();
updateUnreadCount(count);
},
function () {
stopLoadingAnimation();
delete localStorage.unreadCount;
updateIcon();
}
);
}
function getOptions() {
if (!localStorage.hasOwnProperty('options')) {
localStorage["options"] = '1,1,1';
return [1, 1, 1];
} else {
return localStorage.options.split(',');
}
}
function getInboxCount(onSuccess, onError) {
var xhr = new XMLHttpRequest();
var abortTimerId = window.setTimeout(function () {
xhr.abort(); // synchronously calls onreadystatechange
}, requestTimeout);
function handleSuccess(count) {
localStorage.requestFailureCount = 0;
window.clearTimeout(abortTimerId);
if (onSuccess)
onSuccess(count);
}
var invokedErrorCallback = false;
function handleError() {
++localStorage.requestFailureCount;
window.clearTimeout(abortTimerId);
if (onError && !invokedErrorCallback)
onError();
invokedErrorCallback = true;
}
responseArray = [0, 0, 0]
fillUnReadMsgCount(getDefaultUrl(), 0);
fillUnReadMsgCount(getFollowUrl(), 1);
fillUnReadMsgCount(getVoteThankUrl(), 2);
function fillUnReadMsgCount(remoteURL, msgIndex) {
try {
console.log("remoteURL:" + remoteURL)
var xhr = new XMLHttpRequest();
xhr.onreadystatechange = function () {
if (xhr.readyState != 4)
return;
if (xhr.responseText) {
var responseJSON = JSON.parse(xhr.responseText);
var responseData = responseJSON["data"];
var unReadMsgCount = responseData.filter((item) => !item.is_read).length;
responseArray[msgIndex] = unReadMsgCount;
fillLocalStorage();
}
handleError();
return;
};
xhr.onerror = function () {
handleError();
};
xhr.open("GET", remoteURL, true);
xhr.send(null);
} catch (e) {
console.error(chrome.i18n.getMessage("zhihucheck_exception", e));
handleError();
}
}
function fillLocalStorage() {
var lastResponseArray = localStorage["lastResponseArray"];
if (lastResponseArray == responseArray.toString()) {
console.log('nothing changed,break my heart;');
localStorage["hasChanged"] = false;
} else {
localStorage["lastResponseArray"] = responseArray;
localStorage["hasChanged"] = true;
}
localStorage.msg1 = responseArray[0];
localStorage.msg2 = responseArray[1];
localStorage.msg3 = responseArray[2];
var currentOptions = getOptions();
if (currentOptions[0] == 0) {
permission = permission - 1;
}
if (currentOptions[1] == 0) {
permission = permission - 2;
}
if (currentOptions[2] == 0) {
permission = permission - 4;
}
switch (permission) {
case 7:
handleSuccess(responseArray[0] + responseArray[1] + responseArray[2]);
updateTitle(responseArray[0], responseArray[1], responseArray[2]);
break;
case 6:
handleSuccess(responseArray[1] + responseArray[2]);
updateTitle(0, responseArray[1], responseArray[2]);
break;
case 5:
handleSuccess(responseArray[0] + responseArray[2]);
updateTitle(responseArray[0], 0, responseArray[2]);
break;
case 4:
handleSuccess(responseArray[2]);
updateTitle(0, 0, responseArray[2]);
break;
case 3:
handleSuccess(responseArray[0] + responseArray[1]);
updateTitle(responseArray[0], responseArray[1], 0);
break;
case 2:
handleSuccess(responseArray[1]);
updateTitle(0, responseArray[1], 0);
break;
case 1:
handleSuccess(responseArray[0]);
updateTitle(responseArray[0], 0, 0);
break;
case 0:
handleSuccess(0);
updateTitle(0);
break;
}
}
}
function updateUnreadCount(count) {
var changed = localStorage.unreadCount != count;
localStorage.unreadCount = count;
updateIcon();
if (changed)
animateFlip();
}
function updateTitle(msg1, msg2, msg3) {
var contents = "";
var content1 = chrome.i18n.getMessage("zhihumsg_content1", [msg1]);
var content2 = chrome.i18n.getMessage("zhihumsg_content2", [msg2]);
var content3 = chrome.i18n.getMessage("zhihumsg_content3", [msg3]);
var title = chrome.i18n.getMessage("zhihumsg_title");
if (msg1 != 0 && msg1 != undefined) {
contents += ' ' + content1;
} else {
msg1 = 0
}
if (msg2 != 0 && msg2 != undefined) {
contents += ' ' + content2;
} else {
msg2 = 0
}
if (msg3 != 0 && msg3 != undefined) {
contents += ' ' + content3;
} else {
msg3 = 0
}
chrome.browserAction.setTitle({ title: contents });
var currentOptions = getOptions();
console.log('currentOptions[3] == 1', currentOptions[3] == 1, 'msg1 + msg2 + msg3', (msg1 + msg2 + msg3) != "0", 'localStorage.hasChanged=="true"', localStorage.hasChanged == "true");
if (currentOptions[3] == 1 && (msg1 + msg2 + msg3) != "0" && localStorage.hasChanged == "true") {
// Create a simple text notification:
var notification = webkitNotifications.createNotification(
'img/zhihu-logo_48.png', // icon url - can be relative
title, // notification title
contents // notification body text
);
notification.onclick = function () {
goToInbox();
if (notification) {
notification.cancel();
}
};
function getNotificationCloseTimeout() {
var timer = localStorage.notificationCloseTimeout;
if (timer) {
return timer;
} else {
localStorage["notificationCloseTimeout"] = 10;
return 10;
}
}
var notificationCloseTimeout = getNotificationCloseTimeout() * 1000;
console.log('notificationCloseTimeout', notificationCloseTimeout);
if (notificationCloseTimeout != 0) {
setTimeout(function () {
if (notification) {
notification.cancel();
}
}, notificationCloseTimeout);
}
notification.show();
console.log("start notification");
}
}
function ease(x) {
return (1 - Math.sin(Math.PI / 2 + x * Math.PI)) / 2;
}
function animateFlip() {
rotation += 1 / animationFrames;
drawIconAtRotation();
if (rotation <= 1) {
setTimeout(animateFlip, animationSpeed);
} else {
rotation = 0;
updateIcon();
}
}
function drawIconAtRotation() {
canvasContext.save();
canvasContext.clearRect(0, 0, canvas.width, canvas.height);
canvasContext.translate(
Math.ceil(canvas.width / 2),
Math.ceil(canvas.height / 2));
canvasContext.rotate(2 * Math.PI * ease(rotation));
canvasContext.drawImage(loggedInImage,
-Math.ceil(canvas.width / 2),
-Math.ceil(canvas.height / 2));
canvasContext.restore();
chrome.browserAction.setIcon({
imageData: canvasContext.getImageData(0, 0,
canvas.width, canvas.height)
});
}
function activeInbox(tabId) {
if (localStorage.unreadCount != "0") {
chrome.tabs.executeScript(tabId,
{ code: "document.getElementById('zh-top-nav-count-wrap').click()" });
}
}
function goToInbox() {
console.log('Going to inbox...');
chrome.tabs.getAllInWindow(undefined, function (tabs) {
for (var i = 0, tab; tab = tabs[i]; i++) {
if (tab.url && isZhihuUrl(tab.url)) {
console.log('Found Zhihu tab: ' + tab.url + '. ' +
'Focusing and refreshing count...');
chrome.tabs.update(tab.id, { selected: true });
startRequest({ scheduleRequest: false, showLoadingAnimation: false });
activeInbox(tab.id);
return;
}
}
console.log('Could not find Zhihu tab. Creating one...');
chrome.tabs.create({ url: getZhihuUrl() });
activeInbox(null);
});
}
function onInit() {
console.log('onInit');
localStorage.requestFailureCount = 0; // used for exponential backoff
startRequest({ scheduleRequest: true, showLoadingAnimation: true });
if (!oldChromeVersion) {
// (mpcomplete): We should be able to remove this now, but leaving it
// for a little while just to be sure the refresh alarm is working nicely.
chrome.alarms.create('watchdog', { periodInMinutes: 5 });
}
}
function onAlarm(alarm) {
console.log('Got alarm', alarm);
// |alarm| can be undefined because onAlarm also gets called from
// window.setTimeout on old chrome versions.
if (alarm && alarm.name == 'watchdog') {
onWatchdog();
} else {
startRequest({ scheduleRequest: true, showLoadingAnimation: false });
}
}
function onWatchdog() {
chrome.alarms.get('refresh', function (alarm) {
if (alarm) {
console.log('Refresh alarm exists. Yay.');
} else {
console.log('Refresh alarm doesn\'t exist!? ' +
'Refreshing now and rescheduling.');
startRequest({ scheduleRequest: true, showLoadingAnimation: false });
}
});
}
if (oldChromeVersion) {
updateIcon();
onInit();
} else {
chrome.runtime.onInstalled.addListener(onInit);
chrome.alarms.onAlarm.addListener(onAlarm);
}
var filters = {
// (aa): Cannot use urlPrefix because all the url fields lack the protocol
// part. See crbug.com/140238.
url: [
{ urlContains: getZhihuUrl().replace(/^https?\:\/\//, '') }
]
};
function onNavigate(details) {
if (details.url && isZhihuUrl(details.url)) {
console.log('Recognized Zhihu navigation to: ' + details.url + '.' +
'Refreshing count...');
startRequest({ scheduleRequest: false, showLoadingAnimation: false });
}
}
if (chrome.webNavigation && chrome.webNavigation.onDOMContentLoaded &&
chrome.webNavigation.onReferenceFragmentUpdated) {
chrome.webNavigation.onDOMContentLoaded.addListener(onNavigate, filters);
chrome.webNavigation.onReferenceFragmentUpdated.addListener(
onNavigate, filters);
} else {
chrome.tabs.onUpdated.addListener(function (_, details) {
onNavigate(details);
});
}
chrome.browserAction.onClicked.addListener(goToInbox);
if (chrome.runtime && chrome.runtime.onStartup) {
chrome.runtime.onStartup.addListener(function () {
console.log('Starting browser... updating icon.');
startRequest({ scheduleRequest: false, showLoadingAnimation: false });
updateIcon();
});
} else {
// This hack is needed because Chrome 22 does not persist browserAction icon
// state, and also doesn't expose onStartup. So the icon always starts out in
// wrong state. We don't actually use onStartup except as a clue that we're
// in a version of Chrome that has this problem.
chrome.windows.onCreated.addListener(function () {
console.log('Window created... updating icon.');
startRequest({ scheduleRequest: false, showLoadingAnimation: false });
updateIcon();
});
}