[Youtube] Compact sidebar with more buttons

Add more buttons in compact sidebar/mini guide

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

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

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

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

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

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

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

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

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

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

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

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

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

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

// ==UserScript==
// @name            [Youtube] Compact sidebar with more buttons
// @namespace       https://greasyfork.org/users/821661
// @version         1.0.2
// @description     Add more buttons in compact sidebar/mini guide
// @author          hdyzen
// @match           https://www.youtube.com/*
// @icon            https://www.google.com/s2/favicons?domain=www.youtube.com/&sz=64
// @grant           GM_registerMenuCommand
// @grant           GM_setValue
// @grant           GM_getValue
// @grant           GM_addStyle
// @license         GPL-3.0-only
// ==/UserScript==

const COMMADS = {
    home: {
        label: "Home",
        state: true,
    },
    shorts: {
        label: "Shorts",
        state: true,
    },
    subscriptions: {
        label: "Subscriptions",
        state: true,
    },
    music: {
        label: "Youtube Music",
        state: true,
    },
    you: {
        label: "You",
        state: true,
    },
    history: {
        label: "History",
        state: true,
    },
    playlists: {
        label: "Playlists",
        state: true,
    },
    yourVideos: {
        label: "Videos",
        state: true,
    },
    yourCourses: {
        label: "Courses",
        state: true,
    },
    watchLater: {
        label: "Later",
        state: true,
    },
    liked: {
        label: "Liked",
        state: true,
    },
    download: {
        label: "Download",
        state: true,
    },
    yourClips: {
        label: "Clips",
        state: true,
    },
};

function renderMenuCommands() {
    for (const key in COMMADS) {
        const state = GM_getValue(key, COMMADS[key].state);
        const label = COMMADS[key].label;

        document.body.classList.toggle(key, !state);

        GM_registerMenuCommand(`${state ? "✅" : "❌"} ${label}`, () => toggleState(key, state), { id: key, autoClose: false });
    }
}
renderMenuCommands();

function toggleState(key, state) {
    COMMADS[key].state = !state;
    GM_setValue(key, COMMADS[key].state);
    renderMenuCommands();
}

const originalParse = JSON.parse;
JSON.parse = (text) => {
    const result = originalParse(text);
    const items = result?.items?.[0]?.guideSectionRenderer?.items?.[4]?.guideCollapsibleSectionEntryRenderer?.sectionItems;

    if (!items) return result;

    for (const item of items) {
        if (item.guideEntryRenderer) item.guideEntryRenderer.isPrimary = true;
        if (item.guideDownloadsEntryRenderer) item.guideDownloadsEntryRenderer.alwaysShow = true;
    }

    return result;
};

GM_addStyle(`
ytd-mini-guide-renderer.ytd-app {
    overflow: auto;
} 
.home ytd-mini-guide-entry-renderer:has(> a[href="/"]),
.shorts ytd-mini-guide-entry-renderer:has(> a[title="Shorts"]),
.subscriptions ytd-mini-guide-entry-renderer:has(> a[href="/feed/subscriptions"]),
.music ytd-mini-guide-entry-renderer:has(> a[href="https://music.youtube.com/"]),
.you ytd-mini-guide-entry-renderer:has(> a[href="/feed/you"]),
.history ytd-mini-guide-entry-renderer:has(> a[href="/feed/history"]),
.playlists ytd-mini-guide-entry-renderer:has(> a[href="/feed/playlists"]),
.yourVideos ytd-mini-guide-entry-renderer:has(> a[href^="https://studio.youtube.com/"]),   
.yourCourses ytd-mini-guide-entry-renderer:has(> a[href="/feed/courses"]),   
.watchLater ytd-mini-guide-entry-renderer:has(> a[href="/playlist?list=WL"]),
.liked ytd-mini-guide-entry-renderer:has(> a[href="/playlist?list=LL"]),
.download ytd-mini-guide-entry-renderer:has(> a[href="/feed/downloads"]),
.yourClips ytd-mini-guide-entry-renderer:has(> a[href="/feed/clips"]) {
    display: none !important;
}
`);