Fossil SCM
Simplifications to the copybtn.js script.
Commit
5f0479d097fe07212349e1c107bafe0c74127179f2769768e1c4bc7d454092c8
Parent
e055942cf2e008f…
1 file changed
+10
-54
+10
-54
| --- src/copybtn.js | ||
| +++ src/copybtn.js | ||
| @@ -43,16 +43,16 @@ | ||
| 43 | 43 | if( idTarget ) elButton.setAttribute("data-copytarget",idTarget); |
| 44 | 44 | if( cchLength ) elButton.setAttribute("data-copylength",cchLength); |
| 45 | 45 | elButton.onclick = clickCopyButton; |
| 46 | 46 | return elButton; |
| 47 | 47 | } |
| 48 | -onContentLoaded(function(){ | |
| 48 | +setTimeout(function(){ | |
| 49 | 49 | var aButtons = document.getElementsByClassName("copy-button"); |
| 50 | 50 | for ( var i=0; i<aButtons.length; i++ ){ |
| 51 | 51 | initCopyButton(aButtons[i],0,0); |
| 52 | 52 | } |
| 53 | -}); | |
| 53 | +},1); | |
| 54 | 54 | /* The onclick handler for the "Copy Button". */ |
| 55 | 55 | var lockCopyText = false; |
| 56 | 56 | function clickCopyButton(e){ |
| 57 | 57 | e.preventDefault(); /* Mandatory for <a> and <button>. */ |
| 58 | 58 | e.stopPropagation(); |
| @@ -79,60 +79,16 @@ | ||
| 79 | 79 | lockCopyText = false; |
| 80 | 80 | }.bind(null,this.id),400); |
| 81 | 81 | } |
| 82 | 82 | /* Create a temporary <textarea> element and copy the contents to clipboard. */ |
| 83 | 83 | function copyTextToClipboard(text){ |
| 84 | - var textArea = document.createElement("textarea"); | |
| 85 | - textArea.style.position = 'fixed'; | |
| 86 | - textArea.style.top = 0; | |
| 87 | - textArea.style.left = 0; | |
| 88 | - textArea.style.width = '2em'; | |
| 89 | - textArea.style.height = '2em'; | |
| 90 | - textArea.style.padding = 0; | |
| 91 | - textArea.style.border = 'none'; | |
| 92 | - textArea.style.outline = 'none'; | |
| 93 | - textArea.style.boxShadow = 'none'; | |
| 94 | - textArea.style.background = 'transparent'; | |
| 95 | - textArea.value = text; | |
| 96 | - document.body.appendChild(textArea); | |
| 97 | - textArea.focus(); | |
| 98 | - textArea.select(); | |
| 84 | + var x = document.createElement("textarea"); | |
| 85 | + x.style.position = 'absolute'; | |
| 86 | + x.style.left = '-9999px'; | |
| 87 | + x.value = text; | |
| 88 | + document.body.appendChild(x); | |
| 89 | + x.select(); | |
| 99 | 90 | try{ |
| 100 | 91 | document.execCommand('copy'); |
| 101 | - }catch(err){ | |
| 102 | - } | |
| 103 | - document.body.removeChild(textArea); | |
| 104 | -} | |
| 105 | -/* Execute a function as soon as the HTML document has been completely loaded. | |
| 106 | -** The idea for this code is based on the contentLoaded() function presented | |
| 107 | -** here: | |
| 108 | -** | |
| 109 | -** Cross-browser wrapper for DOMContentLoaded | |
| 110 | -** http://javascript.nwbox.com/ContentLoaded/ | |
| 111 | -*/ | |
| 112 | -function onContentLoaded(fnready) { | |
| 113 | - var fninit = function() { | |
| 114 | - if (document.addEventListener || | |
| 115 | - event.type === 'load' || | |
| 116 | - document.readyState === 'complete') { | |
| 117 | - if (document.addEventListener) { | |
| 118 | - document.removeEventListener('DOMContentLoaded',fninit,false); | |
| 119 | - window.removeEventListener('load',fninit,false); | |
| 120 | - } | |
| 121 | - else { | |
| 122 | - document.detachEvent('onreadystatechange',fninit); | |
| 123 | - window.detachEvent('onload',fninit); | |
| 124 | - } | |
| 125 | - } | |
| 126 | - fnready.call(window); | |
| 127 | - }; | |
| 128 | - if (document.readyState === 'complete') | |
| 129 | - fnready.call(window); | |
| 130 | - else if (document.addEventListener) { | |
| 131 | - document.addEventListener('DOMContentLoaded',fninit,false); | |
| 132 | - window.addEventListener('load',fninit,false); | |
| 133 | - } | |
| 134 | - else { | |
| 135 | - document.attachEvent('onreadystatechange',fninit); | |
| 136 | - window.attachEvent('onload',fninit); | |
| 137 | - } | |
| 92 | + }catch(err){} | |
| 93 | + document.body.removeChild(x); | |
| 138 | 94 | } |
| 139 | 95 |
| --- src/copybtn.js | |
| +++ src/copybtn.js | |
| @@ -43,16 +43,16 @@ | |
| 43 | if( idTarget ) elButton.setAttribute("data-copytarget",idTarget); |
| 44 | if( cchLength ) elButton.setAttribute("data-copylength",cchLength); |
| 45 | elButton.onclick = clickCopyButton; |
| 46 | return elButton; |
| 47 | } |
| 48 | onContentLoaded(function(){ |
| 49 | var aButtons = document.getElementsByClassName("copy-button"); |
| 50 | for ( var i=0; i<aButtons.length; i++ ){ |
| 51 | initCopyButton(aButtons[i],0,0); |
| 52 | } |
| 53 | }); |
| 54 | /* The onclick handler for the "Copy Button". */ |
| 55 | var lockCopyText = false; |
| 56 | function clickCopyButton(e){ |
| 57 | e.preventDefault(); /* Mandatory for <a> and <button>. */ |
| 58 | e.stopPropagation(); |
| @@ -79,60 +79,16 @@ | |
| 79 | lockCopyText = false; |
| 80 | }.bind(null,this.id),400); |
| 81 | } |
| 82 | /* Create a temporary <textarea> element and copy the contents to clipboard. */ |
| 83 | function copyTextToClipboard(text){ |
| 84 | var textArea = document.createElement("textarea"); |
| 85 | textArea.style.position = 'fixed'; |
| 86 | textArea.style.top = 0; |
| 87 | textArea.style.left = 0; |
| 88 | textArea.style.width = '2em'; |
| 89 | textArea.style.height = '2em'; |
| 90 | textArea.style.padding = 0; |
| 91 | textArea.style.border = 'none'; |
| 92 | textArea.style.outline = 'none'; |
| 93 | textArea.style.boxShadow = 'none'; |
| 94 | textArea.style.background = 'transparent'; |
| 95 | textArea.value = text; |
| 96 | document.body.appendChild(textArea); |
| 97 | textArea.focus(); |
| 98 | textArea.select(); |
| 99 | try{ |
| 100 | document.execCommand('copy'); |
| 101 | }catch(err){ |
| 102 | } |
| 103 | document.body.removeChild(textArea); |
| 104 | } |
| 105 | /* Execute a function as soon as the HTML document has been completely loaded. |
| 106 | ** The idea for this code is based on the contentLoaded() function presented |
| 107 | ** here: |
| 108 | ** |
| 109 | ** Cross-browser wrapper for DOMContentLoaded |
| 110 | ** http://javascript.nwbox.com/ContentLoaded/ |
| 111 | */ |
| 112 | function onContentLoaded(fnready) { |
| 113 | var fninit = function() { |
| 114 | if (document.addEventListener || |
| 115 | event.type === 'load' || |
| 116 | document.readyState === 'complete') { |
| 117 | if (document.addEventListener) { |
| 118 | document.removeEventListener('DOMContentLoaded',fninit,false); |
| 119 | window.removeEventListener('load',fninit,false); |
| 120 | } |
| 121 | else { |
| 122 | document.detachEvent('onreadystatechange',fninit); |
| 123 | window.detachEvent('onload',fninit); |
| 124 | } |
| 125 | } |
| 126 | fnready.call(window); |
| 127 | }; |
| 128 | if (document.readyState === 'complete') |
| 129 | fnready.call(window); |
| 130 | else if (document.addEventListener) { |
| 131 | document.addEventListener('DOMContentLoaded',fninit,false); |
| 132 | window.addEventListener('load',fninit,false); |
| 133 | } |
| 134 | else { |
| 135 | document.attachEvent('onreadystatechange',fninit); |
| 136 | window.attachEvent('onload',fninit); |
| 137 | } |
| 138 | } |
| 139 |
| --- src/copybtn.js | |
| +++ src/copybtn.js | |
| @@ -43,16 +43,16 @@ | |
| 43 | if( idTarget ) elButton.setAttribute("data-copytarget",idTarget); |
| 44 | if( cchLength ) elButton.setAttribute("data-copylength",cchLength); |
| 45 | elButton.onclick = clickCopyButton; |
| 46 | return elButton; |
| 47 | } |
| 48 | setTimeout(function(){ |
| 49 | var aButtons = document.getElementsByClassName("copy-button"); |
| 50 | for ( var i=0; i<aButtons.length; i++ ){ |
| 51 | initCopyButton(aButtons[i],0,0); |
| 52 | } |
| 53 | },1); |
| 54 | /* The onclick handler for the "Copy Button". */ |
| 55 | var lockCopyText = false; |
| 56 | function clickCopyButton(e){ |
| 57 | e.preventDefault(); /* Mandatory for <a> and <button>. */ |
| 58 | e.stopPropagation(); |
| @@ -79,60 +79,16 @@ | |
| 79 | lockCopyText = false; |
| 80 | }.bind(null,this.id),400); |
| 81 | } |
| 82 | /* Create a temporary <textarea> element and copy the contents to clipboard. */ |
| 83 | function copyTextToClipboard(text){ |
| 84 | var x = document.createElement("textarea"); |
| 85 | x.style.position = 'absolute'; |
| 86 | x.style.left = '-9999px'; |
| 87 | x.value = text; |
| 88 | document.body.appendChild(x); |
| 89 | x.select(); |
| 90 | try{ |
| 91 | document.execCommand('copy'); |
| 92 | }catch(err){} |
| 93 | document.body.removeChild(x); |
| 94 | } |
| 95 |