// ==UserScript==
// @name Youtube 封面
// @name:en Youtube Cover
// @name:zh-CN youtube 封面
// @namespace http://tampermonkey.net/
// @version 1.0.1
// @description 獲取影片封面!
// @description:en Get the cover of youtube video!
// @description:zh-CN 获取视频封面!
// @author Anong0u0
// @include *//www.youtube.com/*
// @grant none
// @downloadURL none
// ==/UserScript==
console.log("youtube Cover is loading");
(async () => // onStart
{
while(true)
{
console.log("try load");
if(document.querySelector("#start")!=null)
{
load();
return;
}
await delay(300);
}
})();
function delay(ms = 0){return new Promise((r)=>{setTimeout(r, ms)})}
function getXY(element)
{
var x = 0, y = 0;
while (element)
{
x += element.offsetLeft - element.scrollLeft + element.clientLeft;
y += element.offsetTop - element.scrollLeft + element.clientTop;
element = element.offsetParent;
}
return {X: x, Y: y}
}
function checkImg(imgUrl)
{
return new Promise(function(resolve)
{
var img = new Image();
img.src = imgUrl;
img.onload = () => {resolve(img);}
})
}
function load()
{
var div = document.createElement("div");
div.innerHTML =`
`
document.querySelector("#start").append(div);
console.log("done");
var ytC = document.querySelector("#ytCover");
var ytLH = document.querySelector("#ytListHead");
document.querySelectorAll(".linkBtn").forEach((e)=>{e.target="_blank"});
ytC.innerText = document.querySelector("html").lang.indexOf("zh")!=-1?"封面":"Cover";;
window.onresize = () => {ytLH.style.left = (getXY(ytC).X/10-1)+"em";}
document.querySelectorAll(".list > .slide").forEach((e)=>
{
let list = e.querySelector(".list");
e.onmouseenter = () =>
{
list.style.top = (getXY(e).Y/10-1)+"em";
list.style.left = parseFloat(ytLH.style.left) + 9.7 + "em";
list.hidden = false;
};
e.onmouseleave = () =>
{
list.hidden = true;
}
})
var hide;
ytC.onmouseenter = () =>
{
//console.log("C in");
hide = false;
ytLH.hidden = false;
};
ytC.onmouseleave = async () =>
{
//console.log("C out");
hide = true;
await delay(500);
ytLH.hidden = hide;
};
ytLH.onmouseenter = () =>
{
//console.log("L in");
hide = false;
};
ytLH.onmouseleave = async () =>
{
//console.log("L out");
hide = true;
await delay(200);
ytLH.hidden = hide;
};
var oldHref = null
new MutationObserver(() => // onUrlChange
{
if (oldHref != document.location.href)
{
oldHref = document.location.href
var video_id = null;
window.location.search.replace("?","").split('&').forEach((s)=>{if(s.startsWith("v=")){video_id=s.replace("v=","")};});
ytC.hidden = (video_id == null);
if (video_id == null) return;
document.querySelectorAll(".list > .slide").forEach(async (e)=>
{
if (e.getAttribute("tag")=="skip") return;
let url = "https://i.ytimg.com/vi/" + video_id + "/" + e.getAttribute("tag") + "default.jpg";
checkImg(url).then((img)=>{e.hidden=!(img.width>120 && img.height>90);});
});
document.querySelectorAll(".linkBtn").forEach((e)=>{e.href = "https://i.ytimg.com/vi/" + video_id + "/" + e.getAttribute("imgTag") + ".jpg";});
}
}).observe(document.body, {childList: true, subtree: true});
}