TEXT

PASTEBIN

Paste code or text, get a shareable link. No login. Choose your expiry.

0 chars
New paste
Developer Reference

Core Algorithm & Standalone Script

Standalone, zero-dependency JavaScript implementation powering this tool. Free to inspect, copy, and build upon.

const API = 'https://paste-api.toolpad.cc';

  const pasteInput  = document.getElementById('paste-input');
  const charCount   = document.getElementById('char-count');
  const langSelect  = document.getElementById('lang-select');
  const expirySelect = document.getElementById('expiry-select');
  const createBtn   = document.getElementById('create-btn');
  const clearBtn    = document.getElementById('clear-btn');
  const createError = document.getElementById('create-error');
  const resultSection = document.getElementById('result-section');
  const shareLink   = document.getElementById('share-link');
  const copyLinkBtn = document.getElementById('copy-link-btn');

  const createSection = document.getElementById('create-section');
  const viewSection   = document.getElementById('view-section');
  const pasteContent  = document.getElementById('paste-content');
  const metaLang      = document.getElementById('meta-lang');
  const metaCreated   = document.getElementById('meta-created');
  const metaExpiry    = document.getElementById('meta-expiry');
  const copyContentBtn = document.getElementById('copy-content-btn');
  const viewError     = document.getElementById('view-error');

  // ── CREATE mode ─────────────────────────────────────────────────
  pasteInput.addEventListener('input', () => {
    charCount.textContent = pasteInput.value.length.toLocaleString() + ' chars';
  });

  clearBtn.addEventListener('click', () => {
    pasteInput.value = '';
    charCount.textContent = '0 chars';
    resultSection.style.display = 'none';
    createError.style.display = 'none';
    pasteInput.focus();
  });

  createBtn.addEventListener('click', async () => {
    const content = pasteInput.value;
    if (!content.trim()) {
      showError(createError, 'Nothing to paste.');
      return;
    }
    createBtn.disabled = true;
    createBtn.textContent = 'Creating…';
    createError.style.display = 'none';

    try {
      const res = await fetch(API + '/', {
        method: 'POST',
        headers: { 'Content-Type': 'application/json' },
        body: JSON.stringify({
          content,
          lang: langSelect.value,
          expiry: expirySelect.value,
        }),
      });
      const data = await res.json();
      if (!res.ok) throw new Error(data.error || 'Failed');

      const url = `${location.origin}/paste/?id=${data.id}`;
      shareLink.value = url;
      resultSection.style.display = 'block';
      resultSection.scrollIntoView({ behavior: 'smooth', block: 'nearest' });
      copyToClipboard(url, copyLinkBtn);
    } catch (e) {
      showError(createError, e.message);
    } finally {
      createBtn.disabled = false;
      createBtn.textContent = 'Create Paste →';
    }
  });

  copyLinkBtn.addEventListener('click', () => copyToClipboard(shareLink.value, copyLinkBtn));

  // ── VIEW mode ────────────────────────────────────────────────────
  const id = new URLSearchParams(location.search).get('id');
  if (id) {
    createSection.style.display = 'none';
    viewSection.style.display = 'block';
    loadPaste(id);
  }

  async function loadPaste(id) {
    try {
      const res = await fetch(`${API}/?id=${encodeURIComponent(id)}`);
      const data = await res.json();
      if (!res.ok) throw new Error(data.error || 'Not found');

      pasteContent.textContent = data.content;
      metaLang.textContent = '⌁ ' + (data.lang || 'plain');

      const created = new Date(data.created);
      metaCreated.textContent = '◷ ' + created.toLocaleDateString(undefined, { year:'numeric', month:'short', day:'numeric', hour:'2-digit', minute:'2-digit' });

      if (data.expires_at) {
        const exp = new Date(data.expires_at);
        const diff = exp - Date.now();
        metaExpiry.textContent = diff > 0
          ? '⏳ Expires ' + formatRelative(diff)
          : '✗ Expired';
      } else {
        metaExpiry.textContent = '∞ Never expires';
      }

      copyContentBtn.addEventListener('click', () => copyToClipboard(data.content, copyContentBtn));

    } catch (e) {
      viewError.textContent = e.message;
      viewError.style.display = 'block';
      pasteContent.textContent = '';
    }
  }

  function formatRelative(ms) {
    const s = Math.floor(ms / 1000);
    if (s < 3600) return `in ${Math.ceil(s / 60)} min`;
    if (s < 86400) return `in ${Math.ceil(s / 3600)} hr`;
    return `in ${Math.ceil(s / 86400)} day${Math.ceil(s / 86400) !== 1 ? 's' : ''}`;
  }

  // ── Helpers ───────────────────────────────────────────────────────
  function copyToClipboard(text, btn) {
    navigator.clipboard.writeText(text).then(() => {
      const orig = btn.textContent;
      btn.textContent = 'Copied!';
      setTimeout(() => btn.textContent = orig, 1800);
    });
  }

  function showError(el, msg) {
    el.textContent = msg;
    el.style.display = 'block';
  }