Youtube Collapse Sidebar

collapse youtube sidebar

当前为 2023-12-13 提交的版本,查看 最新版本

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Greasemonkey 油猴子Violentmonkey 暴力猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Violentmonkey 暴力猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Userscripts ,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展后才能安装此脚本。

(我已经安装了用户脚本管理器,让我安装!)

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

(我已经安装了用户样式管理器,让我安装!)

// ==UserScript==
// @name            Youtube Collapse Sidebar
// @version         1.3.5
// @description     collapse youtube sidebar
// @namespace       https://greasyfork.org/users/821661
// @match           https://www.youtube.com/*
// @grant           GM_addStyle
// @grant           GM_setValue
// @grant           GM_getValue
// @grant           GM_registerMenuCommand
// @icon            https://cdn-icons-png.flaticon.com/512/7711/7711100.png
// @author          hdyzen
// @license         MIT
// ==/UserScript==
(function () {
	('use strict');

	let i = '!important';
	let guideP = 'ytd-app[guide-persistent-and-visible]';
	let guideM = 'ytd-app[mini-guide-visible]';
	let useJS = GM_getValue('useJS', false);

	const id = GM_registerMenuCommand(
		'Using CSS',
		() => {
			GM_setValue('useJS', !useJS);
			location.reload();
		},
		{
			title: 'Use JS',
		},
	);
	if (useJS) {
		GM_registerMenuCommand(
			'Using JS',
			() => {
				GM_setValue('useJS', !useJS);
				location.reload();
			},
			{
				title: 'Use CSS',
				id: id,
			},
		);
		const observer = new MutationObserver((mutations) => {
			mutations.forEach((mutation) => {
				if (mutation.type === 'attributes' && mutation.target.hasAttribute('guide-persistent-and-visible')) {
					console.log(mutation);
					mutation.target.removeAttribute('guide-persistent-and-visible');
					mutation.target.setAttribute('mini-guide-visible', '');
					mutation.target.querySelector('[opened]').removeAttribute('opened');
					observer.disconnect();
				}
			});
		});

		observer.observe(document.querySelector('ytd-app'), {
			childList: true,
			attributes: true,
		});
	}

	if (!useJS) {
		let style = GM_addStyle(`
        @media only screen and (min-width: 1313px) {
            /*== Persist Guide ==*/
            ${guideP} tp-yt-app-drawer {
                display: none ${i};
            }
            ${guideP} tp-yt-app-header {
                left: 72px ${i};
            }
            ${guideP} #channel-container {
                max-height: 218px ${i};
            }
            ${guideP} ytd-page-manager {
                margin-left: var(--ytd-mini-guide-width) ${i};
            }
            ${guideP} ytd-mini-guide-renderer {
                display: unset ${i};
            }
            ${guideP} ytd-playlist-header-renderer.ytd-browse {
                left: var(--ytd-mini-guide-width) ${i};
            }
            /*== Mini Guide ==*/
            ${guideM} tp-yt-app-drawer {
                display: unset ${i};
                visibility: visible ${i};
            }
            ${guideM} tp-yt-app-header {
                left: 240px ${i};
            }
            ${guideM} tp-yt-app-header + #contentContainer  {
                padding-top: 485px ${i};
            }
            ${guideM} #channel-container {
                max-height: 230px ${i};
            }
            ${guideM} ytd-page-manager {
                margin-left: var(--app-drawer-width) ${i};
            }
            ${guideM} ytd-mini-guide-renderer {
                display: none ${i};
            }
            ${guideM} #scrim.tp-yt-app-drawer {
                opacity: 1 ${i};
            }
            ${guideM} #contentContainer.tp-yt-app-drawer {
                transform: translate3d(0, 0, 0) ${i};
            }
            ${guideM} #contentContainer.tp-yt-app-drawer #header.ytd-app {
                display: none ${i};
            }
            ${guideM} ytd-playlist-header-renderer.ytd-browse {
                left: var(--app-drawer-width) ${i};
            }
        }
    `);
	}
})();