|
46 | 46 | white-space: pre; |
47 | 47 | margin-bottom: 1.5rem; |
48 | 48 | border: 1px solid #444; |
| 49 | + position: relative; |
49 | 50 | } |
50 | 51 |
|
51 | 52 | code { |
52 | 53 | background: none; |
53 | 54 | color: inherit; |
54 | 55 | } |
55 | 56 |
|
| 57 | + .copy-button { |
| 58 | + background-color: #4a5568; |
| 59 | + color: #fff; |
| 60 | + border: none; |
| 61 | + border-radius: 4px; |
| 62 | + padding: 0.3rem 0.6rem; |
| 63 | + font-size: 12px; |
| 64 | + font-family: inherit; |
| 65 | + cursor: pointer; |
| 66 | + transition: background-color 0.2s ease; |
| 67 | + position: absolute; |
| 68 | + top: 10px; |
| 69 | + right: 10px; |
| 70 | + z-index: 1; |
| 71 | + } |
| 72 | + |
| 73 | + .copy-button:hover { |
| 74 | + background-color: #2d3748; |
| 75 | + } |
| 76 | + |
56 | 77 | table { |
57 | 78 | width: 100%; |
58 | 79 | border-collapse: collapse; |
@@ -144,13 +165,35 @@ <h1>ValueMapper</h1> |
144 | 165 | async function loadReadme() { |
145 | 166 | const res = await fetch('https://raw.githubusercontent.com/TechFusionMasters/ValueMapper/main/README.md'); |
146 | 167 | const markdown = await res.text(); |
147 | | - |
148 | 168 | const html = marked.parse(markdown); |
149 | 169 | document.getElementById('readme-content').innerHTML = html; |
| 170 | + addCopyButtons(); |
| 171 | + } |
| 172 | + |
| 173 | + function addCopyButtons() { |
| 174 | + const blocks = document.querySelectorAll("pre > code"); |
| 175 | + blocks.forEach((codeBlock) => { |
| 176 | + const button = document.createElement("button"); |
| 177 | + button.innerText = "Copy"; |
| 178 | + button.className = "copy-button"; |
| 179 | + button.addEventListener("click", async () => { |
| 180 | + try { |
| 181 | + await navigator.clipboard.writeText(codeBlock.innerText); |
| 182 | + button.innerText = "Copied!"; |
| 183 | + setTimeout(() => (button.innerText = "Copy"), 1500); |
| 184 | + } catch (err) { |
| 185 | + console.error("Copy failed", err); |
| 186 | + button.innerText = "Error"; |
| 187 | + } |
| 188 | + }); |
| 189 | + |
| 190 | + const pre = codeBlock.parentNode; |
| 191 | + pre.appendChild(button); |
| 192 | + }); |
150 | 193 | } |
151 | 194 |
|
152 | 195 | const script = document.createElement('script'); |
153 | | - script.src = 'https://cdn.jsdelivr.net/npm/marked/marked.min.js?v=1.0.3'; // cache-busting query |
| 196 | + script.src = 'https://cdn.jsdelivr.net/npm/marked/marked.min.js?v=1.0.3'; |
154 | 197 | script.onload = loadReadme; |
155 | 198 | document.body.appendChild(script); |
156 | 199 | </script> |
|
0 commit comments