Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save negipo/114605 to your computer and use it in GitHub Desktop.
Save negipo/114605 to your computer and use it in GitHub Desktop.
// ==UserScript==
// @name IncrementalFilter4GoogleSearch
// @namespace http://polog.org/
// @include http://www.google.co.jp/search*
// ==/UserScript==
// modified http://userscripts.org/scripts/review/48667
(function() {
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#IF4G { padding:5px 10px 5px 20px; position:fixed; bottom:0px; right:0px; } \n"
css += "input#IF4G_input { type: text; }"
Style.add(css);
var IF4G = function() {};
IF4G.nodes = (function(){
return $X(".//li[contains(concat(' ',@class,' '),'g')]");
})();
IF4G.iface = function(){
this.input = $X('.//input[@name="q"]')[0];
this.string_buffer = this.input.value;
this.input.addEventListener("keyup", function(e){
IF4G.search();
},false);
}
IF4G.addFilter = function(){
window.AutoPagerize.addFilter(function(p){
IF4G.nodes = $X(".//li[contains(concat(' ',@class,' '),'g')]");
IF4G.search();
});
}
IF4G.search = function(){
var str = this.input.value.replace(new RegExp('^' + this.string_buffer), '');
var ary = str.split(/[\s ]/);
var nodes = IF4G.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";
});
}
if(window.AutoPagerize){
IF4G.addFilter();
}
IF4G.iface();
//=== 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."));
}
}
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment