forked from alice0775/userChrome.js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpatch_XULrubySupport.uc.js
251 lines (225 loc) · 8.26 KB
/
patch_XULrubySupport.uc.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
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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
// ==UserScript==
// @name Patch_XULrubySupport.uc.js
// @namespace http://space.geocities.yahoo.co.jp/gl/alice0775
// @description XUL/Ruby Suppot パッチ
// @include main
// @compatibility Firefox 3.0 - 9
// @author Alice0775
// @version 2011/09/16 sidebar 閉じたときエラーが出る
// @Note 3.0.2009060901用
// @Note rubysupport.add.ignore.always :trueとすることでデフォルトでルビ変換しない
// @Note rubysupport.add.allowexecute.site :変換を常に許可するサイトをスペース区切り
// ==/UserScript==
// @include chrome://browser/content/bookmarks/bookmarksPanel.xul
// @include chrome://browser/content/history/history-panel.xul
// @include chrome://browser/content/web-panels.xul
(function(){
if(typeof RubyService =='undefined' ) {
debug('XUL/Ruby Suppot: ','Not found.');
return;
}
try{
var func = RubyService.processRubyNodes.toSource();
func = func.replace(
'if (!aWindow.document) {return false;}',
<><![CDATA[
if (!aWindow || !aWindow.document) {return false;}
if (!Patch_XULrubySupport.allowexecute(aWindow))
return false;
]]></>
);
eval("RubyService.processRubyNodes = " + func);
func = RubyService.handleEvent.toSource();
func = func.replace(
'case "MozAfterPaint":',
<><![CDATA[
$&
return;
]]></>
);
eval("RubyService.handleEvent = " + func);
}catch(e){
debug('RubyService.processRubyNodes: ','Error, Could not be replace.');
}
function debug(aScript, aMsg){
const Cc = Components.classes;
Cc['@mozilla.org/consoleservice;1']
.getService(Ci.nsIConsoleService)
.logStringMessage(aScript + aMsg);
}
debug('RubyService.processRubyNodes: ','Successfully replaced.');
})();
var Patch_XULrubySupport = {
EXECUTEURL:[],
debug:function(aMsg){
const Cc = Components.classes;
Cc['@mozilla.org/consoleservice;1']
.getService(Ci.nsIConsoleService)
.logStringMessage(aMsg);
},
init: function(){
const kXULNS =
"http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul";
Patch_XULrubySupport.addPrefListener(Patch_XULrubySupport.PrefListener); // 登録処理
Patch_XULrubySupport.initEXECUTEURL();
var menu = document.createElementNS(kXULNS, "menu");
menu.setAttribute("id", "Patch_XULrubySupportMenu");
menu.setAttribute("label", "XHTML Ruby Support\u306e\u8a2d\u5b9a");
menu.setAttribute("accesskey", "r");
var optionsitem = document.getElementById("prefSep").nextSibling;
optionsitem.parentNode.insertBefore(menu, optionsitem);
var menupopup = document.createElementNS(kXULNS, "menupopup");
menupopup.setAttribute("onpopupshowing", "Patch_XULrubySupport.setMenuCheck();");
menupopup.setAttribute("menugenerated", "true");
menu.appendChild(menupopup);
var menuitem = document.createElementNS(kXULNS, "menuitem");
menuitem.setAttribute("id", "Patch_XULrubySupport");
menuitem.setAttribute("label", "\u30eb\u30d3\u5909\u63db\u3092\u57fa\u672c\u7121\u52b9\u3068\u3059\u308b");
menuitem.setAttribute("accesskey", "e");
menuitem.setAttribute("type", "checkbox");
menuitem.setAttribute("autocheck", "false");
menuitem.setAttribute("oncommand", "Patch_XULrubySupport.toggle();");
menupopup.appendChild(menuitem);
var menuitem = document.createElementNS(kXULNS, "menuitem");
menuitem.setAttribute("label", "\u30eb\u30d3\u3092\u5e38\u306b\u5909\u63db\u3059\u308b\u30b5\u30a4\u30c8\u767b\u9332");
menuitem.setAttribute("accesskey", "r");
menuitem.setAttribute("oncommand", "Patch_XULrubySupport.inputSite();");
menupopup.appendChild(menuitem);
},
uninit: function(){
Patch_XULrubySupport.removePrefListener(Patch_XULrubySupport.PrefListener); // 登録処理
},
toggle:function(){
var ignore = this.getPref('rubysupport.add.ignore.always','bool', false)
this.setPref('rubysupport.add.ignore.always', 'bool', !ignore)
},
setMenuCheck: function() {
var ignore = this.getPref('rubysupport.add.ignore.always','bool', false)
var menuitem = document.getElementById('Patch_XULrubySupport');
menuitem.setAttribute('checked', ignore);
},
initEXECUTEURL: function(){
this.EXECUTEURL = this.getPref('rubysupport.add.allowexecute.site', 'str', '').split(' ');
for(aURL in this.EXECUTEURL) {
if(this.EXECUTEURL[aURL] !==''){
try{
this.EXECUTEURL[aURL] = Patch_XULrubySupport.convert2RegExp(this.EXECUTEURL[aURL]);
}catch(ex){}
}else
this.EXECUTEURL[aURL] ='';
}
},
allowexecute: function(w){
if( !this.getPref('rubysupport.add.ignore.always','bool', false)) return true;
//this.debug(w.location.href);
for(aURL in this.EXECUTEURL) {
if(this.EXECUTEURL[aURL] === '')continue;
try{
if (this.EXECUTEURL[aURL].test(w.location.href)) return true;
}catch(ex){}
}
return false;
},
inputSite:function(){
var text = this.getPref('rubysupport.add.allowexecute.site', 'str', '');
text = window.prompt('\u30eb\u30d3\u5909\u63db\u3092\u8a31\u53ef\u3059\u308b\u30b5\u30a4\u30c8\u767b\u9332(\u30b9\u30da\u30fc\u30b9\u3067\u533a\u5207\u308b)', text);
this.setPref('rubysupport.add.allowexecute.site', 'str', text)
},
convert2RegExp: function( pattern ) {
var s = new String(pattern);
var res = new String('^');
for (var k = 0 ; k < s.length ; k++) {
switch(s[k]) {
case '*' :
res += '.*';
break;
case '.' :
case '?' :
case '^' :
case '$' :
case '+' :
case '{' :
case '[' :
case '|' :
case '(' :
case ')' :
case ']' :
res += '\\' + s[k];
break;
case '\\' :
res += '\\\\';
break;
case ' ' :
// Remove spaces from URLs.
break;
default :
res += s[k];
break;
}
}
// fortunately, we don't need .tld in chrome :)
return new RegExp(res +'$', 'i');
},
getPref: function(aPrefString, aPrefType, aDefault){
var xpPref = Components.classes['@mozilla.org/preferences-service;1']
.getService(Components.interfaces.nsIPrefService);
try{
switch (aPrefType){
case 'str':
return xpPref.getCharPref(aPrefString).toString(); break;
case 'int':
return xpPref.getIntPref(aPrefString); break;
case 'bool':
default:
return xpPref.getBoolPref(aPrefString); break;
}
}catch(e){
}
return aDefault;
},
setPref: function(aPrefString, aPrefType, aValue){
var xpPref = Components.classes['@mozilla.org/preferences-service;1']
.getService(Components.interfaces.nsIPrefService);
try{
switch (aPrefType){
case 'str':
return xpPref.setCharPref(aPrefString, aValue); break;
case 'int':
aValue = parseInt(aValue);
return xpPref.setIntPref(aPrefString, aValue); break;
case 'bool':
default:
return xpPref.setBoolPref(aPrefString, aValue); break;
}
}catch(e){
}
return null;
},
//Thanks Piro.
// 監視を開始する
addPrefListener: function(aObserver) {
try {
var pbi = Components.classes['@mozilla.org/preferences-service;1'].getService(Components.interfaces.nsIPrefBranch2);
pbi.addObserver(aObserver.domain, aObserver, false);
} catch(e) {}
},
// 監視を終了する
removePrefListener: function(aObserver) {
try {
var pbi = Components.classes['@mozilla.org/preferences-service;1'].getService(Components.interfaces.nsIPrefBranch2);
pbi.removeObserver(aObserver.domain, aObserver);
} catch(e) {}
},
PrefListener:{
domain : 'rubysupport.add',
//'rubysupport.add.XXX'という名前の設定が変更された場合全てで処理を行う
observe : function(aSubject, aTopic, aPrefstring) {
if (aTopic == 'nsPref:changed') {
// 設定が変更された時の処理
Patch_XULrubySupport.initEXECUTEURL();
}
}
}
}
Patch_XULrubySupport.init();
window.addEventListener('unload', function(){ Patch_XULrubySupport.uninit(); }, false);