]>
Commit | Line | Data |
---|---|---|
f0ea2da5 AD |
1 | <html> |
2 | <script type="text/javascript"> | |
3 | ||
4 | var last_updated = 0; | |
a689cd47 | 5 | var feeds_last_updated = 0; |
f0ea2da5 AD |
6 | var prefs_last_updated = 0; |
7 | ||
8 | function param_escape(arg) { | |
9 | if (typeof encodeURIComponent != 'undefined') | |
10 | return encodeURIComponent(arg); | |
11 | else | |
12 | return escape(arg); | |
13 | } | |
14 | ||
a689cd47 AD |
15 | function update_feeds() { |
16 | ||
17 | console.log("feeds update"); | |
18 | ||
19 | var requestUrl = localStorage["site_url"] + "/public.php?op=globalUpdateFeeds"; | |
20 | ||
21 | var xhr = new XMLHttpRequest(); | |
22 | ||
23 | xhr.open("POST", requestUrl, true); | |
24 | xhr.send(); | |
25 | ||
26 | var d = new Date(); | |
27 | localStorage["last_feeds_updated"] = d.getTime(); | |
28 | } | |
29 | ||
f0ea2da5 | 30 | function update() { |
a689cd47 AD |
31 | console.log("update"); |
32 | ||
f0ea2da5 AD |
33 | var d = new Date(); |
34 | var login = localStorage["login"]; | |
36d146f8 AD |
35 | var single_user = localStorage["single_user"]; |
36 | ||
37 | if (single_user == "1") login = "admin"; | |
f0ea2da5 | 38 | |
27aaf38a | 39 | var requestUrl = localStorage["site_url"] + "/public.php"; |
fa7c9e65 | 40 | var params = "op=getUnread&fresh=1&login=" + param_escape(login); |
f0ea2da5 AD |
41 | |
42 | var xhr = new XMLHttpRequest(); | |
43 | ||
f0ea2da5 AD |
44 | xhr.open("POST", requestUrl, true); |
45 | xhr.setRequestHeader("Content-type", "application/x-www-form-urlencoded"); | |
46 | xhr.send(params); | |
47 | ||
48 | xhr.onreadystatechange = function() { | |
49 | if (xhr.readyState == 4) { | |
50 | ||
51 | var icon = new Object(); | |
52 | var title = new Object(); | |
01701654 | 53 | var badge = new Object(); |
fa7c9e65 | 54 | var badge_color = new Object(); |
01701654 | 55 | |
36d146f8 AD |
56 | // init stuff |
57 | icon.path = "images/normal.png"; | |
58 | title.title = ""; | |
59 | badge.text = ""; | |
60 | badge_color.color = [0, 0, 0, 0]; | |
61 | ||
fa7c9e65 AD |
62 | var show_badge = localStorage["show_badge"] == "1"; |
63 | var show_fresh = localStorage["show_fresh"] == "1"; | |
64 | ||
f0ea2da5 | 65 | if (xhr.status == 200) { |
fa7c9e65 AD |
66 | var response = xhr.responseText.split(";"); |
67 | ||
68 | var unread = parseInt(response[0]); | |
69 | ||
36d146f8 | 70 | if (isNaN(unread)) unread = 0; |
fa7c9e65 | 71 | |
36d146f8 AD |
72 | var fresh; |
73 | ||
fa7c9e65 AD |
74 | if (response.length == 2) |
75 | fresh = parseInt(response[1]); | |
76 | else | |
77 | fresh = 0; | |
01701654 | 78 | |
36d146f8 AD |
79 | if (isNaN(fresh)) fresh = 0; |
80 | ||
fa7c9e65 | 81 | if (unread > 0) { |
01701654 | 82 | icon.path = "images/alert.png"; |
f0ea2da5 | 83 | title.title = "You have %s unread articles.".replace("%s", unread); |
fa7c9e65 | 84 | |
36d146f8 | 85 | if (show_fresh && fresh > 0) { |
fa7c9e65 AD |
86 | badge.text = fresh + ""; |
87 | badge_color.color = [0, 200, 0, 255]; | |
fa7c9e65 AD |
88 | } else { |
89 | badge.text = unread + ""; | |
90 | badge_color.color = [255, 0, 0, 255]; | |
91 | } | |
92 | ||
01701654 AD |
93 | } else if (unread == -1) { |
94 | icon.path = "images/error.png"; | |
95 | ||
96 | var errorMsg = xhr.responseText.split(";")[1]; | |
97 | ||
98 | title.title = "Error: %s.".replace("%s", errorMsg.trim()); | |
01701654 | 99 | |
f0ea2da5 | 100 | } else { |
f0ea2da5 AD |
101 | title.title = "You have no unread articles."; |
102 | } | |
103 | ||
104 | localStorage["last_updated"] = d.getTime(); | |
105 | localStorage["last_error"] = ""; | |
106 | } else { | |
107 | localStorage["last_error"] = xhr.responseText; | |
108 | ||
109 | icon.path = "images/error.png"; | |
01701654 | 110 | title.title = "Error (%s) while updating.".replace("%s", xhr.status); |
f0ea2da5 AD |
111 | } |
112 | ||
fa7c9e65 | 113 | if (!show_badge) badge.text = ""; |
01701654 | 114 | |
fa7c9e65 | 115 | chrome.browserAction.setBadgeBackgroundColor(badge_color); |
01701654 | 116 | chrome.browserAction.setBadgeText(badge); |
f0ea2da5 AD |
117 | chrome.browserAction.setTitle(title); |
118 | chrome.browserAction.setIcon(icon); | |
119 | ||
120 | } | |
121 | }; | |
122 | ||
123 | } | |
124 | ||
125 | function timeout() { | |
126 | ||
127 | var update_interval; | |
128 | var prefs_updated; | |
a689cd47 | 129 | var feeds_update_interval = 30 * 60 * 1000; |
f0ea2da5 AD |
130 | |
131 | if (localStorage["update_interval"]) | |
132 | update_interval = localStorage["update_interval"] * 60 * 1000; | |
133 | else | |
134 | update_interval = 15 * 60 * 1000; | |
135 | ||
136 | if (localStorage["prefs_updated"]) | |
137 | prefs_updated = localStorage["prefs_updated"]; | |
138 | else | |
139 | prefs_updated = -1; | |
140 | ||
141 | var d = new Date(); | |
142 | ||
143 | if (d.getTime() > last_updated + update_interval || | |
144 | prefs_updated != prefs_last_updated) { | |
145 | ||
146 | last_updated = d.getTime(); | |
f0ea2da5 AD |
147 | try { |
148 | update(); | |
149 | } catch (e) { | |
a689cd47 AD |
150 | console.warn(e); |
151 | } | |
152 | } | |
153 | ||
154 | if (localStorage['update_feeds'] == 1 && (d.getTime() > feeds_last_updated + feeds_update_interval || prefs_updated != prefs_last_updated)) { | |
155 | ||
156 | feeds_last_updated = d.getTime(); | |
157 | ||
158 | try { | |
159 | update_feeds(); | |
160 | } catch (e) { | |
161 | console.warn(e); | |
f0ea2da5 | 162 | } |
a689cd47 | 163 | |
f0ea2da5 AD |
164 | } |
165 | ||
a689cd47 AD |
166 | prefs_last_updated = prefs_updated; |
167 | ||
f0ea2da5 AD |
168 | window.setTimeout("timeout()", 1000); |
169 | } | |
170 | ||
171 | function init() { | |
172 | ||
173 | chrome.browserAction.onClicked.addListener(function() { | |
174 | var site_url = localStorage['site_url']; | |
175 | ||
176 | if (site_url) { | |
177 | var cp = new Object(); | |
178 | ||
179 | cp.url = site_url; | |
180 | ||
181 | chrome.tabs.create(cp); | |
182 | } | |
183 | ||
01701654 | 184 | }); |
f0ea2da5 AD |
185 | |
186 | window.setTimeout("timeout()", 1000); | |
187 | ||
188 | } | |
189 | </script> | |
190 | ||
191 | <body onload="init()"> </body> | |
192 | ||
193 | </html> |