11// ==UserScript==
22// @name Copy Current URL Hotkey
3- // @version 1.2.1
3+ // @version 1.2.2
44// @author aminomancer
55// @homepage https://github.com/aminomancer
66// @description Adds a new hotkey (Ctrl+Alt+C by default) that copies
@@ -14,19 +14,6 @@ class CopyCurrentURL {
1414 // hint anchored to the urlbar.
1515 "copy confirmation hint" : true ,
1616
17- // when you right-click the urlbar, the context menu has a "copy" command.
18- // set this to "true" to show a "Ctrl+Alt+C" hint next to this command, like
19- // firefox does with many other commands. the hint text will reflect the
20- // actual hotkey. so on macOS it will show "Cmd+Alt+C" and if you modify the
21- // modifiers below, it will show your modifiers instead. this setting isn't
22- // enabled by default because 1) unlike our custom hotkey, this command
23- // actually only copies the selection, not the full input content. so it's
24- // disabled if nothing is highlighted. and 2) the context menu is very thin
25- // due to the short names of the commands. adding "Ctrl+Alt+C" makes it kind
26- // of cramped. but it's easy to forget that hotkeys exist if they're not
27- // visually displayed anywhere, so you may want to enable this feature.
28- "context menu shortcut hint" : true ,
29-
3017 shortcut : {
3118 // shortcut key, combined with modifiers.
3219 key : "C" ,
@@ -43,6 +30,7 @@ class CopyCurrentURL {
4330 id : "key_copyCurrentUrl" ,
4431 } ,
4532 } ;
33+
4634 constructor ( ) {
4735 XPCOMUtils . defineLazyServiceGetter (
4836 this ,
@@ -52,60 +40,44 @@ class CopyCurrentURL {
5240 ) ;
5341 this . hotkey = _ucUtils . registerHotkey ( this . config . shortcut , win => {
5442 if ( win === window ) {
55- let val = win . gURLBar . _lastValidURLStr || win . gURLBar . value ;
56- if ( ! val ) return ;
43+ let val ;
44+ try {
45+ let uri = win . gURLBar . makeURIReadable ( win . gBrowser . currentURI ) ;
46+ if ( uri . schemeIs ( "javascript" ) || uri . schemeIs ( "data" ) ) {
47+ val = win . gURLBar . _lastValidURLStr || win . gURLBar . value ;
48+ } else {
49+ val = uri . displaySpec ;
50+ }
51+ if ( win . UrlbarPrefs . get ( "decodeURLsOnCopy" ) ) {
52+ val = decodeURI ( val ) ;
53+ }
54+ } catch ( error ) {
55+ return ;
56+ }
5757 this . ClipboardHelper . copyStringToClipboard ( val , this . clipboard ) ;
5858 if ( this . config [ "copy confirmation hint" ] ) {
59- win . CustomHint ?. show ( win . gURLBar . inputField , "Copied" , {
60- position : "after_start" ,
61- x : 16 ,
62- } ) ;
59+ if ( win . gURLBar . getAttribute ( "pageproxystate" ) == "valid" ) {
60+ win . CustomHint ?. show ( win . gURLBar . inputField , "Copied" , {
61+ position : "after_start" ,
62+ x : 16 ,
63+ } ) ;
64+ } else {
65+ win . CustomHint ?. show (
66+ win . gIdentityHandler . _identityIconBox ,
67+ "Copied" ,
68+ { position : "bottomcenter topleft" , y : 8 }
69+ ) ;
70+ }
6371 }
6472 }
6573 } ) ;
66- if ( this . config [ "context menu shortcut hint" ] ) this . shortcutHint ( ) ;
6774 }
75+
6876 get clipboard ( ) {
6977 return Services . clipboard . supportsSelectionClipboard ( )
7078 ? Services . clipboard . kSelectionClipboard
7179 : Services . clipboard . kGlobalClipboard ;
7280 }
73- handleEvent ( ) {
74- let menuitem = gURLBar . inputField ?. parentElement ?. menupopup ?. querySelector (
75- `[cmd="cmd_copy"]`
76- ) ;
77- if ( menuitem ) {
78- if ( ! this . hintApplied && menuitem . hasAttribute ( "key" ) ) {
79- gURLBar . removeEventListener ( "contextmenu" , this ) ;
80- return ;
81- }
82- if (
83- gURLBar . selectionStart != 0 ||
84- gURLBar . selectionEnd != gURLBar . inputField . textLength
85- ) {
86- menuitem . setAttribute ( "key" , this . config . shortcut . id ) ;
87- } else {
88- menuitem . removeAttribute ( "key" ) ;
89- }
90- this . hintApplied = true ;
91- }
92- }
93- shortcutHint ( ) {
94- if ( gBrowserInit . delayedStartupFinished ) {
95- gURLBar . addEventListener ( "contextmenu" , this ) ;
96- } else {
97- let delayedListener = ( subject , topic ) => {
98- if ( topic == "browser-delayed-startup-finished" && subject == window ) {
99- Services . obs . removeObserver ( delayedListener , topic ) ;
100- gURLBar . addEventListener ( "contextmenu" , this ) ;
101- }
102- } ;
103- Services . obs . addObserver (
104- delayedListener ,
105- "browser-delayed-startup-finished"
106- ) ;
107- }
108- }
10981}
11082
11183window . copyCurrentUrl = new CopyCurrentURL ( ) ;
0 commit comments