@@ -34,40 +34,46 @@ window.addEventListener("hashchange", shiftWindow);
3434/* Copy a script from an HTML element to the clipboard,
3535 * removing comments and blank lines.
3636 * Arguments:
37- * trigger: The button calling the function, whose label will be updated
37+ * trigger: The button calling the function, whose icon will be updated
3838 * elem: The element containing the script to copy
39+ * stripSudo: If true, remove 'sudo ' from the start of lines
3940 */
4041
41- function copyScript ( trigger , elem ) {
42- var raw = document . getElementById ( elem ) . innerHTML ;
42+ function copyScript ( trigger , elem , stripSudo = false ) {
43+ const raw = document . getElementById ( elem ) . innerHTML ;
4344
4445 // Create a scratch div to copy from
45- var scratch = document . createElement ( "div" ) ;
46+ const scratch = document . createElement ( "div" ) ;
4647 document . body . appendChild ( scratch ) ;
4748
4849 // Copy the contents of the script box into the scratch div, removing
49- // comments and blank lines
50- var lines = raw . split ( "\n" ) ;
51- var output = '' ;
52- for ( var l = 0 ; l < lines . length ; l ++ ) {
53- if ( lines [ l ] [ 0 ] != '#' && lines [ l ] . trim ( ) != '' )
54- output += lines [ l ] + '<br />' ;
50+ // comments and blank lines, and optionally stripping sudo
51+ const lines = raw . split ( "\n" ) ;
52+ let output = '' ;
53+ for ( let l = 0 ; l < lines . length ; l ++ ) {
54+ if ( lines [ l ] [ 0 ] != '#' && lines [ l ] . trim ( ) != '' ) {
55+ let line = lines [ l ] ;
56+ if ( stripSudo ) {
57+ line = line . replace ( / ^ ( \s * ) s u d o / , '$1' ) ;
58+ }
59+ output += line + '<br />' ;
60+ }
5561 }
5662 scratch . innerHTML = output . trim ( ) ;
5763
5864 // Perform the copy
5965 if ( document . body . createTextRange ) {
6066 // IE 11
61- var range = document . body . createTextRange ( ) ;
67+ const range = document . body . createTextRange ( ) ;
6268 range . moveToElementText ( scratch ) ;
6369 range . select ( ) ;
6470 document . execCommand ( "Copy" ) ;
6571 document . getSelection ( ) . removeAllRanges ( )
6672 }
6773 else if ( window . getSelection ) {
6874 // Sane browsers
69- var selection = window . getSelection ( ) ;
70- var range = document . createRange ( ) ;
75+ const selection = window . getSelection ( ) ;
76+ const range = document . createRange ( ) ;
7177 range . selectNodeContents ( scratch ) ;
7278 selection . removeAllRanges ( ) ;
7379 selection . addRange ( range ) ;
@@ -79,11 +85,16 @@ function copyScript(trigger, elem) {
7985 scratch . parentNode . removeChild ( scratch ) ;
8086
8187 // Indicate to the user that the script was copied
82- var label = trigger . innerHTML ;
83- trigger . innerHTML = 'Copied!' ;
88+ const icon = trigger . querySelector ( 'i' ) ;
89+ const originalClass = stripSudo ? 'fa-terminal' : 'fa-copy' ;
90+ icon . classList . remove ( originalClass ) ;
91+ icon . classList . add ( 'fa-check' ) ;
92+ trigger . classList . add ( 'copied' ) ;
8493
8594 setTimeout ( function ( ) {
86- trigger . innerHTML = label ;
95+ icon . classList . remove ( 'fa-check' ) ;
96+ icon . classList . add ( originalClass ) ;
97+ trigger . classList . remove ( 'copied' ) ;
8798 } , 3000 ) ;
8899}
89100
0 commit comments