summaryrefslogtreecommitdiff
path: root/utils/notifier/background.html
blob: 8a3acb2beb70b4779fd6cd91955cbd9e0c2645b5 (plain)
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
<html>
<script type="text/javascript">

var last_updated = 0;
var prefs_last_updated = 0;

function param_escape(arg) {
	if (typeof encodeURIComponent != 'undefined')
		return encodeURIComponent(arg);	
	else
		return escape(arg);
}

function update() {
	var d = new Date();
	var login = localStorage["login"];

	var requestUrl = localStorage["site_url"] + "/backend.php";
	var params = "op=getUnread&login=" + param_escape(login);

	var xhr = new XMLHttpRequest();


	xhr.open("POST", requestUrl, true);
	xhr.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	xhr.send(params);

	xhr.onreadystatechange = function() {
		if (xhr.readyState == 4) {

			var icon = new Object();
			var title = new Object();
			var badge = new Object();

			var showBadge = localStorage["show_badge"];

			if (xhr.status == 200) {
				var unread = parseInt(xhr.responseText);
	
				if (unread > 0) {

					icon.path = "images/alert.png";
					title.title = "You have %s unread articles.".replace("%s", unread);
					badge.text = unread + "";
				} else if (unread == -1) {
					icon.path = "images/error.png";		

					var errorMsg = xhr.responseText.split(";")[1];

					title.title = "Error: %s.".replace("%s", errorMsg.trim());
					badge.text = "";

				} else {
					icon.path = "images/normal.png";
					title.title = "You have no unread articles.";
					badge.text = "";
				}

				localStorage["last_updated"] = d.getTime();
				localStorage["last_error"] = "";
			} else {
				localStorage["last_error"] = xhr.responseText;

				icon.path = "images/error.png";
				title.title = "Error (%s) while updating.".replace("%s", xhr.status);
			}

			if (showBadge !== "1") badge.text = "";	

			chrome.browserAction.setBadgeText(badge);

			chrome.browserAction.setTitle(title);
			chrome.browserAction.setIcon(icon);

		}
	};

}

function timeout() {

	var update_interval;
	var prefs_updated;

	if (localStorage["update_interval"])
		update_interval = localStorage["update_interval"] * 60 * 1000;
	else
		update_interval = 15 * 60 * 1000;

	if (localStorage["prefs_updated"])
		prefs_updated = localStorage["prefs_updated"];
	else
		prefs_updated = -1;

	var d = new Date();

	if (d.getTime() > last_updated + update_interval || 
			prefs_updated != prefs_last_updated) {

		last_updated = d.getTime();
		prefs_last_updated = prefs_updated;
		try {
			update();
		} catch (e) {
			//
		}
	}

	window.setTimeout("timeout()", 1000);
}

function init() {

	chrome.browserAction.onClicked.addListener(function() { 
		var site_url = localStorage['site_url'];

		if (site_url) {
			var cp = new Object();

			cp.url = site_url;

			chrome.tabs.create(cp);
		}

	});
		
	window.setTimeout("timeout()", 1000);

}
</script>

<body onload="init()"> </body>

</html>