/* * The guides' one behaviour: a Copy button on every box of text to copy (`.copy`), copying the text of its * `code` element. Without the script the text stays selectable by hand. */ (function () { 'use strict'; async function copy(text, button) { let done = false; try { await navigator.clipboard.writeText(text); done = true; } catch { const area = document.createElement('textarea'); area.value = text; area.style.cssText = 'position:fixed;opacity:0'; document.body.append(area); area.select(); try { done = document.execCommand('copy'); } catch { done = false; } area.remove(); } button.textContent = done ? 'Copied' : 'Select it and copy'; setTimeout(() => { button.textContent = 'Copy'; }, 1500); } for (const box of document.querySelectorAll('.copy')) { const code = box.querySelector('code'); if (!code) continue; const button = document.createElement('button'); button.type = 'button'; button.textContent = 'Copy'; button.addEventListener('click', () => copy(code.textContent, button)); box.append(button); } })();