Fossil SCM

Code maintenance for the `copybtn.js' script: Remove global data, reference DOM elements by function-binding instead of by id (that is possibly reused for the short-lived tooltip), and normalize variable names and string quoting style.

florian 2022-10-23 06:48 trunk
Commit d5f6621527b813c1ab0510982747445e1bc8dc603e5e87d200522e0e31492fea
1 file changed +20 -24
+20 -24
--- src/copybtn.js
+++ src/copybtn.js
@@ -44,56 +44,52 @@
4444
if( cchLength ) elButton.setAttribute("data-copylength",cchLength);
4545
elButton.onclick = clickCopyButton;
4646
return elButton;
4747
}
4848
setTimeout(function(){
49
- var aButtons = document.getElementsByClassName("copy-button");
50
- for ( var i=0; i<aButtons.length; i++ ){
51
- initCopyButton(aButtons[i],0,0);
49
+ var elButtons = document.getElementsByClassName("copy-button");
50
+ for ( var i=0; i<elButtons.length; i++ ){
51
+ initCopyButton(elButtons[i],0,0);
5252
}
5353
},1);
5454
/* The onclick handler for the "Copy Button". */
55
-var lockCopyText = false;
5655
function clickCopyButton(e){
5756
e.preventDefault(); /* Mandatory for <a> and <button>. */
5857
e.stopPropagation();
59
- if( lockCopyText ) return;
60
- lockCopyText = true;
58
+ if( this.getAttribute("data-copylocked") ) return;
59
+ this.setAttribute("data-copylocked","1");
6160
this.style.transition = "opacity 400ms ease-in-out";
6261
this.style.opacity = 0;
6362
var idTarget = this.getAttribute("data-copytarget");
6463
var elTarget = document.getElementById(idTarget);
6564
if( elTarget ){
66
- var text = elTarget.innerText.replace(/^\s+|\s+$/g,'');
65
+ var text = elTarget.innerText.replace(/^\s+|\s+$/g,"");
6766
var cchLength = parseInt(this.getAttribute("data-copylength"));
6867
if( !isNaN(cchLength) && cchLength>0 ){
69
- text = text.slice(0,cchLength); // Assume single-byte chars.
68
+ text = text.slice(0,cchLength); /* Assume single-byte chars. */
7069
}
7170
copyTextToClipboard(text);
7271
}
73
- setTimeout(function(id){
74
- var elButton = document.getElementById(id);
75
- if( elButton ){
76
- elButton.style.transition = "";
77
- elButton.style.opacity = 1;
78
- }
79
- lockCopyText = false;
80
- }.bind(null,this.id),400);
72
+ setTimeout(function(){
73
+ this.style.transition = "";
74
+ this.style.opacity = 1;
75
+ this.removeAttribute("data-copylocked");
76
+ }.bind(this),400);
8177
}
8278
/* Create a temporary <textarea> element and copy the contents to clipboard. */
8379
function copyTextToClipboard(text){
8480
if( window.clipboardData && window.clipboardData.setData ){
85
- window.clipboardData.setData('Text',text);
81
+ window.clipboardData.setData("Text",text);
8682
}else{
87
- var x = document.createElement("textarea");
88
- x.style.position = 'fixed';
89
- x.value = text;
90
- document.body.appendChild(x);
91
- x.select();
83
+ var elTextarea = document.createElement("textarea");
84
+ elTextarea.style.position = "fixed";
85
+ elTextarea.value = text;
86
+ document.body.appendChild(elTextarea);
87
+ elTextarea.select();
9288
try{
93
- document.execCommand('copy');
89
+ document.execCommand("copy");
9490
}catch(err){
9591
}finally{
96
- document.body.removeChild(x);
92
+ document.body.removeChild(elTextarea);
9793
}
9894
}
9995
}
10096
--- src/copybtn.js
+++ src/copybtn.js
@@ -44,56 +44,52 @@
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();
59 if( lockCopyText ) return;
60 lockCopyText = true;
61 this.style.transition = "opacity 400ms ease-in-out";
62 this.style.opacity = 0;
63 var idTarget = this.getAttribute("data-copytarget");
64 var elTarget = document.getElementById(idTarget);
65 if( elTarget ){
66 var text = elTarget.innerText.replace(/^\s+|\s+$/g,'');
67 var cchLength = parseInt(this.getAttribute("data-copylength"));
68 if( !isNaN(cchLength) && cchLength>0 ){
69 text = text.slice(0,cchLength); // Assume single-byte chars.
70 }
71 copyTextToClipboard(text);
72 }
73 setTimeout(function(id){
74 var elButton = document.getElementById(id);
75 if( elButton ){
76 elButton.style.transition = "";
77 elButton.style.opacity = 1;
78 }
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 if( window.clipboardData && window.clipboardData.setData ){
85 window.clipboardData.setData('Text',text);
86 }else{
87 var x = document.createElement("textarea");
88 x.style.position = 'fixed';
89 x.value = text;
90 document.body.appendChild(x);
91 x.select();
92 try{
93 document.execCommand('copy');
94 }catch(err){
95 }finally{
96 document.body.removeChild(x);
97 }
98 }
99 }
100
--- src/copybtn.js
+++ src/copybtn.js
@@ -44,56 +44,52 @@
44 if( cchLength ) elButton.setAttribute("data-copylength",cchLength);
45 elButton.onclick = clickCopyButton;
46 return elButton;
47 }
48 setTimeout(function(){
49 var elButtons = document.getElementsByClassName("copy-button");
50 for ( var i=0; i<elButtons.length; i++ ){
51 initCopyButton(elButtons[i],0,0);
52 }
53 },1);
54 /* The onclick handler for the "Copy Button". */
 
55 function clickCopyButton(e){
56 e.preventDefault(); /* Mandatory for <a> and <button>. */
57 e.stopPropagation();
58 if( this.getAttribute("data-copylocked") ) return;
59 this.setAttribute("data-copylocked","1");
60 this.style.transition = "opacity 400ms ease-in-out";
61 this.style.opacity = 0;
62 var idTarget = this.getAttribute("data-copytarget");
63 var elTarget = document.getElementById(idTarget);
64 if( elTarget ){
65 var text = elTarget.innerText.replace(/^\s+|\s+$/g,"");
66 var cchLength = parseInt(this.getAttribute("data-copylength"));
67 if( !isNaN(cchLength) && cchLength>0 ){
68 text = text.slice(0,cchLength); /* Assume single-byte chars. */
69 }
70 copyTextToClipboard(text);
71 }
72 setTimeout(function(){
73 this.style.transition = "";
74 this.style.opacity = 1;
75 this.removeAttribute("data-copylocked");
76 }.bind(this),400);
 
 
 
77 }
78 /* Create a temporary <textarea> element and copy the contents to clipboard. */
79 function copyTextToClipboard(text){
80 if( window.clipboardData && window.clipboardData.setData ){
81 window.clipboardData.setData("Text",text);
82 }else{
83 var elTextarea = document.createElement("textarea");
84 elTextarea.style.position = "fixed";
85 elTextarea.value = text;
86 document.body.appendChild(elTextarea);
87 elTextarea.select();
88 try{
89 document.execCommand("copy");
90 }catch(err){
91 }finally{
92 document.body.removeChild(elTextarea);
93 }
94 }
95 }
96

Keyboard Shortcuts

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