forked from ywzhaiqi/userChromeJS
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrebuild_userChrome.uc.xul
478 lines (430 loc) · 23.3 KB
/
rebuild_userChrome.uc.xul
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
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
<?xml version="1.0" encoding="UTF-8"?>
<overlay xmlns="http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul">
<!--
// ==UserScript==
// @name rebuild_userChrome.uc.xul
// @namespace http://space.geocities.yahoo.co.jp/gl/alice0775
// @description uc 脚本管理器菜单版,改自 Alice0775 原版
// @include main
// @compatibility Firefox 3.5b4pre 3.6a1pre 4.0b7pre
// @author Alice0775
// @version 2013/08/26
// @homepageURL https://github.com/ywzhaiqi/userChromeJS/tree/master/rebuild_userChrome
// @reviewURL https://github.com/ywzhaiqi/userChromeJS/issues
// @note 2013/11/20 modified by wenLiangcan
// @note 2013/08/26 modified by ywzhaiqi
// @note 2013/03/22 08:02 Added "use strict"
// @note 2013/03/22 08:01 Fixed commands to work properly, even if menus had been moved into other place
// @note 2013/03/22 08:00 Fixed dragdrop target
// @note 2013/03/20 24:00 autocheck=false for script menu
// @note 2012/11/30 22:00 ubuntu12.04.1 "chromeフォルダを開く" に失敗することがあるのを修正
// @note 2012/09/30 09:00 ubuntu12.04.1 "chromeフォルダを開く" に失敗することがあるのを修正
// @note 2012/02/25 23:00 restart
// @note 2010/10/25 22:00 Bug 574688 adon bar
// @note 2010/07/04 00:00 nsDragAndDrop
// @note 2009/11/14 00:00 Seamonkeyに対応
// @note 2009/08/24 00:00 Namoroka3.6a2pre で右クリックでのスクリプト編集時にコンテキストメニューが表示されるのを修正
// @note 2009/04/10 00:00 Minefield3.6a1pre での動作改善
// @note 2009/03/27 00:00 nsIProcess変更
// @note 2008/02/25 00:00 reuseのデフォルト値trueに変更
// @note 2008/01/09 02:00 スクリプト保存ファイルピッカーをキャンセル時のエラー処理追加
// @note 2008/01/04 16:00 スクリプトのドロップをstatusbar-display上に変更
// @note 2007/12/15 18:00 base64データスキームの保存に対応
// @note 2007/12/15 02:00 ttp://の保存に対応
// @note 2007/12/15 01:00 メニューが表示されない場合があるのを修正
// @note 2007/12/14 23:00 saveFolderModokiがある時スクリプトのリンクをステータスバーの左1/3にドロップすることで, chrmeホルダに保存するようにした
// @note 2007/12/14 19:00 日本語のファイル名のスクリプトの有効/無効が機能していなかったのを修正
// @note 2007/12/14 17:00 スクリプトの有効/無効/編集を設定できるようにした
// @Note 使用するエディタを編集しておくこと
// @Note Required Sub-Script/Overlay Loader v3.0.38mod( https://github.com/alice0775/userChrome.js/blob/master/userChrome.js )
// ==/UserScript==
-->
<script type="application/javascript" xmlns="http://www.w3.org/1999/xhtml"><![CDATA[
(function() {
"use strict";
// 要移动的菜单列表,移动到一起统一管理。
var movedMenus = [
// "easydragMODOption", // easydrag_customMOD.uc.js
"wordhighlight-toolbar-menuitem", // WordHighlightToolbar.uc.js
"JSCSS-menuitem", // js、css、ahk 代码着色
"addMenu-rebuild", // addMenu 的重新载入配置
// "sw-menuitem", // siteinfo_write
// "uSuper_preloader-menuitem",
// "GrabScroll_optionsMenu",
// "Patch_XULrubySupportMenu",
// "menutabTooltip",
// "PipeliningToggle",
// "linkInNewTabForSpecifiedPageToolMenu",
// "ieviewModokiTool",
// "linkloadInBackgroundToolMenu",
// "SaveFolderToolsMenu",
// "ucjs_copysysinfo-menu"
];
window.userChromejs = {
// --- config ---
editor: "", // 为空则用 view_source.editor.path
// editor: "C:\\Program Files\\hidemaru\\hidemaru.exe",
// editor: "/usr/bin/gedit",
// --- config ---
_statusDisplay: null,
get statusDisplay() {
if (!this._statusDisplay) this._statusDisplay = document.getElementById('status-bar') || document.getElementById('statusbar-display');
return this._statusDisplay;
},
_addonbar: null,
get addonbar() {
if (!this._addonbar) this._addonbar = document.getElementById('addon-bar');
return this._addonbar;
},
handleEvent: function(event) {
switch (event.type) {
case 'unload':
this.uninit();
break;
}
},
init: function() {
window.addEventListener("unload", this, false);
this.addPrefListener(userChromejs.readLaterPrefListener); // 登録処理
},
uninit: function() {
this.removePrefListener(userChromejs.readLaterPrefListener); // 登録解除
},
rebuild: function() {
var flag = this.getPref("userChrome.enable.reuse", 'bool', true);
this.setPref("userChrome.enable.reuse", 'bool', false);
setTimeout(function() {
OpenBrowserWindow();
}, 0);
setTimeout(function(self, flag) {
self.setPref("userChrome.enable.reuse", 'bool', flag);
}, 2000, this, flag);
},
setting: function() {
var flag = this.getPref("userChrome.enable.reuse", 'bool', true);
this.setPref("userChrome.enable.reuse", 'bool', !flag);
},
onpopup: function() {
var menu;
var flag = this.getPref("userChrome.enable.reuse", 'bool', true);
var menuitem = document.getElementById('userChrome_setting');
menuitem.setAttribute('checked', !flag);
var menupopup = document.getElementById("userChromejs_options");
for (var i = 4, len = menupopup.childNodes.length; i < len; i++) {
menupopup.removeChild(menupopup.lastChild);
}
var menuseparator = document.createElement('menuseparator');
menupopup.appendChild(menuseparator);
menuitem = document.createElement('menuitem');
menuitem.setAttribute('label', 'Toggle userCromeJS');
menuitem.setAttribute('oncommand', 'userChromejs.chgDirStat("*");');
menuitem.setAttribute('onclick', 'if(event.button != 0) {event.stopPropagation();event.preventDefault();userChromejs.clickDirMenuitem(event,true);}');
menuitem.setAttribute('type', 'checkbox');
menuitem.setAttribute('checked', !userChrome_js.dirDisable['*']);
menuitem.dirName = '*';
menupopup.appendChild(menuitem);
for (var j = 0, lenj = userChrome_js.arrSubdir.length; j < lenj; j++) {
var dirName = userChrome_js.arrSubdir[j] == "" ? "root" : userChrome_js.arrSubdir[j];
var flg = false;
for (var i = 0, len = userChrome_js.scripts.length; i < len; i++) {
var script = userChrome_js.scripts[i];
if (script.dir != dirName) continue;
flg = true;
break;
}
if (!flg) {
for (var i = 0, len = userChrome_js.overlays.length; i < len; i++) {
var script = userChrome_js.overlays[i];
if (script.dir != dirName) continue;
flg = true;
break;
}
}
if (!flg) continue;
menu = document.createElement('menu');
menu.setAttribute('label', 'chrome/' + (dirName == "root" ? "" : dirName));
menu.setAttribute('onclick', 'if(event.button != 0) {event.stopPropagation();event.preventDefault();userChromejs.clickDirMenu(event);}');
if (userChrome_js.dirDisable[dirName]) menu.setAttribute('style', 'font-style:italic;');
menu.dirName = dirName;
menupopup = document.createElement('menupopup');
menupopup.setAttribute('onpopupshowing', 'event.stopPropagation();');
menuitem = document.createElement('menuitem');
menuitem.setAttribute('label', 'chrome/' + (dirName == "root" ? "" : dirName) + ' Toggle All Scripts');
menuitem.setAttribute('oncommand', 'userChromejs.chgDirStat(this.dirName);');
menuitem.setAttribute('onclick', 'if(event.button != 0) {event.stopPropagation();event.preventDefault();userChromejs.clickDirMenuitem(event);}');
menuitem.setAttribute('type', 'checkbox');
menuitem.setAttribute('checked', !userChrome_js.dirDisable[dirName]);
menuitem.dirName = dirName;
menupopup.appendChild(menuitem);
menuseparator = document.createElement('menuseparator');
menupopup.appendChild(menuseparator);
var flg = false;
for (var i = 0, len = userChrome_js.scripts.length; i < len; i++) {
var script = userChrome_js.scripts[i];
if (script.dir != dirName) continue;
flg = true;
menuitem = document.createElement('menuitem');
menuitem.setAttribute('label', script.filename);
menuitem.setAttribute('oncommand', 'userChromejs.chgScriptStat(this.script.filename);');
menuitem.setAttribute('onclick', 'if(event.button != 0) {event.stopPropagation();event.preventDefault();userChromejs.clickScriptMenu(event);}');
menuitem.setAttribute('type', 'checkbox');
menuitem.setAttribute('autocheck', 'false');
menuitem.setAttribute('checked', !userChrome_js.scriptDisable[script.filename]);
if (script.description) menuitem.setAttribute('tooltiptext', script.description);
menuitem.script = script;
menupopup.appendChild(menuitem);
}
for (var i = 0, len = userChrome_js.overlays.length; i < len; i++) {
var script = userChrome_js.overlays[i];
if (script.dir != dirName) continue;
if (flg) {
menuseparator = document.createElement('menuseparator');
menupopup.appendChild(menuseparator);
}
flg = false;
menuitem = document.createElement('menuitem');
menuitem.setAttribute('label', script.filename);
menuitem.setAttribute('oncommand', 'userChromejs.chgScriptStat(this.script.filename);');
menuitem.setAttribute('onclick', 'if(event.button != 0) {event.stopPropagation();event.preventDefault();userChromejs.clickScriptMenu(event);}');
menuitem.setAttribute('type', 'checkbox');
menuitem.setAttribute('autocheck', 'false');
menuitem.setAttribute('checked', !userChrome_js.scriptDisable[script.filename]);
if (script.description) menuitem.setAttribute('tooltiptext', script.description);
menuitem.script = script;
menupopup.appendChild(menuitem);
}
menu.appendChild(menupopup);
menupopup = document.getElementById("userChromejs_options");
menupopup.appendChild(menu);
}
},
clickDirMenu: function(event) {
if (event.button == 1 || event.button == 2) {
userChromejs.chgDirStat(event.target.dirName);
if (event.target.firstChild && event.target.firstChild.firstChild) event.target.firstChild.firstChild.setAttribute('checked', !userChrome_js.dirDisable[event.target.dirName]);
if ( !! userChrome_js.dirDisable[event.target.dirName]) event.target.setAttribute('style', 'font-style:italic;');
else event.target.removeAttribute('style');
}
},
clickDirMenuitem: function(event, stop) {
if (event.button == 1 || event.button == 2) {
userChromejs.chgDirStat(event.target.dirName);
event.target.setAttribute('checked', !userChrome_js.dirDisable[event.target.dirName]);
if (!stop && !! userChrome_js.dirDisable[event.target.dirName]) event.target.parentNode.parentNode.setAttribute('style', 'font-style:italic;');
else event.target.parentNode.parentNode.removeAttribute('style');
}
},
clickScriptMenu: function(event) {
if (event.button == 1) {
userChromejs.chgScriptStat(event.target.script.filename);
event.target.setAttribute('checked', !userChrome_js.scriptDisable[event.target.script.filename]);
} else if (event.button == 2) {
this.launchEditor(event.target.script);
}
},
launchEditor: function(aScript) {
var editor = this.editor || Services.prefs.getCharPref("view_source.editor.path");
var UI = Components.classes['@mozilla.org/intl/scriptableunicodeconverter'].createInstance(Components.interfaces.nsIScriptableUnicodeConverter);
var platform = window.navigator.platform.toLowerCase();
if (platform.indexOf('win') > -1) {
UI.charset = 'GB2312'; // Shift_JIS
} else {
UI.charset = 'UTF-8';
}
var path = Components.classes['@mozilla.org/network/io-service;1'].getService(Components.interfaces.nsIIOService).getProtocolHandler('file').QueryInterface(Components.interfaces.nsIFileProtocolHandler).getFileFromURLSpec(aScript.url).path
path = UI.ConvertFromUnicode(path);
var appfile = Components.classes['@mozilla.org/file/local;1'].createInstance(Components.interfaces.nsILocalFile);
appfile.initWithPath(editor);
var process = Components.classes['@mozilla.org/process/util;1'].createInstance(Components.interfaces.nsIProcess);
process.init(appfile);
process.run(false, [path], 1, {});
},
chgDirStat: function(adirName) {
var s = userChromejs.getPref("userChrome.disable.directory", "str", "");
if (!userChrome_js.dirDisable[adirName]) {
s = (s + ',').replace(adirName + ',', '') + adirName + ',';
} else {
s = (s + ',').replace(adirName + ',', '');
}
s = s.replace(/,,/g, ',').replace(/^,/, '');
userChromejs.setPref("userChrome.disable.directory", "str", s);
userChrome_js.dirDisable = this.restoreState(s.split(','));
},
chgScriptStat: function(afilename) {
var s = userChromejs.getPref("userChrome.disable.script", "str", "");
if (!userChrome_js.scriptDisable[afilename]) {
s = (s + ',').replace(afilename + ',', '') + afilename + ',';
} else {
s = (s + ',').replace(afilename + ',', '');
}
s = s.replace(/,,/g, ',').replace(/^,/, '');
userChromejs.setPref("userChrome.disable.script", "str", s);
userChrome_js.scriptDisable = this.restoreState(s.split(','));
},
restoreState: function(arr) {
var disable = [];
for (var i = 0, len = arr.length; i < len; i++)
disable[arr[i]] = true;
return disable;
},
//prefを読み込み
getPref: function(aPrefString, aPrefType, aDefault) {
var xpPref = Components.classes['@mozilla.org/preferences-service;1'].getService(Components.interfaces.nsIPrefBranch2);
try {
switch (aPrefType) {
case 'complex':
return xpPref.getComplexValue(aPrefString, Components.interfaces.nsILocalFile);
break;
case 'str':
return unescape(xpPref.getCharPref(aPrefString).toString());
break;
case 'int':
return xpPref.getIntPref(aPrefString);
break;
case 'bool':
default:
return xpPref.getBoolPref(aPrefString);
break;
}
} catch (e) {}
return aDefault;
},
//prefを書き込み
setPref: function(aPrefString, aPrefType, aValue) {
var xpPref = Components.classes['@mozilla.org/preferences-service;1'].getService(Components.interfaces.nsIPrefBranch2);
try {
switch (aPrefType) {
case 'complex':
return xpPref.setComplexValue(aPrefString, Components.interfaces.nsILocalFile, aValue);
break;
case 'str':
return xpPref.setCharPref(aPrefString, escape(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;
},
// 監視を開始する
addPrefListener: function(aObserver) {
try {
var pbi = Components.classes['@mozilla.org/preferences;1'].getService(Components.interfaces.nsIPrefBranch2);
pbi.addObserver(aObserver.domain, aObserver, false);
} catch (e) {}
},
// 監視を終了する
removePrefListener: function(aObserver) {
try {
var pbi = Components.classes['@mozilla.org/preferences;1'].getService(Components.interfaces.nsIPrefBranch2);
pbi.removeObserver(aObserver.domain, aObserver);
} catch (e) {}
},
readLaterPrefListener: {
domain: 'userChrome.disable',
//"userChrome.disable"という名前の設定が変更された場合全てで処理を行う
observe: function(aSubject, aTopic, aPrefstring) {
if (aTopic == 'nsPref:changed') {
// 設定が変更された時の処理
setTimeout(function() {
var s = userChromejs.getPref("userChrome.disable.directory", "str", "");
userChrome_js.dirDisable = userChromejs.restoreState(s.split(','));
s = userChromejs.getPref("userChrome.disable.script", "str", "");
userChrome_js.scriptDisable = userChromejs.restoreState(s.split(','));
}, 0);
}
}
},
restartApp: function() {
const appStartup = Components.classes["@mozilla.org/toolkit/app-startup;1"].getService(Components.interfaces.nsIAppStartup);
// Notify all windows that an application quit has been requested.
var os = Components.classes["@mozilla.org/observer-service;1"].getService(Components.interfaces.nsIObserverService);
var cancelQuit = Components.classes["@mozilla.org/supports-PRBool;1"].createInstance(Components.interfaces.nsISupportsPRBool);
os.notifyObservers(cancelQuit, "quit-application-requested", null);
// Something aborted the quit process.
if (cancelQuit.data) return;
// Notify all windows that an application quit has been granted.
os.notifyObservers(null, "quit-application-granted", null);
// Enumerate all windows and call shutdown handlers
var wm = Components.classes["@mozilla.org/appshell/window-mediator;1"].getService(Components.interfaces.nsIWindowMediator);
var windows = wm.getEnumerator(null);
var win;
while (windows.hasMoreElements()) {
win = windows.getNext();
if (("tryToClose" in win) && !win.tryToClose()) return;
}
let XRE = Cc["@mozilla.org/xre/app-info;1"].getService(Ci.nsIXULRuntime);
if (typeof XRE.invalidateCachesOnRestart == "function") XRE.invalidateCachesOnRestart();
appStartup.quit(appStartup.eRestart | appStartup.eAttemptQuit);
}
}
window.userChromejs.init();
//メニューが長くなりすぎるので, あまり使わないメニューを"userChrome.jsの各スクリプトの設定"の下に移動させる
var userChromejsScriptOptionsMenu = {
//あまり使わないメニューのリスト
menues: movedMenus,
interval: 500, //0.5秒間隔
maxcount: 50, //最大50回までトライ
count: 0,
timer: null,
run: function() {
//DOMの構築が完了するのを待ってからメニューを移動させる(5秒間隔で最大50回までトライ)
this.timer = setInterval(function(self) {
if (++self.count > self.maxcount || self.moveMenu()) clearInterval(self.timer);
}, this.interval, this);
},
moveMenu: function() {
var menupopup = document.getElementById('userChromejs_script_options');
if (!menupopup) return false;
var i = 0;
while (i < this.menues.length) {
var menu = document.getElementById(this.menues[i])
if (menu) {
setTimeout(function(menupopup, menu) {
menupopup.appendChild(menu);
}, 100, menupopup, menu);
this.menues.splice(i, 1);
continue;
}
i++;
}
return this.menues.length == 0 ? true : false;
},
}
userChromejsScriptOptionsMenu.run();
})();
]]></script>
<menupopup id="appmenuPrimaryPane" idxxxx="contentAreaContextMenu">
<menu id="userChromejs_script_options_Menu" label="UC Scripts Settings"
insertafter="appmenu_webDeveloper" >
<menupopup id="userChromejs_script_options" />
</menu>
<menu label="userChromeJS Settings" accesskey="u"
insertafter="appmenu_webDeveloper" >
<menupopup id="userChromejs_options"
onpopupshowing="userChromejs.onpopup()"
context="">
<menuitem id="userChrome_setting"
label="Reset script at every new window"
accesskey="u"
oncommand="userChromejs.setting();"
type="checkbox" />
<menuitem label="Open new window and reset scripts"
accesskey="x"
oncommand="userChromejs.rebuild();"/>
<menuitem label="Restart browser"
accesskey="r"
oncommand="userChromejs.restartApp();"/>
<menuitem label="Open folder 'chrome'"
accesskey="h"
oncommand='new Components.Constructor("@mozilla.org/file/local;1","nsILocalFile", "initWithPath")(Components.classes["@mozilla.org/file/directory_service;1"].getService(Components.interfaces.nsIProperties).get("UChrm", Components.interfaces.nsIFile).path).reveal();'/>
</menupopup>
</menu>
</menupopup>
</overlay>