-
Notifications
You must be signed in to change notification settings - Fork 35
/
Copy pathmiscMods.uc.js
347 lines (335 loc) · 18.7 KB
/
miscMods.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
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
// ==UserScript==
// @name miscMods.uc.js
// @long-description
// @description
/* 没有分类的脚本合集
1. 粘贴并转到增加 Access Key
2. 中键单击地址栏复制当前地址
3. 右键地址栏收藏按钮打开书签管理
4. 右键刷新按钮强制刷新
5. 右键 xiaoxiaoflood 的扩展管理管理器打开扩展管理页面
6. 中键下载按钮提示保存 URL
7. 右键下载按钮打开下载管理
8. 左键侧边栏按钮打开书签侧边栏
9. 中键侧边栏按钮切换侧边栏方向
10.右键侧边栏按钮打开历史侧边栏
11.双击侧边栏标题切换侧边栏显示位置
12.中键 SidebarModoki 按钮切换侧边栏方向
13.CTRL + F 开关查找栏
*/
// @license MIT License
// @compatibility Firefox 90
// @version 20250131
// @charset UTF-8
// @include chrome://browser/content/browser.xul
// @include chrome://browser/content/browser.xhtml
// @homepageURL https://github.com/benzBrake/FirefoxCustomize/tree/master/userChromeJS
// @note 20250131 修复 Bug 1937080 Block inline event handlers in Nightly and collect telemetry
// @note 20240806 修复 Ctrl + F 优先级太高,新增中键 SidebarModoki 按钮切换侧边栏方向
// @note 20240710 修复中键地址栏复制地址,修复中键下载按钮提示保存 URL
// @note 20240614 移除 Styloaix 按钮功能,继续修复 Bug 1880914
// @note 20240602 Bug 1892965 - Rename SidebarUI and SidebarLauncher
// @note 20240417 Bug 1880914 Move Browser* helper functions used from global menubar and similar commands to a single object in a separate file, loaded as-needed
// ==/UserScript==
(function () {
const CustomizableUI = globalThis.CustomizableUI || Cu.import("resource:///modules/CustomizableUI.jsm").CustomizableUI;
const Services = globalThis.Services || Cu.import("resource://gre/modules/Services.jsm").Services;
const SidebarController = globalThis.SidebarController || globalThis.SidebarUI;
const isZh = Services.locale.appLocaleAsLangTag.startsWith("zh");
const config = {
"urlbar paste and go add accesskey": { // 地址栏右键粘贴并前往增加 AccessKey
enabled: true, // true 是启用, false 是禁用
key: 'S'
},
"urlbar middle click copy url": true,
"searchbar paste and go add accesskey": { // 搜索框右键粘贴并搜索增加 AccessKey
enabled: true, // true 是启用, false 是禁用
key: 'S'
},
"star button box add middle and right click": true, // 书签按钮点击功能
"reload button right click to force reload": true, // 右键点击刷新按钮强制刷新
"right click panel ui button to open sidebar": true, // 右键点击三道杠按钮打开侧边栏
"right click extensions options menu button to open addons management": false, // https://github.com/xiaoxiaoflood/firefox-scripts/blob/master/chrome/extensionOptionsMenu.uc.js
"right click styloaix button to open themes management": true, // https://github.com/xiaoxiaoflood/firefox-scripts/blob/master/chrome/styloaix.uc.js
"downloads button add middle and right click": true, // 中键点击保存剪贴板链接,右键打开下载管理
"modify sidebar button behavior": true, // 左键侧边栏按钮打开书签侧边栏,中键侧边栏按钮切换侧边栏方向,右键侧边栏按钮打开历史侧边栏, 双击侧边栏标题按钮切换侧边栏方向
"middle click sidebarmodoki button toggle sidebar direction": true, // 中键 SidebarModoki 按钮切换侧边栏方向
"ctrl f to toggle findbar": true, // Ctrl + F 开关查找栏,
}
function MiscUtils () {
Services.obs.addObserver(this, 'domwindowopened', false);
}
MiscUtils.prototype = {
observe: function (aSubject, aTopic, aData) {
aSubject.addEventListener('load', this, true);
},
handleEvent: function (aEvent) {
if (aEvent.type === "load") {
let document = aEvent.originalTarget;
if (document.location.href.startsWith('chrome://browser/content/browser.x')) {
this.init(document, document.ownerGlobal);
}
}
},
init: function (document, window) {
if (config["urlbar paste and go add accesskey"].enabled) {
let accesskey = config["urlbar paste and go add accesskey"].accesskey || 'S';
let input = CustomizableUI.getWidget('urlbar-input').forWindow(window).node;
if (input)
input.addEventListener("contextmenu", function () {
document.getElementById('paste-and-go').setAttribute('accesskey', accesskey);
});
}
if (config["urlbar middle click copy url"]) {
let input = document.getElementById('urlbar');
if (input)
input.addEventListener('click', function (e) {
if (e.button == 1) {
e.preventDefault();
e.stopPropagation();
Cc["@mozilla.org/widget/clipboardhelper;1"].getService(Ci.nsIClipboardHelper).copyString(gBrowser.currentURI.spec);
}
}, false);
}
if (config["searchbar paste and go add accesskey"].enabled) {
let accesskey = config["urlbar paste and go add accesskey"].accesskey || 'S';
let input = CustomizableUI.getWidget('searchbar').forWindow(window).node;
if (input)
input.addEventListener("contextmenu", function () {
document.querySelector('.searchbar-paste-and-search').setAttribute('accesskey', accesskey);
});
}
if (config["star button box add middle and right click"]) {
let star = CustomizableUI.getWidget('star-button-box').forWindow(window).node;
if (star) {
let callback = function () {
star.removeEventListener('mouseover', callback);
star.setAttribute('tooltiptext', isZh ? "左键:将此页加入书签(CTRL+D)\n中键:显示/隐藏书签工具栏\n右键:打开书签管理器" : "Left click: show extensions options menu(CTRL+D)\nMiddle click: toggle places toolbar\nRight click: open addons management")
let clickFn = function (e) {
if (e.button === 0) {
return;
} else if (e.button === 1) {
e.preventDefault();
e.stopPropagation();
var bar = document.getElementById("PersonalToolbar"); setToolbarVisibility(bar, bar.collapsed);
} else if (e.button === 2) {
e.preventDefault();
e.stopPropagation();
PlacesCommandHook.showPlacesOrganizer('AllBookmarks');
}
}
star.addEventListener('click', clickFn, true);
}
star.addEventListener('mouseover', callback);
}
}
if (config["reload button right click to force reload"]) {
let reload = CustomizableUI.getWidget('reload-button').forWindow(window).node;
if (reload) {
let callback = function () {
reload.removeEventListener('mouseover', callback);
reload.setAttribute('tooltiptext', isZh ? '左键:刷新\n右键:强制刷新' : 'Left click: refresh page\nRight click: force refresh page');
let clickFn = function (event) {
if (event.button == 2) {
const global = event.target.ownerGlobal;
event.preventDefault();
"BrowserReloadSkipCache" in global ? global.BrowserReloadSkipCache() : global.BrowserReloadWithFlags(Ci.nsIWebNavigation.LOAD_FLAGS_BYPASS_CACHE);
}
}
reload.addEventListener('click', clickFn);
};
reload.addEventListener('mouseover', callback);
}
}
if (config["right click panel ui button to open sidebar"]) {
var OpenAllTabs = document.getElementById('PanelUI-menu-button');
if (!OpenAllTabs) return;
OpenAllTabs.addEventListener("click", function (e) {
if (e.button == 2) {
e.preventDefault();
e.stopPropagation();
if (document.getElementById("sidebar-button")) {
document.getElementById("sidebar-button").click();
} else {
SidebarController.toggle("viewBookmarksSidebar");
}
Services.prefs.setBoolPref("sidebar.position_start", false);
}
}, false);
}
if (config["right click extensions options menu button to open addons management"] && CustomizableUI.getPlacementOfWidget('eom-button', true)) {
let eom = CustomizableUI.getWidget('eom-button').forWindow(window).node;
let callback = function () {
eom.removeEventListener('mouseover', callback);
eom.setAttribute('tooltiptext', isZh ? '左键:拓展选项菜单\n右键:扩展管理' : 'Left click: show extensions options menu\nRight click: open addons management');
let clickFn = function (event) {
if (event.button == 2 && event.target.localName == 'toolbarbutton') {
event.preventDefault();
AddonMgr('addons://list/extension');
}
}
eom.addEventListener('click', clickFn);
};
eom.addEventListener('mouseover', callback);
}
if (config["downloads button add middle and right click"]) {
let btn = CustomizableUI.getWidget('downloads-button').forWindow(window).node;
if (btn) {
btn.setAttribute('tooltiptext', isZh ? '左键:下载历史\n右键:我的足迹' : 'Left click: show downloads history\nRight click: show my footprints');
let clickFn = function (e) {
if (e.button == 1) {
e.preventDefault();
e.stopPropagation();
var input = { value: readFromClipboard() || "" }; // default the edit field to Bob
var result = Services.prompt.prompt(null, isZh ? "保存 URL" : "Save URL", isZh ? "请输入 URL?" : "Please enter URL?", input, null, {});
if (!result)
return;
if (!(/(chrome|resource|ftp|http|https):\/\//i.test(input.value))) return;
let cookieJarSettings = gBrowser.selectedBrowser.cookieJarSettings;
//saveURL(aURL, aOriginalURL, aFileName, aFilePickerTitleKey, aShouldBypassCache,
// aSkipPrompt, aReferrer, aCookieJarSettings,
// aSourceDocument,
// aIsContentWindowPrivate,
// aPrincipal)
saveURL(
input.value,
null,
null,
null,
true,
false,
null,
cookieJarSettings,
null,
PrivateBrowsingUtils.isWindowPrivate(window),
Services.scriptSecurityManager.createNullPrincipal({})
);
} else if (e.button == 2 && !e.shiftKey) {
// 右键打开下载历史
e.preventDefault();
e.stopPropagation();
if (typeof ucjs_downloadManager === "undefined")
DownloadsPanel.showDownloadsHistory();
else
ucjs_downloadManager.openDownloadManager(true);
}
}
btn.addEventListener('click', clickFn);
}
}
if (config["modify sidebar button behavior"]) {
let btn = CustomizableUI.getWidget('sidebar-button').forWindow(window).node;
if (btn) {
btn.setAttribute('tooltiptext', isZh ? '左键:显示书签侧边栏\n中键:切换侧边栏方向\n右键:显示历史侧边栏' : 'Left click: show bookmarks sidebar\nMiddle click: toogle sidebar postion\nRight click: show history sidebar');
let clickFn = function (e) {
e.preventDefault();
e.stopPropagation();
switch (e.button) {
case 2:
SidebarController.toggle("viewHistorySidebar");
break;
case 1:
Services.prefs.setBoolPref("sidebar.position_start", !Services.prefs.getBoolPref("sidebar.position_start"));
break;
case 0:
SidebarController.toggle("viewBookmarksSidebar")
break;
}
}
btn.addEventListener('click', clickFn);
}
if (document.getElementById("sidebar-header")) {
let header = document.getElementById("sidebar-header");
header.addEventListener('dblclick', ({ target }) => {
if (target !== header) return;
Services.prefs.setBoolPref("sidebar.position_start", !Services.prefs.getBoolPref("sidebar.position_start"));
});
}
if (window.SidebarModoki) {
let header = document.getElementById("SM_header");
if (!header) {
// 使用 MutationObserver 等待 SM_header
let observer = new MutationObserver(function (mutations) {
mutations.forEach(function (mutation) {
if (mutation.type === "childList" && mutation.addedNodes.length > 0) {
// mutation 的 ID 是 SM_header
header = document.getElementById("SM_header");
if (header) {
addEvent(header);
observer.disconnect();
}
}
});
});
} else {
addEvent(header);
}
function addEvent (header) {
header.addEventListener('dblclick', ({ target }) => {
if (target !== header) return;
Services.prefs.setBoolPref("sidebar.position_start", !Services.prefs.getBoolPref("sidebar.position_start"));
});
}
}
}
if (config["middle click sidebarmodoki button toggle sidebar direction"]) {
let btn = document.getElementById("SM_Button"), count = 0;
if (!btn) {
var t = setInterval(() => {
count++;
btn = document.getElementById("SM_Button");
if (count > 10) clearInterval(t);
if (btn) {
bindSMButton(btn);
clearInterval(t);
}
}, 100);
}
window.addEventListener("aftercustomization", function () {
bindSMButton(document.getElementById("SM_Button"));
});
function bindSMButton (btn) {
if (!btn) return;
if (btn.getAttribute("misc_bind") == "true") return;
btn.setAttribute("misc_bind", "true");
btn.addEventListener('click', function (e) {
if (e.button == 1) {
e.preventDefault();
e.stopPropagation();
Services.prefs.setBoolPref("sidebar.position_start", !Services.prefs.getBoolPref("sidebar.position_start"));
}
});
}
}
if (config["ctrl f to toggle findbar"]) {
document.addEventListener("keydown", function (e) {
if (e.ctrlKey && e.key == "f") {
e.preventDefault();
e.stopPropagation();
if (!gFindBar || gFindBar.hidden) {
gLazyFindCommand('onFindCommand');
} else {
gFindBar.close();
}
}
})
}
}
}
function AddonMgr () {
let args = [...arguments], b = "openAddonsMgr";
eval(`${parseInt(Services.appinfo.version) < 126
? "Browser" + b[0].toUpperCase() + b.slice(1)
: "BrowserAddonUI." + b}(...args)`);
}
const miscUtils = new MiscUtils();
if (gBrowserInit.delayedStartupFinished) miscUtils.init(document, window); else {
let delayedListener = (subject, topic) => {
if (topic == "browser-delayed-startup-finished" && subject == window) {
Services.obs.removeObserver(delayedListener, topic);
miscUtils.init(subject.document, subject);
}
};
Services.obs.addObserver(delayedListener, "browser-delayed-startup-finished");
}
})();