YouTube Link-Player

Open YouTube hyperlinks as embedded videos.

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

// ==UserScript==
// @name         YouTube Link-Player
// @namespace    https://github.com/adeoladev/youtube-linkplayer
// @version      1.2.7
// @description  Open YouTube hyperlinks as embedded videos.
// @author       Adeola Boye
// @match        *://*/*
// @icon         https://addons.mozilla.org/user-media/addon_icons/2877/2877505-64.png
// @grant        none
// @license      MIT
// ==/UserScript==

(function() {
    'use strict';

    document.addEventListener('contextmenu', function(e) {
        var link = e.originalTarget.href;
        if(link.includes("youtube.com/watch")) {
        if(link.includes('&t=')) {
            var time = "?s=linkplayer&start="+link.substring(link.indexOf("t=") + 2);
            var seconds = time.slice(0, -1);
        } else {
            var seconds = '';
        }
        var id = link.substring(link.indexOf("=") + 1, link.indexOf("=") + 12);
        var randomNum = Math.random();
        e.originalTarget.insertAdjacentHTML('afterend', "<div id='button"+randomNum+"' style='display:inline;background-color:red;border-radius:4px;color:white;font-size:8px;text-align:center;padding-top: 1px;padding-left: 5px;padding-right: 5px;padding-bottom:2px;margin-left: 4px;height: fit-content;width: 10px;font-weight:bold;'>&#9658;</div>");
        var toggle = document.getElementById('button'+randomNum);
        toggle.addEventListener("click", function() {
            if(document.getElementById('frame'+id)){
            document.getElementById('frame'+id).remove();
            } else {
            toggle.insertAdjacentHTML('afterend','<iframe style="display: block;border-radius: 10px;margin-top: 5px;margin-bottom: 5px;" id="frame'+id+'" width="560" height="315" src="https://www.youtube.com/embed/'+id+seconds+'" frameborder="0" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share" referrerpolicy="strict-origin-when-cross-origin" allowfullscreen></iframe>');
            }
        });
    }
    });

        setTimeout(function () {
        if (!window.location.hostname.includes("youtube.com")) {
    var youtubeLinks = document.querySelectorAll('a[href*="youtube.com/watch"]');
    var miniYoutubeLinks = document.querySelectorAll('a[href*="https://youtu.be/"]');
    var youtubeShorts = document.querySelectorAll('a[href*="youtube.com/shorts"]');

    youtubeLinks.forEach(function(element){
        var children = Array.from(element.childNodes);
        let nodeTypes = children.map(el=>el.tagName);
        if(element.innerText || nodeTypes.includes(':header')) {
        var id = element.href.substring(element.href.indexOf("=") + 1, element.href.indexOf("=") + 12);
        var randomNum = Math.random();
        var button = "<div id='button"+randomNum+"' style='display:inline;background-color:red;border-radius:4px;color:white;font-size:8px;text-align:center;padding-top: 1px;padding-left: 5px;padding-right: 5px;padding-bottom:2px;margin-left: 4px;height: fit-content;width: 10px;font-weight:bold;'>&#9658;</div>";
        element.insertAdjacentHTML('afterend', button);
        var toggle = document.getElementById('button'+randomNum);
        toggle.addEventListener("click", function() {
            if(document.getElementById('frame'+id)){
            document.getElementById('frame'+id).remove();
            } else {
            toggle.insertAdjacentHTML('afterend','<iframe style="display: block;border-radius: 10px;margin-top: 5px;margin-bottom: 5px;" id="frame'+id+'" width="560" height="315" src="https://www.youtube.com/embed/'+id+'" frameborder="0" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share" referrerpolicy="strict-origin-when-cross-origin" allowfullscreen></iframe>');
            }
        });
        }
    });

    miniYoutubeLinks.forEach(function(element){
        var children = Array.from(element.childNodes);
        let nodeTypes = children.map(el=>el.tagName);
        if(element.innerText || nodeTypes.includes(':header')) {
        var id = element.href.substring(element.href.indexOf("e/") +2, 28);
        var randomNum = Math.random();
        var button = "<div id='button"+randomNum+"' style='display:inline;background-color:red;border-radius:4px;color:white;font-size:8px;text-align:center;padding-top: 1px;padding-left: 5px;padding-right: 5px;padding-bottom:2px;margin-left: 4px;height: fit-content;width: 10px;font-weight:bold;'>&#9658;</div>";
        element.insertAdjacentHTML('afterend', button);
        var toggle = document.getElementById('button'+randomNum);
        toggle.addEventListener("click", function() {
            if(document.getElementById('frame'+id)){
            document.getElementById('frame'+id).remove();
            } else {
            toggle.insertAdjacentHTML('afterend','<iframe style="display: block;border-radius: 10px;margin-top: 5px;margin-bottom: 5px;" id="frame'+id+'" width="560" height="315" src="https://www.youtube.com/embed/'+id+'" frameborder="0" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share" referrerpolicy="strict-origin-when-cross-origin" allowfullscreen></iframe>');
            }
        });
        }
    });

    youtubeShorts.forEach(function(element){
        var children = Array.from(element.childNodes);
        let nodeTypes = children.map(el=>el.tagName);
        if(element.innerText || nodeTypes.includes(':header')) {
        var id = element.href.substring(element.href.indexOf("s/") +2, element.href.indexOf("s/") + 13);
        var randomNum = Math.random();
        var button = "<div id='button"+randomNum+"' style='display:inline;background-color:red;border-radius:4px;color:white;font-size:8px;text-align:center;padding-top: 1px;padding-left: 5px;padding-right: 5px;padding-bottom:2px;margin-left: 4px;height: fit-content;width: 10px;font-weight:bold;'>&#9658;</div>";
        element.insertAdjacentHTML('afterend', button);
        var toggle = document.getElementById('button'+randomNum);
        toggle.addEventListener("click", function() {
            if(document.getElementById('frame'+id)){
            document.getElementById('frame'+id).remove();
            } else {
            toggle.insertAdjacentHTML('afterend','<iframe style="display: block;border-radius: 10px;margin-top: 5px;margin-bottom: 5px;" id="frame'+id+'" width="279" height="496" src="https://www.youtube.com/embed/'+id+'" frameborder="0" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share" referrerpolicy="strict-origin-when-cross-origin" allowfullscreen></iframe>');
            }
        });
        }
    });
        }
    }, 1000);
})();