if (!self.fetch) {
	throw new Error("Browser does not support fetch(); manual refresh required");
}

var to = window.location.pathname.replace("/room/", "/poll/");
var since = document.getElementsByClassName("question").length;
var newdata = false;
var shown = true;
console.log(to);

function poll() {
	fetch(to + "?since=" + since).then(function(res) {
		if (!res.ok) {
			res.text().then(function (txt) {
				console.error("Fetch failed: " + txt);
			});
			return;
		}

		if (res.status == 204) {
			return poll();
		}

		res.json().then(function (res) {
			res.forEach(function(q) {
				var qs = document.querySelectorAll(".question");
				var qe = qs[qs.length-1].cloneNode(true);

				if (!q.by_instructor) {
					qe.classList.remove("instructor");
				}

				if (q.author_nic == "") {
					qe.querySelector("footer").remove();
				} else {
					qe.querySelector("footer > cite").textContent = q.author_nic;
				}

				qe.getElementsByTagName("p")[0].textContent = q.text;
				qs[0].parentNode.insertBefore(qe, qs[0]);
			});

			if (!shown) {
				document.getElementById("icon").href = "/static/new-question.ico";
			}
			newdata = true

			since = document.getElementsByClassName("question").length;
			poll();
		});
	});
}

window.addEventListener("focus", function(e) {
	if (newdata) {
		document.getElementById("icon").href = "/static/no-question.ico";
		newdata = false;
	}
	shown = true
}, false);

window.addEventListener("blur", function(e) {
	shown = false
}, false);

poll();

@@ -235,21 +235,17 @@
 wdata = 
-false
+0
 ;%0Avar sh
@@ -260,23 +260,34 @@
 ue;%0A
-console.log(to)
+var title = document.title
 ;%0A%0Af
@@ -1026,23 +1026,41 @@
 qs%5B0%5D);%0A
+%09%09%09%09newdata += 1;%0A
 %09%09%09%7D);%0A
-
 %0A%09%09%09if (
@@ -1148,24 +1148,28 @@
 %09%09%09%7D%0A%09%09%09
+if (
 newdata 
 = true%0A%0A
@@ -1160,22 +1160,79 @@
 newdata 
+!
 = 
-true
+0) %7B%0A%09%09%09%09document.title = %22(%22 + newdata + %22) %22 + title;%0A%09%09%09%7D
 %0A%0A%09%09%09sin
@@ -1372,16 +1372,21 @@
 (newdata
+ != 0
 ) %7B%0A%09%09do
@@ -1455,23 +1455,109 @@
 ;%0A%09%09
-newdata = false
+// show (newdata) in window title to give notification bubble%0A%09%09document.title = title;%0A%09%09newdata = 0
 ;%0A%09%7D

Put notification count in title