forked from aminomancer/uc.css.js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbackspacePanelNav.uc.js
136 lines (134 loc) · 5.87 KB
/
backspacePanelNav.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
// ==UserScript==
// @name Backspace Panel Navigation
// @version 1.0
// @author aminomancer
// @homepage https://github.com/aminomancer
// @description Press backspace to navigate back/forward in popup panels.
// @license This Source Code Form is subject to the terms of the Creative Commons Attribution-NonCommercial-ShareAlike International License, v. 4.0. If a copy of the CC BY-NC-SA 4.0 was not distributed with this file, You can obtain one at http://creativecommons.org/licenses/by-nc-sa/4.0/ or send a letter to Creative Commons, PO Box 1866, Mountain View, CA 94042, USA.
// ==/UserScript==
(function () {
function init() {
let appMenu = PanelView.forNode(PanelUI.mainView);
Object.getPrototypeOf(appMenu).keyNavigation = function (event) {
if (!this.active) return;
let focus = this.document.activeElement;
if (
focus &&
!(this.node.compareDocumentPosition(focus) & Node.DOCUMENT_POSITION_CONTAINED_BY)
)
focus = null;
if (focus && (focus.tagName == "browser" || focus.tagName == "iframe")) return;
let stop = () => {
event.stopPropagation();
event.preventDefault();
};
let tabOnly = () => {
return focus && this._isNavigableWithTabOnly(focus);
};
let isContextMenuOpen = () => {
if (!focus) return false;
let contextNode = focus.closest("[context]");
if (!contextNode) return false;
let context = contextNode.getAttribute("context");
if (!context) return false;
let popup = this.document.getElementById(context);
return popup && popup.state == "open";
};
let keyCode = event.code;
switch (keyCode) {
case "ArrowDown":
case "ArrowUp":
if (tabOnly()) break;
case "Tab": {
if (
isContextMenuOpen() ||
(focus && focus.localName == "menulist" && focus.open)
)
break;
stop();
let isDown = keyCode == "ArrowDown" || (keyCode == "Tab" && !event.shiftKey);
let button = this.moveSelection(isDown, keyCode != "Tab");
Services.focus.setFocus(button, Services.focus.FLAG_BYKEY);
break;
}
case "Home":
if (tabOnly() || isContextMenuOpen()) break;
stop();
this.focusFirstNavigableElement(true);
break;
case "End":
if (tabOnly() || isContextMenuOpen()) break;
stop();
this.focusLastNavigableElement(true);
break;
case "Backspace":
if (tabOnly() || isContextMenuOpen()) break;
stop();
if (PanelMultiView.forNode(this.node.panelMultiView).openViews.length > 1)
this.node.panelMultiView.goBack();
else PanelMultiView.forNode(this.node.panelMultiView).hidePopup();
break;
case "ArrowLeft":
case "ArrowRight": {
if (tabOnly() || isContextMenuOpen()) break;
stop();
if (
(!this.window.RTL_UI && keyCode == "ArrowLeft") ||
(this.window.RTL_UI && keyCode == "ArrowRight")
) {
this.node.panelMultiView.goBack();
break;
}
let button = this.selectedElement;
if (!button || !button.classList.contains("subviewbutton-nav")) break;
}
case "Space":
case "NumpadEnter":
case "Enter": {
if (tabOnly() || isContextMenuOpen()) break;
let button = this.selectedElement;
if (!button) break;
stop();
this._doingKeyboardActivation = true;
let commandEvent = event.target.ownerDocument.createEvent("xulcommandevent");
commandEvent.initCommandEvent(
"command",
true,
true,
event.target.ownerGlobal,
0,
event.ctrlKey,
event.altKey,
event.shiftKey,
event.metaKey,
0,
null,
0
);
button.dispatchEvent(commandEvent);
let dispEvent = new event.target.ownerGlobal.MouseEvent("mousedown", {
bubbles: true,
});
button.dispatchEvent(dispEvent);
dispEvent = new event.target.ownerGlobal.MouseEvent("click", {
bubbles: true,
});
button.dispatchEvent(dispEvent);
this._doingKeyboardActivation = false;
break;
}
}
};
}
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");
}
})();