PageSourceSearch

https://jorritsma.cc/home/chrome/wikiautocomplete/js/wikiautocomplete.js

js jorritsma.cc collected 2026-09-29 21:45:09 UTC 5,163 bytes, 137 lines download raw bytes

1jQuery(document).ready(function($) {
2    if (Array.prototype.forEach === undefined)
3        return;
4
5    var cache = {};
6
7    function escape_newvalue(value) {
8        return value.replace(/\$/g, '$$$$');
9    }
10
11    function context(text) {
12        return text.replace(/\{{3}(?:.|\n)*?(?:\}{3}|$)/g, '{{{}}}')
13                   .replace(/`[^`\n]*(?:`|$)/gm, '``');
14    }
15
16    function search(strategy) {
17        if (strategy.cache) {
18            return function(term, callback) {
19                function invoke_callback(resp) {
20                    var matches = $.grep(resp, function(item) {
21                        return item.value.toString().substr(0, term.length) === term;
22                    });
23                    if (matches.length === 1) {
24                        matches[0].single_match = true;
25                    }
26                    callback(matches);
27                }
28                if (cache[strategy.name] !== undefined) {
29                    invoke_callback(cache[strategy.name]);
30                    return;
31                }
32                var data = {
33                    realm: wikiautocomplete.realm,
34                    id: wikiautocomplete.id
35                };
36                $.getJSON(wikiautocomplete.url + '/' + strategy.name, data)
37                    .done(function(resp) {
38                        cache[strategy.name] = resp;
39                        invoke_callback(resp);
40                    })
41                    .fail(function() { callback([]) });
42            };
43        } else {
44            return function (term, callback) {
45                var data = {
46                    q: term,
47                    realm: wikiautocomplete.realm,
48                    id: wikiautocomplete.id
49                };
50                $.getJSON(wikiautocomplete.url + '/' + strategy.name, data)
51                    .done(function (resp) { callback(resp); })
52                    .fail(function () { callback([]); });
53            };
54        }
55    }
56
57    function template(strategy) {
58        var prefix = strategy.template_prefix || '';
59        var suffix = strategy.template_suffix || '';
60        return function (item) {
61            var fmt = '<span class="wikiautocomplete-menu-descr">$1</span>';
62            var value = prefix + item.value + suffix;
63            var text = $.htmlEscape(value);
64            if (item.single_match && item.details_html) {
65                text += $.format('<div>$1</div>', item.details_html);
66            }
67            else if (item.title_html) {
68                text += $.format(fmt, item.title_html);
69            }
70            else if (item.title && item.title !== value) {
71                text += $.htmlFormat(fmt, item.title);
72            }
73            return text;
74        };
75    }
76
77    function replace(strategy) {
78        var prefix = strategy.replace_prefix || '';
79        var suffix = strategy.replace_suffix || '';
80        var end = strategy.replace_end;
81        var quote = strategy.quote_whitespace;
82        return function (item) {
83            var value = item.value.toString();
84            if (quote && /\s/.test(value))
85                value = '"' + value + '"';
86            value = escape_newvalue(value);
87            value = prefix + value + suffix;
88            if (end) return [value, end]; 
89            else return value;
90        };
91    }
92
93    var TextareaAdapter = $.fn.textcomplete.Textarea;
94    function Adapter(element, completer, option) {
95        this.initialize(element, completer, option);
96    }
97    $.extend(Adapter.prototype, TextareaAdapter.prototype, {
98        _skipSearchOrig: TextareaAdapter.prototype._skipSearch,
99        _skipSearch: function (clickEvent) {
100            if (clickEvent.keyCode === 9)
101                return;  // do not skip TAB key
102            return this._skipSearchOrig(clickEvent);
103        }
104    });
105
106    var strategies = wikiautocomplete.strategies.map(function (strategy) {
107        var result = {
108            match: new RegExp(strategy.match, strategy.match_flags),
109            search: search(strategy),
110            index: strategy.index,
111            template: template(strategy),
112            replace: replace(strategy),
113            cache: true
114        };
115        if (strategy.name != 'processor') result.context = context;
116        return result;
117    });
118    var options = {
119        appendTo: $('body'),
120        adapter: Adapter,
121        maxCount: 10000
122    };
123    $('textarea.wikitext').attr("autocomplete", "off").textcomplete(strategies, options);
124    $('input[type="text"].wikitext').attr("autocomplete", "off").textcomplete(strategies, options);
125
126    if (/^1\.[78]\./.test($.fn.jquery) && $.browser.mozilla &&
127        navigator.userAgent.indexOf('like Gecko') === -1 /* is not IE 11 */)
128    {
129        var margin = $('body').css('margin-top');
130        if (margin && margin !== '0px') {
131            if (!/px$/.test(margin))
132                margin += 'px';
133            $('<style type="text/css">.dropdown-menu { margin-top: ' +
134              margin + ' !important }</style>').appendTo('head');
135        }
136    }
137});

Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.