-
Notifications
You must be signed in to change notification settings - Fork 27
/
copyCurrentUrlHotkey.uc.js
100 lines (93 loc) · 3.72 KB
/
copyCurrentUrlHotkey.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
// ==UserScript==
// @name Copy Current URL Hotkey
// @version 1.2.5
// @author aminomancer
// @homepageURL https://github.com/aminomancer
// @description Adds a new hotkey (Ctrl+Alt+C by default) that copies whatever is in the urlbar, even when it's not in focus.
// @downloadURL https://cdn.jsdelivr.net/gh/aminomancer/uc.css.js@master/JS/copyCurrentUrlHotkey.uc.js
// @updateURL https://cdn.jsdelivr.net/gh/aminomancer/uc.css.js@master/JS/copyCurrentUrlHotkey.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.
// ==/UserScript==
class CopyCurrentURL {
// Add these in about:config instead of modifying the script, if you want the
// settings to persist between script restarts.
config = {
// if you have customHintProvider.uc.js, copying will open a confirmation
// hint anchored to the urlbar.
"show confirmation hint": Services.prefs.getBoolPref(
"copyCurrentUrl.showConfirmationHint",
true
),
shortcut: {
// shortcut key, combined with modifiers.
key: Services.prefs.getStringPref("copyCurrentUrl.shortcut.key", "C"),
// ctrl + alt or cmd + alt (use accel, it's cross-platform. it can be
// changed in about:config with ui.key.accelKey. if you leave the "" quotes
// empty, no modifier will be used. that means the hotkey will just be "C"
// which is a bad idea — only do that if your "key" value is something
// obscure like a function key, since this key will be active at all times
// and in almost all contexts.
modifiers: Services.prefs.getStringPref(
"copyCurrentUrl.shortcut.modifiers",
"accel alt"
),
// no need to change this.
id: "key_copyCurrentUrl",
},
};
constructor() {
XPCOMUtils.defineLazyServiceGetter(
this,
"ClipboardHelper",
"@mozilla.org/widget/clipboardhelper;1",
"nsIClipboardHelper"
);
this.hotkey = _ucUtils.registerHotkey(this.config.shortcut, win => {
if (win === window) {
let val;
try {
let uri = win.gURLBar.makeURIReadable(win.gBrowser.currentURI);
if (uri.schemeIs("javascript") || uri.schemeIs("data")) {
val = win.gURLBar._lastValidURLStr || win.gURLBar.value;
} else {
val = uri.displaySpec;
}
if (val === "about:blank") {
return;
}
if (win.UrlbarPrefs.get("decodeURLsOnCopy")) {
val = decodeURI(val);
}
if (!val) {
return;
}
} catch (error) {
return;
}
this.ClipboardHelper.copyStringToClipboard(val, this.clipboard);
if (this.config["show confirmation hint"]) {
if (win.gURLBar.getAttribute("pageproxystate") == "valid") {
win.CustomHint?.show(win.gURLBar.inputField, "Copied", {
position: "after_start",
x: 16,
});
} else {
win.CustomHint?.show(
win.gIdentityHandler._identityIconBox,
"Copied",
{ position: "bottomcenter topleft", y: 8 }
);
}
}
}
});
}
get clipboard() {
return Services.clipboard.isClipboardTypeSupported(
Services.clipboard.kSelectionClipboard
)
? Services.clipboard.kSelectionClipboard
: Services.clipboard.kGlobalClipboard;
}
}
window.copyCurrentUrl = new CopyCurrentURL();