Skip to content

Instantly share code, notes, and snippets.

@hail2u
Created June 2, 2009 16:48
Show Gist options
  • Select an option

  • Save hail2u/122353 to your computer and use it in GitHub Desktop.

Select an option

Save hail2u/122353 to your computer and use it in GitHub Desktop.
/**
* 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 - 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);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment