CSP Header Parser

by ledlogic

HTML

<div class="header">
  <h1>CSP Parser</h1>
  <p>// paste → split → inspect</p>
</div>

<div class="container">

  <!-- Input Panel -->
  <div class="panel">
    <div class="panel-header">
      <span class="panel-label">Input</span>
      <div class="dot-row"><div class="dot"></div><div class="dot"></div><div class="dot"></div></div>
    </div>
    <textarea id="inputArea" placeholder="Paste your CSP header here...&#10;&#10;default-src 'self'; script-src 'self' https://cdn.example.com; style-src 'self' 'unsafe-inline'; img-src *; connect-src 'self' https://api.example.com"></textarea>
    <div class="stats-bar">
      <div class="stat"><span class="stat-val" id="charCount">0</span><span class="stat-label">chars</span></div>
      <div class="stat"><span class="stat-val" id="semiCount">0</span><span class="stat-label">semicolons</span></div>
    </div>
  </div>

  <!-- Actions -->
  <div class="actions">
    <button class="btn-primary" onclick="parseCSP()">⚡ Parse</button>
    <button class="btn-secondary copy-all-btn" id="copyAllBtn" onclick="copyAll()">Copy All</button>
    <button class="btn-secondary" onclick="loadExample()">Load Example</button>
    <button class="btn-danger" onclick="clearAll()">Clear</button>
  </div>

  <!-- Output Panel -->
  <div class="panel">
    <div class="panel-header">
      <span class="panel-label">Directives</span>
      <span class="stat"><span class="stat-val" id="directiveCount">—</span><span class="stat-label" style="margin-left:6px">found</span></span>
    </div>
    <div class="output-list" id="outputList">
      <div class="empty-state">
        <span class="empty-icon">⬡</span>
        Paste a CSP header above and click Parse
      </div>
    </div>
  </div>

</div>

CSS

@import url('https://fonts.googleapis.com/css2?family=JetBrains+Mono:wght@400;600;700&family=Syne:wght@400;700;800&display=swap');

  *, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }

  :root {
    --bg: #0a0c10;
    --surface: #111318;
    --surface2: #181c24;
    --border: #252933;
    --accent: #00e5ff;
    --accent2: #7b61ff;
    --warn: #ff6b35;
    --text: #e2e8f0;
    --muted: #5a6478;
    --success: #00d084;
  }

  body {
    background: var(--bg);
    color: var(--text);
    font-family: 'Syne', sans-serif;
    min-height: 100vh;
    display: flex;
    flex-direction: column;
    align-items: center;
    padding: 40px 20px;
    position: relative;
    overflow-x: hidden;
  }

  body::before {
    content: '';
    position: fixed;
    top: -200px; left: 50%;
    transform: translateX(-50%);
    width: 800px; height: 400px;
    background: radial-gradient(ellipse, rgba(0,229,255,0.06) 0%, transparent 70%);
    pointer-events: none;
  }

  .header {
    text-align: center;
    margin-bottom: 40px;
  }

  .header h1 {
    font-size: clamp(2rem, 5vw, 3.2rem);
    font-weight: 800;
    letter-spacing: -0.03em;
    background: linear-gradient(135deg, var(--accent) 0%, var(--accent2) 100%);
    -webkit-background-clip: text;
    -webkit-text-fill-color: transparent;
    background-clip: text;
  }

  .header p {
    color: var(--muted);
    font-family: 'JetBrains Mono', monospace;
    font-size: 0.8rem;
    margin-top: 8px;
    letter-spacing: 0.05em;
  }

  .container {
    width: 100%;
    max-width: 900px;
    display: flex;
    flex-direction: column;
    gap: 24px;
  }

  .panel {
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: 12px;
    overflow: hidden;
  }

  .panel-header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 14px 20px;
    border-bottom: 1px solid...

JavaScript

const inputArea = document.getElementById('inputArea');
  const outputList = document.getElementById('outputList');
  const charCount = document.getElementById('charCount');
  const semiCount = document.getElementById('semiCount');
  const directiveCount = document.getElementById('directiveCount');
  const copyAllBtn = document.getElementById('copyAllBtn');

  // Live stats
  inputArea.addEventListener('input', updateStats);

  function updateStats() {
    const val = inputArea.value;
    charCount.textContent = val.length;
    semiCount.textContent = (val.match(/;/g) || []).length;
  }

  function parseCSP() {
    const raw = inputArea.value.trim();
    if (!raw) return;

    // Split on semicolons, clean up whitespace, remove empty entries
    const directives = raw
      .split(';')
      .map(d => d.trim())
      .filter(d => d.length > 0);

    directiveCount.textContent = directives.length;
    copyAllBtn.classList.toggle('visible', directives.length > 0);

    if (directives.length === 0) {
      outputList.innerHTML = '<div class="empty-state"><span class="empty-icon">⚠</span>No directives found. Make sure your input contains semicolons.</div>';
      return;
    }

    outputList.innerHTML = directives.map((directive, i) => {
      const parts = directive.split(/\s+/);
      const key = parts[0];
      const values = parts.slice(1).join(' ');

      const highlighted = values
        ? `<span class="directive-key">${escHtml(key)}</span> <span class="directive-values">${escHtml(values)}</span>`
        : `<span class="directive-key">${escHtml(key)}</span>`;

      return `
        <div class="directive-item" style="animation-delay:${i * 40}ms">
          <div class="directive-num">${String(i + 1).padStart(2, '0')}</div>
          <div class="directive-content">${highlighted}</div>
          <button class="directive-copy" onclick="copyDirective(this, ${JSON.stringify(directive)})">copy</button>
       ...