Enhanced Ticker: Autoupdate threads

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

当前为 2016-05-11 提交的版本,查看 最新版本

您需要先安装一个扩展,例如 篡改猴Greasemonkey暴力猴,之后才能安装此脚本。

You will need to install an extension such as Tampermonkey to install this script.

您需要先安装一个扩展,例如 篡改猴暴力猴,之后才能安装此脚本。

您需要先安装一个扩展,例如 篡改猴Userscripts ,之后才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey,才能安装此脚本。

您需要先安装用户脚本管理器扩展后才能安装此脚本。

(我已经安装了用户脚本管理器,让我安装!)

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

(我已经安装了用户样式管理器,让我安装!)

// ==UserScript==
// @name        Enhanced Ticker: Autoupdate threads
// @namespace   MrBrax
// @description	Updates threads automatically if the ticker is open. Requires "Enhanced Ticker" 3.32 or upwards.
// @include     https://facepunch.com/showthread.php?*
// @version     0.12
// @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 + " Year" + ( interval != 1 ? "s" : "");
    }
    interval = Math.floor(seconds / 2592000);
    if (interval > 1) {
        return interval + " Month" + ( interval != 1 ? "s" : "");
    }
    interval = Math.floor(seconds / 86400);
    if (interval > 1) {
        return interval + " Day" + ( interval != 1 ? "s" : "");
    }
    interval = Math.floor(seconds / 3600);
    if (interval > 1) {
        return interval + " Hour" + ( interval != 1 ? "s" : "");
    }
    interval = Math.floor(seconds / 60);
    if (interval > 0) {
        return interval + " Minute" + ( interval != 1 ? "s" : "");
    }
    return Math.floor(seconds) + " second" + ( seconds != 1 ? "s" : "");
}

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();

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(true);
				}
			};
			xhr.send();
		}
	}
};
window.addEventListener("storage", storageHandler, false);