-
Notifications
You must be signed in to change notification settings - Fork 25
Expand file tree
/
Copy pathcopy-code.html
More file actions
68 lines (58 loc) · 1.48 KB
/
copy-code.html
File metadata and controls
68 lines (58 loc) · 1.48 KB
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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
<style>
.copy-btn {
position: absolute;
top: 5px;
right: 5px;
padding: 4px 8px;
font-size: 18px;
background: #969696;
color: white;
border: none;
border-radius: 3px;
cursor: pointer;
opacity: 0.7;
transition: opacity 0.3s;
}
.copy-btn:hover {
opacity: 1;
}
.copy-btn.copied {
background: #636363;
}
pre {
position: relative;
}
</style>
<script>
document.addEventListener('DOMContentLoaded', function() {
// Find all code blocks
const codeBlocks = document.querySelectorAll('pre code');
codeBlocks.forEach(function(codeBlock) {
const pre = codeBlock.parentElement;
// Create copy button
const button = document.createElement('button');
button.className = 'copy-btn';
button.textContent = '⎘';
// Add button to pre element
pre.appendChild(button);
// Add click event
button.addEventListener('click', function() {
// Create temporary textarea to copy text
const textarea = document.createElement('textarea');
textarea.value = codeBlock.textContent;
document.body.appendChild(textarea);
textarea.select();
document.execCommand('copy');
document.body.removeChild(textarea);
// Update button text
button.textContent = 'Copied!';
button.classList.add('copied');
// Reset button after 2 seconds
setTimeout(function() {
button.textContent = '⎘';
button.classList.remove('copied');
}, 2000);
});
});
});
</script>