-
Notifications
You must be signed in to change notification settings - Fork 39
/
copy.js
36 lines (36 loc) · 1.28 KB
/
copy.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
window.onload = function() {
var pre = document.getElementsByTagName('pre');
for (var i = 0; i < pre.length; i++) {
var b = document.createElement('button');
b.className = 'clipboard fa fa-clipboard';
if (pre[i].childNodes.length === 1 && pre[i].childNodes[0].nodeType === 3) {
var div = document.createElement('div');
div.textContent = pre[i].textContent;
pre[i].textContent = '';
pre[i].appendChild(div);
}
pre[i].appendChild(b);
}
var clipboard = new ClipboardJS('.clipboard', {
target: function(b) {
var p = b.parentNode;
if (p.className.includes("highlight")) {
var elems = p.getElementsByTagName("code");
if (elems.length > 0)
return elems[0];
}
return p.childNodes[0];
}
});
clipboard.on('success', function(e) {
e.clearSelection();
e.trigger.className = e.trigger.className.replace("fa-clipboard", "fa-check");
setTimeout(function() {
e.trigger.textContent = 'Copied';
}, 2000);
});
clipboard.on('error', function(e) {
console.error('Action:', e.action, e);
console.error('Trigger:', e.trigger);
});
};