forked from summerblue/chrome-phphub-notifier
-
Notifications
You must be signed in to change notification settings - Fork 0
/
background.js
120 lines (99 loc) · 3.54 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
;(function(){
'use strict';
// message listener to accept request from content script
chrome.runtime.onMessage.addListener(function(request, sender, sendResponse) {
console.log('get message request: key=' + request.key);
sendResponse({result: getConfig()[request.key]});
});
var GitHubNotification;
var notificationCountUrl = 'https://phphub.org/notifications/count';
var notificationUrl = 'https://phphub.org/notifications';
var blue = [1, 128, 255, 255];
var gray = [190, 190, 190, 230];
function _isNotificationUrl(url) {
return url.indexOf(notificationUrl) === 0;
}
function _goToNotificationTab() {
_displayUnreadCount(0);
console.log('Going to notification tab...');
chrome.tabs.getAllInWindow(undefined, function(tabs) {
for (var i = 0, tab; tab = tabs[i]; i++) {
if (tab.url && _isNotificationUrl(tab.url)) {
console.log('Found notification tab: ' + tab.url + '. ' +
'Focusing and refreshing count...');
chrome.tabs.update(tab.id, {selected: true});
GitHubNotification.checkNotifications();
return;
}
}
console.log('Could not find notification tab. Creating one...');
chrome.tabs.create({url: notificationUrl});
});
}
function _loginWarning() {
chrome.browserAction.setBadgeBackgroundColor({color: gray});
chrome.browserAction.setBadgeText({text: '?'});
chrome.browserAction.setTitle({title: chrome.i18n.getMessage("github_login_needed")});
}
function _getBadgeText(num) {
return num != 0 ? num+'' : ''
}
function _getTitle(num) {
return num + " unread " +
(num == 1 ? 'notification' : 'notifications');
}
function _displayUnreadCount(response) {
var unreadCount = parseInt(response);
unreadCount = isNaN(unreadCount) ? 0 : unreadCount;
console.log('Get ' + unreadCount + ' unread notifications');
chrome.browserAction.setBadgeBackgroundColor({color: blue});
chrome.browserAction.setBadgeText({text: _getBadgeText(unreadCount)});
chrome.browserAction.setTitle({title: _getTitle(unreadCount)});
}
GitHubNotification = {
getInterval: function() {
return parseInt(getConfig()['feature-2-interval']) * 60 * 1000;
},
isEnabled: function() {
return getConfig()['feature-2-enable'];
},
init: function() {
this.checkNotificationsLoop();
chrome.browserAction.onClicked.addListener(this.goToNotificationTab.bind(this));
},
goToNotificationTab: _goToNotificationTab,
checkNotificationsLoop: function(){
// check notification again if it's enabled and the date range since last checked is longer
// than interval.
if (GitHubNotification.isEnabled() && (
typeof(localStorage.last_checked_date) === 'undefined' ||
(Date.now() - localStorage.last_checked_date) >= GitHubNotification.getInterval()
)) {
this.checkNotifications();
localStorage.last_checked_date = Date.now();
}
// loop;
window.setTimeout(GitHubNotification.checkNotificationsLoop.bind(GitHubNotification), 60000);
},
checkNotifications: function() {
var xhr = new XMLHttpRequest();
xhr.onreadystatechange = function() {
if (xhr.readyState == 4) {
var response = xhr.response;
if (response.match(/<body id="body/)) {
// not logged in
console.log('not logged in currently');
_loginWarning();
} else {
// logged in
_displayUnreadCount(response);
}
}
};
xhr.open("GET", notificationCountUrl, true);
console.log('making request to get notifications...');
xhr.send(null);
}
};
GitHubNotification.init();
})();