negipo (owner)

Revisions

gist: 114605 Download_button fork
public
Public Clone URL: git://gist.github.com/114605.git
Embed All Files: show embed
incrementalfilter4googlesearch_mod.user.js #
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
// ==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."));
        }
    }
})();