← back to Sdcc Mockups

extension-v0/content/badge.js

53 lines

// SDCC Borrower Helper — content script
// Runs on studentaid.gov + nslds.ed.gov pages.
// Adds a small floating badge in the corner that opens the extension popup.
// Reads the page DOM (passive, read-only) but does NOT send anything anywhere.
// Verifiable: open DevTools Network tab while this runs — zero outbound requests.

(function () {
  if (window.__sdcc_helper_loaded) return;
  window.__sdcc_helper_loaded = true;

  const badge = document.createElement('div');
  badge.id = 'sdcc-helper-badge';
  badge.setAttribute('role', 'complementary');
  badge.setAttribute('aria-label', 'SDCC Borrower Helper');
  badge.innerHTML = `
    <div class="sdcc-pulse" aria-hidden="true"></div>
    <div class="sdcc-text">
      <div class="sdcc-eyebrow">SDCC Helper · private</div>
      <div class="sdcc-msg" id="sdcc-msg">scanning page for loan data…</div>
    </div>
    <button class="sdcc-close" aria-label="Hide" type="button">×</button>
  `;
  badge.querySelector('.sdcc-close').addEventListener('click', () => {
    badge.remove();
    sessionStorage.setItem('sdcc_helper_dismissed', '1');
  });

  if (sessionStorage.getItem('sdcc_helper_dismissed') === '1') return;
  document.body.appendChild(badge);

  // Local-only DOM observation. We never call fetch() or postMessage outside the page.
  const msg = badge.querySelector('#sdcc-msg');

  function scan() {
    const text = document.body.innerText.slice(0, 5000); // bounded read
    const findings = [];
    if (/total balance|outstanding balance|principal balance/i.test(text)) findings.push('balance');
    if (/SAVE|Income-Driven|IDR|PAYE|IBR/i.test(text)) findings.push('IDR plan');
    if (/MOHELA|Aidvantage|Nelnet|EdFinancial/i.test(text)) findings.push('servicer');
    if (/PSLF|Public Service Loan Forgiveness/i.test(text)) findings.push('PSLF');
    if (findings.length > 0) {
      msg.textContent = 'spotted: ' + findings.join(', ') + ' · click for tools';
      badge.classList.add('sdcc-active');
    } else {
      msg.textContent = 'no loan data on this page · click for tools';
    }
  }
  scan();
  // Re-scan on SPA navigation (studentaid.gov is a SPA)
  let last = location.href;
  setInterval(() => { if (location.href !== last) { last = location.href; setTimeout(scan, 500); } }, 1500);
})();