2006-11-21

anond pickup of the day

以下の二つに留意、つまり乱用禁止。

取説未満

// ==UserScript==
// @name	anond pickup of the day
// @namespace	http://anond.hatelabo.jp/
// @description	pickup section of the day at Hatelabo::AnonymousDiary
// @include	http://anond.hatelabo.jp/2*
// ==/UserScript==
(function(){
	var trackbackThreshold = 10;
	var ignoreList = {
		"/20070801172335": 33,
		"/20070806163721": 10,
	};

	// only section of the day
	if (! location.pathname.match(/^\/\d{8}$/)) {
		return;
	}

	// regist ancher that kick main routine
	var a = document.createElement("a");
	a.href = "#";
	a.innerHTML = "pickup of the day";
	a.addEventListener("click", grab, false);
	var firstPager_l = document.evaluate("//div[@class='pager-l']",document,null,XPathResult.FIRST_ORDERED_NODE_TYPE,null).singleNodeValue;
	firstPager_l.appendChild(a);

	function Outline() {
		this.outline = document.createElement("ul");
		this.text = document.createElement("textarea");
		this.text.style.overflow = "auto";
		this.text.style.width = "100%";
		this.text.style.height = "15em";
		this.text.innerHTML = "</ul><\n><ul>\n";
		this.list = new Array();
	}
	Outline.prototype.setup = function() {
		var parent = document.getElementById("body");
		parent.insertBefore(this.outline, parent.firstChild);
		parent.insertBefore(this.text, parent.firstChild);
	}
	Outline.prototype.append = function(section) {
		var h3 = section.getElementsByTagName("h3")[0];
		var sectionName = h3.firstChild.pathname.replace(/\//,"");
		var sectionText = h3.textContent.replace(/\s*$/,"");
		if (sectionText == "\u25a0") {
			sectionText = sectionName;
		}
		this.text.innerHTML +=
			'<h2>[http://anond.hatelabo.jp/'+sectionName+
			":title="+sectionText.replace(/^\u25a0/,"").replace(/]/g,"&amp;#93;")+"] "+
			'<a href="http://b.hatena.ne.jp/entry/http://anond.hatelabo.jp/'+sectionName+'">'+
			'<img src="http://b.hatena.ne.jp/entry/image/http://anond.hatelabo.jp/'+sectionName+'">'+
			"</a></h2>\n";
		h3.firstChild.name = sectionName;
		var li = document.createElement("li");
		li.innerHTML =
			'<a href="#'+sectionName+'">' +
			sectionText.replace(/&amp;/g,"&amp;").replace(/</g,"&lt;").replace(/>/g,"&gt;") +
			"</a>";
		var nextSibling = 0;
		for (var i in this.list) {
			if (nextSibling < i &amp;&amp; i < sectionName) {
				nextSibling = i;
			}
		}
		if (nextSibling) {
			this.outline.insertBefore(li, this.list[nextSibling]);
		} else {
			this.outline.appendChild(li);
		}
		this.list[sectionName] = li;
	}
	var outline = new Outline();

	function CC(day) {
		this.li = document.createElement("li");
		this.li.innerHTML = '<a href="/'+day+'" target="_blank">' + day + "</a>";
		this.day = day;
		this.n = new Array();
	}
	CC.prototype.pp = function(id) {
		this.n.push(id);
		if (this.n.length > 10) {
			this.li.innerHTML = '<a href="/'+this.day+'" target="_blank">' + this.day + "</a> " + this.n.length;
		} else {
			this.li.innerHTML += ' <a href="/'+id+'" target="_blank">*</a>';
		}
	}

	function Count() {
		this.count = document.createElement("ul");
		this.list = new Array();
	}
	Count.prototype.setup = function() {
		var parent = document.getElementById("body");
		parent.insertBefore(this.count, parent.firstChild);
	}
	Count.prototype.append = function(day, id) {
		var nextSibling = 0;
		for (var i in this.list) {
			if (nextSibling < i &amp;&amp; i <= day) {
				nextSibling = i;
			}
		}
		if (nextSibling == day) {
			this.list[nextSibling].pp(id);
		} else {
			var cc = new CC(day);
			if (nextSibling) {
				this.count.insertBefore(cc.li, this.list[nextSibling].li);
			} else {
				this.count.appendChild(cc.li);
			}
			this.list[day] = cc;
			cc.pp(id);
		}
	}
	Count.prototype.appendSection = function(section) {
		var id = section.getElementsByTagName("h3")[0].firstChild.pathname.replace(/\//,"");
		var today = id.match(/\d{8}/)[0];
		var anchors = section.getElementsByTagName("a");
		for (var i=0; i<anchors.length; i++) {
			if (anchors[i].href &amp;&amp; anchors[i].host == "anond.hatelabo.jp" &amp;&amp; anchors[i].pathname.match(/\/(\d{8})\d{6}/) &amp;&amp; RegExp.$1 != today) {
				this.append(RegExp.$1, id);
			}
		}
	}
	var count = new Count();

	function Hide(){}
	Hide.prototype.setup = function() {
		this.style = document.createElement("style");
		this.style.id = "hide";
		this.style.type = "text/css";
		document.getElementsByTagName("head")[0].appendChild(this.style);
		var self = this;
		this.a = new Object();
		this.a.visible = document.createElement("a");
		this.a.visible.id = "visible";
		this.a.visible.href = "#";
		this.a.visible.innerHTML = "visible hide section";
//		this.a.visible.setAttribute("onclick","document.getElementById('hide').innerHTML = 'div.hide {display: block}';document.getElementById('visible').style.display = 'none';document.getElementById('unvisible').style.display = 'inline';");
		this.a.visible.addEventListener("click", function(){self.visible()}, false);
		firstPager_l.parentNode.insertBefore(this.a.visible, firstPager_l);
		this.a.unvisible = document.createElement("a");
		this.a.unvisible.id = "unvisible";
		this.a.unvisible.href = "#";
		this.a.unvisible.innerHTML = "unvisible hide section";
//		this.a.visible.setAttribute("onclick","document.getElementById('hide').innerHTML = 'div.hide {display: none}';document.getElementById('visible').style.display = 'inline';document.getElementById('unvisible').style.display = 'none';");
		this.a.unvisible.addEventListener("click", function(){self.unvisible()}, false);
		firstPager_l.parentNode.insertBefore(this.a.unvisible, firstPager_l);
		this.unvisible();
	}
	Hide.prototype.visible = function() {
		this.style.innerHTML = "div.hide {display: block}";
		this.a.visible.style.display = "none";
		this.a.unvisible.style.display = "inline";
	}
	Hide.prototype.unvisible = function() {
		this.style.innerHTML = "div.hide {display: none}";
		this.a.visible.style.display = "inline";
		this.a.unvisible.style.display = "none";
	}
	Hide.prototype.append = function(section) {
		if (section.className.match(/hide/)) {
			return;
		}
		section.className += " hide";
		count.appendSection(section);
	}
	Hide.prototype.is = function(section) {
		return section.className.match(/hide/);
	}
	var hide = new Hide();

	// main routine
	function grab(){
		if (! document.body.innerHTML.match(/<div class="pager-r">(\d+)/)) {
			return;
		}
		var pages = RegExp.$1 -0;
		if (pages <= 0 || pages > 40) { // check error and limit 1000 entry
			return;
		}
//pages = 2;

		firstPager_l.style.display = "none";
		outline.setup();
		hide.setup();
		count.setup();

		var mainbody = document.evaluate("//div[@class='body']", document,null,XPathResult.FIRST_ORDERED_NODE_TYPE,null).singleNodeValue;
		mainbody.innerHTML = "\n";
		for (var i=1; i<=pages; i++) {
			cat(mainbody, i);
		}
	}

	// page load and concatenate
	function cat(container, page) {
		container.innerHTML += "<!-- page " + page + " -->\n";
		GM_xmlhttpRequest({
			method: "GET",
			url: "http://anond.hatelabo.jp" + location.pathname + "?page=" + page,
			onload: function(result) {
				result.responseText.match(/<div class="body">((.|\s)*?)\s*<\/div>\s*<\/div>\s*<div class="pager-l">/);
				container.innerHTML = container.innerHTML.replace("<!-- page " + page + " -->", RegExp.$1);
				if (! container.innerHTML.match(/<!-- page \d+ -->/)) {
//					document.documentElement.innerHTML = document.documentElement.innerHTML.replace(/(src|href)=\"\//mg, "$1=\"http://anond.hatelabo.jp/");
					pickup();
				}
			}
		});
	}

	// pickup section at last cat() concatenate after
	function pickup() {
		var target = document.evaluate(
			"//div[@class='section' and child::*[not(@class='sectionfooter') and descendant::a[starts-with(@href,'http://anond.hatelabo.jp/2') or starts-with(@href,'/2') and not(child::span[@class='sanchor'])]]]",
			document,null,XPathResult.UNORDERED_NODE_SNAPSHOT_TYPE,null);
		for (var i=0; i<target.snapshotLength; i++) {
			hide.append(target.snapshotItem(i));
		}

		var tbs = document.evaluate(
			"//p[@class='sectionfooter']/a[2]",
			document,null,XPathResult.UNORDERED_NODE_SNAPSHOT_TYPE,null);
		for (var i=0; i<tbs.snapshotLength; i++) {
			var tb = tbs.snapshotItem(i);
			if (tb.innerHTML == "\u30c8\u30e9\u30c3\u30af\u30d0\u30c3\u30af(0)") {
				hide.append(tb.parentNode.parentNode);
			} else if (! hide.is(tb.parentNode.parentNode)) {
				trackback(tb);
			}
		}
	}

	// count trackbacks
	function trackback(tb) {
		GM_xmlhttpRequest({
			method: "GET",
			url: "http://anond.hatelabo.jp/" + tb.pathname.match(/\d{14}/),
			onload: function(result) {
				var link = result.responseText.match(/<a name="tb">(.|\s)*/)[0].match(/<li>\s*<a href="http:\/\/anond.hatelabo.jp\/\d{14}"/g);
				var n = link.length;
				for (var l in link) {
					var m = "/" + link[l].match(/\d{14}/);
					if (m in ignoreList) {
						n -= ignoreList[m];
					}
				}
				if (n < trackbackThreshold) {
					hide.append(tb.parentNode.parentNode);
				} else {
					tb.innerHTML = tb.innerHTML.replace(/\)$/, "/"+n+")");
					outline.append(tb.parentNode.parentNode);
				}
			}
		});
	}

})();

  • はてなキーワードのテストをしてみる。 職場 人間 仕事 日本 識字率 世界一 spam地図 辞書 最近 http://anond.hatelabo.jp/20061121024615

記事への反応(ブックマークコメント)

ログイン ユーザー登録
ようこそ ゲスト さん