Greasy Fork

来自缓存

Greasy Fork is available in English.

YouTube新标签页打开器

Open YouTube videos in new tab with intelligent filtering

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

// ==UserScript==
// @name         YouTube New Tab Opener
// @name:zh-CN   YouTube新标签页打开器
// @namespace    your_namespace
// @version      1.0.2
// @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 = /(\/watch\?v=|\/shorts\/)/.test(target.href);

            // 排除条件优化
            const shouldExclude = 
                target.classList.contains('ytp-title-link') ||      // 播放器标题
                target.closest('ytd-channel-name') ||              // 频道名称
                target.closest('ytd-playlist-panel-video-renderer') // 播放列表视频
                ;

            if (isVideoLink && !shouldExclude) {
                // 处理短视频页面
                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);
})();