YouTube Mobile Repeated Recommendations Hider

Hides any videos that are recommended more than 2 times at the mobile homepage

目前為 2021-01-04 提交的版本,檢視 最新版本

您需要先安裝使用者腳本管理器擴展,如 TampermonkeyGreasemonkeyViolentmonkey 之後才能安裝該腳本。

You will need to install an extension such as Tampermonkey to install this script.

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyViolentmonkey 後才能安裝該腳本。

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyUserscripts 後才能安裝該腳本。

你需要先安裝一款使用者腳本管理器擴展,比如 Tampermonkey,才能安裝此腳本

您需要先安裝使用者腳本管理器擴充功能後才能安裝該腳本。

(我已經安裝了使用者腳本管理器,讓我安裝!)

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

(我已經安裝了使用者樣式管理器,讓我安裝!)

// ==UserScript==
// @name            YouTube Mobile Repeated Recommendations Hider
// @description     Hides any videos that are recommended more than 2 times at the mobile homepage
// @version         1.5.1
// @author          BLBC (github.com/hjk789, greasyfork.org/users/679182-hjk789)
// @copyright       2020+, BLBC (github.com/hjk789, greasyfork.org/users/679182-hjk789)
// @homepage        https://github.com/hjk789/Creations/tree/master/JavaScript/Userscripts/YouTube-Mobile-Repeated-Recommendations-Hider
// @license         https://github.com/hjk789/Creations/tree/master/JavaScript/Userscripts/YouTube-Mobile-Repeated-Recommendations-Hider#license
// @match           https://m.youtube.com
// @grant           GM.setValue
// @grant           GM.getValue
// @grant           GM.listValues
// @grant           GM.deleteValue
// @namespace https://greasyfork.org/users/679182
// ==/UserScript==

//**********************

const maxRepetitions = 2    // The maximum number of times that the same recommended video is allowed to appear on your
                            // homepage before starting to get hidden. Set this to 1 if you want one-time recommendations.
//**********************

let processedVideosList

GM.listValues().then(function(GmList) 
{
    processedVideosList = GmList

    const recommendationsContainer = document.querySelector(".rich-grid-renderer-contents")

    const firstVideos = recommendationsContainer.querySelectorAll("ytm-rich-item-renderer")

    for (let i=0; i < firstVideos.length; i++)
        processRecommendation(firstVideos[i])


    const loadedRecommendedVideosObserver = new MutationObserver(function(mutations) 
    {
        for (let i=0; i < mutations.length; i++)
            processRecommendation(mutations[i].addedNodes[0])
    })

    loadedRecommendedVideosObserver.observe(recommendationsContainer, {childList: true})
})

async function processRecommendation(node)
{
    if (!node) return
    
    const videoTitleEll = node.querySelector("h3")
    const videoTitleText = videoTitleEll.textContent
    const videoUrl = videoTitleEll.parentElement.href
    
    if (processedVideosList.includes("hide::"+videoUrl) || processedVideosList.includes("hide::"+videoTitleText))
        node.style.display = "none"
    else
    {
        if (maxRepetitions == 1)
        {
            GM.setValue("hide::"+videoUrl,"")
            return
        }
        else 
            var value = await GM.getValue(videoUrl)
        
        if (typeof value == "undefined")
            value = 1
        else
        {
            if (value >= maxRepetitions)
            {
                node.style.display = "none"

                GM.deleteValue(videoUrl)
                GM.setValue("hide::"+videoUrl,"")
                return
            }

            value++
        }
        
        GM.setValue(videoUrl, value)
    }
    
}