-
Notifications
You must be signed in to change notification settings - Fork 11
/
background.js
151 lines (130 loc) · 4.2 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
//Global object
var RN = {}
window.RN = RN;
//Interval that we use to check feeds
RN.interval = 30000;
//Maximum length of list of seen posts
RN.MAX_SEEN_LENGTH = 1000;
//Load the Google Feeds API
google.load("feeds", "1");
//Load the list of posts we've seen
chrome.storage.sync.get("seen", function(items){
if(items.seen){
RN.seen = items.seen;
} else {
RN.seen = [];
}
})
// Map of notification IDs to the URLs to open when they are clicked
RN.notificationToUrl = {};
//Add a post to the list of posts we've seen
function addSeen(id){
RN.seen.push(id);
while(JSON.stringify(RN.seen).length > chrome.storage.sync.QUOTA_BYTES_PER_ITEM){
RN.seen.shift();
}
chrome.storage.sync.set({"seen": RN.seen});
}
//Load a list of feed URLs we're monitoring
chrome.storage.sync.get("feedURLs", function(items){
if(items["feedURLs"]){
RN.feedURLs = items["feedURLs"];
} else if(localStorage["feedURLs"]){
RN.feedURLs = JSON.parse(localStorage["feedURLs"]);
} else {
RN.feedURLs = [];
}
})
//List of feeds
RN.feeds = [];
//Initialize the list of feeds
function initializeFeeds(){
for(index in RN.feedURLs){
RN.feeds.push(new google.feeds.Feed(RN.feedURLs[index]));
}
}
//Add a feed to the list of feeds we are monitoring
function addFeed(feedURL){
feedURL = feedURL.trim();
RN.feeds.push(new google.feeds.Feed(feedURL));
RN.feedURLs.push(feedURL);
if(RN.feedURLs.length > 100000){
RN.feedURLs.splice(0, RN.feedURLs.length - 100000);
}
chrome.storage.sync.set({"feedURLs": RN.feedURLs});
}
//Remove a feed from the list of feeds we are monitoring
function removeFeed(feedURL){
RN.feedURLs = RN.feedURLs.filter(function(element){ return (element !== feedURL);});
RN.feeds = RN.feeds.filter(function(element){ return (element.O !== feedURL);});
chrome.storage.sync.set({"feedURLs": RN.feedURLs});
}
//Check a feed for new posts (feed onLoad callback)
function onFeedLoad(result){
console.log("Checking feed...");
if(!result.error){
var container = document.getElementById("feed");
for (var i = 0; i < result.feed.entries.length; i++) {
var entry = result.feed.entries[i];
//If we have not seen this post
if(RN.seen.indexOf(entry.link) < 0){
sendNotification(entry, result.feed);
addSeen(entry.link);
}
}
}
else{
console.error("There was an error loading the feed.")
}
}
//Send a notification about a new post (feed entry)
function sendNotification(entry, feed){
chrome.notifications.create('',{
title: entry.title,
type: 'basic',
iconUrl: '128.png',
message: strip(entry.content).trim(),
contextMessage: feed.title,
isClickable: true
}, function(notificationID){
RN.notificationToUrl[notificationID] = entry.link;
});
}
function strip(html)
{
var tmp = document.createElement("DIV");
tmp.innerHTML = html;
return tmp.textContent || tmp.innerText || "";
}
//The index of the feed in the feed list to check on the next run
RN.feedIndex = 0;
//Load a feed and check it for new posts
function loadFeed(){
if(RN.feeds.length > 0){
var current = RN.feeds[RN.feedIndex];
current.load(onFeedLoad);
RN.feedIndex = (RN.feedIndex + 1) % RN.feeds.length;
}
//Run again next interval
setTimeout(loadFeed, RN.interval);
}
function onNotificationClick(notificationID){
chrome.tabs.create({url: RN.notificationToUrl[notificationID]});
}
//Initialize the feeds and start the regular monitoring
function start(){
initializeFeeds();
loadFeed();
chrome.notifications.onClicked.addListener(onNotificationClick);
console.log("Started checking feeds...");
}
//Once the Google Feeds API starts check the feeds
google.setOnLoadCallback(start);
var _gaq = _gaq || [];
_gaq.push(['_setAccount', 'UA-16826446-5']);
_gaq.push(['_trackPageview']);
(function() {
var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
ga.src = 'https://ssl.google-analytics.com/ga.js';
var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
})();