forked from aminomancer/uc.css.js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
bookmarksPopupShadowRoot.uc.js
140 lines (133 loc) · 5.74 KB
/
bookmarksPopupShadowRoot.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
// ==UserScript==
// @name Bookmarks Popup Mods
// @version 1.2.3
// @author aminomancer
// @homepageURL https://github.com/aminomancer/uc.css.js
// @long-description
// @description
/*
Implement smooth scrolling for all bookmarks popups that are tall enough to scroll. Add special click functions to their scroll buttons — hovering a scroll button will scroll at a constant rate, as normal. (though faster than vanilla) But clicking a scroll button will immediately jump to the top/bottom of the list.
This script no longer styles the scroll buttons, since I now style all arrowscrollbox scrollbuttons equally with [arrowscrollbox.css][]. To do that requires [chrome.manifest][]. That file replaces the built-in arrowscrollbox.css with my version that makes the scrollbuttons look a lot prettier. If you want to customize them, just edit arrowscrollbox.css. This script still adds custom classes though, in case you want to use them to style the elements in userChrome.css.
[arrowscrollbox.css]: https://github.com/aminomancer/uc.css.js/blob/master/resources/layout/arrowscrollbox.css
[chrome.manifest]: https://github.com/aminomancer/uc.css.js/blob/master/utils/chrome.manifest
*/
// @downloadURL https://cdn.jsdelivr.net/gh/aminomancer/uc.css.js@master/JS/bookmarksPopupShadowRoot.uc.js
// @updateURL https://cdn.jsdelivr.net/gh/aminomancer/uc.css.js@master/JS/bookmarksPopupShadowRoot.uc.js
// @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.
// @include main
// ==/UserScript==
(function() {
const bookmarksPopupShadowRoot = {
handleEvent(e) {
if (!e.target.getAttribute("placespopup") || !e.target.scrollBox) return;
if (!e.target.getAttribute("uc-init")) {
setTimeout(() => {
this.checkPopups(e.target);
}, 0);
}
let scrollbox = e.target.scrollBox.scrollbox;
let height = window.screen.availHeight;
e.target.scrollBox.parentElement?.classList.toggle(
"BMBsmallContentBox",
scrollbox.scrollTopMax < height && scrollbox.clientHeight < height
);
},
checkPopups(popup) {
popup.setAttribute("uc-init", true);
this.setUpScroll(popup);
},
setUpScroll(popup) {
popup.scrollBox.parentElement.classList.add("BMB-special-innerbox");
popup.scrollBox.smoothScroll = true;
popup.scrollBox._scrollIncrement = 150;
popup.scrollBox._scrollButtonUp.classList.add(
"BMB-special-scrollbutton-up"
);
popup.scrollBox._scrollButtonDown.classList.add(
"BMB-special-scrollbutton-down"
);
popup.scrollBox._onButtonMouseOver = function _onButtonMouseOver(index) {
if (
this._ensureElementIsVisibleAnimationFrame ||
this._arrowScrollAnim.requestHandle
) {
return;
}
if (this._clickToScroll) this._continueScroll(index);
else this._startScroll(index);
};
popup.scrollBox._onButtonMouseOut = function _onButtonMouseOut() {
if (
this._ensureElementIsVisibleAnimationFrame ||
this._arrowScrollAnim.requestHandle
) {
return;
}
if (this._clickToScroll) this._pauseScroll();
else this._stopScroll();
};
popup.scrollBox._scrollButtonDown.onclick = function scrollToBottom() {
bookmarksPopupShadowRoot.scrollByIndex(
popup.scrollBox,
popup.children.length
);
};
popup.scrollBox._scrollButtonUp.onclick = function scrollToTop() {
bookmarksPopupShadowRoot.scrollByIndex(
popup.scrollBox,
-popup.children.length
);
};
},
scrollByIndex(box, index, aInstant) {
if (index == 0) return;
var rect = box.scrollClientRect;
var [start, end] = box.startEndProps;
var x = index > 0 ? rect[end] + 1 : rect[start] - 1;
var nextElement = box._elementFromPoint(x, index);
if (!nextElement) return;
var targetElement;
if (box.isRTLScrollbox) index *= -1;
while (index < 0 && nextElement) {
if (box._canScrollToElement(nextElement)) targetElement = nextElement;
nextElement = nextElement.previousElementSibling;
index++;
}
while (index > 0 && nextElement) {
if (box._canScrollToElement(nextElement)) targetElement = nextElement;
nextElement = nextElement.nextElementSibling;
index--;
}
if (!targetElement || !box._canScrollToElement(targetElement)) return;
box._stopScroll();
let animFrame = window.requestAnimationFrame(() => {
targetElement.scrollIntoView({
block: "nearest",
behavior: aInstant ? "instant" : "auto",
});
box._ensureElementIsVisibleAnimationFrame = 0;
box._arrowScrollAnim.requestHandle = 0;
});
box._ensureElementIsVisibleAnimationFrame = animFrame;
box._arrowScrollAnim.requestHandle = animFrame;
},
init() {
addEventListener("popupshowing", this, true);
CustomizableUI.removeListener(this);
},
};
if (gBrowserInit.delayedStartupFinished) {
bookmarksPopupShadowRoot.init();
} else {
let delayedListener = (subject, topic) => {
if (topic == "browser-delayed-startup-finished" && subject == window) {
Services.obs.removeObserver(delayedListener, topic);
bookmarksPopupShadowRoot.init();
}
};
Services.obs.addObserver(
delayedListener,
"browser-delayed-startup-finished"
);
}
})();