|
1 | | -<!-- Code for version selectors --> |
| 1 | +<!-- Code for copy to clipboard button --> |
| 2 | +<script> |
| 3 | + document.addEventListener("DOMContentLoaded", function () { |
| 4 | + |
| 5 | + // don't run script for tabbed code examples |
| 6 | + if (!document.querySelector('.codetabs.cli.group.flex.justify-start.items-center.flex-wrap.box-border.rounded-lg.mt-0.mb-0.mx-auto.bg-slate-900')) { |
| 7 | + document.querySelectorAll('div.highlight').forEach(block => { |
| 8 | + // Create a wrapper for the button and message |
| 9 | + const wrapper = document.createElement('div'); |
| 10 | + |
| 11 | + if (block.parentElement.classList.contains("expand-content")) { |
| 12 | + wrapper.style = 'position:absolute;top:10px;right:20px;z-index:1;display:flex;align-items:center;gap:6px;'; |
| 13 | + } else { |
| 14 | + wrapper.style = 'position:absolute;top:10px;right:10px;z-index:1;display:flex;align-items:center;gap:6px;'; |
| 15 | + } |
| 16 | + // Create the copy button |
| 17 | + const button = document.createElement('button'); |
| 18 | + button.innerHTML = ` |
| 19 | + <button class="clipboard-button text-neutral-400 hover:text-slate-100 bg-slate-600 relative h-7 w-7 p-1 rounded rounded-mx" title="Copy to clipboard"> |
| 20 | + <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 48 48" fill="currentColor"> |
| 21 | + <path d="M9 43.95q-1.2 0-2.1-.9-.9-.9-.9-2.1V10.8h3v30.15h23.7v3Zm6-6q-1.2 0-2.1-.9-.9-.9-.9-2.1v-28q0-1.2.9-2.1.9-.9 2.1-.9h22q1.2 0 2.1.9.9.9.9 2.1v28q0 1.2-.9 2.1-.9.9-2.1.9Zm0-3h22v-28H15v28Zm0 0v-28 28Z"></path> |
| 22 | + </svg> |
| 23 | + `; |
| 24 | + |
| 25 | + // Create the tooltip container |
| 26 | + const tooltipContainer = document.createElement('div'); |
| 27 | + tooltipContainer.className = 'tooltip relative inline-block'; |
| 28 | + |
| 29 | + // Create the tooltip span |
| 30 | + const tooltip = document.createElement('span'); |
| 31 | + tooltip.className = 'tooltiptext hidden bg-slate-200 rounded rounded-mx text-slate-800 text-center text-xs p-1 absolute'; |
| 32 | + tooltip.style.bottom = '-0.6rem'; |
| 33 | + tooltip.style.left = '-5.5rem'; |
| 34 | + tooltip.textContent = 'Copied!'; |
2 | 35 |
|
| 36 | + tooltipContainer.appendChild(tooltip); |
| 37 | + button.appendChild(tooltipContainer); |
| 38 | + |
| 39 | + // Handle copy logic |
| 40 | + button.addEventListener('click', () => { |
| 41 | + const lines = block.querySelectorAll('span.cl'); |
| 42 | + const text = Array.from(lines).map(line => line.textContent).join(''); |
| 43 | + |
| 44 | + navigator.clipboard.writeText(text).then(() => { |
| 45 | + tooltip.style.display = 'block'; |
| 46 | + setTimeout(() => { |
| 47 | + tooltip.style.display = 'none'; |
| 48 | + }, 2000); |
| 49 | + }).catch(err => { |
| 50 | + console.error("Copy failed", err); |
| 51 | + }); |
| 52 | + }); |
| 53 | + |
| 54 | + // Append button and message |
| 55 | + wrapper.appendChild(button); |
| 56 | + wrapper.appendChild(tooltipContainer); |
| 57 | + block.style.position = 'relative'; |
| 58 | + block.appendChild(wrapper); |
| 59 | + }); |
| 60 | + } |
| 61 | + }); |
| 62 | +</script> |
| 63 | + |
| 64 | + |
| 65 | +<!-- Code for version selectors --> |
3 | 66 | <script> |
4 | 67 | window.onload = function(){ |
5 | 68 | var currentUrl = window.location.href |
|
0 commit comments