forked from aminomancer/uc.css.js
-
Notifications
You must be signed in to change notification settings - Fork 1
/
toggleMenubarHotkey.uc.js
158 lines (154 loc) · 7.32 KB
/
toggleMenubarHotkey.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
// ==UserScript==
// @name Toggle Menubar Hotkey
// @version 1.0
// @author aminomancer
// @homepage https://github.com/aminomancer
// @description Press alt+M to toggle the menubar.
// ==/UserScript==
(function () {
const hotkeyId = "key_toggleMenubar";
let hotkeyRegistered = _ucUtils.registerHotkey(
{
id: hotkeyId,
modifiers: "alt",
key: "M",
},
(win, key) => {
if (win === window)
Services.obs.notifyObservers(
null,
"browser-set-toolbar-visibility",
JSON.stringify([
CustomizableUI.AREA_MENUBAR,
AutoHideMenubar._node.getAttribute("inactive"),
])
);
}
);
function init() {
if (!hotkeyRegistered) return;
document.getElementById("toolbar-menubar").setAttribute("key", hotkeyId);
window.onViewToolbarsPopupShowing = function (aEvent, aInsertPoint) {
var popup = aEvent.target;
if (popup != aEvent.currentTarget) return;
for (var i = popup.children.length - 1; i >= 0; --i) {
var deadItem = popup.children[i];
if (deadItem.hasAttribute("toolbarId")) popup.removeChild(deadItem);
}
MozXULElement.insertFTLIfNeeded("browser/toolbarContextMenu.ftl");
let firstMenuItem = aInsertPoint || popup.firstElementChild;
let toolbarNodes = gNavToolbox.querySelectorAll("toolbar");
for (let toolbar of toolbarNodes) {
if (!toolbar.hasAttribute("toolbarname")) continue;
if (toolbar.id == "PersonalToolbar" && gBookmarksToolbar2h2020) {
let menu = BookmarkingUI.buildBookmarksToolbarSubmenu(toolbar);
popup.insertBefore(menu, firstMenuItem);
} else {
let menuItem = document.createXULElement("menuitem");
menuItem.setAttribute("id", "toggle_" + toolbar.id);
menuItem.setAttribute("toolbarId", toolbar.id);
menuItem.setAttribute("type", "checkbox");
menuItem.setAttribute("label", toolbar.getAttribute("toolbarname"));
let hidingAttribute =
toolbar.getAttribute("type") == "menubar" ? "autohide" : "collapsed";
menuItem.setAttribute(
"checked",
toolbar.getAttribute(hidingAttribute) != "true"
);
menuItem.setAttribute("accesskey", toolbar.getAttribute("accesskey"));
if (toolbar.hasAttribute("key"))
menuItem.setAttribute("key", toolbar.getAttribute("key"));
popup.insertBefore(menuItem, firstMenuItem);
menuItem.addEventListener("command", onViewToolbarCommand);
}
}
let moveToPanel = popup.querySelector(".customize-context-moveToPanel");
let removeFromToolbar = popup.querySelector(".customize-context-removeFromToolbar");
if (!moveToPanel || !removeFromToolbar) return;
let toolbarItem = popup.triggerNode;
if (toolbarItem && toolbarItem.localName == "toolbarpaletteitem")
toolbarItem = toolbarItem.firstElementChild;
else if (toolbarItem && toolbarItem.localName != "toolbar")
while (toolbarItem && toolbarItem.parentElement) {
let parent = toolbarItem.parentElement;
if (
(parent.classList && parent.classList.contains("customization-target")) ||
parent.getAttribute("overflowfortoolbar") ||
parent.localName == "toolbarpaletteitem" ||
parent.localName == "toolbar"
)
break;
toolbarItem = parent;
}
else toolbarItem = null;
let showTabStripItems = toolbarItem && toolbarItem.id == "tabbrowser-tabs";
for (let node of popup.querySelectorAll('menuitem[contexttype="toolbaritem"]'))
node.hidden = showTabStripItems;
for (let node of popup.querySelectorAll('menuitem[contexttype="tabbar"]'))
node.hidden = !showTabStripItems;
document
.getElementById("toolbar-context-menu")
.querySelectorAll("[data-lazy-l10n-id]")
.forEach((el) => {
el.setAttribute("data-l10n-id", el.getAttribute("data-lazy-l10n-id"));
el.removeAttribute("data-lazy-l10n-id");
});
let menuSeparator = document.getElementById("toolbarItemsMenuSeparator");
menuSeparator.hidden = false;
document.getElementById(
"toolbarNavigatorItemsMenuSeparator"
).hidden = !showTabStripItems;
if (
!CustomizationHandler.isCustomizing() &&
CustomizableUI.isSpecialWidget(toolbarItem?.id || "")
) {
moveToPanel.hidden = true;
removeFromToolbar.hidden = true;
menuSeparator.hidden = !showTabStripItems;
}
if (showTabStripItems) {
let multipleTabsSelected = !!gBrowser.multiSelectedTabsCount;
document.getElementById(
"toolbar-context-bookmarkSelectedTabs"
).hidden = !multipleTabsSelected;
document.getElementById(
"toolbar-context-bookmarkSelectedTab"
).hidden = multipleTabsSelected;
document.getElementById(
"toolbar-context-reloadSelectedTabs"
).hidden = !multipleTabsSelected;
document.getElementById(
"toolbar-context-reloadSelectedTab"
).hidden = multipleTabsSelected;
document.getElementById(
"toolbar-context-selectAllTabs"
).disabled = gBrowser.allTabsSelected();
document.getElementById("toolbar-context-undoCloseTab").disabled =
SessionStore.getClosedTabCount(window) == 0;
return;
}
let movable =
toolbarItem && toolbarItem.id && CustomizableUI.isWidgetRemovable(toolbarItem);
if (movable) {
if (CustomizableUI.isSpecialWidget(toolbarItem.id))
moveToPanel.setAttribute("disabled", true);
else moveToPanel.removeAttribute("disabled");
removeFromToolbar.removeAttribute("disabled");
} else {
moveToPanel.setAttribute("disabled", true);
removeFromToolbar.setAttribute("disabled", true);
}
};
}
if (gBrowserInit.delayedStartupFinished) {
init();
} else {
let delayedListener = (subject, topic) => {
if (topic == "browser-delayed-startup-finished" && subject == window) {
Services.obs.removeObserver(delayedListener, topic);
init();
}
};
Services.obs.addObserver(delayedListener, "browser-delayed-startup-finished");
}
})();