IncrementalFilter4LDRize

// ==UserScript==
// @name           IncrementalFilter4LDRize
// @namespace      jimo1001
// @include        *
// ==/UserScript==

(function(){
	if(!document.getElementById("gm_ldrize")) return;
	var $X = window.Minibuffer.$X;
 	var IF4L = function(){
		this.nodes = this.getNodes();
	}
	IF4L.prototype.getNodes = function(){
		return $X(window.LDRize.getSiteinfo().paragraph);
	}
	IF4L.prototype.search = function(){
		var str = $X("id('IF4L_input')")[0].value;
		var ary = str.split(" ");
		this.nodes.forEach(function(node){
			var regFunc = function(s){
				return node.textContent.match(new RegExp(s, "i"));
			}
			node.style.display = (ary.some(regFunc)) ? "block" : "none";
		});
	}
	var addFilter = function(p){
		window.AutoPagerize.addFilter(function(){
			p.nodes = p.getNodes();
			p.search();
		});
	}
	var addStyle = function(css){
		var link = document.createElement('link');
		link.rel = 'stylesheet';
		link.href = 'data:text/css,' + escape(css);
		document.documentElement.childNodes[0].appendChild(link);
	}
	var setEvent = function(i, p){
		window.Minibuffer.addShortcutkey({
			key: 'C-c f',
			description: 'IncrementalFilter4LDRize',
			command: function(e){
				i.focus();
			}
		});
		var setOpacity = function(num){
			i.parentNode.setAttribute("style","opacity:"+num);
		}
		i.addEventListener("keyup", function(){p.search()}, false);
		i.addEventListener("focus", function(e){setOpacity(1.0)}, false);
		i.addEventListener("blur", function(e){setOpacity(null)}, false);
	}
	var UI = function(){
		var css = "div#IF4L { opacity:0.2; padding:5px 10px 5px 20px; position:fixed; bottom:0px; right:0px; } \n";
		css += "div#IF4L:hover { opacity:1.0 } \n";
		css += "input#IF4L_input { type: text; }";
		var root = document.createElement("div");
		var title = document.createTextNode("絞り込み文字列: ");
		var input = document.createElement("input");
		addStyle(css);
		root.setAttribute("id","IF4L");
		input.setAttribute("id","IF4L_input");
		root.appendChild(title);
		root.appendChild(input);
		document.body.appendChild(root);
		return input;
	}
	if(document.body){
		var p = new IF4L()
		setEvent(UI(), p);
		if(window.AutoPagerize){
			addFilter(p);
		}
	}
})();