@@ -635,16 +635,23 @@ function tsugiSha256(ascii) {
635635 return result ;
636636}
637637
638+ // Adapted from
638639// https://dev.to/mornir/-how-to-easily-copy-text-to-clipboard-a1a
639- function copyToClipboard ( par , textToCopy ) {
640+ // Added avoiding the scrolling effect by appending the new input
641+ // tag as a child of a nearby element (the parent element)
642+ // Usage:
643+ // <a href="#" onclick="copyToClipboardNoScroll(this, 'texttocopy');return false;">Copy</a>
644+ // <a href="#" onclick="copyToClipboardNoScroll(this, $('#pass').text());return false;">Copy</a>
645+ // <a href="#" onclick="copyToClipboardNoScroll(this, $('#myInput').val());return false;">Copy</a>
646+ function copyToClipboardNoScroll ( parent_element , textToCopy ) {
640647 // 1) Add the text to the DOM (usually achieved with a hidden input field)
641648 const input = document . createElement ( 'input' ) ;
642649
643650 // 1.5) Move off to the left but inline with the current item to avoid scroll effects
644651 input . style . position = 'absolute' ;
645652 input . style . left = '-1000px' ;
646- par . appendChild ( input ) ;
647- input . value = textToCopy ;
653+ parent_element . appendChild ( input ) ;
654+ input . value = textToCopy . trim ( ) ;
648655
649656 // 2) Select the text
650657 input . focus ( ) ;
@@ -662,6 +669,12 @@ function copyToClipboard(par, textToCopy) {
662669 input . remove ( ) ;
663670}
664671
672+ // TODO: Remove Legacy one of these days
673+ // https://dev.to/mornir/-how-to-easily-copy-text-to-clipboard-a1a
674+ function copyToClipboard ( par , textToCopy ) {
675+ copyToClipBoardNoScroll ( par , textToCopy ) ;
676+ }
677+
665678// Make sure format is present
666679// https://stackoverflow.com/questions/610406/javascript-equivalent-to-printf-string-format
667680// https://stackoverflow.com/a/4673436
0 commit comments