YouTube /embed/ forwarder

Forwards YouTube links to the youtube.com/embed/* page, so there's just the video in your window and nothing else.

2023-08-14 या दिनांकाला. सर्वात नवीन आवृत्ती पाहा.

ही स्क्रिप्ट इंस्टॉल करण्यासाठी तुम्हाला Tampermonkey, Greasemonkey किंवा Violentmonkey यासारखे एक्स्टेंशन इंस्टॉल करावे लागेल.

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

ही स्क्रिप्ट इंस्टॉल करण्यासाठी तुम्हाला Tampermonkey किंवा Violentmonkey यासारखे एक्स्टेंशन इंस्टॉल करावे लागेल..

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

ही स्क्रिप्ट इंस्टॉल करण्यासाठी तुम्हाला Tampermonkey यासारखे एक्स्टेंशन इंस्टॉल करावे लागेल..

ही स्क्रिप्ट इंस्टॉल करण्यासाठी तुम्हाला एक युझर स्क्रिप्ट व्यवस्थापक एक्स्टेंशन इंस्टॉल करावे लागेल.

(माझ्याकडे आधीच युझर स्क्रिप्ट व्यवस्थापक आहे, मला इंस्टॉल करू द्या!)

ही स्टाईल इंस्टॉल करण्यासाठी तुम्हाला Stylus सारखे एक्स्टेंशन इंस्टॉल करावे लागेल.

ही स्टाईल इंस्टॉल करण्यासाठी तुम्हाला Stylus सारखे एक्स्टेंशन इंस्टॉल करावे लागेल.

ही स्टाईल इंस्टॉल करण्यासाठी तुम्हाला Stylus सारखे एक्स्टेंशन इंस्टॉल करावे लागेल.

ही स्टाईल इंस्टॉल करण्यासाठी तुम्हाला एक युझर स्टाईल व्यवस्थापक इंस्टॉल करावे लागेल.

ही स्टाईल इंस्टॉल करण्यासाठी तुम्हाला एक युझर स्टाईल व्यवस्थापक इंस्टॉल करावे लागेल.

ही स्टाईल इंस्टॉल करण्यासाठी तुम्हाला एक युझर स्टाईल व्यवस्थापक इंस्टॉल करावे लागेल.

(माझ्याकडे आधीच युझर स्टाईल व्यवस्थापक आहे, मला इंस्टॉल करू द्या!)

// ==UserScript==
// @name        YouTube /embed/ forwarder
// @description Forwards YouTube links to the youtube.com/embed/* page, so there's just the video in your window and nothing else.
// @namespace   https://greasyfork.org/en/users/1148791-vuccala
// @author      Vuccala
// @icon        https://archive.org/download/yt_icon/yt.png
// @match       *://*.youtube.com/*
// @match       *://*.youtu.be/*
// @run-at      document-start
// @version     0.3
// @grant       none
// @license     MIT

// ==/UserScript==

(function() {
    var embedBaseUrl = 'https://youtube.com/embed/';

    function getId(u) {
        var idMatch = /(?:[?&]v=|\/(?:embed\/|v\/|shorts\/))([^&?/]+)/.exec(u);
        return idMatch ? idMatch[1] : '';
    }

    function updateLinks(links) {
        links.forEach(function(link) {
            var linkHref = link.getAttribute('href');
            var videoIdMatch = /(?:[?&]v=|\/(?:embed\/|v\/|shorts\/))([^&?/]+)/.exec(linkHref);

            if (videoIdMatch) {
                var videoId = videoIdMatch[1];
                var embedUrl = embedBaseUrl + videoId;
                link.href = embedUrl;
            }
        });
    }

    function observeDOM() {
        var targetNode = document.body;

        var observer = new MutationObserver(function(mutationsList) {
            for (var mutation of mutationsList) {
                if (mutation.type === 'childList') {
                    var newLinks = mutation.addedNodes[0].querySelectorAll('a[href*="/watch?v="], a[href*="/shorts/"]');
                    updateLinks(newLinks);
                }
            }
        });

        var observerConfig = { childList: true, subtree: true };
        observer.observe(targetNode, observerConfig);
    }

    var url = window.location.href;

    if (url.includes('/watch?v=') || url.includes('/shorts/')) {
        var videoId = getId(url);
        var newURL = embedBaseUrl + videoId;

        // Change the location to the new URL
        if (newURL !== url) {
            window.location.href = newURL;
        }
    } else {
        observeDOM();
    }
})();