-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathFloatToolbarsInFullScreen@Merci.chao.uc.js
More file actions
391 lines (349 loc) · 12.9 KB
/
Copy pathFloatToolbarsInFullScreen@Merci.chao.uc.js
File metadata and controls
391 lines (349 loc) · 12.9 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
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
// ==UserScript==
// @name Float Toolbars in Full Screen
// @description Float the toolbars over the page in full screen mode, instead of making the web page jumpy when the toolbars showing / hiding.
// @version 2026-05-04
// @author Merci chao
// @homepageURL https://github.com/Merci-chao/userChrome.js#float-toolbars-in-full-screen
// @changelogURL https://github.com/Merci-chao/userChrome.js#changelog-4
// @supportURL https://github.com/Merci-chao/userChrome.js/issues/new
// @updateURL https://github.com/Merci-chao/userChrome.js/raw/refs/heads/main/FloatToolbarsInFullScreen@Merci.chao.uc.js
// ==/UserScript==
/* global
Services, Cc, Ci, gNotificationBox, FullScreen, fullScreen, gNavToolbox
*/
if (document.documentElement.matches(`[windowtype="navigator:browser"]`))
try {
const SCRIPT_NAME = "Float Toolbars in Full Screen";
const SCRIPT_FILE_NAME = "FloatToolbarsInFullScreen@Merci.chao.uc.js";
let prefBranchStr = "extensions.FloatToolbarsInFullScreen@Merci.chao.";
let defPrefs = {
checkUpdate: 1,
checkUpdateFrequency: 7,
checkUpdateAutoApply: 1,
};
let setDefaultPrefs = (branch, data) => Object.entries(data).forEach(([name, value]) =>
branch[`set${{string:"String",number:"Int",boolean:"Bool"}[typeof value]}Pref`](name, value));
let getPrefs = (branch, data) => Object.fromEntries(Object.entries(data).map(([name, value]) =>
[name, branch[`get${{string:"String",number:"Int",boolean:"Bool"}[typeof value]}Pref`](name)]));
setDefaultPrefs(Services.prefs.getDefaultBranch(prefBranchStr), defPrefs);
let prefs = getPrefs(Services.prefs.getBranch(prefBranchStr), defPrefs);
const SCRIPT_ID = 4;
if (prefs.checkUpdate && (Date.now() / 1000 - prefs.checkUpdate) / 60 / 60 / 24 >= Math.max(prefs.checkUpdateFrequency, 1)) {
Services.prefs.setIntPref(prefBranchStr + "checkUpdate", Date.now() / 1000);
(async () => {
let auto = prefs.checkUpdateAutoApply;
let getVer = code => code?.match(/^\/\/\s*@version\s+(.+?)\s*$/mi)?.[1];
let localFileURI = new Error().stack.match(/(?<=@).+?(?=:\d+:\d+$)/m)[0];
let localScript = await (await fetch(localFileURI)).text();
let updateURL = localScript.match(/^\/\/\s*@updateURL\s+(.+?)\s*$/mi)[1];
let homeURL = "https://github.com/Merci-chao/userChrome.js";
let remoteScript = (await (await fetch(updateURL)).text()).trim();
let local = getVer(localScript);
let remote = getVer(remoteScript);
if (
!remote ||
remote.localeCompare(local, undefined, {numeric: true}) <= 0
)
return;
let l10n = {
en: {
message: `${SCRIPT_NAME} (${SCRIPT_FILE_NAME}) version ${remote} is released.`,
update: "Update Now",
updateKey: "N",
download: "Update Manually",
downloadKey: "M",
changelog: "Changelog",
changelogKey: "C",
finished: "Done",
finishedKey: "D",
later: "Remind Tomorrow",
laterKey: "R",
link: "#changelog-" + SCRIPT_ID,
done: `${SCRIPT_NAME} has been updated to version ${remote}. You may restart Firefox to apply.`,
error: `Failed to apply the update of ${SCRIPT_NAME} version ${remote}. Please ensure the file is not read-only or locked by another program:`,
},
ja: {
message: `${SCRIPT_NAME}(${SCRIPT_FILE_NAME})の新バージョン ${remote} がリリースされました。`,
update: "今すぐ更新",
download: "手動で更新",
changelog: "変更履歴",
finished: "完了",
later: "明日再通知する",
link: "/blob/main/README.jp.md#変更履歴-" + SCRIPT_ID,
done: `${SCRIPT_NAME} ${remote} に更新しました。Firefox を再起動すると変更が適用になります。`,
error: `${SCRIPT_NAME} バージョン ${remote} の更新処理に失敗しました。ファイルが読み取り専用でないこと、または他のプログラムによってロックされていないことを確認してください:`,
},
};
l10n = Object.assign(l10n.en, l10n[Services.locale.appLocaleAsLangTag.split("-")[0]]);
if (auto > 1)
install();
else
showNotification(
l10n.message,
[
{
label: l10n.update,
accessKey: l10n.updateKey,
callback: install,
primary: true,
},
{
label: l10n.download,
accessKey: l10n.downloadKey,
callback: showChangelog,
},
{
label: l10n.later,
accessKey: l10n.laterKey,
callback: () => Services.prefs.setIntPref(
prefBranchStr + "checkUpdate",
Date.now() / 1000 - (Math.max(prefs.checkUpdateFrequency, 1) - 1) * 24 * 60 * 60,
),
},
],
"chrome://browser/skin/update-badge.svg",
);
async function showNotification(label, buttons, icon, color) {
let box = await gNotificationBox.appendNotification(
"multitabrows",
{
label,
priority: gNotificationBox.PRIORITY_INFO_HIGH,
},
buttons,
true,
);
if (icon) {
let node = box.shadowRoot.querySelector(".icon");
node.src = icon;
Object.assign(node.style, {
fill: color,
color,
});
node.style.setProperty("--message-bar-icon-url", `url(${icon})`);
}
}
function install() {
let file;
try {
/*global FileUtils, NetUtil*/
file =
NetUtil.newChannel({
uri: Services.io.newURI(localFileURI),
loadUsingSystemPrincipal: true,
}).URI.QueryInterface(Ci.nsIFileURL).file;
let converter = Cc["@mozilla.org/intl/converter-output-stream;1"]
.createInstance(Ci.nsIConverterOutputStream);
converter.init(
FileUtils.openFileOutputStream(
file,
FileUtils.MODE_WRONLY | FileUtils.MODE_TRUNCATE,
),
"UTF-8",
);
converter.writeString(remoteScript);
converter.close();
if (auto < 3)
//Delay a bit to make the installation feel like it's actually running
setTimeout(() => showNotification(
l10n.done,
[
{
label: l10n.changelog,
accessKey: l10n.changelogKey,
callback: showChangelog,
primary: true,
},
{
label: l10n.finished,
accessKey: l10n.finishedKey,
callback: () => {},
},
],
"chrome://browser/skin/migration/success.svg",
"var(--panel-banner-item-update-supported-bgcolor)",
), 500);
} catch (e) {
Services.prompt.alert(window, l10n.title, [l10n.error, file?.path || localFileURI, e.message].join("\n\n"));
return true;
}
}
function showChangelog() {
/* global openURL */
openURL(homeURL + l10n.link);
}
})();
}
function FloatToolbarsInFullScreen() {
Object.assign(this, {
transitionEndListener: null,
mouseOverNavToolbox: false,
originalFunctions: {},
});
this.bind();
}
FloatToolbarsInFullScreen.prototype = {
bind() {
let {originalFunctions} = this;
let docElt = document.documentElement;
let contentDeck = document.getElementById("browser");
if (fullScreen)
// eslint-disable-next-line no-global-assign
fullScreen = false;
window[FloatToolbarsInFullScreen.SYMBOL] = this;
["showNavToolbox", "hideNavToolbox", "toggle", "getMouseTargetRect",
"_setPopupOpen", "setAutohide"].forEach(fn =>
originalFunctions[fn] = FullScreen[fn]);
let mouseEventHandler = {
handleEvent: e => {
if (e.target == e.currentTarget)
switch(e.type) {
case "mouseenter":
this.mouseOverNavToolbox = true;
break;
case "mouseleave":
this.mouseOverNavToolbox = false;
break;
}
},
};
document.body.appendChild(document.createElement("style")).innerHTML = `
#navigator-toolbox[data-float-in-fullscreen-ready] {
z-index: calc(1/0) !important;
position: relative !important;
}
:root[data-float-in-fullscreen-hide-bg],
:root[data-float-in-fullscreen-hide-bg] #navigator-toolbox {
background: none !important;
}
`;
FullScreen.toggle = () => {
if (fullScreen) {
gNavToolbox.setAttribute("data-float-in-fullscreen-ready", true);
} else {
gNavToolbox.removeAttribute("data-float-in-fullscreen-ready");
this.mouseOverNavToolbox = false;
contentDeck.style.clipPath = "";
}
["mouseenter", "mouseleave"].forEach(e =>
gNavToolbox[fullScreen ? "addEventListener" : "removeEventListener"](e, mouseEventHandler, true));
docElt.toggleAttribute("data-float-in-fullscreen-hide-bg", fullScreen && needToHideBackground());
originalFunctions.toggle.call(FullScreen);
};
FullScreen.showNavToolbox = trackMouse => {
let {height} = gNavToolbox.getBoundingClientRect();
Object.assign(contentDeck.style, fullScreen ?
{
marginTop: `-${height}px`,
clipPath: `inset(${height}px 0 0 0)`,
} :
{
marginTop: "",
clipPath: "",
});
originalFunctions.showNavToolbox.call(FullScreen, trackMouse);
Object.assign(contentDeck.style, {
transitionDelay: "",
transitionDuration: "",
transitionProperty: "",
transitionTimingFunction: "",
});
if (fullScreen)
requestAnimationFrame(() => {
if (!gNavToolbox.hasAttribute("fullscreenShouldAnimate")) {
let {height} = gNavToolbox.getBoundingClientRect();
Object.assign(contentDeck.style, {
marginTop: `-${height}px`,
clipPath: `inset(${height}px 0 0 0)`,
});
}
});
FullScreen.fullScreenToggler.style.display = "";
gNavToolbox.style.pointerEvents = "";
gNavToolbox.removeEventListener("transitionend", this.transitionEndListener, true);
};
FullScreen.getMouseTargetRect = () => {
let rect = Object.assign({}, originalFunctions.getMouseTargetRect.call(FullScreen));
rect.top += gNavToolbox.getBoundingClientRect().height;
return rect;
};
FullScreen._setPopupOpen = aEvent => {
if (aEvent.type == "popuphidden")
setTimeout(() => {
if (document.querySelector(":is(menu,button,toolbarbutton)[open]:not([hidden]), [panelopen]:not([hidden]), panel[animate=open]:not([hidden])"))
return;
FullScreen._isPopupOpen = false;
if (!this.mouseOverNavToolbox)
originalFunctions._setPopupOpen.call(FullScreen, aEvent);
}, 50);
else
originalFunctions._setPopupOpen.call(FullScreen, aEvent);
};
FullScreen.hideNavToolbox = aAnimate => {
if (this.mouseOverNavToolbox
|| document.querySelector(":is(menu,button,toolbarbutton)[open]:not([hidden]), [panelopen]:not([hidden]), panel[animate=open]:not([hidden])"))
return;
if (contentDeck.style.marginTop)
aAnimate = false;
let toHide = !FullScreen._isChromeCollapsed;
originalFunctions.hideNavToolbox.call(FullScreen, aAnimate);
if (toHide)
if (aAnimate) {
let {
transitionDelay,
transitionDuration,
transitionTimingFunction
} = getComputedStyle(gNavToolbox);
Object.assign(contentDeck.style, {
transitionDelay,
transitionDuration,
transitionProperty: "margin-top",
transitionTimingFunction,
marginTop: "",
clipPath: "",
});
gNavToolbox.style.pointerEvents = "none";
FullScreen.fullScreenToggler.style.display = "none";
this.transitionEndListener = e => {
if (e.target != e.currentTarget)
return;
gNavToolbox.removeEventListener("transitionend", this.transitionEndListener, true);
Object.assign(contentDeck.style, {
transitionDelay: "",
transitionDuration: "",
transitionProperty: "",
transitionTimingFunction: "",
});
gNavToolbox.style.pointerEvents = "";
FullScreen.fullScreenToggler.style.display = "";
};
gNavToolbox.addEventListener("transitionend", this.transitionEndListener, true);
} else if (FullScreen.navToolboxHidden) {
Object.assign(contentDeck.style, {
transitionDuration: "",
marginTop: "",
clipPath: "",
});
}
};
FullScreen.setAutohide = () => {
originalFunctions.setAutohide.call(FullScreen);
if (Services.prefs.getBoolPref("browser.fullscreen.autohide"))
Object.assign(contentDeck.style, {marginTop: "", clipPath: ""});
else
//since the autohide is set via context menu, the hideNavToolbox may be called when the toolbox is trying to display
//call the showNavToolbox again to fix some weird conflict
FullScreen.showNavToolbox();
};
},
};
FloatToolbarsInFullScreen.SYMBOL = Symbol("FloatToolbarsInFullScreen");
new FloatToolbarsInFullScreen();
function needToHideBackground() {
let micaPref = "widget.windows.mica";
return Services.prefs.getPrefType(micaPref) &&
Services.prefs.getBoolPref(micaPref) &&
!matchMedia("(-moz-windows-accent-color-in-titlebar)").matches &&
["default-theme@mozilla.org", ""].includes(Services.prefs.getStringPref("extensions.activeThemeID"));
}
} catch(e) {alert(["FloatToolbarsInFullScreen@Merci.chao.uc.js",e,e.stack].join("\n"));console.error(e)}