Greasy Fork 还支持 简体中文。

Open Reddit YouTube Player in New Tab

Opens embedded YouTube videos from Reddit in a new tab

您需要先安裝使用者腳本管理器擴展,如 TampermonkeyGreasemonkeyViolentmonkey 之後才能安裝該腳本。

You will need to install an extension such as Tampermonkey to install this script.

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyViolentmonkey 後才能安裝該腳本。

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyUserscripts 後才能安裝該腳本。

你需要先安裝一款使用者腳本管理器擴展,比如 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');
})();