60 lines
2.1 KiB
JavaScript
60 lines
2.1 KiB
JavaScript
(function(){
|
|
// Copy-to-clipboard for code blocks
|
|
function setupCopyButtons(){
|
|
document.querySelectorAll('[data-copy-target]').forEach(function(btn){
|
|
btn.addEventListener('click', async function(){
|
|
var sel = btn.getAttribute('data-copy-target');
|
|
var el = document.querySelector(sel);
|
|
if(!el) return;
|
|
var text = el.innerText || el.textContent || '';
|
|
try{
|
|
await navigator.clipboard.writeText(text.trim());
|
|
var old = btn.innerHTML;
|
|
btn.innerHTML = 'Copied';
|
|
btn.classList.add('btn-success');
|
|
btn.classList.remove('btn-outline-secondary');
|
|
setTimeout(function(){
|
|
btn.innerHTML = old;
|
|
btn.classList.remove('btn-success');
|
|
btn.classList.add('btn-outline-secondary');
|
|
}, 1200);
|
|
}catch(e){
|
|
// Fallback
|
|
var ta = document.createElement('textarea');
|
|
ta.value = text.trim();
|
|
document.body.appendChild(ta);
|
|
ta.select();
|
|
try{ document.execCommand('copy'); }catch(_){}
|
|
document.body.removeChild(ta);
|
|
}
|
|
});
|
|
});
|
|
}
|
|
|
|
// Asciinema embed helper:
|
|
// Put <div class="asciicast" data-asciinema-id="12345"></div>
|
|
// Or provide a self-hosted player by swapping the script URL.
|
|
function setupAsciinema(){
|
|
document.querySelectorAll('.asciicast[data-asciinema-id]').forEach(function(el){
|
|
var id = el.getAttribute('data-asciinema-id');
|
|
if(!id || id === 'REPLACE_ME'){
|
|
el.innerHTML = '<div class="alert alert-warning mb-0">Add your asciinema id here: <code>data-asciinema-id</code>.</div>';
|
|
return;
|
|
}
|
|
// Avoid injecting twice
|
|
if(document.getElementById('asciinema-embed-'+id)) return;
|
|
|
|
var s = document.createElement('script');
|
|
s.src = 'https://asciinema.org/a/' + encodeURIComponent(id) + '.js';
|
|
s.id = 'asciinema-embed-'+id;
|
|
s.async = true;
|
|
// The script replaces a placeholder div with the player if it's directly after it.
|
|
el.appendChild(s);
|
|
});
|
|
}
|
|
|
|
document.addEventListener('DOMContentLoaded', function(){
|
|
setupCopyButtons();
|
|
setupAsciinema();
|
|
});
|
|
})();
|