repositories

daemon-sec-lotl

DÆMONBins: GTFOBins × LOLBAS × WADComs × LOOBins in one filterable catalog

clonegit clone https://git.daemon-sec.xyz/daemon-sec-lotl.git
copy.tsTypeScript50 lines1.8 KiBraw download
// Copy-to-clipboard for every [data-copy] button on the site — the per-tool
// pages and the catalog island share this one path. Delegated on `document`
// so rows rendered later (the catalog fetch, "Show more") need no re-wiring.
// The button copies the text of the nearest `[data-cmdbar] code`.

export function copyText(text: string, btn: HTMLElement): void {
  const restore = btn.textContent;
  const flash = (label: string) => {
    btn.textContent = label;
    btn.classList.toggle('is-copied', label === 'copied');
    setTimeout(() => { btn.textContent = restore; btn.classList.remove('is-copied'); }, 1400);
  };
  const done = () => flash('copied');
  const failed = () => flash('failed');
  if (navigator.clipboard?.writeText) {
    navigator.clipboard.writeText(text).then(done).catch(() => fallback(text, done, failed));
  } else {
    fallback(text, done, failed);
  }
}

function fallback(text: string, done: () => void, failed: () => void): void {
  const ta = document.createElement('textarea');
  ta.value = text;
  ta.setAttribute('readonly', '');
  ta.style.position = 'fixed';
  ta.style.opacity = '0';
  document.body.appendChild(ta);
  ta.select();
  let ok = false;
  try { ok = document.execCommand('copy'); } catch { ok = false; }
  document.body.removeChild(ta);
  if (ok) done(); else failed();
}

function onClick(e: Event): void {
  const btn = (e.target as HTMLElement | null)?.closest<HTMLElement>('[data-copy]');
  if (!btn) return;
  const bar = btn.closest('[data-cmdbar]');
  const code = bar?.querySelector('code');
  copyText(code?.textContent || '', btn);
}

// Modules run once per JS context (the ClientRouter keeps it across
// navigations), so a module-level flag is the right double-bind guard.
let bound = false;
if (!bound) {
  bound = true;
  document.addEventListener('click', onClick);
}