js-tampermonkey-public-scripts/LE: Delete NoPopular Video.js

62 lines
2.1 KiB
JavaScript
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

// ==UserScript==
// @name LE: Delete NoPopular Video
// @namespace http://tampermonkey.net/
// @version 2025-01-04
// @description Удаляет непопулярные видео на YouTube.
// @author LE
// @match https://www.youtube.com/watch?v=*
// @icon https://www.google.com/s2/favicons?sz=64&domain=youtube.com
// @grant none
// @run-at document-end
// ==/UserScript==
(function() {
let lastUrl = location.href;
let tabActivatedOnce = false;
function deleteNoPopularVideos() {
document.querySelectorAll(`span.inline-metadata-item.style-scope.ytd-video-meta-block`).forEach(span => {
const text = span.textContent.trim();
if (/^\d+\sпросмот*/.test(text)) {
let parent = span;
while (parent && parent.tagName !== 'YTD-COMPACT-VIDEO-RENDERER') {
parent = parent.parentElement;
}
if (parent) {
parent.remove();
}
}
});
}
function startScript(vTime) {
setTimeout(deleteNoPopularVideos, vTime);
}
// Запускаем при загрузке страницы
startScript(5000);
startScript(10000);
startScript(20000);
// Следим за изменением URL (например, при переходе по видео без полной перезагрузки страницы)
const observer = new MutationObserver(() => {
if (location.href !== lastUrl) {
lastUrl = location.href;
startScript(5000);
}
});
observer.observe(document.body, { childList: true, subtree: true });
// Запуск скрипта, если вкладка была открыта в фоне и на неё переключились
document.addEventListener(`visibilitychange`, () => {
if (!tabActivatedOnce && document.visibilityState === `visible`) {
tabActivatedOnce = true;
setTimeout(() => {
startScript(3000);
}, 3000);
}
});
})();