PageSourceSearch

https://www.aprs4r.org/trac/chrome/common/js/search.js

js aprs4r.org collected 2026-09-28 11:16:51 UTC 3,284 bytes, 90 lines download raw bytes

1(function($){
2
3  /* Adapted from http://www.kryogenix.org/code/browser/searchhi/ */
4  $.fn.highlightText = function(text, className, caseSensitive) {
5    function highlight(node) {
6      if (node.nodeType == 3) { // Node.TEXT_NODE
7        var val = node.nodeValue;
8        var pos = (caseSensitive ? val : val.toLowerCase()).indexOf(text);
9        if (pos >= 0 && !$(node.parentNode).hasClass(className)) {
10          var span = document.createElement("span");
11          span.className = className;
12          var txt = document.createTextNode(val.substr(pos, text.length));
13          span.appendChild(txt);
14          node.parentNode.insertBefore(span, node.parentNode.insertBefore(
15            document.createTextNode(val.substr(pos + text.length)),
16              node.nextSibling));
17          node.nodeValue = val.substr(0, pos);
18        }
19      } else if (!$(node).is("button, select, textarea")) {
20        $.each(node.childNodes, function() { highlight(this) });
21      }
22    }
23    return this.each(function() { highlight(this) });
24  };
25
26  $(function() {
27    $("p.filters").exclusiveOnClick(":checkbox, :checkbox + label");
28
29    var elems = $(".searchable");
30    if (!elems.length) return;
31
32    function getSearchTerms(url) {
33      if (url.indexOf("?") == -1) return [];
34      var params = url.substr(url.indexOf("?") + 1).split("&");
35      for (var p in params) {
36        var param = params[p].split("=");
37        if (param.length < 2) continue;
38        if (param[0] == "q" || param[0] == "p") {// q= for Google, p= for Yahoo
39          var query = decodeURIComponent(param[1].replace(/\+/g, " "));
40          if (query[0] == "!") query = query.slice(1);
41          var terms = [];
42          $.each(query.split(/(".*?"|'.*?'|\s+)/), function() {
43            if (terms.length < 10) {
44              var term = this.replace(/^\s+$/, "")
45                         .replace(/^['"]/, "")
46                         .replace(/['"]$/, "");
47              if (term.length >= 3)
48                terms.push(term);
49            }
50          });
51          return terms;
52        }
53      }
54      return [];
55    }
56
57    var terms = getSearchTerms(document.URL);
58    if (!terms.length) terms = getSearchTerms(document.referrer);
59    if (terms.length) {
60      $.each(terms, function(idx) {
61        elems.highlightText(this.toLowerCase(), "searchword" + (idx % 5));
62      });
63    } else {
64      function scrollToHashSearchMatch() {
65        var h = window.location.hash;
66        var direction = h[1];
67        var case_insensitive = h.match(/\/i$/);
68        if (direction == '/' || direction == '?') {
69          var hterm = h.substr(2);
70          if (case_insensitive)
71            hterm = hterm.substr(0, hterm.length - 2).toLowerCase();
72          $('.searchword0').each(function() {
73            $(this).after($(this).html()).remove();
74          });
75          elems.highlightText(hterm, "searchword0", !case_insensitive);
76          var hmatches = $('.searchword0');
77          if (direction == '?')
78            hmatches = hmatches.last();
79          hmatches.first().each(function() {
80            var offset = $(this).offset().top;
81            window.scrollTo(0, offset);
82          });
83        }
84      }
85      window.onhashchange = scrollToHashSearchMatch;
86      scrollToHashSearchMatch();
87    }
88  });
89
90})(jQuery);

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.