Fossil SCM

Simplifications to the copybtn.js script.

drh 2019-06-10 01:53 trunk
Commit 5f0479d097fe07212349e1c107bafe0c74127179f2769768e1c4bc7d454092c8
1 file changed +10 -54
+10 -54
--- src/copybtn.js
+++ src/copybtn.js
@@ -43,16 +43,16 @@
4343
if( idTarget ) elButton.setAttribute("data-copytarget",idTarget);
4444
if( cchLength ) elButton.setAttribute("data-copylength",cchLength);
4545
elButton.onclick = clickCopyButton;
4646
return elButton;
4747
}
48
-onContentLoaded(function(){
48
+setTimeout(function(){
4949
var aButtons = document.getElementsByClassName("copy-button");
5050
for ( var i=0; i<aButtons.length; i++ ){
5151
initCopyButton(aButtons[i],0,0);
5252
}
53
-});
53
+},1);
5454
/* The onclick handler for the "Copy Button". */
5555
var lockCopyText = false;
5656
function clickCopyButton(e){
5757
e.preventDefault(); /* Mandatory for <a> and <button>. */
5858
e.stopPropagation();
@@ -79,60 +79,16 @@
7979
lockCopyText = false;
8080
}.bind(null,this.id),400);
8181
}
8282
/* Create a temporary <textarea> element and copy the contents to clipboard. */
8383
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();
9990
try{
10091
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);
13894
}
13995
--- 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

Keyboard Shortcuts

Open search /
Next entry (timeline) j
Previous entry (timeline) k
Open focused entry Enter
Show this help ?
Toggle theme Top nav button