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