Skip to content

Instantly share code, notes, and snippets.

@think49
Created May 16, 2011 11:29
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save think49/974272 to your computer and use it in GitHub Desktop.
Save think49/974272 to your computer and use it in GitHub Desktop.
google-highlight.js: Google検索結果の検索語をハイライトします。Proxomitronフィルタ用。
---------------------------------------------------------------------
Google: High Light by js
---------------------------------------------------------------------
概要
Google検索結果で検索語を最大7つまでハイライトします。(要 JavaScript)
仕様
- JavaScript を利用しているため、ブラウザの JavaScript を有効にしてください。
- インスタント検索にも対応するため、HTML5 規定の hashchange イベントを利用しています。最近のブラウザは実装していますが、IE7- はサポートしていないようです。
- 検索語は、半角スペース()|:*% 及び、接頭語の -+~ を除いた単語として解釈されます
- フレーズ検索に対応しています。
- 基本的なアルゴリズムは「Google: High Light」を元にしているので性質もほぼ同じです。
使い方
1. "GoogleSearch.txt" を Proxomitron の「Lists」フォルダへコピー
2. "google-highlight.js" を Proxomitron の「html」フォルダへコピー
3. 下記をクリップボードにコピー
4. Proxomitron 起動
5. [Webページ] のリスト内の任意の場所で右クリック
6. [フィルタをクリップボードからインポート]
---------------------------------------------------------------------
[Blocklists]
List.GoogleSearch = "..\Lists\GoogleSearch.txt"
[Patterns]
Name = "Google: High Light by js [2011/05/15]"
Active = TRUE
URL = "$LST(GoogleSearch)$TYPE(htm)"
Limit = 256
Match = "(^(^</head>))$STOP()"
Replace = "<script type="text/javascript" src="http://local.ptron/google-highlight.js"></script>"
---------------------------------------------------------------------
/**
* google-highlight.js
* Google search words are highlighted.
*
* @version 1.0.3
* @author think49
* @url https://gist.github.com/974272
* @license http://www.opensource.org/licenses/mit-license.php (The MIT License)
* @see http://d.hatena.ne.jp/think49/20110516/1305541739
* @see <a href="http://www.google.com/instant/">Google Instant</a>
*/
(function () {
function highLight (doc, string) {
var _RegExp, reg, result, words, word, backgroundColors, elements, elementsList, element, style, i, j, k, l, m;
_RegExp = RegExp;
words = [];
reg = /\u0022([^\u0022]+)\u0022|[^\u0020!%(-*:\u003C-@|]+/g;
reg.lastIndex = 0;
while (result = reg.exec(string)) {
words.push(new _RegExp('^' + (typeof result[1] === 'string' && result[1].length > 0 ? result[1] : result[0]) + '$', 'i'));
}
backgroundColors = ['#bbeeff', '#ffddaa', '#88ebaa', '#ccbbff', '#ffaaaa', '#99ccff', '#eebbaa'];
elementsList = 'querySelectorAll' in doc ? [doc.querySelectorAll('em,b')] : [doc.getElementsByTagName('b'), doc.getElementsByTagName('em')];
k = elementsList.length;
while (k--) {
elements = elementsList[k];
for (i = 0, l = elements.length, m = words.length; i < l; ++i) {
element = elements[i];
word = element.textContent || element.innerText;
for (j = 0; j < m; ++j) {
if (words[j].test(word)) {
style = element.style;
style.backgroundColor = backgroundColors[j];
style.fontWeight = 'normal';
style.fontStyle = 'normal';
break;
}
}
}
}
}
function handleLoad (event) {
var _location, reg, query;
_location = location;
reg = /^(?:(?!q=)\w+=[^&]*\&)*q=([^&]*)/;
reg.lastIndex = 0;
query = reg.exec(_location.search.slice(1));
if (!query) {
reg.lastIndex = 0;
query = reg.exec(_location.hash.slice(1));
if (event.type === 'DOMContentLoaded') {
return addEventListener('load', handleLoad, false);
}
}
if (!query) {
return;
}
highLight(event.target || document, decodeURIComponent(query[1].replace(/\u002B/g, '\u0020')));
}
function handleHashchange (event) { // for Google Instant
var query, target;
query = /^(?:(?!q=)\w+=[^&]*\&)*q=([^&]*)/.exec(location.hash.slice(1));
if (!query) {
return;
}
target = event.target;
highLight(target ? target.document : document, decodeURIComponent(query[1].replace(/\u002B/g, '\u0020')));
}
function handleUnload (event) {
if (typeof removeEventListener === 'function') {
event.target.removeEventListener('DOMContentLoaded', handleLoad, false);
removeEventListener('load', handleLoad, false);
removeEventListener('hashchange', handleHashchange, false);
removeEventListener('unload', handleUnload, false);
} else if (typeof detachEvent === 'object') {
detachEvent('onload', handleLoad);
detachEvent('onhashchange', handleHashchange);
detachEvent('onunload', handleUnload);
}
}
if (typeof addEventListener === 'function') {
document.addEventListener('DOMContentLoaded', handleLoad, false);
addEventListener('hashchange', handleHashchange, false);
addEventListener('unload', handleUnload, false);
} else if (typeof attachEvent === 'object') {
attachEvent('onload', handleLoad);
attachEvent('onhashchange', handleHashchange);
attachEvent('onunload', handleUnload);
}
})();
# GoogleSearch.txt
#
# @reference http://www.hyperposition.com/google/server.html
www.google.co(m|.jp)/(search\?|custom\?|webhp\?)
images.google.co(m|.jp)/images\?
news.google.co(m|.jp)/news\?(^edit=)
groups.google.co(m|.jp)/groups\?
blogsearch.google.co(m|.jp)/blogsearch\?
# local.google.co(m|.jp)/local\?
maps.google.co(m|.jp)/maps\?
www.google.com/codesearch\?
gfe.google.com/search\?
gfe-[a-z0-9]+{2,3}.google.com/search\?
216.239.[#32:63].[#0:255]/search(^(^\?))*[?&]q=(^cache:)
64.233.[#160:191].[#0:255]/search(^(^\?))*[?&]q=(^cache:)
66.102.[#0:15].[#0:255]/search(^(^\?))*[?&]q=(^cache:)
66.249.[#64:95].[#0:255]/search(^(^\?))*[?&]q=(^cache:)
72.14.[#192:239].[#0:255]/search(^(^\?))*[?&]q=(^cache:)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment