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
tool.tsTypeScript47 lines2.3 KiBraw download
// A tool page's tag row: keep only the techniques that match the pressed
// context and capability tags. Any pressed value within a row, both rows at
// once — the catalog's rule. Without it the page is the full static list.

function boot() {
  const bar = document.querySelector<HTMLElement>('[data-tool-filter]');
  if (!bar || bar.dataset.booted) return;
  bar.dataset.booted = '1';

  const techs = [...document.querySelectorAll<HTMLElement>('.tool__techs .tech')];
  const tags = [...bar.querySelectorAll<HTMLButtonElement>('.tag')];
  const shown = bar.querySelector<HTMLElement>('[data-tool-shown]')!;
  const clear = bar.querySelector<HTMLButtonElement>('[data-tool-clear]')!;
  const pressed: Record<string, Set<string>> = { ctx: new Set(), cap: new Set() };

  function render() {
    let n = 0;
    for (const el of techs) {
      const caps = (el.dataset.cap || '').split('|');
      const hit = (!pressed.ctx.size || pressed.ctx.has(el.dataset.ctx || '')) && (!pressed.cap.size || caps.some((c) => pressed.cap.has(c)));
      el.hidden = !hit;
      if (hit) n++;
    }
    for (const b of tags) b.setAttribute('aria-pressed', String(pressed[b.dataset.kind!].has(b.dataset.value!)));
    const narrowed = pressed.ctx.size + pressed.cap.size > 0;
    shown.textContent = narrowed ? `${n} of ${techs.length} techniques` : `${techs.length} techniques`;
    clear.hidden = !narrowed;
  }
  function reset() { pressed.ctx.clear(); pressed.cap.clear(); render(); }

  bar.addEventListener('click', (e) => {
    const b = (e.target as HTMLElement).closest<HTMLButtonElement>('button');
    if (!b) return;
    if (b === clear) { reset(); return; }
    const set = pressed[b.dataset.kind!], value = b.dataset.value!;
    set.has(value) ? set.delete(value) : set.add(value);
    render();
  });
  // A link to one technique (#id, from the catalog) must never land on a row
  // the tags have hidden.
  const onHash = () => { const el = location.hash && document.getElementById(decodeURIComponent(location.hash.slice(1))); if (el && (el as HTMLElement).hidden) { reset(); el.scrollIntoView(); } };
  window.addEventListener('hashchange', onHash);
  document.addEventListener('astro:before-swap', () => window.removeEventListener('hashchange', onHash), { once: true });
}

boot();
document.addEventListener('astro:page-load', boot);