]> git.wh0rd.org - tt-rss.git/blob - utils/notifier/background.html
a26e9fc472b275d797036b206db3c46445e815d2
[tt-rss.git] / utils / notifier / background.html
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";
19 var params = "op=getUnread&login=" + param_escape(login);
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();
33
34 if (xhr.status == 200) {
35 var unread = parseInt(xhr.responseText);
36
37 if (unread > 0) {
38 icon.path = "images/alert.png";
39 title.title = "You have %s unread articles.".replace("%s", unread);
40 } else {
41 icon.path = "images/normal.png";
42 title.title = "You have no unread articles.";
43 }
44
45 localStorage["last_updated"] = d.getTime();
46 localStorage["last_error"] = "";
47 } else {
48 localStorage["last_error"] = xhr.responseText;
49
50 icon.path = "images/error.png";
51 title.title = "Error (%s) while requesting article information.".replace("%s", xhr.status);
52 }
53
54 chrome.browserAction.setTitle(title);
55 chrome.browserAction.setIcon(icon);
56
57 }
58 };
59
60 }
61
62 function timeout() {
63
64 var update_interval;
65 var prefs_updated;
66
67 if (localStorage["update_interval"])
68 update_interval = localStorage["update_interval"] * 60 * 1000;
69 else
70 update_interval = 15 * 60 * 1000;
71
72 if (localStorage["prefs_updated"])
73 prefs_updated = localStorage["prefs_updated"];
74 else
75 prefs_updated = -1;
76
77 var d = new Date();
78
79 if (d.getTime() > last_updated + update_interval ||
80 prefs_updated != prefs_last_updated) {
81
82 last_updated = d.getTime();
83 prefs_last_updated = prefs_updated;
84 try {
85 update();
86 } catch (e) {
87 //
88 }
89 }
90
91 window.setTimeout("timeout()", 1000);
92 }
93
94 function init() {
95
96 chrome.browserAction.onClicked.addListener(function() {
97 var site_url = localStorage['site_url'];
98
99 if (site_url) {
100 var cp = new Object();
101
102 cp.url = site_url;
103
104 chrome.tabs.create(cp);
105 }
106
107 });
108
109 window.setTimeout("timeout()", 1000);
110
111 }
112 </script>
113
114 <body onload="init()"> </body>
115
116 </html>