/* Injected into the webpage we're linking to 
 * I recommend using GreaseMonkey or something similar to automatically inject,
 * but you can also just do something like:
 * <script src='http://127.0.0.1:9001/js/socket.js'></script>
 */

(function () {

	// Change port/address if needed 
	var socket = new WebSocket("ws://127.0.0.1:9001/");

	socket.onopen = function(evt) {  };
	socket.onclose = function(evt) {  };
	socket.onmessage = function(evt) { 
		switch (evt.data) {
			case "css":
				reloadCSS();
			break;
			case "page":
				window.location.reload();
			break;
			default:
				console.log(evt.data);
				eval(evt.data);
			break;
		}
	};
	socket.onerror = function(evt) { console.log(evt); };

	var reloadCSS = function () {
		var elements = document.getElementsByTagName("link");

		for (var i = 0; i < elements.length; i++) {
			if (elements[i].rel == "stylesheet") {
				var href = elements[i].getAttribute("data-href")

				if (href == null) {
					href = elements[i].href;
					elements[i].setAttribute("data-href", href);
				}

				if (window.__BL_OVERRIDE_CACHE) {
					var link = document.createElement("link");
					link.href = href;
					link.rel = "stylesheet";
					document.head.appendChild(link);

					document.head.removeChild(elements[i]);

					continue;
				}
				elements[i].href = href + ((href.indexOf("?") == -1) ? "?" : "&") + "c=" + (new Date).getTime();
			}
		}
	}

	var log = console.log;
	console.log = function(str) {
		log.call(console, str);
		var err = (new Error).stack;
		err = err.replace("Error", "").replace(/\s+at\s/g, '@').replace(/@/g, "\n@");

		socket.send(str + "\n" + err + "\n\n");
	}
})();


@@ -789,16 +789,30 @@
 ink%22);%0A%0A
+%09%09var c = 0;%0A%0A
 %09%09for (v
@@ -849,16 +849,45 @@
  i++) %7B%0A
+%09%09%09console.log(elements%5Bi%5D);%0A
 %09%09%09if (e
@@ -894,17 +894,17 @@
 lements%5B
-i
+c
 %5D.rel ==
@@ -972,16 +972,17 @@
 a-href%22)
+;
 %0A%0A%09%09%09%09if
@@ -1016,25 +1016,25 @@
  = elements%5B
-i
+c
 %5D.href;%0A%09%09%09%09
@@ -1043,17 +1043,17 @@
 lements%5B
-i
+c
 %5D.setAtt
@@ -1300,25 +1300,25 @@
 ld(elements%5B
-i
+c
 %5D);%0A%0A%09%09%09%09%09co
@@ -1437,16 +1437,24 @@
 );%0A%09%09%09%7D%0A
+%09%09%09c++;%0A
 %09%09%7D%0A%09%7D%0A%0A

Fix collision problem