YouTube Premium Auto Authuser Switch (With Search Page Support)

Automatically switch to specified Google account (authuser) on YouTube homepage and search, silently and without reload.

// ==UserScript==
// @name         YouTube Premium 自動指定多帳號(支援搜尋)
// @name:en      YouTube Premium Auto Authuser Switch (With Search Page Support)
// @namespace    https://twitch.tv/Ladell
// @version      3.5
// @description  自動切換至指定的 Google 帳號(authuser),在 YouTube 首頁與搜尋頁靜默處理,不重整、不循環。
// @description:zh-TW 自動切換至指定的 Google 帳號(authuser),在 YouTube 首頁與搜尋頁靜默處理,不重整、不循環。
// @description:en Automatically switch to specified Google account (authuser) on YouTube homepage and search, silently and without reload.
// @author       ChatGPT + Twitch.tv/Ladell
// @match        *://www.youtube.com/*
// @icon         https://www.youtube.com/s/desktop/6e55fb3d/img/favicon.ico
// @grant        none
// @license      MIT
// ==/UserScript==

(function () {
    'use strict';

    const desiredUser = "1"; // 想要切換到的 Google 帳號(authuser=1)
    const redirectFlag = "authuser_redirected"; // 防止重複轉向

    // 判斷是否需要處理此頁面:首頁或搜尋頁
    function shouldHandlePage() {
        const url = new URL(window.location.href);
        const pathname = url.pathname;
        return pathname === "/" || (pathname === "/results" && url.searchParams.has("search_query"));
    }

    function maybeSwitchAccount() {
        const url = new URL(window.location.href);
        const currentUser = url.searchParams.get("authuser");
        const alreadyRedirected = url.searchParams.get(redirectFlag);

        if (shouldHandlePage() && currentUser !== desiredUser && !alreadyRedirected) {
            console.log("[YouTube Auto Authuser] 靜默切換到 authuser=" + desiredUser);

            url.searchParams.set("authuser", desiredUser);
            url.searchParams.set(redirectFlag, "1");
            window.history.replaceState({}, '', url.toString());
        } else if (alreadyRedirected) {
            url.searchParams.delete(redirectFlag);
            window.history.replaceState({}, '', url.toString());
        }
    }

    maybeSwitchAccount();

    // 偵測 YouTube 為 SPA,使用 MutationObserver 監聽網址變化
    let lastHref = location.href;
    new MutationObserver(() => {
        if (location.href !== lastHref) {
            lastHref = location.href;
            maybeSwitchAccount();
        }
    }).observe(document, {subtree: true, childList: true});
})();