Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- (() => {
- const box = document.createElement("div");
- box.style.cssText = `
- position: fixed;
- display: none;
- width: 340px;
- background: #111;
- color: #fff;
- z-index: 99999;
- font-family: sans-serif;
- font-size: 14px;
- overflow: hidden;
- pointer-events: none;
- `;
- document.body.appendChild(box);
- const img = document.createElement("img");
- img.style.width = "100%";
- box.appendChild(img);
- const title = document.createElement("div");
- title.textContent = "Загрузка…";
- box.appendChild(title);
- const cache = new Map();
- let currentId = null;
- function getIdFromHref(href) {
- try {
- const u = new URL(href);
- if (u.hostname.includes("youtu.be")) return u.pathname.slice(1);
- if (u.searchParams.has("v")) return u.searchParams.get("v");
- } catch {}
- return null;
- }
- document.addEventListener("mousemove", e => {
- if (!currentId) return;
- box.style.left = e.clientX + 20 + "px";
- box.style.top = e.clientY + 20 + "px";
- });
- document.addEventListener("mouseover", e => {
- const link = e.target.closest("a[href]");
- if (!link) return;
- const id = getIdFromHref(link.href);
- if (!id || id === currentId) return;
- currentId = id;
- box.style.display = "block";
- img.src = `https://img.youtube.com/vi/${id}/hqdefault.jpg`;
- if (cache.has(id)) {
- title.textContent = cache.get(id);
- return;
- }
- title.textContent = "Загрузка…";
- fetch(`https://www.youtube.com/oembed?url=https://www.youtube.com/watch?v=${id}&format=json`)
- .then(r => r.ok ? r.json() : null)
- .then(d => {
- if (!d || currentId !== id) return;
- cache.set(id, d.title);
- title.textContent = d.title;
- })
- .catch(() => {
- title.textContent = "";
- });
- });
- document.addEventListener("mouseout", e => {
- if (!e.relatedTarget || !e.relatedTarget.closest?.("a")) {
- currentId = null;
- box.style.display = "none";
- }
- });
- })();
Advertisement
Add Comment
Please, Sign In to add comment