Guest User

Kara YT Embed Fix

a guest
Dec 18th, 2021
248
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // ==UserScript==
  2. // @name         Kara YT Embed Fix
  3. // @namespace    karachan.org
  4. // @version      0.11
  5. // @description  Naprawia zepsute embedy na kara
  6. // @downloadURL  https://gist.github.com/kotjea2137/274d94980cd6867c60bf57c6b2cc5cb8/raw
  7. // @updateURL    https://gist.github.com/kotjea2137/274d94980cd6867c60bf57c6b2cc5cb8/raw
  8. // @author       Anonymous
  9. // @match        *://*.karachan.org/*
  10. // @exclude      http://www.karachan.org/*/src/*
  11. // @exclude      https://www.karachan.org/*/src/*
  12. // @exclude      http://karachan.org/*/src/*
  13. // @exclude      https://karachan.org/*/src/*
  14. // @grant        none
  15. // ==/UserScript==
  16.  
  17. (function () {
  18.   "use strict";
  19.  
  20.   const embeds = document.querySelectorAll("iframe");
  21.  
  22.   const fetchVideoTitle = async (id) => {
  23.     const body = await fetch(
  24.       `https://youtube.com/oembed?url=https://www.youtube.com/watch?v=${id}&format=json`
  25.     );
  26.  
  27.     if (body.status === 404) return "Film nie istnieje";
  28.     if (body.status === 401) return "Nie można pobrać tytułu";
  29.  
  30.     const json = await body.json();
  31.  
  32.     return json.title;
  33.   };
  34.  
  35.   const createThumbnail = (embed, newLink) => {
  36.     const videoSuffix = embed.src.split("/").pop();
  37.  
  38.     const newDiv = document.createElement("div");
  39.     newDiv.style.cssText = "position: relative;";
  40.  
  41.     const newImage = document.createElement("img");
  42.     newImage.src = `https://i.ytimg.com/vi/${videoSuffix}/hqdefault.jpg`;
  43.     newImage.style.cssText = "width:250px;height:250px;object-fit:cover;";
  44.  
  45.     newLink.appendChild(newImage);
  46.     newDiv.appendChild(newLink);
  47.  
  48.     embed.parentNode.replaceChild(newDiv, embed);
  49.     return newDiv;
  50.   };
  51.  
  52.   const createTitle = async (embed) => {
  53.     const title = await fetchVideoTitle(embed.src.split("/").pop());
  54.  
  55.     const newTitle = document.createElement("div");
  56.     newTitle.style.cssText =
  57.       "position: absolute;top: 10px;left: 10px;color: white;font-family: Arial, Helvetica, sans-serif;font-weight: bold;text-shadow: -1px -1px 0 #000, 1px -1px 0 #000, -1px 1px 0 #000, 1px 1px 0 #000;";
  58.     newTitle.innerHTML = title;
  59.  
  60.     return newTitle;
  61.   };
  62.  
  63.   const createYoutubeIcon = () => {
  64.     const youtubeIcon = document.createElement("img");
  65.     youtubeIcon.src = `https://www.freeiconspng.com/uploads/white-youtube-logo-png-28.png`;
  66.     youtubeIcon.style.cssText = "width:80px;height:60px;";
  67.  
  68.     return youtubeIcon;
  69.   };
  70.  
  71.   const createYoutubeIconDiv = (newLink, youtubeIcon) => {
  72.     const youtubeIconDiv = document.createElement("div");
  73.     youtubeIconDiv.style.cssText = "position: absolute;top: 193px;left: 5px;";
  74.  
  75.     newLink.appendChild(youtubeIcon);
  76.     youtubeIconDiv.appendChild(newLink);
  77.  
  78.     return youtubeIconDiv;
  79.   };
  80.  
  81.   const createYoutubeLink = (embed) => {
  82.     const videoSuffix = embed.src.split("/").pop();
  83.  
  84.     const newLink = document.createElement("a");
  85.     newLink.href = `http://www.youtube.com/watch?v=${videoSuffix}`;
  86.     newLink.target = "_blank";
  87.  
  88.     return newLink;
  89.   };
  90.  
  91.   const loopThroughEmbeds = async (embeds) => {
  92.     const thumbnailsArray = [];
  93.     const ytIcon = createYoutubeIcon();
  94.  
  95.     for (const embed of embeds) {
  96.       if (embed.src.includes("youtube")) {
  97.         const ytLink = createYoutubeLink(embed);
  98.         thumbnailsArray.push(createThumbnail(embed, ytLink));
  99.       }
  100.     }
  101.  
  102.     let embedCounter = 0;
  103.  
  104.     for (const embed of embeds) {
  105.       if (embed.src.includes("youtube")) {
  106.         const ytLink = createYoutubeLink(embed);
  107.         thumbnailsArray[embedCounter].appendChild(await createTitle(embed));
  108.         thumbnailsArray[embedCounter].appendChild(
  109.           createYoutubeIconDiv(ytLink, ytIcon.cloneNode())
  110.         );
  111.         embedCounter++;
  112.       }
  113.     }
  114.   };
  115.  
  116.   const observeIncomingEmbeds = () => {
  117.     const threads = document.querySelector(".thread.reqCaptcha");
  118.     const options = { childList: true };
  119.  
  120.     const callback = (mutations) => {
  121.       for (const mutation of mutations) {
  122.         if (mutation.type === "childList") {
  123.           const incomingEmbeds =
  124.             mutation.addedNodes[0].querySelectorAll("iframe");
  125.  
  126.           loopThroughEmbeds(incomingEmbeds);
  127.         }
  128.       }
  129.     };
  130.  
  131.     const observer = new MutationObserver(callback);
  132.     observer.observe(threads, options);
  133.   };
  134.  
  135.   loopThroughEmbeds(embeds);
  136.   observeIncomingEmbeds();
  137. })();
Advertisement
Add Comment
Please, Sign In to add comment