YouTube Force Link Open in New Tab (Updated)

Opens YouTube links in new tabs without simulating Ctrl+Click, keeping the original page unchanged.

// ==UserScript==
// @name         YouTube Force Link Open in New Tab (Updated)
// @name:zh-CN   YouTube强制新标签页打开链接(更新版)
// @namespace    http://tampermonkey.net/
// @version      1.2
// @description  Opens YouTube links in new tabs without simulating Ctrl+Click, keeping the original page unchanged.
// @description:zh-CN  让YouTube链接在新标签页中打开,不使用模拟Ctrl+点击,同时保持原页面不变。
// @author       YourName
// @match        *://*.youtube.com/*
// @grant        none
// @license      MIT
// @run-at       document-start
// @icon         https://img.icons8.com/?size=64&id=121211&format=png
// ==/UserScript==

(function() {
    'use strict';

    const YOUTUBE_PATTERNS = ['/watch', '/channel', '/user', '/playlist', '/shorts'];

    function isYouTubeLink(url) {
        return url.includes('youtube.com') || url.startsWith('/') || url.startsWith('https://youtu.be/');
    }

    function shouldOpenInNewTab(url) {
        return YOUTUBE_PATTERNS.some(pattern => url.includes(pattern));
    }

    function handleClick(event) {
        if (event.ctrlKey || event.metaKey) return;

        const anchor = event.target.closest('a');
        if (!anchor || !anchor.href) return;

        if (isYouTubeLink(anchor.href) && shouldOpenInNewTab(anchor.href)) {
            event.preventDefault();
            event.stopPropagation();
            window.open(anchor.href, '_blank');
        }
    }

    document.addEventListener('click', handleClick, true);
})();