-
Notifications
You must be signed in to change notification settings - Fork 55
/
AwesomeBookmarkbar.uc.js
395 lines (358 loc) · 15.8 KB
/
AwesomeBookmarkbar.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
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
// ==UserScript==
// @name AwesomeBookmarkbar.uc.js
// @description 智能书签工具栏
// @author feiruo
// @compatibility Firefox 24.0
// @charset UTF-8
// @include chrome://browser/content/browser.xul
// @id [73FCA65B]
// @inspect window.AwesomeBookmarkbar
// @startup window.AwesomeBookmarkbar.init();
// @shutdown window.AwesomeBookmarkbar.onDestroy();
// @optionsURL about:config?filter=AwesomeBookmarkbar.
// @config window.AwesomeBookmarkbar.openPref();
// @reviewURL http://bbs.kafan.cn/thread-1726260-1-1.html
// @homepageURL https://github.com/feiruo/userChromeJS
// @downloadURL https://github.com/feiruo/userChromeJS/AwesomeBookmarkbar.uc.js
// @note 点击地址栏显示书签工具栏。
// @note 地址栏任意按键,地址栏失去焦点后自动隐藏书签工具栏。
// @note 左键点击书签后自动隐藏书签工具栏。
// @version 0.3.3 2016.10.15 14:00 增加“页面内双击”。
// @version 0.3.2 2015.10.11 17:00 地址栏只在输入框生效,“地址栏失去焦点”修改“页内点击”。
// @version 0.3.1 2015.04.17 10:00 更多功能,表达能力略微提升.
// @version 0.3 2015.04.11 20:00 绘制UI设置界面.
// @version 0.2.1 去除鼠标移到地址栏自动显示书签工具栏
// @version 0.2 增加鼠标移到地址栏自动显示书签工具栏,移出隐藏
// ==/UserScript==
location == "chrome://browser/content/browser.xul" && (function() {
if (window.AwesomeBookmarkbar) {
window.AwesomeBookmarkbar.onDestroy();
delete window.AwesomeBookmarkbar;
}
var AwesomeBookmarkbar = {
get prefs() {
delete this.prefs;
return this.prefs = Services.prefs.getBranch("userChromeJS.AwesomeBookmarkbar.");
},
get Window() {
var windowsMediator = Components.classes["@mozilla.org/appshell/window-mediator;1"]
.getService(Components.interfaces.nsIWindowMediator);
return windowsMediator.getMostRecentWindow("AwesomeBookmarkbar:Preferences");
},
get CurrentURI() {
var windowMediator = Cc["@mozilla.org/appshell/window-mediator;1"].getService(Ci.nsIWindowMediator);
var topWindowOfType = windowMediator.getMostRecentWindow("navigator:browser");
if (topWindowOfType)
return topWindowOfType.document.getElementById("content").currentURI;
return null;
},
init: function() {
var ins = $("devToolsSeparator");
ins.parentNode.insertBefore($C("menuitem", {
id: "AwesomeBookmarkbar_set",
label: "智能书签栏配置",
oncommand: "AwesomeBookmarkbar.openPref();",
image: "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABAAAAAQCAYAAAAf8/9hAAAByklEQVQ4jZ2Rz4sSYRjHn2yxYmlpqYV+LOye6uCtS3uu6LBFMJCJgviC0jonfxzUndMrHvI9hDiOh/eVGUE3kIEQHEedlx1eh7nVn1D/wF5rLwX9uCQsgRP6uTw8D3w/fOEBuIAQYkMIsXFhv+X7/rYQYt913XsQhGma14QQR67rItM0wwAAnuc9FkKczufzj57nPQsUOI6zyTn/wDnvTSaTKwAAs9ks6zjOD8757+l02hkOhzeWCjDGIdu292zb3vvbKDwej19alnUyGo18y7Ke9Hq9zWX5SwAA0Wj08mLqun4dAKDf798ZDAYHgfUNw3hoGMaxYRgZAABd1193u13R6XSCgwsopSnG2C9Kqd9ut28zxiTG2Dml1FdVdScwjDEOqar6VFXVsaZpx5qm3cQYh1qtVq7ZbL5tNBpvCCG7/21BCNnFGG/9c3tFCDmv1+vvMcbLP7CMSqWyXavVTqrV6k+M8TuE0NWVJaVS6a6iKDNFUb6Xy+XS4lMrkcvlHhSLxU+FQuFbPp9HKwsAAGRZPpBl+Us2mz3LZDKHa0nS6fRzhNBZKpX6nEwmH60lSSQSKB6Pf43FYmQtQSQSCUuS9EKSpPsAAH8A5AfFG6aqwRIAAAAASUVORK5CYII=",
class: "menuitem-iconic",
}), ins);
this.loadSetting();
this.prefs.addObserver('', this.PrefsObs, false);
window.addEventListener("unload", function() {
AwesomeBookmarkbar.onDestroy();
}, false);
},
onDestroy: function() {
this.AddListener(false, 2, "click", "UClick_S", "Show");
this.AddListener(false, 2, "click", "UClick_H", "Hide");
this.AddListener(false, 2, "dblclick", "UDblclick_S", "Show");
this.AddListener(false, 2, "dblclick", "UDblclick_H", "Hide");
this.AddListener(false, 2, "blur", "UBlur_H", "Hide");
this.AddListener(false, 2, "blur", "UBlur_HH", "Hide");
this.AddListener(false, 2, "keydown", "UKey_H", "keyHide");
this.AddListener(false, 2, "mouseover", "UMove_S", "MShow");
this.AddListener(false, 2, "mouseout", "UMouseout_H", "MHide");
this.AddListener(false, 0, "mouseover", "PMouseover_S", "MShow");
this.AddListener(false, 0, "mouseout", "PMouseout_H", "MHide");
this.AddListener(false, 0, "command", "PersonalToolbarClick0", "PHide");
this.AddListener(false, 0, "click", "PersonalToolbarClick1", "PHide");
this.AddListener(false, 1, "command", "PersonalToolbarClick2", "PHide");
this.ShowToolbar(true);
this.prefs.removeObserver('', this.PrefsObs, false);
window.getBrowser().removeProgressListener(AwesomeBookmarkbar.progressListener);
if (this.Window) this.Window.close();
Services.obs.notifyObservers(null, "startupcache-invalidate", "");
},
PrefsObs: function(subject, topic, data) {
if (topic == 'nsPref:changed') {
switch (data) {
case 'PClick_H':
case 'PMouseover_S':
case 'PMouseout_H':
case 'UClick_H':
case 'UMouseout_H':
case 'UKey_H':
case 'UBlur_H':
case 'UBlur_HH':
case 'UDblclick_S':
case 'UDblclick_H':
case 'UClick_S':
case 'UMove_S':
case 'LShow':
case 'Hide_Time':
case 'Show_Time':
case 'NewTabS':
AwesomeBookmarkbar.loadSetting(data);
break;
}
}
},
loadSetting: function(type) {
if (!type || type === "LShow") {
this.LShow = this.getPrefs(0, "LShow");
if (!type)
this.ShowToolbar(this.LShow);
}
if (!type || type === "Hide_Time")
this.Hide_Time = this.getPrefs(1, "Hide_Time");
if (!type || type === "Show_Time")
this.Show_Time = this.getPrefs(1, "Show_Time");
if (!type || type === "UClick_S")
this.AddListener(this.getPrefs(0, "UClick_S"), 2, "click", "UClick_S", "Show");
if (!type || type === "UClick_H")
this.AddListener(this.getPrefs(0, "UClick_H"), 2, "click", "UClick_H", "Hide");
if (!type || type === "UDblclick_S")
this.AddListener(this.getPrefs(0, "UDblclick_S"), 2, "dblclick", "UDblclick_S", "Show");
if (!type || type === "UDblclick_H")
this.AddListener(this.getPrefs(0, "UDblclick_H"), 2, "dblclick", "UDblclick_H", "Hide");
if (!type || type === "UBlur_H") {
//this.AddListener(this.getPrefs(0, "UBlur_H"), 2, "blur", "UBlur_H", "Hide");
this.AddListener(this.getPrefs(0, "UBlur_H"), 3, "click", "UBlur_H", "Hide");
}
if (!type || type === "UBlur_HH") {
this.AddListener(this.getPrefs(0, "UBlur_HH"), 3, "dblclick", "UBlur_HH", "Hide");
}
if (!type || type === "UKey_H")
this.AddListener(this.getPrefs(0, "UKey_H"), 2, "keydown", "UKey_H", "keyHide");
if (!type || type === "UMove_S")
this.AddListener(this.getPrefs(0, "UMove_S"), 2, "mouseover", "UMove_S", "Show");
if (!type || type === "UMouseout_H") {
this.AddListener(this.getPrefs(0, "UMouseout_H"), 2, "mouseout", "UMouseout_H", "Hide");
this.AddListener(this.getPrefs(0, "UMouseout_H"), 0, "mouseover", "PMouseover_S", "Show");
}
if (!type || type === "PMouseout_H")
this.AddListener(this.getPrefs(0, "PMouseout_H"), 0, "mouseout", "PMouseout_H", "Hide");
if (!type || type === "NewTabS")
this.TabListen(this.getPrefs(0, "NewTabS"));
if (!type || type === "PClick_H") {
this.AddListener(this.getPrefs(0, "PClick_H"), 0, "command", "PersonalToolbarClick0", "PHide");
this.AddListener(this.getPrefs(0, "PClick_H"), 0, "click", "PersonalToolbarClick1", "PHide");
this.AddListener(this.getPrefs(0, "PClick_H"), 1, "command", "PersonalToolbarClick2", "PHide");
}
},
/*****************************************************************************************/
TabListen: function(enable) {
window.getBrowser().removeProgressListener(AwesomeBookmarkbar.progressListener);
if (!enable) return;
this.progressListener = {
onLocationChange: function() {
AwesomeBookmarkbar.onLocationChange();
},
onProgressChange: function() {},
onSecurityChange: function() {},
onStateChange: function() {},
onStatusChange: function() {}
};
window.getBrowser().addProgressListener(AwesomeBookmarkbar.progressListener);
},
onLocationChange: function() {
var url = this.CurrentURI.spec;
if (url == Services.prefs.getCharPref("browser.startup.homepage") || url == "about:newtab")
AwesomeBookmarkbar.ShowToolbar(true);
else
AwesomeBookmarkbar.ShowToolbar(false);
},
AddListener: function(enable, tag, action, name, command) {
if (tag === 0)
tag = $("PersonalToolbar");
else if (tag === 1)
tag = $("placesCommands");
else if (tag === 2)
tag = gURLBar;
else if (tag === 3)
tag = gBrowser.mPanelContainer;
tag.removeEventListener(action, AwesomeBookmarkbar["Listener_" + name], false);
if (!enable) return;
(function(name, command) {
AwesomeBookmarkbar["Listener_" + name] = function(e) {
AwesomeBookmarkbar.Listener(e, command);
};
})(name, command);
tag.addEventListener(action, AwesomeBookmarkbar["Listener_" + name], false);
},
Listener: function(e, command) {
var exclu;
if (e.target.nodeName != 'textbox')
exclu = true;
switch (command) {
case "Show":
if (exclu || e.button != 0) return;
this.ShowToolbar(true);
break;
case "Hide":
//if (exclu || e.button != 0) return;
this.ShowToolbar(false);
break;
case "PHide":
if (e.button == 2 || e.button == 1 || (e.button == 0 && !(e.metaKey || e.shiftKey || e.ctrlKey))) return;
this.ShowToolbar(false);
break;
case "keyHide":
if (window.event ? e.keyCode : e.which)
this.ShowToolbar(false);
break;
}
},
ShowToolbar: function(show) {
var Bar = $("PersonalToolbar");
var time;
if (show) {
show = true;
time = this.Show_Time;
} else {
show = false;
time = this.Hide_Time;
}
setTimeout(function() {
setToolbarVisibility(Bar, show);
}, time);
},
/*****************************************************************************************/
getPrefs: function(type, name, val) {
switch (type) {
case 0:
if (!this.prefs.prefHasUserValue(name) || this.prefs.getPrefType(name) != Ci.nsIPrefBranch.PREF_BOOL)
this.prefs.setBoolPref(name, false);
return this.prefs.getBoolPref(name);
break;
case 1:
if (!this.prefs.prefHasUserValue(name) || this.prefs.getPrefType(name) != Ci.nsIPrefBranch.PREF_INT)
this.prefs.setIntPref(name, 0);
return this.prefs.getIntPref(name);
break;
case 2:
if (!this.prefs.prefHasUserValue(name) || this.prefs.getPrefType(name) != Ci.nsIPrefBranch.PREF_STRING)
this.prefs.setCharPref(name, "");
return this.prefs.getCharPref(name);
break;
}
},
openPref: function() {
if (this.Window)
this.Window.focus();
else {
var option = this.option();
window.openDialog("data:application/vnd.mozilla.xul+xml;charset=UTF-8," + option, '', 'chrome,titlebar,toolbar,centerscreen,dialog=no');
}
},
option: function() {
xul = '<?xml version="1.0"?><?xml-stylesheet href="chrome://global/skin/" type="text/css"?>\
<prefwindow xmlns="http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul"\
id="AwesomeBookmarkbar_Settings"\
ignorekeys="true"\
title="智能书签栏配置"\
buttons="accept,cancel,extra1"\
ondialogextra1="Resets();"\
windowtype="AwesomeBookmarkbar:Preferences">\
<prefpane id="main" flex="1">\
<preferences>\
<preference id="PClick_H" type="bool" name="userChromeJS.AwesomeBookmarkbar.PClick_H"/>\
<preference id="PMouseout_H" type="bool" name="userChromeJS.AwesomeBookmarkbar.PMouseout_H"/>\
<preference id="UClick_H" type="bool" name="userChromeJS.AwesomeBookmarkbar.UClick_H"/>\
<preference id="UMouseout_H" type="bool" name="userChromeJS.AwesomeBookmarkbar.UMouseout_H"/>\
<preference id="UKey_H" type="bool" name="userChromeJS.AwesomeBookmarkbar.UKey_H"/>\
<preference id="UBlur_H" type="bool" name="userChromeJS.AwesomeBookmarkbar.UBlur_H"/>\
<preference id="UBlur_HH" type="bool" name="userChromeJS.AwesomeBookmarkbar.UBlur_HH"/>\
<preference id="UDblclick_S" type="bool" name="userChromeJS.AwesomeBookmarkbar.UDblclick_S"/>\
<preference id="UDblclick_H" type="bool" name="userChromeJS.AwesomeBookmarkbar.UDblclick_H"/>\
<preference id="UClick_S" type="bool" name="userChromeJS.AwesomeBookmarkbar.UClick_S"/>\
<preference id="UMove_S" type="bool" name="userChromeJS.AwesomeBookmarkbar.UMove_S"/>\
<preference id="LShow" type="bool" name="userChromeJS.AwesomeBookmarkbar.LShow"/>\
<preference id="NewTabS" type="bool" name="userChromeJS.AwesomeBookmarkbar.NewTabS"/>\
<preference id="Hide_Time" type="int" name="userChromeJS.AwesomeBookmarkbar.Hide_Time"/>\
<preference id="Show_Time" type="int" name="userChromeJS.AwesomeBookmarkbar.Show_Time"/>\
</preferences>\
<script>\
function Resets() {\
$("PClick_H").value = false;\
$("PMouseout_H").value = false;\
$("UClick_H").value = false;\
$("UMouseout_H").value = false;\
$("UKey_H").value = false;\
$("UBlur_H").value = false;\
$("UBlur_HH").value = false;\
$("UDblclick_S").value = false;\
$("UClick_S").value = false;\
$("UDblclick_H").value = false;\
$("UMove_S").value = false;\
$("LShow").value = false;\
$("NewTabS").value = false;\
$("Hide_Time").value = 0;\
$("Show_Time").value = 0;\
opener.AwesomeBookmarkbar.ShowToolbar();\
}\
function $(id) document.getElementById(id);\
</script>\
<groupbox>\
<checkbox id="LShow" label="启动时显示书签栏" preference="LShow"/>\
<checkbox id="NewTabS" label="主页和新标签显示书签栏" preference="NewTabS"/>\
</groupbox>\
<hbox>\
<groupbox>\
<caption label="隐藏书签工具栏"/>\
<row align="center">\
<label value="隐藏延时:"/>\
<textbox id="Hide_Time" type="number" preference="Hide_Time" style="width:125px" tooltiptext="单位:毫秒!"/>\
</row>\
<checkbox id="UClick_H" label="地址栏点击" preference="UClick_H"/>\
<checkbox id="UDblclick_H" label="地址栏双击" preference="UDblclick_H"/>\
<checkbox id="UKey_H" label="地址栏任意按键" preference="UKey_H"/>\
<checkbox id="UBlur_H" label="页面内点击" preference="UBlur_H"/>\
<checkbox id="UBlur_HH" label="页面内双击" preference="UBlur_HH"/>\
<checkbox id="UMouseout_H" label="鼠标移出地址栏" preference="UMouseout_H"/>\
<checkbox id="PMouseout_H" label="鼠标移出书签工具栏" preference="PMouseout_H"/>\
<checkbox id="PClick_H" label="书签工具栏点击书签之后" preference="PClick_H"/>\
</groupbox>\
<groupbox>\
<caption label="显示书签工具栏"/>\
<row align="center">\
<label value="显示延时:"/>\
<textbox id="Show_Time" type="number" preference="Show_Time" style="width:125px" tooltiptext="单位:毫秒!"/>\
</row>\
<checkbox id="UDblclick_S" label="地址栏双击" preference="UDblclick_S"/>\
<checkbox id="UClick_S" label="地址栏单击" preference="UClick_S"/>\
<checkbox id="UMove_S" label="鼠标移入地址栏" preference="UMove_S"/>\
</groupbox>\
</hbox>\
<hbox flex="1">\
<button dlgtype="extra1" label="还原默认值" />\
<spacer flex="1" />\
<button dlgtype="accept"/>\
<button dlgtype="cancel"/>\
</hbox>\
</prefpane>\
</prefwindow>\
';
return encodeURIComponent(xul);
},
};
function $(id) {
return document.getElementById(id);
}
function $C(name, attr) {
var el = document.createElement(name);
if (attr) Object.keys(attr).forEach(function(n) el.setAttribute(n, attr[n]));
return el;
}
AwesomeBookmarkbar.init();
window.AwesomeBookmarkbar = AwesomeBookmarkbar;
})();