forked from adamzr/RSS-Alert
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbackground.html
124 lines (107 loc) · 3.49 KB
/
background.html
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
<html>
<script type="text/javascript" src="https://www.google.com/jsapi"></script>
<script type="text/javascript">
//Global object
var RN = {}
//Interval that we use to check feeds
RN.interval = 30000;
//Load the Google Feeds API
google.load("feeds", "1");
//Load the list of posts we've seen
RN.seen = localStorage["seen"];
if(!RN.seen){
RN.seen = [];
}
else{
RN.seen = JSON.parse(RN.seen);
}
//Add a post to the list of posts we've seen
function addSeen(id){
RN.seen.push(id);
localStorage["seen"] = JSON.stringify(RN.seen);
}
//Load a list of feed URLs we're monitoring
RN.feedURLs = localStorage["feedURLs"];
if(!RN.feedURLs){
RN.feedURLs = [];
}
else{
RN.feedURLs = JSON.parse(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){
RN.feeds.push(new google.feeds.Feed(feedURL));
RN.feedURLs.push(feedURL);
localStorage["feedURLs"] = JSON.stringify(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);});
localStorage["feedURLs"] = JSON.stringify(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);
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){
var notification = webkitNotifications.createHTMLNotification(
'notification.html?title=' + encodeURIComponent(entry.title) + '&url=' + encodeURIComponent(entry.link) + '&post=' + encodeURIComponent(entry.content) + '&byline=' + encodeURIComponent("Posted on " + entry.publishedDate)
);
notification.show();
}
//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);
}
//Initialize the feeds and start the regular monitoring
function start(){
initializeFeeds();
loadFeed();
console.log("Started checking feeds...");
}
//Once the Google Feeds API starts check the feeds
google.setOnLoadCallback(start);
</script>
<script>
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);
})();
</script>
</html>