Guest User

Untitled

a guest
Jan 1st, 2026
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.04 KB | None | 0 0
  1. (() => {
  2. const box = document.createElement("div");
  3. box.style.cssText = `
  4. position: fixed;
  5. display: none;
  6. width: 340px;
  7. background: #111;
  8. color: #fff;
  9. z-index: 99999;
  10. font-family: sans-serif;
  11. font-size: 14px;
  12. overflow: hidden;
  13. pointer-events: none;
  14. `;
  15. document.body.appendChild(box);
  16.  
  17. const img = document.createElement("img");
  18. img.style.width = "100%";
  19. box.appendChild(img);
  20.  
  21. const title = document.createElement("div");
  22. title.textContent = "Загрузка…";
  23. box.appendChild(title);
  24.  
  25. const cache = new Map();
  26. let currentId = null;
  27.  
  28. function getIdFromHref(href) {
  29. try {
  30. const u = new URL(href);
  31. if (u.hostname.includes("youtu.be")) return u.pathname.slice(1);
  32. if (u.searchParams.has("v")) return u.searchParams.get("v");
  33. } catch {}
  34. return null;
  35. }
  36.  
  37. document.addEventListener("mousemove", e => {
  38. if (!currentId) return;
  39. box.style.left = e.clientX + 20 + "px";
  40. box.style.top = e.clientY + 20 + "px";
  41. });
  42.  
  43. document.addEventListener("mouseover", e => {
  44. const link = e.target.closest("a[href]");
  45. if (!link) return;
  46.  
  47. const id = getIdFromHref(link.href);
  48. if (!id || id === currentId) return;
  49.  
  50. currentId = id;
  51. box.style.display = "block";
  52. img.src = `https://img.youtube.com/vi/${id}/hqdefault.jpg`;
  53.  
  54. if (cache.has(id)) {
  55. title.textContent = cache.get(id);
  56. return;
  57. }
  58.  
  59. title.textContent = "Загрузка…";
  60.  
  61. fetch(`https://www.youtube.com/oembed?url=https://www.youtube.com/watch?v=${id}&format=json`)
  62. .then(r => r.ok ? r.json() : null)
  63. .then(d => {
  64. if (!d || currentId !== id) return;
  65. cache.set(id, d.title);
  66. title.textContent = d.title;
  67. })
  68. .catch(() => {
  69. title.textContent = "";
  70. });
  71. });
  72.  
  73. document.addEventListener("mouseout", e => {
  74. if (!e.relatedTarget || !e.relatedTarget.closest?.("a")) {
  75. currentId = null;
  76. box.style.display = "none";
  77. }
  78. });
  79. })();
  80.  
Advertisement
Add Comment
Please, Sign In to add comment