Skip to content

Instantly share code, notes, and snippets.

@showyou
Created November 7, 2009 09:58
Show Gist options
  • Save showyou/228632 to your computer and use it in GitHub Desktop.
Save showyou/228632 to your computer and use it in GitHub Desktop.
/**
* 猫化ブックマークレット
*
* 使用方法は、変換を適用させたいページをブラウザに表示させてから
* アドレス欄に
* javascript:(function(){var u="http://ブックマークレット設置箇所";var d=docume
nt;var s=d.createElement('script');s.charset="UTF-8";s.src=u;d.body.appendChild(
s);})()
* と入力します。
*/
// 名前空間用のオブジェクト window.Inajob が存在しなければ定義
if (!("Inajob" in window))
{
Inajob = {};
}
// 名前空間用のオブジェクト window.Inajob.Bookmarklet が存在しなければ定義
if (!("Bookmarklet" in window.Inajob))
{
Inajob.Bookmarklet = {};
}
/**
* HTML 文章を猫化するブックマークレットの新しいインスタンスを初期化します。
*/
window.Inajob.Bookmarklet.Arisyu = function ()
{
this.filters = [];
}
/**
* 指定した要素に変換フィルタを適用します。
*
* @param HTMLElement element 対象の要素。
* @param bool recursive 子要素にも再帰的に適用するなら TRUE、しないなら FALSE。
*/
window.Inajob.Bookmarklet.Arisyu.prototype.filter = function (element, recursive
)
{
for (var i = 0, iLast = this.filters.length; i < iLast; i++)
{
var f = this.filters[i];
f.apply(element);
if (recursive)
{
for (var j = 0, jLast = element.childNodes.length; j < jLast; j++)
{
var child = element.childNodes[j];
this.filter(child, recursive);
}
}
}
}
/**
* 指定した要素が持つ全ての子要素のうち、変換対象の要素のテキストを猫化します。
* 子要素は再帰的に列挙されます。
*
* @param HTMLElement element 対象の要素。
*/
window.Inajob.Bookmarklet.Arisyu.prototype.apply = function (container)
{
this.filter(container, true);
}
/**
* 正規表現により文章を置換するフィルタの新しいインスタンスを初期化します。
*/
window.Inajob.Bookmarklet.Arisyu.RegExpFilter = function ()
{
/**
* 置換候補の正規表現と結果の辞書
*/
this.dictionaries = [
/*{
"pattern": "(ありま(す|せん))",
"option": "gm",
"replace": "ありん$2"
},*/
{
"pattern": "(まし([^ろ]))|まし$",
"option": "gm",
"replace": "ましろ色シンフォニー$2"
},
/*{
"pattern": "(まし|)(た|る)(、|。)",
"option": "gm",
"replace": "$2んじゃ$3"
},*/
];
/**
* 正規表現オブジェクトのキャッシュ
*/
this.regexpCache = [];
}
/**
* 指定された要素がフィルタの適用対象かどうかを取得します。
*
* @param HTMLElement 調べる対象の要素。
* @return bool 適用対象なら TRUE、対象外なら FALSE。
*/
window.Inajob.Bookmarklet.Arisyu.RegExpFilter.prototype.isAcceptElement = functi
on (element)
{
var isAccept;
switch (element.nodeName.toLowerCase())
{
case 'style':
case 'script':
case 'frame':
case 'code':
isAccept = false;
break;
default:
isAccept = true;
}
return isAccept;
}
/**
* 指定された要素へフィルタを適用します。
*
* @param HTMLElement 適用対象の要素。
*/
window.Inajob.Bookmarklet.Arisyu.RegExpFilter.prototype.apply = function (elemen
t)
{
if ("parentNode" in element && element.parentNode != null)
{
if (!this.isAcceptElement(element.parentNode))
{
return;
}
}
if (element.nodeName.toLowerCase() != "#text")
{
return;
}
var nodeValue = element.nodeValue;
for (var i = 0, iLast = this.dictionaries.length; i < iLast; i++)
{
var dictionary = this.dictionaries[i];
if (!(i in this.regexpCache))
{
this.regexpCache[i] = new RegExp(dictionary.pattern, dictionary.option);
}
var regexp = this.regexpCache[i];
nodeValue = nodeValue.replace(regexp, dictionary.replace);
}
element.nodeValue = nodeValue;
}
////////////////////////////////////////////////////////////////////////////
// ここから実行
////////////////////////////////////////////////////////////////////////////
var arisyu = new Inajob.Bookmarklet.Arisyu();
// フィルタ設定
arisyu.filters = [
new Inajob.Bookmarklet.Arisyu.RegExpFilter()
];
// トップ レベルの文章に適用
arisyu.apply(window.document);
// フレームがあればそちらも適用
if ("frames" in window)
{
for (var i = 0, iLast = window.frames.length; i < iLast; i++)
{
//arisyu.apply(window.frames[i].document);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment