-
Notifications
You must be signed in to change notification settings - Fork 35
Expand file tree
/
Copy pathUrlBarProgress.uc.js
More file actions
257 lines (222 loc) · 8.49 KB
/
Copy pathUrlBarProgress.uc.js
File metadata and controls
257 lines (222 loc) · 8.49 KB
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
// ==UserScript==
// @name UrlBarProgress.uc.js
// @description 在地址栏背景显示当前页面加载进度
// @license MIT License
// @compatibility Firefox 152
// @version 20260625.1
// @charset UTF-8
// @include main
// @homepageURL https://github.com/VicDobrov/UserChromeFiles/blob/main/profile_ucf_dobrov/chrome/user_chrome_files/custom_scripts/LocationBarEnhancer.js
// @note 20260625 迁移为当前 userChrome.js loader 可直接加载的普通 .uc.js 脚本
// @note 20260625 改为每窗口初始化与清理,避免重复监听和样式残留
// @note 20260625 about/chrome/resource/moz-src 等内置页面不显示进度
// @note 20260625 限定进度样式作用域,避免地址栏下拉面板显示加载动画
// ==/UserScript==
(function (css) {
"use strict";
const SCRIPT_ID = "urlbar-progress";
const STYLE_ID = `${SCRIPT_ID}-style`;
const ROOT_ATTR = "urlbar-progress-root";
const MIN_FINISH_PROGRESS = 0.95;
const COMPLETE_DELAY = 1000;
const HIDE_DELAY = 1000;
const INTERNAL_SCHEMES = new Set([
"about",
"chrome",
"moz-icon",
"moz-src",
"resource"
]);
const Services = globalThis.Services || Cu.import("resource://gre/modules/Services.jsm").Services;
function addStyle(doc) {
const oldStyle = doc.getElementById(STYLE_ID);
if (oldStyle) {
oldStyle.remove();
}
const style = doc.createElementNS("http://www.w3.org/1999/xhtml", "style");
style.id = STYLE_ID;
style.textContent = css;
return doc.documentElement.appendChild(style);
}
function init() {
if (window.UrlBarProgress) {
window.UrlBarProgress.destroy();
}
const urlbar = document.getElementById("urlbar");
const urlbarBackground = urlbar?.querySelector("#urlbar-input-container > #urlbar-background")
|| urlbar?.querySelector(".urlbar-input-container > .urlbar-background")
|| document.getElementById("urlbar-background")
|| urlbar?.querySelector(".urlbar-background")
|| document.querySelector(".urlbar-background");
if (!urlbarBackground || !window.gBrowser) {
return;
}
const style = addStyle(document);
const timers = new Set();
let pageProgress = 0;
urlbarBackground.setAttribute(ROOT_ATTR, "true");
function setProgress(value) {
const progress = Math.max(0, Math.min(1, Number(value) || 0));
pageProgress = progress;
urlbarBackground.style.backgroundSize = `${progress * 100}% 100%`;
if (progress > 0) {
urlbarBackground.setAttribute("urlbar-progress-loading", "true");
} else {
urlbarBackground.removeAttribute("urlbar-progress-loading");
}
}
function resetProgress() {
pageProgress = 0;
urlbarBackground.style.backgroundSize = "0% 100%";
urlbarBackground.removeAttribute("urlbar-progress-loading");
}
function getRequestURI(request) {
if (!request) {
return null;
}
try {
return request.QueryInterface(Ci.nsIChannel).URI;
} catch (e) {
return null;
}
}
function isInternalURI(uri) {
if (!uri) {
return false;
}
let scheme = "";
try {
scheme = uri.scheme;
} catch (e) {
return false;
}
if (scheme === "view-source") {
try {
return isInternalURI(Services.io.newURI(uri.spec.slice("view-source:".length)));
} catch (e) {
return false;
}
}
return INTERNAL_SCHEMES.has(scheme);
}
function shouldShowProgress(browser, request) {
if (browser !== gBrowser.selectedBrowser) {
return false;
}
const requestURI = getRequestURI(request);
const browserURI = browser.currentURI;
return !isInternalURI(requestURI) && !isInternalURI(browserURI);
}
function delay(callback, timeout) {
const timer = window.setTimeout(() => {
timers.delete(timer);
callback();
}, timeout);
timers.add(timer);
}
const listener = {
onChangeTab() {
resetProgress();
},
onProgressChange(browser, webProgress, request, curSelfProgress, maxSelfProgress, curTotalProgress, maxTotalProgress) {
if (!shouldShowProgress(browser, request) || maxTotalProgress <= 0 || curTotalProgress < 0) {
return;
}
const progress = Math.max(pageProgress, curTotalProgress / maxTotalProgress);
setProgress(progress);
if (progress > 0.9) {
delay(() => {
if (pageProgress > MIN_FINISH_PROGRESS) {
setProgress(1);
}
}, COMPLETE_DELAY);
}
},
onStateChange(browser, webProgress, request, stateFlags) {
if (browser !== gBrowser.selectedBrowser) {
return;
}
const wpl = Ci.nsIWebProgressListener;
if (stateFlags & wpl.STATE_START && stateFlags & wpl.STATE_IS_NETWORK) {
if (!shouldShowProgress(browser, request)) {
resetProgress();
return;
}
setProgress(0.08);
return;
}
if (stateFlags & wpl.STATE_STOP && stateFlags & wpl.STATE_IS_NETWORK) {
if (!shouldShowProgress(browser, request)) {
resetProgress();
return;
}
setProgress(1);
delay(resetProgress, HIDE_DELAY);
}
}
};
gBrowser.tabContainer.addEventListener("TabSelect", listener.onChangeTab);
gBrowser.addTabsProgressListener(listener);
resetProgress();
window.UrlBarProgress = {
destroy() {
for (const timer of timers) {
window.clearTimeout(timer);
}
timers.clear();
gBrowser.tabContainer.removeEventListener("TabSelect", listener.onChangeTab);
gBrowser.removeTabsProgressListener(listener);
urlbarBackground.style.removeProperty("background-size");
urlbarBackground.removeAttribute("urlbar-progress-loading");
urlbarBackground.removeAttribute(ROOT_ATTR);
style.remove();
if (window.UrlBarProgress === this) {
delete window.UrlBarProgress;
}
}
};
window.addEventListener("unload", () => window.UrlBarProgress?.destroy(), { once: true });
}
if (gBrowserInit.delayedStartupFinished) {
init();
} else {
const 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");
}
})(`
[urlbar-progress-root] {
background-image:
repeating-linear-gradient(
-45deg,
rgba(255, 255, 255, 0),
rgba(255, 255, 255, 0) 6px,
rgba(255, 255, 255, .28) 6px,
rgba(255, 255, 255, .28) 12px
),
linear-gradient(to right, #72d3ff 0%, #bff7ee 70%, #e8fff6 100%);
background-position: 0 0;
background-repeat: repeat-x, no-repeat;
background-size: 0% 100%;
transition: background-size 350ms ease;
}
[urlbar-progress-root][urlbar-progress-loading] {
animation: urlbar-progress-stripes 2s linear infinite;
}
#urlbar[open] [urlbar-progress-root] {
background-image: none;
animation: none;
}
@keyframes urlbar-progress-stripes {
from {
background-position: 0 0;
}
to {
background-position: 51px 0;
}
}
`);