-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathbackground.html
187 lines (166 loc) · 6 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
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
<html>
<head>
<script src="fancy-options/lib/store.js"></script>
<script>
// Global settings
var settings = null;
var deal_to_link = [];
var new_deals = [];
// TODO: Probably should make a func out of this
var city_feed_objs = [
{
'city': 'honolulu',
'city_display': 'Honolulu, HI',
'feeds': [
{'title': 'Groupon', 'url': 'http://ajax.googleapis.com/ajax/services/feed/load?v=1.0&q=http://feeds.feedburner.com/grouponhonolulu'},
{'title': 'Living Social', 'url': 'http://ajax.googleapis.com/ajax/services/feed/load?v=1.0&q=http://livingsocial.com/cities/82-honolulu.atom'},
{'title': 'Hot Deals Hawaii', 'url': 'http://ajax.googleapis.com/ajax/services/feed/load?v=1.0&q=http://www.hotdealshawaii.com/engine/SplashRSS.aspx?contestid=49927'},
{'title': 'Tippr', 'url': 'http://ajax.googleapis.com/ajax/services/feed/load?v=1.0&q=http://tippr.com/feed/tippr-honolulu/rss'},
{'title': 'Crowd Savings (Play Hawaii)', 'url': 'http://ajax.googleapis.com/ajax/services/feed/load?v=1.0&q=http://www.crowdsavings.com/honolulu/rss'}
]
},
{
'city': 'chicago',
'city_display': 'Chicago, IL',
'feeds': [
{'title': 'Groupon', 'url': 'http://ajax.googleapis.com/ajax/services/feed/load?v=1.0&q=http://feeds.feedburner.com/groupon'},
]
},
{
'city': 'dc',
'city_display': 'Washington, DC',
'feeds': [
{'title': 'Groupon', 'url': 'http://ajax.googleapis.com/ajax/services/feed/load?v=1.0&q=http://feeds.feedburner.com/grouponwashington-dc'},
{ 'title': 'Living Social', 'url': 'http://ajax.googleapis.com/ajax/services/feed/load?v=1.0&q=http://www.livingsocial.com/cities/1-washington-d-c.atom'}
]
}
];
// Initialize
init();
run_background();
setInterval("run_background()", 3600000);
function init() {
var city_key = 'honolulu';
if (settings != null) {
var city_key = settings.get('dealios-location');
}
settings = new Store("settings", {'dealios-location': city_key});
};
function get_user_city_feed_obj() {
var city_key = settings.get('dealios-location');
for (feed in city_feed_objs) {
if (city_key == city_feed_objs[feed]['city']) {
return city_feed_objs[feed];
}
}
return null;
};
function crawl_json_feed(url, callback) {
var xhr = new XMLHttpRequest();
xhr.onreadystatechange = function()
{
if (xhr.readyState == 4)
{
if (xhr.status == 200)
{
var json_response = JSON.parse(xhr.responseText);
callback(json_response);
}
else
{
callback(null);
}
}
};
xhr.open("GET", url, true);
xhr.send();
};
function add_deals_to_element(element)
{
for(deal_source in window.localStorage) {
if(deal_source != 'store.settings.dealios-location'){
var title = document.createElement('h3');
var title_text = document.createTextNode(deal_source);
title.appendChild(title_text);
element.appendChild(title);
var ul = document.createElement('ul');
var deal_titles = JSON.parse(window.localStorage.getItem(deal_source));
for (key in deal_titles) {
var title = deal_titles[key];
var li = document.createElement('li');
var link = document.createElement('a');
link.setAttribute('href', deal_to_link[title]);
link.setAttribute('target', '_blank');
var text = document.createTextNode(title);
link.appendChild(text);
li.appendChild(link);
ul.appendChild(li);
var is_new = false;
for(index in new_deals){
if(new_deals[index] == title){
is_new = true;
break;
}
}
if(is_new){
link.setAttribute('class', 'newdeal');
}
else{
link.setAttribute('class', 'olddeal');
}
}
element.appendChild(ul);
}
}
};
function run_background()
{
var user_city_feed_obj = get_user_city_feed_obj();
var city_display = user_city_feed_obj.city_display;
var feeds = user_city_feed_obj.feeds;
for (feed in feeds) {
var deal_source = feeds[feed]['title'];
var url = feeds[feed]['url'];
// Closure
(function(deal_source, url) {
crawl_json_feed(url,
function(response) {
var entries = response.responseData.feed.entries;
store_deal(deal_source, entries);
}
);
})(deal_source, url);
}
}
function store_deal(deal_source, entries)
{
var deal_titles = JSON.parse(localStorage.getItem(deal_source));
if (deal_titles == null) {
var deal_titles = [];
}
var current_deal_titles = [];
for (index in entries) {
var title = entries[index].title;
var link = entries[index].link;
if(deal_source == 'Living Social')
{
title = entries[index].contentSnippet;
}
// If the deal doesn't exist, update the browser icon.
if (deal_titles.indexOf(title) == -1) {
chrome.browserAction.setIcon({"path" : "images/dealios22.png"});
new_deals.push(title);
}
current_deal_titles.push(title);
deal_to_link[title] = link;
}
window.localStorage.setItem(deal_source, JSON.stringify(current_deal_titles));
};
function clear_new(){
new_deals = [];
};
</script>
</head>
<body>
</body>
</html>