← Pablo Murad

Snippets

Updoot buttons for Pika posts

Another one by Robert Birming: NeatStats updoot buttons for Pika. Emoji buttons readers can click under each post, shown only on post pages, placed above the tags, and working even when Pika swaps pages without reloading.

Another one by Robert Birming, saved here with credit. It adds updoots to a Pika blog: small emoji buttons at the end of each post that readers can click, like a "like" without accounts. The counting is done by NeatStats1.

It goes into Pika under Settings, Custom Code, in the custom <body> code box. A few details make it nicer than pasting the buttons straight into a post:

  • It only shows on post pages, and removes itself everywhere else.
  • It moves the buttons just above the tags and the post footer, or to the end of the text when there's no footer.
  • It loads the NeatStats script only after the buttons are in place.
  • It still works when Pika changes pages without a full reload.

The emojis are set with data-emoji on each <span>, and adding or removing a line adds or removes a button. data-slowmode="true" allows one vote per visitor, per day, per button; without it, clicks are unlimited and holding the button down adds up to 100.

The NeatStats logo: a black pie-chart icon above the word "NeatStats" in a black script font, on a yellow gradient.
NeatStats, where the updoots come from.
updoot-buttons-for-pika-posts.html raw
<!--
  Neatstats updoot buttons for Pika
  Paste into Settings > Custom Code > custom <body> code.

  Customising:
  - Change the emojis by editing data-emoji="..." on each <span>.
  - Add or remove buttons by adding or deleting <span> lines.
  - data-slowmode="true" allows one vote per visitor per day per button.
    Delete it to allow unlimited clicks (press-and-hold adds up to 100).
  - Change the space above the buttons with marginTop below (e.g. "3rem").
-->

<div id="post-updoots">
  <span class="updoot" data-emoji="❤️" data-slowmode="true"></span>
  <span class="updoot" data-emoji="👏" data-slowmode="true"></span>
  <span class="updoot" data-emoji="💡" data-slowmode="true"></span>
</div>
<script>
  (function () {
    function addDoots() {
      var doots = document.getElementById("post-updoots");
      if (!doots) return;

      // Only show on post pages
      if (!document.body.classList.contains("site-post-body")) { doots.remove(); return; }

      var meta = document.querySelector(".site-post .site-meta");
      var content = document.querySelector(".site-post .trix-content");

      // Spacing and layout
      doots.style.marginTop = "2rem";
      doots.style.display = "flex";
      doots.style.gap = "0.5rem";

      // Place just above the tags/footer, or at the end of the post text
      if (meta) meta.before(doots);
      else if (content) content.after(doots);
      else { doots.remove(); return; }

      // Load Neatstats once the buttons are in place
      var s = document.createElement("script");
      s.src = "https://neatstats.net/updoot.js";
      document.body.appendChild(s);
    }

    // Works on a normal page load and when Pika swaps pages without reloading
    if (document.readyState === "loading") {
      document.addEventListener("DOMContentLoaded", addDoots, { once: true });
    } else {
      addDoots();
    }
  })();
</script>

curl -O https://pablomurad.com/snippets/updoot-buttons-for-pika-posts.html

Notes

  1. NeatStats is a small, privacy-first stats service from Neatnik, the person behind omg.lol. It logs no IP addresses, uses no cookies, and costs $5 a year. Updoots are one of its features: any emoji can become a button with a click counter. ↩

Reply by email

Comments

Leave a comment

Comments are read before they show up here. No email address needed.