Greasy Fork

Greasy Fork is available in English.

YouTube新标签页打开器

智能识别YouTube视频链接并在新标签页打开

当前为 2025-01-22 提交的版本,查看 最新版本

// ==UserScript==
// @name         YouTube New Tab Opener
// @name:zh-CN   YouTube新标签页打开器
// @namespace    your_namespace
// @version      1.0
// @description  Open YouTube videos in new tab with intelligent filtering
// @description:zh-CN 智能识别YouTube视频链接并在新标签页打开
// @author       YourName
// @license      MIT
// @match        *://*.youtube.com/*
// @icon         https://www.youtube.com/favicon.ico
// @grant        none
// @supportURL   https://github.com/yourname/repo/issues
// ==/UserScript==

(function() {
    'use strict';

    // 检测是否是短视频页面
    function isShortPage() {
        return window.location.pathname === '/shorts/';
    }

    // 主处理函数
    function handleClick(event) {
        // 获取被点击的元素
        let target = event.target;

        // 向上查找最近的<a>标签
        while (target && target.tagName !== 'A') {
            target = target.parentElement;
        }

        if (target && target.href) {
            // 匹配视频链接(普通视频和短视频)
            const isVideoLink = target.href.includes('/watch?v=') || target.href.includes('/shorts/');

            // 排除不需要的链接
            const isIgnoredLink = target.classList.contains('ytp-title-link') || // 排除播放器标题
                                 target.closest('#owner') ||                     // 排除频道链接
                                 target.closest('#meta');                        // 排除视频元数据

            if (isVideoLink && !isIgnoredLink) {
                // 如果是短视频页面直接处理
                if (isShortPage()) {
                    window.open(target.href, '_blank');
                    window.location.replace("about:blank");
                    event.preventDefault();
                    event.stopPropagation();
                    return;
                }

                // 阻止默认行为
                event.preventDefault();
                event.stopPropagation();

                // 在新标签页打开
                window.open(target.href, '_blank');
            }
        }
    }

    // 使用捕获阶段来确保执行
    document.addEventListener('click', handleClick, true);
})();