repository

BlowfishCMS

A small content management system for OpenBSD

version 0.45.6 · ISC · 2026-12-03 · releases

68 lines · 3.6 KB · download this file

(() => {
  // Share, copy-link, like and comment enhancements for any item's page (articles,
  // pictures, tracks, videos). Every control also works as a plain form or link without JavaScript.
  const actions = document.getElementById('post-actions');
  if (actions) {
    const url = actions.dataset.url;
    const title = actions.dataset.title;
    const status = actions.querySelector('.share-status');
    const say = text => { status.textContent = text; };
    const share = actions.querySelector('[data-share]');
    const copy = actions.querySelector('[data-copy-link]');
    const field = actions.querySelector('.share-link');
    // The browser's own share sheet (phones), where there is one.
    if (navigator.share) {
      share.hidden = false;
      share.addEventListener('click', async () => {
        try { await navigator.share({title, url}); }
        catch (error) { if (error.name !== 'AbortError') say('Sharing isn’t available here. Copy the link instead.'); }
      });
    }
    // "copy link", where the browser allows it; otherwise the link stays shown to
    // copy by hand.
    if (navigator.clipboard?.writeText) {
      copy.hidden = false;
      field.hidden = true;
      copy.addEventListener('click', async () => {
        try { await navigator.clipboard.writeText(url); say('Link copied.'); }
        catch { field.hidden = false; field.querySelector('input').select(); say('Copy the selected link.'); }
      });
    }
    // Liking without reloading the page: the form is sent with fetch, and the
    // button and count are updated from the answer.
    const like = actions.querySelector('.like-form');
    like?.addEventListener('submit', async event => {
      event.preventDefault();
      const button = like.querySelector('button');
      button.disabled = true;
      try {
        const response = await fetch(like.action, {method: 'POST', body: new FormData(like), headers: {'X-Requested-With': 'fetch'}});
        const result = await response.json();
        if (!response.ok || !result.ok) throw new Error(result.message);
        button.setAttribute('aria-pressed', result.liked ? 'true' : 'false');
        button.querySelector('.like-label').textContent = result.liked ? 'liked' : 'like';
        like.querySelector('.like-count').textContent = `${result.count} ${result.count === 1 ? 'like' : 'likes'}`;
      } catch (error) { say(error.message || 'Couldn’t save your like. Try again.'); }
      finally { button.disabled = false; }
    });
  }
  // Posting a comment: sent with fetch; on success the page reloads at the new
  // comment, on a refusal the reason is shown and the text stays in the box.
  const form = document.getElementById('comment-form');
  form?.addEventListener('submit', async event => {
    event.preventDefault();
    const button = form.querySelector('button[type=submit]');
    const status = form.querySelector('.comment-status');
    button.disabled = true;
    status.textContent = 'Posting…';
    try {
      const response = await fetch(form.action, {method: 'POST', body: new FormData(form), headers: {'X-Requested-With': 'fetch'}});
      const result = await response.json();
      if (!result.ok) { status.textContent = result.message || 'Your comment couldn’t be posted.'; return; }
      const next = new URL(result.location, location.href);
      if (next.pathname + next.search === location.pathname + location.search) { location.hash = next.hash; location.reload(); }
      else location.assign(next);
    } catch { status.textContent = 'Connection problem. Your comment is still here; try again.'; }
    finally { button.disabled = false; }
  });
})();