showyou (owner)

Revisions

gist: 228632 Download_button fork
public
Public Clone URL: git://gist.github.com/228632.git
Embed All Files: show embed
JavaScript #
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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
/**
* 猫化ブックマークレット
*
* 使用方法は、変換を適用させたいページをブラウザに表示させてから
* アドレス欄に
* 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);
  }
}