YouTube to YouTube No-Cookie Embed Player

Automatically redirect YouTube video URLs to a much privacy-friendly viewer which is the No Cookie Official YouTube Embed. Enjoy YouTube videos with no ads and trackers.

目前為 2024-08-25 提交的版本,檢視 最新版本

// ==UserScript==
// @name         YouTube to YouTube No-Cookie Embed Player
// @namespace    https://gist.github.com/thedoggybrad/4e17b0046ce072afc3f31610dcdef32a
// @version      0.0.2
// @description  Automatically redirect YouTube video URLs to a much privacy-friendly viewer which is the No Cookie Official YouTube Embed. Enjoy YouTube videos with no ads and trackers.
// @author       TheDoggyBrad Software Labs
// @match        https://www.youtube.com/*
// @grant        none
// @license      MIT--0
// ==/UserScript==

(function() {
    'use strict';

    function redirectToEmbed() {
        // Get the current URL
        let currentUrl = window.location.href;

        // Check if the URL matches the YouTube video pattern
        let match = currentUrl.match(/https:\/\/www\.youtube\.com\/watch\?v=([a-zA-Z0-9_-]+)/);

        if (match) {
            let videoId = match[1];

            let embedUrl = `https://www.youtube-nocookie.com/embed/${videoId}`;

            if (window.location.href !== embedUrl) {
                window.location.href = embedUrl;
            }
        }
    }

    redirectToEmbed();

    const observer = new MutationObserver(() => {
        redirectToEmbed();
    });

    observer.observe(document, { childList: true, subtree: true });
})();