Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Feature Request] YouTube Embeds #2036

Open
loregamer opened this issue Oct 25, 2024 · 2 comments
Open

[Feature Request] YouTube Embeds #2036

loregamer opened this issue Oct 25, 2024 · 2 comments

Comments

@loregamer
Copy link

loregamer commented Oct 25, 2024

Describe the problem

Can you guys make it so YouTube embeds use an iframe and displayed like this?
image

Describe the solution you'd like

I've made a userscript to make this possible, but would be nice if built in:

(function () {
  "use strict";

  function fixYouTubeEmbeds() {
    const embedContainers = document.querySelectorAll(
      "div.prxiv40._1mqalmd1._1mqalmd0.prxiv41.prxiv41t._1pb2z300"
    );

    embedContainers.forEach((container) => {
      const link = container.querySelector('a[href*="youtu"]');
      if (link) {
        const url = new URL(link.href);
        let videoId;

        if (url.pathname.includes("/watch")) {
          videoId = url.searchParams.get("v");
        } else if (url.pathname.includes("/shorts/")) {
          videoId = url.pathname.split("/shorts/")[1].split("?")[0];
        } else if (url.hostname === "youtu.be") {
          videoId = url.pathname.slice(1).split("?")[0];
        }

        if (videoId) {
          const embed = document.createElement("iframe");
          embed.src = `https://www.youtube.com/embed/${videoId}`;
          embed.width = "480";
          embed.height = "270";
          embed.frameBorder = "0";
          embed.allow =
            "accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture";
          embed.allowFullscreen = true;

          // Find the scroll container before modifying the DOM
          const scrollContainer = document.querySelector(
            "div._4yxtfd2._1mqalmd1._1mqalmd0._4yxtfd4._4yxtfdc._4yxtfdg._4yxtfdi._4yxtfdn"
          );
          const scrollBottom = scrollContainer
            ? scrollContainer.scrollHeight -
              scrollContainer.scrollTop -
              scrollContainer.clientHeight
            : 0;

          container.innerHTML = "";
          container.appendChild(embed);

          container.className = "";
          container.style.display = "flex";
          container.style.justifyContent = "center";
          container.style.alignItems = "center";

          // Adjust scroll position if we were at the bottom
          if (scrollContainer && scrollBottom < 10) {
            scrollContainer.scrollTop = scrollContainer.scrollHeight;
          }
        }
      }
    });
  }

  function debounce(func, wait) {
    let timeout;
    return function executedFunction(...args) {
      const later = () => {
        clearTimeout(timeout);
        func(...args);
      };
      clearTimeout(timeout);
      timeout = setTimeout(later, wait);
    };
  }

  function setupObservers() {
    const debouncedFixEmbeds = debounce(fixYouTubeEmbeds, 100);

    const intersectionObserver = new IntersectionObserver(
      (entries) => {
        if (entries.some((entry) => entry.isIntersecting)) {
          debouncedFixEmbeds();
        }
      },
      { subtree: true, childList: true }
    );

    intersectionObserver.observe(document.body);

    const mutationObserver = new MutationObserver((mutations) => {
      if (
        mutations.some(
          (mutation) =>
            mutation.type === "childList" && mutation.addedNodes.length > 0
        )
      ) {
        debouncedFixEmbeds();
      }
    });

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

  function initializeScript() {
    fixYouTubeEmbeds();
    setupObservers();
  }

  initializeScript();
})();

Alternatives considered

No response

Additional context

No response

@notramo
Copy link

notramo commented Nov 6, 2024

Definitely wouldn't be nice if built-in. I recommend to use Discord if you want embedded tracker in your messaging app. Cinny is meant to be private.

@loregamer loregamer changed the title YouTube Embeds [Feature Request] YouTube Embeds Nov 30, 2024
@loregamer
Copy link
Author

Definitely wouldn't be nice if built-in.

Cinny is meant to be private.

image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants
@notramo @loregamer and others