Open Reddit YouTube Player in New Tab

Opens embedded YouTube videos from Reddit in a new tab

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Greasemonkey 油猴子Violentmonkey 暴力猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Violentmonkey 暴力猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Userscripts ,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展后才能安装此脚本。

(我已经安装了用户脚本管理器,让我安装!)

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

(我已经安装了用户样式管理器,让我安装!)

// ==UserScript==
// @name         Open Reddit YouTube Player in New Tab
// @namespace    http://tampermonkey.net/
// @version      0.6
// @description  Opens embedded YouTube videos from Reddit in a new tab
// @author       Henry Suen
// @match        https://*.reddit.com/*
// @grant        none
// @license MIT 
// ==/UserScript==

(function() {
    'use strict';

    // Debug mode - press 'd' to toggle
    let debugMode = false;

    function debugLog(step, message, data = '') {
        if (debugMode) {
            console.log(`[Debug] Step ${step}: ${message}`, data);
        }
    }

    function extractVideoId(url) {
        const embedMatch = url.match(/\/embed\/([^/?]+)/);
        if (embedMatch) return embedMatch[1];

        const watchMatch = url.match(/[?&]v=([^&]+)/);
        if (watchMatch) return watchMatch[1];

        return null;
    }

    function handleClick(event) {
        if (debugMode) {
            console.log('Clicked element:', event.target);
            console.log('Clicked element classes:', event.target.className);
        }

        // Check if we clicked on a shreddit-embed element
        const embedElement = event.target.closest('shreddit-embed');
        debugLog(1, 'Looking for shreddit-embed element', embedElement);

        if (embedElement) {
            // Get the HTML attribute directly from the shreddit-embed element
            const htmlAttr = embedElement.getAttribute('html');
            debugLog(2, 'Found HTML attribute', htmlAttr);

            if (htmlAttr && htmlAttr.includes('youtube.com/embed/')) {
                event.preventDefault();
                event.stopPropagation();

                // Extract the src URL from the HTML attribute
                const srcMatch = htmlAttr.match(/src="([^"]+)"/);
                debugLog(3, 'Extracted src from HTML', srcMatch ? srcMatch[1] : null);

                if (srcMatch) {
                    const videoId = extractVideoId(srcMatch[1]);
                    debugLog(4, 'Extracted video ID', videoId);

                    if (videoId) {
                        const youtubeUrl = `https://www.youtube.com/watch?v=${videoId}`;
                        debugLog(5, 'Opening YouTube URL', youtubeUrl);
                        window.open(youtubeUrl, '_blank');
                    }
                }
            }
        }
    }

    // Add click event listener to the document
    document.addEventListener('click', handleClick, true);

    // Add debug mode toggle
    document.addEventListener('keypress', (e) => {
        if (e.key === 'd') {
            debugMode = !debugMode;
            console.log(`Debug mode ${debugMode ? 'enabled' : 'disabled'}`);
        }
    });

    // Initial debug status message
    console.log('Reddit YouTube Redirect script loaded. Press "d" to toggle debug mode');
})();