-
Notifications
You must be signed in to change notification settings - Fork 123
/
SidebarModoki.uc.js
453 lines (411 loc) · 18.4 KB
/
SidebarModoki.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
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
// ==UserScript==
// @name SidebarModoki
// @namespace http://space.geocities.yahoo.co.jp/gl/alice0775
// @description TST
// @include main
// @include chrome://browser/content/downloads/contentAreaDownloadsView.xul?SM
// @compatibility Firefox 69
// @author Alice0775
// @note Tree Style Tab がある場合にブックマークと履歴等を別途"サイドバーもどき"で表示
// @version 2019/08/07 15:00 fix adding key(renamde from key to keyvalue in jsonToDOM)
// @version 2019/07/13 13:00 fix wrong commit
// @version 2019/07/10 10:00 fix 70 Bug 1558914 - Disable Array generics in Nightly
// @version 2019/05/29 16:00 Bug 1519514 - Convert tab bindings
// @version 2018/12/23 14:00 Adjust margin
// @version 2018/12/23 00:00 Add option of SidebarModoki posiotion SM_RIGHT
// @version 2018/05/10 00:00 for 61 wip Bug 1448810 - Rename the Places sidebar files
// @version 2018/05/08 21:00 use jsonToDOM(https://developer.mozilla.org/en-US/docs/Archive/Add-ons/Overlay_Extensions/XUL_School/DOM_Building_and_HTML_Insertion)
// @version 2018/05/08 19:00 get rid loadoverlay
// @version 2017/11/24 19:50 do nothing if window is popup(window.open)
// @version 2017/11/24 19:20 change close button icon style to 57
// @version 2017/11/24 19:10 add key(accel(ctrl)+alt+s) and close button
// @version 2017/11/24 19:00 hack for DL manager
// @version 2017/11/24 15:00 remove unused variable
// @version 2017/11/23 13:10 restore initial tab index/width and more unique id
// @version 2017/11/23 12:30 try catch. download manager
// @version 2017/11/23 00:30 Make button icon
// @version 2017/11/23 00:00 Make button customizable
// @version 2017/11/22 23:00 fullscreen
// @version 2017/11/22 23:00 DOM fullscreen
// @version 2017/11/22 22:00 F11 fullscreen
// @version 2017/11/15 09:00
// ==/UserScript==
//xxxx download manager hack
if (location.href=="chrome://browser/content/downloads/contentAreaDownloadsView.xul?SM") {
let style = `
@namespace url(http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul);
#contentAreaDownloadsView
{
padding: 0;
}
#downloadsRichListBox > richlistitem.download
{
height: auto;
}
.downloadTypeIcon,
.downloadBlockedBadge
{
margin-left:0;
margin-right:1px;
}
.downloadButton {
padding-left:0;
padding-right:0;
}
`;
style = style.replace(/\s+/g, " ").replace(/\{SM_WIDTH\}/g, this.SM_WIDTH);
let sspi = document.createProcessingInstruction(
'xml-stylesheet',
'type="text/css" href="data:text/css,' + encodeURIComponent(style) + '"'
);
document.insertBefore(sspi, document.documentElement);
sspi.getAttribute = function(name) {
return document.documentElement.getAttribute(name);
};
throw 'not an error, just load contentAreaDownloadsView.xul';
}
var SidebarModoki = {
// -- config --
SM_RIGHT: false, // SidebarModoki position
SM_WIDTH : 130,
SM_AUTOHIDE : false, //F11 Fullscreen
TAB0_SRC : "chrome://browser/content/places/bookmarksSidebar.xul", //"chrome://browser/content/bookmarks/bookmarksPanel.xul",
TAB0_LABEL : "Bookmarks",
TAB1_SRC : "chrome://browser/content/places/historySidebar.xul", //"chrome://browser/content/history/history-panel.xul",
TAB1_LABEL : "History",
TAB2_SRC : "chrome://browser/content/downloads/contentAreaDownloadsView.xul?SM",
TAB2_LABEL : "DL",
// -- config --
kSM_Open : "userChrome.SidebarModoki.Open",
kSM_lastSelectedTabIndex : "userChrome.SidebarModoki.lastSelectedTabIndex",
kSM_lastSelectedTabWidth : "userChrome.SidebarModoki.lastSelectedTabWidth",
ToolBox: null,
Button: null,
get prefs(){
delete this.prefs;
return this.prefs = Services.prefs;
},
jsonToDOM: function(jsonTemplate, doc, nodes) {
jsonToDOM.namespaces = {
html: "http://www.w3.org/1999/xhtml",
xul: "http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul"
};
jsonToDOM.defaultNamespace = jsonToDOM.namespaces.xul;
function jsonToDOM(jsonTemplate, doc, nodes) {
function namespace(name) {
var reElemNameParts = /^(?:(.*):)?(.*)$/.exec(name);
return { namespace: jsonToDOM.namespaces[reElemNameParts[1]], shortName: reElemNameParts[2] };
}
// Note that 'elemNameOrArray' is: either the full element name (eg. [html:]div) or an array of elements in JSON notation
function tag(elemNameOrArray, elemAttr) {
// Array of elements? Parse each one...
if (Array.isArray(elemNameOrArray)) {
var frag = doc.createDocumentFragment();
Array.prototype.forEach.call(arguments, function(thisElem) {
frag.appendChild(tag.apply(null, thisElem));
});
return frag;
}
// Single element? Parse element namespace prefix (if none exists, default to defaultNamespace), and create element
var elemNs = namespace(elemNameOrArray);
var elem = doc.createElementNS(elemNs.namespace || jsonToDOM.defaultNamespace, elemNs.shortName);
// Set element's attributes and/or callback functions (eg. onclick)
for (var key in elemAttr) {
var val = elemAttr[key];
if (nodes && key == "keyvalue") { //for later convenient JavaScript access) by giving them a 'keyvalue' attribute; |nodes|.|keyvalue|
nodes[val] = elem;
continue;
}
var attrNs = namespace(key);
if (typeof val == "function") {
// Special case for function attributes; don't just add them as 'on...' attributes, but as events, using addEventListener
elem.addEventListener(key.replace(/^on/, ""), val, false);
} else {
// Note that the default namespace for XML attributes is, and should be, blank (ie. they're not in any namespace)
elem.setAttributeNS(attrNs.namespace || "", attrNs.shortName, val);
}
}
// Create and append this element's children
var childElems = Array.prototype.slice.call(arguments, 2);
childElems.forEach(function(childElem) {
if (childElem != null) {
elem.appendChild(
childElem instanceof doc.defaultView.Node ? childElem :
Array.isArray(childElem) ? tag.apply(null, childElem) :
doc.createTextNode(childElem));
}
});
return elem;
}
return tag.apply(null, jsonTemplate);
}
return jsonToDOM(jsonTemplate, doc, nodes);
},
init: function() {
let chromehidden = document.getElementById("main-window").hasAttribute("chromehidden");
if (chromehidden &&
document.getElementById("main-window").getAttribute("chromehidden").includes("extrachrome")) { return; // do nothing
}
let MARGINHACK = this.SM_RIGHT ? "0 -4px 0 0" : "0 -6px 0 -4px";
let style = `
@namespace url(http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul);
#SM_toolbox
{
width: {SM_WIDTH}px;
background-color: -moz-dialog;
color: -moz-dialogtext;
text-shadow: none;
}
#SM_toolbox:not(.titlebar-color) {
width: 130px;
background-color: var(--toolbar-bgcolor);
color: var(--toolbar-color);
}
/*フルスクリーン*/
#SM_toolbox[moz-collapsed="true"],
#SM_splitter[moz-collapsed="true"]
{
visibility:collapse;
}
/*ポップアップの時*/
#main-window[chromehidden~="extrachrome"] #SM_toolbox,
#main-window[chromehidden~="extrachrome"] #SM_splitter
{
visibility: collapse;
}
/*プリントプレビュー*/
#print-preview-toolbar[printpreview="true"] + #navigator-toolbox + #browser #SM_toolbox,
#print-preview-toolbar[printpreview="true"] + #navigator-toolbox + #browser #SM_splitter
{
visibility:collapse;
}
#SM_tabpanels
{
padding: 0;
margin: {MARGINHACK}; /*hack*/
}
#SM_tabpanels:not(.titlebar-color) {
background-color: var(--toolbar-bgcolor);
color: var(--toolbar-color);
}
#SM_Button,
#SM_Button:-moz-lwtheme-darktext
{
list-style-image: url('data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABAAAAAQCAYAAAAf8/9hAAAAQ0lEQVQ4jWNgoAL4z8DA8N/AwAArTQRGFSBBI4YBDHhonC6n3AA1NTUMZ6F5gyQXYFNEsheweWnUBfRyAbmYcgMoAgBFX4a/wlDliwAAAABJRU5ErkJggg==');
}
#SM_Button:-moz-lwtheme-brighttext
{
list-style-image: url('data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABAAAAAQCAYAAAAf8/9hAAAANklEQVQ4jWP4TyFg+P///38GBgayMHUNwEdjdTrVDcDnTKJdgEsRSV5ACaBRF9DZBQObFygBAMeIxVdCQIJTAAAAAElFTkSuQmCC');
}
`;
style = style.replace(/\s+/g, " ").replace(/\{SM_WIDTH\}/g, this.SM_WIDTH).replace(/\{MARGINHACK\}/g, MARGINHACK);
let sspi = document.createProcessingInstruction(
'xml-stylesheet',
'type="text/css" href="data:text/css,' + encodeURIComponent(style) + '"'
);
document.insertBefore(sspi, document.documentElement);
sspi.getAttribute = function(name) {
return document.documentElement.getAttribute(name);
};
Components.utils.import("resource:///modules/CustomizableUI.jsm");
// xxxx try-catch may need for 2nd window
try {
CustomizableUI.createWidget({ //must run createWidget before windowListener.register because the register function needs the button added first
id: 'SM_Button',
type: 'custom',
defaultArea: CustomizableUI.AREA_NAVBAR,
onBuild: function(aDocument) {
var toolbaritem = aDocument.createElementNS('http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul', 'toolbarbutton');
var props = {
id: "SM_Button",
class: "toolbarbutton-1 chromeclass-toolbar-additional",
tooltiptext: "Sidebar Modoki",
oncommand: "SidebarModoki.toggle();",
type: "button",
label: "Sidebar Modoki",
removable: "true"
};
for (var p in props) {
toolbaritem.setAttribute(p, props[p]);
}
return toolbaritem;
}
});
}catch(e){}
let template = ["command", {id: "cmd_SidebarModoki", oncommand: "SidebarModoki.toggle()"}];
document.getElementById("mainCommandSet").appendChild(this.jsonToDOM(template, document, {}));
template = ["key", {id: "key_SidebarModoki", key: "B", modifiers: "accel,alt", command: "cmd_SidebarModoki",}];
document.getElementById("mainKeyset").appendChild(this.jsonToDOM(template, document, {}));
template =
["vbox", {id: "SM_toolbox", ordinal: this.SM_RIGHT ? "10" : "0"},
["hbox", {id: "SM_header", align: "center"},
["label", {}, "SidebarModoki"],
["spacer", {flex: "1000"}],
["toolbarbutton", {id: "SM_closeButton", class: "close-icon tabbable", tooltiptext: "Close SidebarModoki", oncommand: "SidebarModoki.close();"}]
],
["tabbox", {flex: "1"},
["tabs", {id: "SM_tabs"},
["tab", {id: "SM_tab0", label: this.TAB0_LABEL}],
["tab", {id: "SM_tab1", label: this.TAB1_LABEL}],
["tab", {id: "SM_tab2", label: this.TAB2_LABEL}]
],
["tabpanels", {id: "SM_tabpanels", flex: "1", style: "border: none;"},
["tabpanel", {id: "SM_tab0-container", orient: "vertical", flex: "1"},
["browser", {id: "SM_tab0-browser", flex: "1", autoscroll: "false", src: this.TAB0_SRC}]
],
["tabpanel", {id: "SM_tab1-container", orient: "vertical", flex: "1"},
["browser", {id: "SM_tab1-browser", flex: "1", autoscroll: "false", src: this.TAB1_SRC}]
],
["tabpanel", {id: "SM_tab2-container", orient: "vertical", flex: "1"},
["browser", {id: "SM_tab2-browser", flex: "1", autoscroll: "false", src: this.TAB2_SRC}]
]
]
]
];
let sidebar = document.getElementById("sidebar-box");
sidebar.parentNode.insertBefore(this.jsonToDOM(template, document, {}), sidebar);
template =
["splitter", {id: "SM_splitter", ordinal: this.SM_RIGHT ? "9" : "0", state: "open", collapse: this.SM_RIGHT ? "after" :"before", resizebefore: "closest", resizeafter: "closest"},
["grippy", {}]
];
sidebar.parentNode.insertBefore(this.jsonToDOM(template, document, {}), sidebar);
//xxx 69 hack
let index = document.getElementById("SM_tabpanels").selectedIndex;
let tb0 = document.getElementById("SM_tab0");
let tb1 = document.getElementById("SM_tab1");
let tb2 = document.getElementById("SM_tab2");
tb0.parentNode.insertBefore(tb0, tb1);
tb0.parentNode.insertBefore(tb1, tb2);
document.getElementById("SM_tabs").selectedIndex = index;
setTimeout(function(){this.observe();}.bind(this), 0);
//F11 fullscreen
FullScreen.showNavToolbox_org = FullScreen.showNavToolbox;
FullScreen.showNavToolbox = function(trackMouse = true) {
FullScreen.showNavToolbox_org(trackMouse);
if (!!SidebarModoki.ToolBox) {
SidebarModoki.ToolBox.removeAttribute("moz-collapsed");
SidebarModoki.Splitter.removeAttribute("moz-collapsed");
}
}
FullScreen.hideNavToolbox_org = FullScreen.hideNavToolbox;
FullScreen.hideNavToolbox = function(aAnimate = false) {
FullScreen.hideNavToolbox_org(aAnimate);
if (SidebarModoki.SM_AUTOHIDE && !!SidebarModoki.ToolBox) {
SidebarModoki.ToolBox.setAttribute("moz-collapsed", "true");
SidebarModoki.Splitter.setAttribute("moz-collapsed", "true");
}
}
//DOM fullscreen
window.addEventListener("MozDOMFullscreen:Entered", this,
/* useCapture */ true,
/* wantsUntrusted */ false);
window.addEventListener("MozDOMFullscreen:Exited", this,
/* useCapture */ true,
/* wantsUntrusted */ false);
},
observe: function() {
this.ToolBox = document.getElementById("SM_toolbox");
this.Splitter = document.getElementById("SM_splitter");
if (this.getPref(this.kSM_Open, "bool", true)) {
this.toggle(true);
} else {
this.close();
}
document.getElementById("SM_tabs").addEventListener("focus", this, true);
window.addEventListener("aftercustomization", this, false);
// xxxx native sidebar changes ordinal when change position of the native sidebar and open/close
this.SM_Observer = new MutationObserver(function(mutations) {
mutations.forEach(function(mutation) {
switch (mutation.attributeName) {
case "ordinal":
this.ToolBox.setAttribute("ordinal", this.SM_RIGHT ? "10" : "0");
this.Splitter.setAttribute("ordinal", this.SM_RIGHT ? "9" : "0");
break;
}
}.bind(this));
}.bind(this));
// pass in the target node, as well as the observer options
this.SM_Observer.observe(document.getElementById("appcontent"),
{attribute: true, attributeFilter: ["ordinal"]});
},
onSelect: function(event) {
let aIndex = document.getElementById("SM_tabpanels").selectedIndex;
this.prefs.setIntPref(this.kSM_lastSelectedTabIndex, aIndex);
width = this.getPref(this.kSM_lastSelectedTabWidth + aIndex, "int", this.SM_WIDTH);
document.getElementById("SM_toolbox").width = width;
},
toggle: function(forceopen) {
this.Button = document.getElementById("SM_Button");
if (!this.Button.hasAttribute("checked") || forceopen) {
this.Button.setAttribute("checked", true);
this.ToolBox.style.removeProperty("visibility");
this.Splitter.style.removeProperty("visibility");
let index = this.getPref(this.kSM_lastSelectedTabIndex, "int", 0);
document.getElementById("SM_tabs").selectedIndex = index;
width = this.getPref(this.kSM_lastSelectedTabWidth + index, "int", this.SM_WIDTH);
document.getElementById("SM_toolbox").width = width;
this.prefs.setBoolPref(this.kSM_Open, true)
addEventListener("resize", this, false);
} else {
this.close();
}
},
close: function() {
removeEventListener("resize", this, false);
this.Button = document.getElementById("SM_Button");
this.Button.removeAttribute("checked");
this.ToolBox.style.setProperty("visibility", "collapse", "");
this.Splitter.style.setProperty("visibility", "collapse", "");
this.prefs.setBoolPref(this.kSM_Open, false)
},
//ここからは, 大きさの調整
onResize: function(event) {
let width = this.ToolBox.getBoundingClientRect().width;
let aIndex = document.getElementById("SM_tabs").selectedIndex;
this.prefs.setIntPref(this.kSM_lastSelectedTabWidth + aIndex, width);
},
handleEvent: function(event) {
switch(event.type) {
case 'focus':
this.onSelect(event);
break;
case 'resize':
this.onResize(event);
break;
case 'MozDOMFullscreen:Entered':
if (!!this.ToolBox) {
this.ToolBox.setAttribute("moz-collapsed", "true");
this.Splitter.setAttribute("moz-collapsed", "true");
}
break;
case 'MozDOMFullscreen:Exited':
if (!!this.ToolBox) {
this.ToolBox.removeAttribute("moz-collapsed");
this.Splitter.removeAttribute("moz-collapsed");
}
break;
case 'aftercustomization':
if (this.getPref(this.kSM_Open, "bool", true)) {
this.Button.setAttribute("checked", true);
}
break;
}
},
//pref読み込み
getPref: function(aPrefString, aPrefType, aDefault) {
try{
switch (aPrefType){
case "str":
return this.prefs.getCharPref(aPrefString).toString(); break;
case "int":
return this.prefs.getIntPref(aPrefString); break;
case "bool":
default:
return this.prefs.getBoolPref(aPrefString); break;
}
}catch(e){
}
return aDefault;
}
}
SidebarModoki.init();