Enhanced Ticker: Autoupdate threads

Updates threads automatically if the ticker is open. Requires "Enhanced Ticker" 1.32 or upwards.

Od 11.05.2016.. Pogledajte najnovija verzija.

// ==UserScript==
// @name        Enhanced Ticker: Autoupdate threads
// @namespace   MrBrax
// @description	Updates threads automatically if the ticker is open. Requires "Enhanced Ticker" 1.32 or upwards.
// @include     https://facepunch.com/showthread.php?*
// @version     0.1
// @grant       GM_addStyle
// ==/UserScript==

function timeSince(date) {

    var seconds = Math.floor((new Date() - date) / 1000);

    var interval = Math.floor(seconds / 31536000);

    if (interval > 1) {
        return interval + " Years";
    }
    interval = Math.floor(seconds / 2592000);
    if (interval > 1) {
        return interval + " Months";
    }
    interval = Math.floor(seconds / 86400);
    if (interval > 1) {
        return interval + " Days";
    }
    interval = Math.floor(seconds / 3600);
    if (interval > 1) {
        return interval + " Hours";
    }
    interval = Math.floor(seconds / 60);
    if (interval > 1) {
        return interval + " Minutes";
    }
    return Math.floor(seconds) + " seconds";
}

GM_addStyle(".au_bar { background: #cce; border: 1px solid #777; border-bottom-width: 0; clear: both; display: block; font: 12px Tahoma; padding: 4px; width: 100%; box-sizing: border-box; }");

var thread = location.href.match(/\?t=([0-9]+)/);

var paginator = document.getElementById("yui-gen1");
var plist = document.getElementById("posts");

if( paginator ){
	var s = paginator.innerHTML.trim().match(/Page ([0-9]+) of ([0-9]+)/);
	console.log( s[1], s[2], s[1] == s[2] );
	if(s[1] != s[2]){
		var au_info = document.createElement("div");
		au_info.className = "au_bar";
		au_info.innerHTML = "<strong>Auto updater not running, go to the last page.</strong>";
		plist.appendChild(au_info);
		return;
	}
}

var title = document.title;
var newposts = 0;

document.title = "[" + newposts + "] " + title;

var au_info = document.createElement("div");
au_info.className = "au_bar";
au_info.innerHTML = "<strong>Auto updater start</strong>";
plist.appendChild(au_info);

function updateTime(a){
	var t = document.querySelectorAll(".date");
	for(i in t){
		var d = new Date( t[i].title );
		t[i].innerHTML = timeSince( d.getTime() ) + " Ago";
	}
	if(a) setTimeout( updateTime, 10000 );
}

updateTime(true);

var storageHandler = function (e) {
	if(e.key == "ETicker_LastPost"){
		var d = e.newValue.split(".");
		if(d[0] == thread[1]){

			var url = "https://facepunch.com/showthread.php?t=" + d[0] + "&p=" + d[1];

			console.log("Update thread", thread[1], url);

			var xhr = new XMLHttpRequest();
			xhr.open("GET", url, true);
			xhr.responseType = "document";
			xhr.onreadystatechange = function (){
				if (xhr.readyState == 4 && xhr.status == 200){
					var data = xhr.responseXML;
					console.log(data);
					var newp = data.getElementById("post_" + d[1]);
					if(newp){
						if(newp.parentNode.childNodes[1] == newp){
							var s = document.createElement("div");
							s.className = "au_bar";
							s.innerHTML = "<strong>New page</strong>";
							plist.appendChild(s);
							console.log("new page");
						}
						plist.appendChild(newp);
					}
					newposts++;
					document.title = "[" + newposts + "] " + title;
					updateTime(false);
				}
			};
			xhr.send();
		}
	}
};
window.addEventListener("storage", storageHandler, false);