YouTube用低視聴回数動画の非表示

YouTubeでおすすめに表示される再生回数の少ない動画を非表示にします。コードから再生回数のしきい値を変更できます。デフォルトでは1000再生数以下の動画を非表示に設定しています。登録チャンネル内の動画は無視するオプションを追加しました。

当前为 2025-04-22 提交的版本,查看 最新版本

// ==UserScript==
// @name         YouTube用低視聴回数動画の非表示
// @namespace    http://tampermonkey.net/
// @description  YouTubeでおすすめに表示される再生回数の少ない動画を非表示にします。コードから再生回数のしきい値を変更できます。デフォルトでは1000再生数以下の動画を非表示に設定しています。登録チャンネル内の動画は無視するオプションを追加しました。
// @author       sun
// @match        *://*.youtube.com/*
// @icon         https://www.google.com/s2/favicons?sz=64&domain=youtube.com
// @grant        GM_registerMenuCommand
// @version      1.30
// ==/UserScript==

const VIEW_THRESHOLD = 1000;
const DISABLE_ON_SUBSCRIPTIONS = true;
const OPTION_STORAGE_KEY = 'YT_LowViewFilter_Settings';
let menuRegistered = false;

function parseViewCount(text) {
    let numText = text.replace(/[^0-9\.KM万億]/g, "");
    const multipliers = { "K": 1000, "M": 1000000, "万": 10000, "億": 100000000 };

    let multiplier = Object.keys(multipliers).find(unit => numText.includes(unit)) || "";
    numText = numText.replace(multiplier, "");

    return numText ? parseFloat(numText) * (multipliers[multiplier] || 1) : 0;
}

function IsBadVideo(videoViews) {
    if (!videoViews) return false;

    let text = videoViews.innerText;
    let viewCount = parseViewCount(text);
    return viewCount > 0 && viewCount < VIEW_THRESHOLD;
}

function IsSubscriptionsPage() {
    return location.pathname.includes('/@') && location.pathname.includes('/videos');
}

function HideBadVideo(videoElement) {
    if (!videoElement) return;

    const settings = JSON.parse(localStorage.getItem(OPTION_STORAGE_KEY)) || {};
    const disableOnSubs = settings.disableOnSubs ?? DISABLE_ON_SUBSCRIPTIONS;

    if (disableOnSubs && IsSubscriptionsPage()) return;

    let videoViews = videoElement.querySelector(".inline-metadata-item.style-scope.ytd-video-meta-block");
    if (IsBadVideo(videoViews)) {
        videoElement.style.display = "none";
    }
}

const observer = new IntersectionObserver((entries) => {
    entries.forEach(entry => {
        if (entry.isIntersecting) {
            HideBadVideo(entry.target);
            observer.unobserve(entry.target);
        }
    });
}, { rootMargin: "300px" });

function Update() {
    let videos = document.querySelectorAll("ytd-rich-item-renderer, ytd-compact-video-renderer");
    videos.forEach(video => {
        HideBadVideo(video);
        observer.observe(video);
    });
}


function InitTampermonkeyMenu() {
    if (menuRegistered) return;
    menuRegistered = true;

    const settings = JSON.parse(localStorage.getItem(OPTION_STORAGE_KEY)) || {};

    GM_registerMenuCommand(
        `登録チャンネル内の動画は無視する: ${settings.disableOnSubs ? "ON" : "OFF"}`,
        () => {
            const newValue = !settings.disableOnSubs;
            localStorage.setItem(OPTION_STORAGE_KEY, JSON.stringify({
                disableOnSubs: newValue
            }));
            window.location.reload();
        }
    );
}


window.addEventListener("load", () => {
    InitTampermonkeyMenu();
    Update();

    const checkNavigation = () => {
        let ytApp = document.querySelector("ytd-app");
        if (ytApp) {
            ytApp.addEventListener("yt-navigate-finish", () => {
                setTimeout(Update, 500);
            });
        }
    };
    checkNavigation();

    const mutationObserver = new MutationObserver((mutations) => {
        mutations.forEach((mutation) => {
            mutation.addedNodes.forEach((node) => {
                if (node.nodeType === 1) {
                    if (node.matches("ytd-rich-item-renderer, ytd-compact-video-renderer")) {
                        observer.observe(node);
                    } else {
                        let newVideos = node.querySelectorAll?.("ytd-rich-item-renderer, ytd-compact-video-renderer");
                        newVideos.forEach(video => observer.observe(video));
                    }
                }
            });
        });
    });

    mutationObserver.observe(document.body, { childList: true, subtree: true });
});

QingJ © 2025

镜像随时可能失效,请加Q群300939539或关注我们的公众号极客氢云获取最新地址