🎉 欢迎访问GreasyFork.Org 镜像站!本镜像站由公众号【爱吃馍】搭建,用于分享脚本。联系邮箱📮

Greasy fork 爱吃馍镜像

Greasy Fork is available in English.

Youtube Video hider (to only listen to audio)

A userscript that hides most of the video content on youtube, so you can listen rather than watch!

이 스크립트를 설치하려면 Tampermonkey, Greasemonkey 또는 Violentmonkey와 같은 확장 프로그램이 필요합니다.

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

이 스크립트를 설치하려면 Tampermonkey 또는 Violentmonkey와 같은 확장 프로그램이 필요합니다.

이 스크립트를 설치하려면 Tampermonkey 또는 Userscripts와 같은 확장 프로그램이 필요합니다.

이 스크립트를 설치하려면 Tampermonkey와 같은 확장 프로그램이 필요합니다.

이 스크립트를 설치하려면 유저 스크립트 관리자 확장 프로그램이 필요합니다.

(이미 유저 스크립트 관리자가 설치되어 있습니다. 설치를 진행합니다!)

🚀 安装遇到问题?关注公众号获取帮助

公众号二维码

扫码关注【爱吃馍】

回复【脚本】获取最新教程和防失联地址

이 스타일을 설치하려면 Stylus와 같은 확장 프로그램이 필요합니다.

이 스타일을 설치하려면 Stylus와 같은 확장 프로그램이 필요합니다.

이 스타일을 설치하려면 Stylus와 같은 확장 프로그램이 필요합니다.

이 스타일을 설치하려면 유저 스타일 관리자 확장 프로그램이 필요합니다.

이 스타일을 설치하려면 유저 스타일 관리자 확장 프로그램이 필요합니다.

이 스타일을 설치하려면 유저 스타일 관리자 확장 프로그램이 필요합니다.

(이미 유저 스타일 관리자가 설치되어 있습니다. 설치를 진행합니다!)

🚀 安装遇到问题?关注公众号获取帮助

公众号二维码

扫码关注【爱吃馍】

回复【脚本】获取最新教程和防失联地址

// ==UserScript==
// @name         Youtube Video hider (to only listen to audio)
// @description  A userscript that hides most of the video content on youtube, so you can listen rather than watch!
// @grant       none
// @version     2.0
// @author      DJ Panaflex
// @description 10/7/2024, 11:33:35 AM
// @include     http://*youtube.*/*watch*
// @include     https://*youtube.*/*watch*
// @include     https://*youtube.*/embed*
// @include     //*youtube.*/embed*
// @require     https://code.jquery.com/jquery-3.6.0.min.js
// @run-at      document-end
// @namespace https://greasyfork.org/users/44041
// ==/UserScript==

(function($) {
    'use strict';

    // === Configuration ===

    // Selectors for elements to hide
    const selectorsToHide = [
        'video', // All <video> elements
        '.ytp-cued-thumbnail-overlay', // YouTube's thumbnail overlay
        '.video-overlay', // Example: Another overlay class (add as needed)
        // Add more selectors here if there are other overlay elements you want to hide
    ];

    // Selector for the <h1> element where the checkbox will be appended
    const targetH1Selector = 'h1.style-scope.ytd-watch-metadata';

    // Storage key for checkbox state
    const storageKey = 'hideVideosAndOverlays';

    // === Functions ===

    /**
     * Hide all specified elements.
     */
    function hideElements() {
        selectorsToHide.forEach(selector => {
            $(selector).hide();
            console.log(`Hidden elements with selector: ${selector}`);
        });
    }

    /**
     * Show all specified elements.
     */
    function showElements() {
        selectorsToHide.forEach(selector => {
            $(selector).show();
            console.log(`Shown elements with selector: ${selector}`);
        });
    }

    /**
     * Toggle visibility based on the checkbox state.
     * @param {boolean} hide - Whether to hide or show elements.
     */
    function toggleVisibility(hide) {
        if (hide) {
            hideElements();
        } else {
            showElements();
        }
    }

    /**
     * Initialize the checkbox and its event listener.
     * @param {jQuery} $h1 - The jQuery object of the target <h1> element.
     */
    function initializeCheckbox($h1) {
        // Check if the checkbox already exists to prevent duplication
        if ($h1.find('#toggleHideVideosCheckbox').length > 0) {
            return;
        }

        // Create the checkbox element
        const $checkbox = $('<input>', {
            type: 'checkbox',
            id: 'toggleHideVideosCheckbox',
            style: 'margin-left: 10px; cursor: pointer;',
            checked: true // Checked by default
        });

        // Create the label element (omitted as per requirement)

        // Append checkbox to the <h1> element
        $h1.append($checkbox);

        // Apply initial visibility based on the stored state or default to hidden
        const storedState = localStorage.getItem(storageKey);
        const isChecked = storedState !== null ? (storedState === 'true') : true; // Default to true
        $checkbox.prop('checked', isChecked);
        toggleVisibility(isChecked);

        // Event listener for checkbox state change
        $checkbox.on('change', function() {
            const hide = $(this).is(':checked');
            toggleVisibility(hide);
            // Store the state in localStorage
            localStorage.setItem(storageKey, hide);
        });

        console.log('Checkbox to toggle video and overlay visibility has been added.');
    }

    /**
     * Observe the DOM for changes to dynamically handle new content.
     */
    function observeDOMChanges() {
        const observer = new MutationObserver((mutations) => {
            mutations.forEach((mutation) => {
                // Handle added nodes
                mutation.addedNodes.forEach((node) => {
                    const $node = $(node);
                    // If the target <h1> is added, initialize the checkbox
                    if ($node.is(targetH1Selector)) {
                        initializeCheckbox($node);
                    }
                    // If the target <h1> is nested within the added node
                    else if ($node.find(targetH1Selector).length > 0) {
                        initializeCheckbox($node.find(targetH1Selector));
                    }

                    // Optionally, hide any new elements that match the selectors
                    selectorsToHide.forEach(selector => {
                        if ($node.is(selector)) {
                            $node.hide();
                            console.log(`Hidden newly added element with selector: ${selector}`);
                        }
                        $node.find(selector).hide();
                        if ($node.find(selector).length > 0) {
                            console.log(`Hidden newly added elements matching selector: ${selector}`);
                        }
                    });
                });

                // Handle removed nodes if necessary
                // (Not required for hiding/showing elements)
            });
        });

        // Configuration for the observer
        const config = { childList: true, subtree: true };

        // Start observing the document body
        observer.observe(document.body, config);

        console.log('MutationObserver has been set up to monitor DOM changes.');
    }

    /**
     * Main initialization function.
     */
    function init() {
        // Find all existing target <h1> elements and initialize the checkbox
        $(targetH1Selector).each(function() {
            initializeCheckbox($(this));
        });

        // Set up DOM mutation observer
        observeDOMChanges();
    }

    // === Run the Script ===

    $(document).ready(function() {
        init();
    });

})(jQuery);