我非常喜欢谷歌在我的头像上漂浮的生日气球,那么有什么方法可以让气球在我的头像上漂浮呢? 是否可以编写一个 Tampermonkey 脚本来完成这个需求?
let divNav = document.querySelector('div[role="navigation"]')
let btnAccount = undefined, btnArr = divNav.getElementsByTagName("a")
for (let objA of btnArr) {
if (objA.getAttribute("aria-label").includes("account")) {btnAccount = objA; break;}
}
if (btnAccount != undefined) {
let imgEle = document.createElement("img")
imgEle.className = "gb_d"
imgEle.src = "https://ssl.gstatic.com/gb/images/birthday/apd_desktop_dark_2x.gif"
imgEle.style.zIndex = 991
imgEle.style.position = "absolute"
imgEle.style.top = "-4px"
imgEle.style.bottom = "-4px"
imgEle.style.left = "-4px"
imgEle.style.right = "-4px"
btnAccount.parentNode.insertBefore(imgEle, btnAccount);
}
if (objA.getAttribute("aria-label").includes("账户"))
includes()
区分大小写,我认为您的意思是输入 Account
而不是 account
。
无需循环遍历所有
<a>
标签,直接使用属性选择器选择您需要的元素。
这是一个修订版本,应该适用于所有谷歌网站(和iframe),包括Youtube:
function main(){
const images = document.querySelectorAll('a[aria-label^="Google Account:"] > img, img[alt="Profile image"], img[alt="Avatar image"]');
if (!images.length) return;
const imgEle = document.createElement("img");
imgEle.className = "balloons"; // changed classname to avoid inherited styles
imgEle.src = 'https://ssl.gstatic.com/gb/images/birthday/apd_desktop_dark_2x.gif';
//imgEle.style.zIndex = 991 // causes weird behavior sometimes (used img.after instead)
imgEle.style.position = 'absolute';
imgEle.style.height = '100%';
imgEle.style.width = '100%';
imgEle.style.top = '0px';
imgEle.style.left = '0px';
imgEle.style.transform = 'scale(1.1)'; // not needed but looks better with
images.forEach(img => {
img.parentElement.style.position ='relative'; // fixes issue on youtube
img.after(imgEle.cloneNode(true)); // used after instead of before (takes care of z-index)
});
}
window.addEventListener('load', () => setTimeout(main, 1000)); // sometimes elements take some time to load (especially iframes)
匹配谷歌和YouTube:
// @match https://*.google.com/*
// @match https://www.youtube.com/*