Advertisement
Guest User

Untitled

a guest
Aug 27th, 2023
349
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.55 KB | None | 0 0
  1. // ==UserScript==
  2. // @name Kara YT Embed Fix
  3. // @namespace karachan.org
  4. // @version 0.13
  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. if (body.status === 403) return "Film prywatny";
  30.  
  31. const json = await body.json();
  32.  
  33. return json.title;
  34. };
  35.  
  36. const createThumbnail = (embed) => {
  37. const videoSuffix = embed.src.split("/").pop();
  38.  
  39. const newImage = document.createElement("img");
  40. newImage.src = `https://i.ytimg.com/vi/${videoSuffix}/hqdefault.jpg`;
  41. newImage.style.cssText = "width:250px;height:250px;object-fit:cover;";
  42.  
  43. return newImage;
  44. };
  45.  
  46. const createTitle = async (embed) => {
  47. const title = await fetchVideoTitle(embed.src.split("/").pop());
  48.  
  49. const newTitle = document.createElement("div");
  50. newTitle.style.cssText = `position: absolute;top: 10px;left: 10px;color: white;
  51. font-family: Arial, Helvetica, sans-serif;font-weight: bold;
  52. text-shadow: -1px -1px 0 #000, 1px -1px 0 #000, -1px 1px 0 #000, 1px 1px 0 #000;`;
  53. newTitle.innerHTML = title;
  54.  
  55. return newTitle;
  56. };
  57.  
  58. const createEmbedDiv = (embed) => {
  59. const newDiv = document.createElement("div");
  60. newDiv.style.cssText = "position: relative;";
  61.  
  62. newDiv.addEventListener("click", () => {
  63. newDiv.parentNode.replaceChild(createEmbed(embed), newDiv);
  64. });
  65.  
  66. return newDiv;
  67. }
  68.  
  69. const createEmbed = (embed) => {
  70. const videoSuffix = embed.src.split("/").pop();
  71. const newEmbed = document.createElement("iframe");
  72. newEmbed.width = 250;
  73. newEmbed.height = 250;
  74. newEmbed.src = `https://www.youtube.com/embed/${videoSuffix}`;
  75. newEmbed.referrerPolicy = "unsafe-url";
  76. newEmbed.setAttribute("allowfullscreen", "true");
  77. newEmbed.style.border = "none";
  78.  
  79. return newEmbed;
  80. }
  81.  
  82. const createYoutubeIcon = () => {
  83. const youtubeIcon = document.createElement("img");
  84. youtubeIcon.src = `https://www.freeiconspng.com/uploads/white-youtube-logo-png-28.png`;
  85. youtubeIcon.style.cssText = "width:80px;height:60px;";
  86.  
  87. return youtubeIcon;
  88. };
  89.  
  90. const createYoutubeIconDiv = (youtubeIcon) => {
  91. const youtubeIconDiv = document.createElement("div");
  92. youtubeIconDiv.style.cssText = "position: absolute;top: 193px;left: 5px;";
  93.  
  94. youtubeIconDiv.appendChild(youtubeIcon);
  95.  
  96. return youtubeIconDiv;
  97. };
  98.  
  99. const loopThroughEmbeds = async (embeds) => {
  100. const thumbnailsArray = [];
  101. const ytIcon = createYoutubeIcon();
  102.  
  103. for (const embed of embeds) {
  104. if (embed.src.includes("youtube")) {
  105. thumbnailsArray.push(createThumbnail(embed));
  106. }
  107. }
  108.  
  109. let embedCounter = 0;
  110.  
  111. for (const embed of embeds) {
  112. if (embed.src.includes("youtube")) {
  113. const ytDiv = createEmbedDiv(embed);
  114. ytDiv.appendChild(await createTitle(embed));
  115. ytDiv.appendChild(createYoutubeIconDiv(ytIcon.cloneNode()));
  116. ytDiv.appendChild(thumbnailsArray[embedCounter]);
  117.  
  118. embed.parentNode.replaceChild(ytDiv, embed);
  119.  
  120. embedCounter++;
  121. }
  122.  
  123. }
  124. };
  125.  
  126. const observeIncomingEmbeds = () => {
  127. const threads = document.querySelector(".thread.reqCaptcha");
  128. const options = { childList: true };
  129.  
  130. const callback = (mutations) => {
  131. for (const mutation of mutations) {
  132. if (mutation.type === "childList") {
  133. const incomingEmbeds =
  134. mutation.addedNodes[0].querySelectorAll("iframe");
  135.  
  136. loopThroughEmbeds(incomingEmbeds);
  137. }
  138. }
  139. };
  140.  
  141. const observer = new MutationObserver(callback);
  142. observer.observe(threads, options);
  143. };
  144.  
  145. loopThroughEmbeds(embeds);
  146. observeIncomingEmbeds();
  147. })();
  148.  
  149. localStorage.xD = "xD"
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement