Surprise! We've been running on hardware provided by BuyVM for a few months and wanted to show them a little appreciation.
Running a paste site comes with unique challenges, ones that aren't always obvious and hard to control. As such, BuyVM offered us a home where we could worry less about the hosting side of things and focus on maintaining a clean and useful service! Go check them out and show them some love!
Submitted on September 14, 2024 at 12:30 AM

New Paste 1 (Text)

// ==UserScript==
// @name         Bluesky Post Embedder
// @version      1.1
// @description  Embeds Bluesky posts in Xenforo-compatible forums and adjusts iframe height dynamically.
// @match        *://*/*
// ==/UserScript==

(function() {
  'use strict';

  const blueskyRegex = /https:\/\/bsky\.app\/profile\/(?:did:plc:[^\/]+|[^\/]+)\/post\/[^\/]+/;
  const EMBED_URL = 'https://embed.bsky.app';

  function handleToDid(userHandle) {
    return fetch(`https://api.allorigins.win/raw?url=https://bsky.social/xrpc/com.atproto.identity.resolveHandle?handle=${userHandle}`)
      .then(response => response.text())
      .then(data => JSON.parse(data))
      .then(jsonData => jsonData.did);
  }

  async function createBlueskyIframe(link) {
    const href = link.getAttribute('href');
    let postUsername, postID;

    if (href.includes("did:plc:")) {
      postUsername = href.split("/")[4];
    } else if (!href.startsWith("did:")) {
      try {
        postUsername = await handleToDid(href.split("/")[4]);
      } catch (error) {
        console.error("Error resolving handle:", error);
        return;
      }
    } else {
      postUsername = href;
    }

    const urlParts = href.split("/");
    if (urlParts[3] === "profile") {
      postID = urlParts[6];
    } else {
      postID = urlParts[6];
    }

    const embedHTML = `
      <div class="bluesky-embed-container" style="max-width: 100%; margin: 0 auto; overflow: hidden;">
        <iframe src="https://embed.bsky.app/embed/${postUsername}/app.bsky.feed.post/${postID}"
                frameborder="0"
                width="100%"
                style="border: none; display: block; flex-grow: 1;"
                scrolling="no"
                allowtransparency="true"
                title="Bluesky post"
                data-bluesky-id="bluesky-${postID}">
        </iframe>
        <script async src="https://embed.bsky.app/static/embed.js"
                data-handle="${postUsername}"
                data-skeet="${postID}">
        </script>
      </div>
    `;

    link.outerHTML = embedHTML;
  }

  function replaceLinks() {
    const links = document.querySelectorAll('a[href]');
    links.forEach(link => {
      if (link.href.match(blueskyRegex)) {
        createBlueskyIframe(link).catch(error => console.error("Error creating iframe:", error));
      }
    });
  }

  function adjustIframeHeight(event) {
    if (event.origin !== EMBED_URL) {
      console.warn('Received message from unauthorized origin:', event.origin);
      return;
    }

    const id = event.data.id;
    const height = event.data.height;

    if (height) {
      if (id) {
        const embed = document.querySelector(`[data-bluesky-id="${id}"]`);
        if (embed) {
          console.log(`Setting height for iframe ID ${id} to ${height}px`);
          embed.style.height = `${height}px`;
        } else {
          console.warn('No matching iframe found for ID:', id);
        }
      } else {
        // Handle cases where ID is null
        const iframes = document.querySelectorAll('iframe[data-bluesky-id]');
        iframes.forEach((iframe) => {
          console.log('Setting height for iframe without ID to', height, 'px');
          iframe.style.height = `${height}px`;
        });
      }
    } else {
      console.warn('Received message without height data:', event);
    }
  }

  // Use MutationObserver to handle dynamic content loading
  const observer = new MutationObserver((mutationsList, observer) => {
    for (let mutation of mutationsList) {
      if (mutation.type === 'childList') {
        mutation.addedNodes.forEach(node => {
          if (node.nodeType === 1) { // Element node
            replaceLinks();
          }
        });
      }
    }
  });

  observer.observe(document.body, { childList: true, subtree: true });

  addEventListener("load", function() {
    replaceLinks();
  });

  // Add message listener for iframe height adjustment
  window.addEventListener('message', adjustIframeHeight);

})();