|
1 | 1 | // ==UserScript== |
2 | 2 | // @name Copy Current URL Hotkey |
3 | | -// @version 1.1.3 |
| 3 | +// @version 1.2.0 |
4 | 4 | // @author aminomancer |
5 | 5 | // @homepage https://github.com/aminomancer |
6 | 6 | // @description Adds a new hotkey (Ctrl+Alt+C by default) that copies |
|
9 | 9 | // ==/UserScript== |
10 | 10 |
|
11 | 11 | class CopyCurrentURL { |
12 | | - static config = { |
| 12 | + config = { |
13 | 13 | // if you have customHintProvider.uc.js, copying will open a confirmation |
14 | 14 | // hint anchored to the urlbar. |
15 | 15 | "copy confirmation hint": true, |
@@ -44,46 +44,52 @@ class CopyCurrentURL { |
44 | 44 | }, |
45 | 45 | }; |
46 | 46 | 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", { |
53 | 60 | position: "after_start", |
54 | 61 | x: 16, |
55 | 62 | }); |
| 63 | + } |
56 | 64 | } |
57 | 65 | }); |
58 | | - if (CopyCurrentURL.config["context menu shortcut hint"]) this.shortcutHint(); |
| 66 | + if (this.config["context menu shortcut hint"]) this.shortcutHint(); |
59 | 67 | } |
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; |
67 | 72 | } |
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"]`); |
72 | 75 | 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; |
75 | 84 | } |
76 | 85 | } |
77 | | - setupHint() { |
78 | | - gURLBar.addEventListener("contextmenu", this); |
79 | | - } |
80 | 86 | shortcutHint() { |
81 | | - if (gBrowserInit.delayedStartupFinished) this.setupHint(); |
| 87 | + if (gBrowserInit.delayedStartupFinished) gURLBar.addEventListener("contextmenu", this); |
82 | 88 | else { |
83 | 89 | let delayedListener = (subject, topic) => { |
84 | 90 | if (topic == "browser-delayed-startup-finished" && subject == window) { |
85 | 91 | Services.obs.removeObserver(delayedListener, topic); |
86 | | - this.setupHint(); |
| 92 | + gURLBar.addEventListener("contextmenu", this); |
87 | 93 | } |
88 | 94 | }; |
89 | 95 | Services.obs.addObserver(delayedListener, "browser-delayed-startup-finished"); |
|
0 commit comments