YouTube to YouTube No-Cookie Embed Player

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

您需要先安裝使用者腳本管理器擴展,如 TampermonkeyGreasemonkeyViolentmonkey 之後才能安裝該腳本。

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

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyViolentmonkey 後才能安裝該腳本。

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyUserscripts 後才能安裝該腳本。

你需要先安裝一款使用者腳本管理器擴展,比如 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();

})();