IncrementalFilter4LDRize

オモシロイ。インクリメンタル・サーチ。

ちょっとだけ改造した。検索窓を opacity low にしてみた。本当は :hover で display:block するほうがいいかなと思ったんだけど。これは他にもつかえそうだな。LDRize の SITEINFO を利用してるってところが素敵。あと addEventListener のところとか、オレ全然わかってないところだからこれは良く見ておこー。

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

(function(){
	if(!document.getElementById("gm_ldrize")) return;
	var Style = function(){};
	Style.add = function(css){
		var link = document.createElement('link');
		link.rel = 'stylesheet';
		link.href = 'data:text/css,' + escape(css);
		document.documentElement.childNodes[0].appendChild(link);
	}

	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; }"
	Style.add(css);

 	var IF4L = function() {};
	IF4L.nodes = (function(){
		return $X(window.LDRize.getSiteinfo().paragraph);
	})();
 	IF4L.iface = function(){
		var root = document.createElement("div");
		root.setAttribute("id","IF4L");
		var title = document.createTextNode("search:")
		root.appendChild(title);
		var input = document.createElement("input");
		input.setAttribute("id","IF4L_input");
		input.addEventListener("keyup", function(e){
			IF4L.search();
		},false);
		root.appendChild(input);
		document.body.appendChild(root);
 	}
	IF4L.addFilter = function(){
		window.AutoPagerize.addFilter(function(p){
			IF4L.nodes = $X(window.LDRize.getSiteinfo().paragraph);
			IF4L.search();
		});
	}
	IF4L.search = function(){
		var str = $X("id('IF4L_input')")[0].value;
		var ary = str.split(" ");
		var nodes = IF4L.nodes;
		nodes.forEach(function(node){
			var match = true;
			var content = node.textContent;
			ary.forEach(function(s){
				var re = new RegExp(s, "i");
				if(!content.match(re))
					match = false;
			});
			if(!match)
				node.style.display="none";
			else
				node.style.display="block";
		});
	}
	IF4L.iface();
	if(window.AutoPagerize){
	  	IF4L.addFilter();
	}

	//=== extend version of $X ===
	/* $X(exp);
       $X(exp, context);
       $X(exp, type);
       $X(exp, context, type);
       http://coderepos.org/share/browser/lang/javascript/userscripts/jautopagerize.user.js?rev=1966 */
	function $X (exp, context, type /* want type */) {
		if (arguments.callee.forceRelative || navigator.userAgent.indexOf("Safari/523.12") != -1)
			exp = exp.replace(/id\(\s*([\"\'])([^\"\']+)\1\s*\)/g, '//*[@id="$2"]');
		if (arguments.callee.forceRelative)
			exp = exp.indexOf("(//") == 0
			? "(.//" + exp.substring(3)
			: (exp[0] == "/" ? "." : "./") + exp;
		if (typeof context == "function") {
			type = context;
			context = null;
		}
		if (!context) context = document;
		exp = (context.ownerDocument || context).createExpression(exp, function (prefix) {
			return document.createNSResolver((context.ownerDocument == null ? context
											  : context.ownerDocument).documentElement)
				.lookupNamespaceURI(prefix) || document.documentElement.namespaceURI;
		});
		switch (type) {
		case String:
			return exp.evaluate(context, XPathResult.STRING_TYPE, null).stringValue;
		case Number:
			return exp.evaluate(context, XPathResult.NUMBER_TYPE, null).numberValue;
		case Boolean:
			return exp.evaluate(context, XPathResult.BOOLEAN_TYPE, null).booleanValue;
		case Array:
			var result = exp.evaluate(context, XPathResult.ORDERED_NODE_SNAPSHOT_TYPE, null);
			var ret = [];
			for (var i = 0, len = result.snapshotLength; i < len; i++) {
				ret.push(result.snapshotItem(i));
			}
			return ret;
		case undefined:
			var result = exp.evaluate(context, XPathResult.ANY_TYPE, null);
			switch (result.resultType) {
			case XPathResult.STRING_TYPE : return result.stringValue;
			case XPathResult.NUMBER_TYPE : return result.numberValue;
			case XPathResult.BOOLEAN_TYPE: return result.booleanValue;
			case XPathResult.UNORDERED_NODE_ITERATOR_TYPE: {
				/* not ensure the order. */
				var ret = [];
				var i = null;
				while (i = result.iterateNext()) {
					ret.push(i);
				}
				return ret;
			}
			}
			return null;
		default:
			throw(TypeError("$X: specified type is not valid type."));
		}
	}
})();