This repository was archived by the owner on Mar 8, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 51
Expand file tree
/
Copy pathbackground.js
More file actions
271 lines (228 loc) · 9.55 KB
/
background.js
File metadata and controls
271 lines (228 loc) · 9.55 KB
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
//A background page that manages notifications for GetHub.
//
//Copyright (C) 2011 Puneeth Chaganti <punchagan+gethub@gmail.com>
//Copyright (C) 2011 Thomas Stephen Lee <lee.iitb@gmail.com>
//
//This file is part of GetHub.
//
//GetHub is free software: you can redistribute it and/or modify it
//under the terms of the GNU General Public License as published by
//the Free Software Foundation, either version 3 of the License, or
//(at your option) any later version.
//
//GetHub is distributed in the hope that it will be useful, but
//WITHOUT ANY WARRANTY; without even the implied warranty of
//MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
//General Public License for more details.
//
//You should have received a copy of the GNU General Public License
//along with GetHub. If not, see <http://www.gnu.org/licenses/>.
//
//A portion of the code in this file is based on sample extensions
//for chromium, news_a11y, available at
//http://src.chromium.org/viewvc/chrome/trunk/src/chrome/common/extensions/docs/examples/extensions/news_a11y/
//and notifications, available at
//http://src.chromium.org/viewvc/chrome/trunk/src/chrome/common/extensions/docs/examples/api/notifications/
//whose license is available at
//http://code.google.com/google_bsd_license.html
'use strict';
/*
Displays a notification with the current time. Requires "notifications"
permission in the manifest file (or calling
"webkitNotifications.requestPermission" beforehand).
*/
var show = function (title, desc, link, image_url) {
image_url = image_url || "octocat-chrome.png"
var notification = webkitNotifications.createNotification(
image_url,
title,
desc
);
if (link == undefined) {
link = 'https://github.com/'
}
notification.onclick = function () {
chrome.tabs.create({url: link});
notification.cancel();
};
notification.ondisplay = function () {
setTimeout(function () { notification.cancel() }, localStorage.timeOut * 1000);
};
notification.show();
}
// The maximum number of feed items to show in the preview.
var maxFeedItems = 999;
var initialize = function(){
setup_alarm();
fetch_updates();
chrome.alarms.onAlarm.addListener(function(alarm){
console.log('Some alarm went off!');
if (alarm && alarm.name=='refresh') {
fetch_updates();
}
});
};
// Sets-up the alarm that triggers the periodic check for updates
var setup_alarm = function(){
var delay = parseFloat(localStorage.frequency) || 10;
chrome.alarms.create('refresh', {periodInMinutes: delay});
};
// Fetches the data from GitHub
var fetch_updates = function(){
var status_dict = {'private': false, 'actor': !JSON.parse(localStorage.showActions)};
var entries = [];
var feed_urls = [localStorage.hostname + localStorage.login + '.private.atom?token=' + localStorage.token];
// If user's actions are not required, do not add to list of urls to check
if (!status_dict.actor) {
feed_urls.push(localStorage.hostname + localStorage.login + '.private.actor.atom?token=' + localStorage.token)
};
var url;
while (url = feed_urls.shift()) {
var req = new XMLHttpRequest();
req.onload = function(response){
var doc = handle_response(response);
if (doc) {get_entries(doc, entries, status_dict)}
};
req.onerror = handle_errors;
req.open("GET", url, true);
req.send(null);
};
};
var get_entries = function(doc, entries, status_dict) {
console.log(doc);
var id = doc.getElementsByTagName('id')[0], type = id.textContent.split(localStorage.login + '.')[1];
status_dict[type] = true;
var new_entries = doc.getElementsByTagName('entry');
var index = entries.length;
for (var i = 0; i < new_entries.length; i++)
entries[index++] = new_entries[i];
console.log(entries, entries.length);
show_updates(entries, status_dict);
};
// Handles errors during the XMLHttpRequest.
var handle_errors = function() {
console.log('Failed to fetch RSS feed.');
}
// Handles parsing the feed data we got back from XMLHttpRequest.
var handle_response = function (response) {
var req = response.target, doc = req.responseXML;
if (!doc) {
if (req.status == 404 || req.status == 401) {
show('Error', "Didn't get a feed. Did you set a valid username/token?");
} else {
console.log('Bad Bad Donut.');
}
}
return doc;
}
// Shows the notifications for new updates, given a feed of actions
var show_updates = function (entries, status_dict) {
for (var key in status_dict)
if (!status_dict[key]) return;
if (localStorage.lastPublished == '') {
var lastDate = undefined;
}
else {
var lastDate = new Date(localStorage.lastPublished);
}
// Sort the entries;
entries.sort(sortEntries);
var count = Math.min(entries.length, maxFeedItems);
// For each entry
for (var i = 0; i < count; i++) {
var item = entries[i];
// Grab the publish date for the feed item.
var itemDate = new getItemDate(item)
// Check if the item is newer; if not, return!
if (lastDate != undefined && itemDate <= lastDate) {
return;
};
// Grab the title for the feed item.
var itemTitle = item.getElementsByTagName('title')[0];
var userUrl = item.getElementsByTagName('thumbnail')[0].getAttribute('url');
itemTitle = itemTitle.textContent;
var userRegex = new RegExp('^' + localStorage.login + '\\s|\\s' +
localStorage.login + '\\s|\\s' +
localStorage.login + '$', 'g')
itemTitle = itemTitle.replace(userRegex, " you "); // Replace quotes
// Grab the link
var itemLink = item.getElementsByTagName('link')[0];
itemLink = itemLink.getAttribute('href');
// Grab the description.
var itemDesc = item.getElementsByTagName('content')[0];
itemDesc = itemDesc.textContent;
itemDesc = itemDesc.replace(/<blockquote(.|\s)*?>/g, " -- "); // Replace blockquote with dash
itemDesc = itemDesc.replace(/<\/blockquote>/g, " || "); // Replace /blockquote with ;;
itemDesc = itemDesc.replace(/(<br>\s*)+/g, "; "); // Strip out all other tags
itemDesc = itemDesc.replace(/<.*?>/g, ""); // Strip out all other tags
itemDesc = itemDesc.replace(///g, " "); // Strip out /
itemDesc = itemDesc.replace(/\s+/g, " "); // Replace multiple spaces with one
itemDesc = itemDesc.replace(/(^\s*--\s*|\s*\|\|\s*$)/g, " "); // Strip dashes
itemDesc = itemDesc.replace(/"/g, "'"); // Replace quotes
itemDesc = itemDesc.replace(userRegex, " you "); // Replace username
// Grab the event type.
var itemEvent = item.getElementsByTagName('id')[0];
itemEvent = itemEvent.textContent.replace('/',':')
itemEvent = itemEvent.split(':')[2]
// All events are
// CommitCommentEvent, CreateEvent, FollowEvent, ForkEvent,
// GollumEvent, GistEvent, IssuesEvent, PullRequestEvent,
// PushEvent, WatchEvent
var events = ["CommitCommentEvent", "CreateEvent", "FollowEvent",
"GistEvent", "IssuesEvent", "PullRequestEvent",
"PushEvent", "WatchEvent"]
if ( events.indexOf(itemEvent)>=0 ) {
var itemText = itemDesc;
} else {
itemText = itemDate.toLocaleString();
}
// Show a desktop notification.
if (lastDate == undefined) {
show(itemTitle, itemText , itemLink, userUrl);
localStorage.lastPublished = itemDate; // Date
return // Return after showing the first item.
};
show(itemTitle, itemText, itemLink, userUrl);
if (i==0) {
localStorage.lastPublished = itemDate; // Save latest date
};
}
};
// Initialization on install
// Also could add code, that runs on updates.
chrome.runtime.onInstalled.addListener(
function(details) {
if (details.reason === 'install') {
if (!webkitNotifications) {
// Show a new tab with an error message.
chrome.tabs.create({url: 'error.html'});
return;
}
// Initialize the required values
localStorage.isActivated = true; // The display activation.
localStorage.frequency = 5; // The display frequency, in minutes.
localStorage.timeOut = 5; // The display notification timeOut, in seconds
localStorage.isInitialized = true; // The option initialization.
localStorage.token = ''; // The initialization.
localStorage.login = ''; // The initialization.
localStorage.hostname = 'https://github.com/'
localStorage.lastPublished = ''; // Intialize
localStorage.showActions = true; // The initialization.
show('GetHub','Installed! || Set username and token');
chrome.tabs.create({url: 'options.html'}); // Open options after install
}
// Initialize alarm and fetch updates
initialize();
}
);
// Hook up alarms and stuff, on start up.
chrome.runtime.onStartup.addListener(initialize);
var getItemDate = function (item) {
var itemPublished = item.getElementsByTagName('published')[0];
itemPublished = itemPublished.textContent;
var itemDate = new Date(itemPublished)
return itemDate
};
var sortEntries = function (a, b){
return getItemDate(b) - getItemDate(a)
};