Skip to content

Commit b5882ca

Browse files
committed
update copy current url hotkey script.
1 parent 63ac875 commit b5882ca

1 file changed

Lines changed: 33 additions & 27 deletions

File tree

JS/copyCurrentUrlHotkey.uc.js

Lines changed: 33 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
// ==UserScript==
22
// @name Copy Current URL Hotkey
3-
// @version 1.1.3
3+
// @version 1.2.0
44
// @author aminomancer
55
// @homepage https://github.com/aminomancer
66
// @description Adds a new hotkey (Ctrl+Alt+C by default) that copies
@@ -9,7 +9,7 @@
99
// ==/UserScript==
1010

1111
class CopyCurrentURL {
12-
static config = {
12+
config = {
1313
// if you have customHintProvider.uc.js, copying will open a confirmation
1414
// hint anchored to the urlbar.
1515
"copy confirmation hint": true,
@@ -44,46 +44,52 @@ class CopyCurrentURL {
4444
},
4545
};
4646
constructor() {
47-
this.showHint = !!CopyCurrentURL.config["copy confirmation hint"];
48-
this.hotkey = _ucUtils.registerHotkey(CopyCurrentURL.config.shortcut, (win, key) => {
49-
if (win === window && gURLBar.value) {
50-
this.clipboardHelper.copyString(gURLBar.value);
51-
this.showHint &&
52-
win.CustomHint?.show(gURLBar.inputField, "Copied", {
47+
XPCOMUtils.defineLazyServiceGetter(
48+
this,
49+
"ClipboardHelper",
50+
"@mozilla.org/widget/clipboardhelper;1",
51+
"nsIClipboardHelper"
52+
);
53+
this.hotkey = _ucUtils.registerHotkey(this.config.shortcut, win => {
54+
if (win === window) {
55+
let val = win.gURLBar._lastValidURLStr || win.gURLBar.value;
56+
if (!val) return;
57+
this.ClipboardHelper.copyStringToClipboard(val, this.clipboard);
58+
if (this.config["copy confirmation hint"]) {
59+
win.CustomHint?.show(win.gURLBar.inputField, "Copied", {
5360
position: "after_start",
5461
x: 16,
5562
});
63+
}
5664
}
5765
});
58-
if (CopyCurrentURL.config["context menu shortcut hint"]) this.shortcutHint();
66+
if (this.config["context menu shortcut hint"]) this.shortcutHint();
5967
}
60-
get clipboardHelper() {
61-
return (
62-
this._clipboardHelper ||
63-
(this._clipboardHelper = Cc["@mozilla.org/widget/clipboardhelper;1"].getService(
64-
Ci.nsIClipboardHelper
65-
))
66-
);
68+
get clipboard() {
69+
return Services.clipboard.supportsSelectionClipboard()
70+
? Services.clipboard.kSelectionClipboard
71+
: Services.clipboard.kGlobalClipboard;
6772
}
68-
handleEvent(_e) {
69-
const menuitem = gURLBar
70-
.querySelector("moz-input-box")
71-
?.menupopup?.querySelector(`[cmd="cmd_copy"]`);
73+
handleEvent() {
74+
let menuitem = gURLBar.inputField?.parentElement?.menupopup?.querySelector(`[cmd="cmd_copy"]`);
7275
if (menuitem) {
73-
menuitem.setAttribute("key", CopyCurrentURL.config.shortcut.id);
74-
gURLBar.removeEventListener("contextmenu", this);
76+
if (!this.hintApplied && menuitem.hasAttribute("key")) {
77+
gURLBar.removeEventListener("contextmenu", this);
78+
return;
79+
}
80+
if (gURLBar.selectionStart != 0 || gURLBar.selectionEnd != gURLBar.inputField.textLength) {
81+
menuitem.setAttribute("key", this.config.shortcut.id);
82+
} else menuitem.removeAttribute("key");
83+
this.hintApplied = true;
7584
}
7685
}
77-
setupHint() {
78-
gURLBar.addEventListener("contextmenu", this);
79-
}
8086
shortcutHint() {
81-
if (gBrowserInit.delayedStartupFinished) this.setupHint();
87+
if (gBrowserInit.delayedStartupFinished) gURLBar.addEventListener("contextmenu", this);
8288
else {
8389
let delayedListener = (subject, topic) => {
8490
if (topic == "browser-delayed-startup-finished" && subject == window) {
8591
Services.obs.removeObserver(delayedListener, topic);
86-
this.setupHint();
92+
gURLBar.addEventListener("contextmenu", this);
8793
}
8894
};
8995
Services.obs.addObserver(delayedListener, "browser-delayed-startup-finished");

0 commit comments

Comments
 (0)