youtube新标签页打开🙌😁😁😁

支持首页,搜索,频道,个人主页等在新标签打开,你懂的

目前为 2023-05-16 提交的版本。查看 最新版本

// ==UserScript==
// @name         youtube新标签页打开🙌😁😁😁
// @namespace    neo_world_js
// @version      1.3.1
// @description  支持首页,搜索,频道,个人主页等在新标签打开,你懂的
// @author       neoWorld
// @match        https://www.youtube.com/*
// @icon         https://www.google.com/s2/favicons?domain=youtube.com
// @grant        none
// @license      MIT
// ==/UserScript==

(function () {
    "use strict";


    const updateAnchorElements = function (selector) {
        const anchorElements = document.querySelectorAll(selector);

        for (const anchorElement of anchorElements) {
            if (!anchorElement.hasAttribute("target")) {
                anchorElement.setAttribute("target", "_blank");
            }

            anchorElement.addEventListener("click", (e) => {
                e.stopPropagation();
                if (!anchorElement.hasAttribute("opened")) {
                    anchorElement.setAttribute("opened", "true");
                }
                return false;
            });
        }
    };

    const getSelectorAtCurPath = function () {
        const path = window.location.pathname;

        return globalVars.selectorPathMap[path] || {
            observeEle: "#page-manager",
            aEle: ['#dismissible a[href^="/"]'],
        };
    };

    const initObserver = function (selector) {
        const observeEle = document.querySelector(selector.observeEle);
        if (!observeEle) return;

        new MutationObserver(() => {
            updateAnchorElements(selector.aEle);
        }).observe(observeEle, {
            childList: true,
            subtree: true, // 监视子孙节点
        });
    };

    const watchPathChange = function (cb = () => { }) {
        window.addEventListener("popstate", cb);

    };


    // init
    const globalVars = {
        selectorPathMap: {
            "/": {
                observeEle: "#page-manager",
                aEle: ['#dismissible a[href^="/"]'],
            },
            "/results": {
                observeEle: "#page-manager",
                aEle: ['#dismissible a[href^="/"]'],
            },
            "/channel": {
                observeEle: "#page-manager",
                aEle: ['#dismissible a[href^="/"]', 'ytd-grid-playlist-renderer a[href^="/"]', '#channel a[href^="/"]'],
            },
            "/watch": {
                observeEle: "#page-manager",
                aEle: ['#dismissible a[href^="/"]', 'ytd-video-owner-renderer a[href^="/"]'],
            },
            "/playlist": {
                observeEle: "#page-manager",
                aEle: ['#content a[href^="/"]'],
            },
            "/user": {
                observeEle: "#page-manager",
                aEle: ['#dismissible a[href^="/"]'],
            },
            "/c": {
                observeEle: "#page-manager",
                aEle: ['ytd-grid-playlist-renderer a[href^="/"]', '#dismissible a[href^="/"]'],
            },
            "/feed": {
                observeEle: "#page-manager",
                aEle: ['#dismissible a[href^="/"]'],
            },
        },
        initPath: window.location.pathname,
        intervalDefaultTimes: 10,
        intervalTimes: 10,
    };
    const selector = getSelectorAtCurPath();
    window.onload = () => {
        initObserver(selector);
        updateAnchorElements(selector.aEle);
    };

    watchPathChange(() => {
        initObserver(selector);
        updateAnchorElements(selector.aEle);
    });
})();

QingJ © 2025

镜像随时可能失效,请加Q群300939539或关注我们的公众号极客氢云获取最新地址