YouTube "Latest" Filter Toggle

Adds a toggle button next to YouTube search filters to switch "Latest" results ON/OFF

ही स्क्रिप्ट इंस्टॉल करण्यासाठी तुम्हाला Tampermonkey, Greasemonkey किंवा Violentmonkey यासारखे एक्स्टेंशन इंस्टॉल करावे लागेल.

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

ही स्क्रिप्ट इंस्टॉल करण्यासाठी तुम्हाला Tampermonkey किंवा Violentmonkey यासारखे एक्स्टेंशन इंस्टॉल करावे लागेल..

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

ही स्क्रिप्ट इंस्टॉल करण्यासाठी तुम्हाला Tampermonkey यासारखे एक्स्टेंशन इंस्टॉल करावे लागेल..

ही स्क्रिप्ट इंस्टॉल करण्यासाठी तुम्हाला एक युझर स्क्रिप्ट व्यवस्थापक एक्स्टेंशन इंस्टॉल करावे लागेल.

(माझ्याकडे आधीच युझर स्क्रिप्ट व्यवस्थापक आहे, मला इंस्टॉल करू द्या!)

ही स्टाईल इंस्टॉल करण्यासाठी तुम्हाला Stylus सारखे एक्स्टेंशन इंस्टॉल करावे लागेल.

ही स्टाईल इंस्टॉल करण्यासाठी तुम्हाला Stylus सारखे एक्स्टेंशन इंस्टॉल करावे लागेल.

ही स्टाईल इंस्टॉल करण्यासाठी तुम्हाला Stylus सारखे एक्स्टेंशन इंस्टॉल करावे लागेल.

ही स्टाईल इंस्टॉल करण्यासाठी तुम्हाला एक युझर स्टाईल व्यवस्थापक इंस्टॉल करावे लागेल.

ही स्टाईल इंस्टॉल करण्यासाठी तुम्हाला एक युझर स्टाईल व्यवस्थापक इंस्टॉल करावे लागेल.

ही स्टाईल इंस्टॉल करण्यासाठी तुम्हाला एक युझर स्टाईल व्यवस्थापक इंस्टॉल करावे लागेल.

(माझ्याकडे आधीच युझर स्टाईल व्यवस्थापक आहे, मला इंस्टॉल करू द्या!)

// ==UserScript==
// @name         YouTube "Latest" Filter Toggle
// @namespace    https://tampermonkey.net/
// @version      1.2
// @description  Adds a toggle button next to YouTube search filters to switch "Latest" results ON/OFF
// @match        https://www.youtube.com/*
// @grant        none
// @license MIT
// ==/UserScript==

(function () {
    'use strict';

    const FILTER_PARAM = "sp=CAI%253D";
    const STORAGE_KEY = "yt-latest-filter-enabled";

    function isEnabled() {
        return localStorage.getItem(STORAGE_KEY) !== "off";
    }

    function setEnabled(state) {
        localStorage.setItem(STORAGE_KEY, state ? "on" : "off");
    }

    function applyFilterIfNeeded() {
        if (!isEnabled()) return;

        const url = new URL(location.href);

        if (url.pathname !== "/results") return;
        if (url.searchParams.has("sp")) return;

        url.search += (url.search ? "&" : "?") + FILTER_PARAM;
        location.replace(url.toString());
    }

    function createButton() {
        if (document.getElementById("yt-latest-toggle")) return;

        const button = document.createElement("button");
        button.id = "yt-latest-toggle";

        const updateText = () => {
            button.textContent = isEnabled() ? "Latest: ON" : "Latest: OFF";
            button.style.opacity = isEnabled() ? "1" : "0.5";
        };

        button.style.cssText = `
            margin-right: 12px;
            padding: 6px 10px;
            border-radius: 18px;
            border: 1px solid #3ea6ff;
            background: transparent;
            color: #3ea6ff;
            font-size: 13px;
            cursor: pointer;
            white-space: nowrap;
        `;

        button.onclick = () => {
            setEnabled(!isEnabled());
            updateText();
            applyFilterIfNeeded();
        };

        updateText();
        return button;
    }

    function insertButton() {
        const target =
            document.querySelector("ytd-search-sub-menu-renderer #container") ||
            document.querySelector("ytd-search-sub-menu-renderer");

        if (!target) return;

        if (document.getElementById("yt-latest-toggle")) return;

        const btn = createButton();
        if (btn) target.prepend(btn);
    }

    // obserwacja zmian (YouTube SPA)
    const observer = new MutationObserver(() => {
        insertButton();
        applyFilterIfNeeded();
    });

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

    // start
    setTimeout(() => {
        insertButton();
        applyFilterIfNeeded();
    }, 1000);

})();