YouTube to YouTube No-Cookie Embed Player

RedirectYouTube video URLs to the no-cookie embed player and stop all requests from loading

您需要先安装一个扩展,例如 篡改猴Greasemonkey暴力猴,之后才能安装此脚本。

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

您需要先安装一个扩展,例如 篡改猴暴力猴,之后才能安装此脚本。

您需要先安装一个扩展,例如 篡改猴Userscripts ,之后才能安装此脚本。

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

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

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

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

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

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

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

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

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

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

// ==UserScript==
// @name         YouTube to YouTube No-Cookie Embed Player
// @namespace    https://gist.github.com/thedoggybrad/4e17b0046ce072afc3f31610dcdef32a
// @version      0.0.3
// @description  RedirectYouTube video URLs to the no-cookie embed player and stop all requests from loading
// @author       TheDoggyBrad Software Labs
// @match        https://www.youtube.com/*
// @grant        none
// @license      MIT--0
// @run-at       document-start
// ==/UserScript==

(function() {
    'use strict';

    // Function to block all network requests
    function blockNetworkRequests() {
        // Intercept fetch API
        const originalFetch = window.fetch;
        window.fetch = function() {
            return new Promise((resolve, reject) => {
                // Reject all network requests made to YouTube domains
                if (arguments[0].includes('youtube.com') || arguments[0].includes('google.com')) {
                    reject('Blocked YouTube Request');
                } else {
                    resolve(originalFetch.apply(this, arguments));
                }
            });
        };

        // Intercept XMLHttpRequest
        const originalXHR = window.XMLHttpRequest;
        window.XMLHttpRequest = function() {
            const xhr = new originalXHR();
            const originalOpen = xhr.open;
            xhr.open = function(method, url) {
                if (url.includes('youtube.com') || url.includes('google.com')) {
                    console.log('Blocked YouTube Request');
                    xhr.abort();
                } else {
                    originalOpen.apply(this, arguments);
                }
            };
            return xhr;
        };
    }

    // Function to redirect to no-cookie embed
    function redirectToEmbed() {
        let currentUrl = window.location.href;

        // Check if the current URL is a YouTube video page
        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}`;

            // Redirect immediately without letting the original page load
            if (window.location.href !== embedUrl) {
                window.location.replace(embedUrl); // Prevent page load by redirecting
            }
        }
    }

    // Run the redirection as early as possible (before any content loads)
    redirectToEmbed();

    // Block all network requests (scripts, images, etc.) to YouTube and Google domains
    blockNetworkRequests();

})();