hail2u (owner)

Revisions

gist: 122353 Download_button fork
public
Public Clone URL: git://gist.github.com/122353.git
Embed All Files: show embed
highlight-search-terms.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
/**
* highlight-search-terms.js
*
* Copyright (c) 2009 Kyo Nagashima <kyo@hail2u.net>
* This library licensed under MIT license:
* http://opensource.org/licenses/mit-license.php
*/
$(function () {
  var ref = document.referrer;
 
  if (ref) {
    var referrerPattern = [
      "^http://www\.google\.com.+[&?]q=([^&]+).*$",
      "^http://www\.google\.co\.jp.+[&?]q=([^&]+).*$",
      "^http://search\.yahoo\.com.+[&?]p=([^&]+).*$",
      "^http://search\.yahoo\.co\.jp.+[&?]p=([^&]+).*$",
      "^http://www\.bing\.com.+[&?]q=([^&]+).*$",
      "^http://hail2u\.net.+[&?]q=([^&]+).*$"
    ];
    var unsafechars = /[!-*,-\/:-@[-`{-~]/g;
 
    // extract words from referrer URL
    var words;
    $.each(referrerPattern, function () {
      var pattern = new RegExp(this, "i");
 
      if (pattern.exec(ref)) {
        var query = decodeURIComponent(RegExp.$1).replace(unsafechars, "+").replace(/^\+*(.*?)\+*$/, "$1").replace(/\++/g, "|");
        words = new RegExp("(" + query + ")", "gi");
        return false; // break $.each
      }
    });
 
    // Hilight words
    try {
      $("#contents > .section *").not("iframe").contents().each(function () {
        if (this.nodeType === 3) {
          var s = this.nodeValue.replace(words, "<em class=\"highlight\">$1</em>");
          $(this).replaceWith(s);
        }
      });
    } catch (e) {
      // console.log(e.message);
    }
  }
});
 
jquery.highlight-search-terms.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
/**
* jquery.highlight-search-terms.js - version 0.1
* Highlight search words in referrer URL.
*
* Copyright (c) 2009 Kyo Nagashima <kyo@hail2u.net>
* This library licensed under MIT license:
* http://opensource.org/licenses/mit-license.php
*/
(function($) {
  $.fn.highlightSearchTerms = function (options) {
    var o = $.extend({}, $.fn.highlightSearchTerms.defaults, options);
    $.merge(o.referrerPatterns, $.fn.highlightSearchTerms.builtinReferrerPatterns);
    var ref = document.referrer;
 
    if (ref) {
      var words = $.fn.highlightSearchTerms.extractWordsFromReferrer(ref, o);
 
      // Highlight words
      this.find(":not(iframe)").contents().each(function () {
        if (this.nodeType === 3) {
          var s = this.nodeValue.replace(words, "<em class=\"" + o.className + "\">$1</em>");
          $(this).replaceWith(s);
        }
      });
    }
 
    return this;
  };
 
  // Extract words from referrer
  $.fn.highlightSearchTerms.extractWordsFromReferrer = function (ref, o) {
    var words;
 
    $.each(o.referrerPatterns, function () {
      var pattern = new RegExp(this, "i");
 
      if (pattern.exec(ref)) {
        var unsafe = new RegExp(o.unsafeChars, "g");
        var query = decodeURIComponent(RegExp.$1).replace(unsafe, "+").replace(/^\+*(.*?)\+*$/, "$1").replace(/\++/g, "|");
        words = new RegExp("(" + query + ")", "gi");
        return false; // break $.each
      }
    });
 
    return words;
  };
 
  $.fn.highlightSearchTerms.defaults = {
    className: "highlight",
    referrerPatterns: [],
    unsafeChars: "[!-*,-/:-@[-`{-~]"
  };
 
  $.fn.highlightSearchTerms.builtinReferrerPatterns = [
    "^http://www\.google\.com.+[&?]q=([^&]+).*$",
    "^http://www\.google\.co\.jp.+[&?]q=([^&]+).*$",
    "^http://search\.yahoo\.com.+[&?]p=([^&]+).*$",
    "^http://search\.yahoo\.co\.jp.+[&?]p=([^&]+).*$",
    "^http://www\.bing\.com.+[&?]q=([^&]+).*$"
  ];
})(jQuery);