Elamigos - Embed YouTube Video

Embed YouTube videos on elamigos.site/data/* pages if a YouTube link is present.

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

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

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

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

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

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

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

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

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

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

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

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

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

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

// ==UserScript==
// @name         Elamigos - Embed YouTube Video
// @namespace    http://tampermonkey.net/
// @version      0.2
// @description  Embed YouTube videos on elamigos.site/data/* pages if a YouTube link is present.
// @author       Drigtime
// @match        https://elamigos.site/data/*
// @icon         https://www.google.com/s2/favicons?sz=64&domain=elamigos.site
// @grant        none
// @license      MIT
// ==/UserScript==

(function() {
    'use strict';

    // Function to extract the YouTube video ID from a YouTube link
    function extractVideoId(url) {
        const match = url.match(/(?:youtu\.be\/|youtube\.com\/(?:embed\/|v\/|watch\?v=|watch\?feature=player_embedded&v=))([^&\n?#]+)/);
        return match && match[1];
    }

    // Find all links on the page
    const allParagraphs = document.querySelectorAll('p');

    // Loop through each link
    allParagraphs.forEach(paragraph => {
        const text = paragraph.textContent;
        const videoId = extractVideoId(text);
        if (videoId) {
            // Create an iframe to embed the YouTube video
            const iframe = document.createElement('iframe');
            iframe.width = '560';
            iframe.height = '315';
            iframe.src = `https://www.youtube.com/embed/${videoId}`;
            iframe.frameborder = '0';
            iframe.allow = 'fullscreen';

            // Replace the link with the embedded video
            paragraph.parentNode.replaceChild(iframe, paragraph);
        }
    });
})();