Resolve YouTube Network Error

To Fix issues like https://www.reddit.com/r/youtube/comments/14n9nql/

You will need to install an extension such as Tampermonkey, Greasemonkey or Violentmonkey to install this script.

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

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

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

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

You will need to install a user script manager extension to install this script.

(I already have a user script manager, let me install it!)

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

(I already have a user style manager, let me install it!)

// ==UserScript==
// @name                Resolve YouTube Network Error
// @namespace           UserScript
// @version             1.0.1
// @license             MIT
// @author              CY Fung
// @match               https://www.youtube.com/*
// @grant               none
// @unwrap
// @allFrames           true
// @inject-into         page
// @run-at              document-start
// @description         To Fix issues like https://www.reddit.com/r/youtube/comments/14n9nql/
// ==/UserScript==

const RT = (this || window).Request
window.Request = (() => {

    class Request extends RT {

        constructor(...args) {
            if (args.length === 2) {
                const [url, conf] = args;
                if (url.startsWith('/youtubei/v1/next?key=') && conf && conf.headers) {

                    /*
                    conf.headers = {

                        "headers": {
                            "Content-Type": "application/json", // content type of the body data in POST request
                            "Accept-Encoding": "gzip, deflate, br", // YouTube Response - br
                            "Accept": "application/json",
                        },

                    };
                    */

                    // conf.headers = Object.assign({}, conf.headers, {
                    //     "X-Youtube-Bootstrap-Logged-In": false,
                    //     //  "Authorization": "",
                    //     // "X-Goog-AuthUser": "",
                    //     // "X-Origin": "",
                    //     // "X-Goog-PageId": "",
                    //     //     'X-Goog-PageId':''
                    // });
                    // //   delete conf.headers['X-Goog-PageId'];
                    // delete conf.headers['Authorization'];
                    // //   delete conf.headers['X-Goog-AuthUser'];
                    // //    delete conf.headers['X-Origin'];


                    const newHeaders = {}
                    const keep = ["X-Youtube-Client-Name", "X-Youtube-Client-Version", "X-Goog-Visitor-Id"];

                    for (var k in conf.headers) {
                        if (keep.indexOf(k)>=0) newHeaders[k] = conf.headers[k];
                    }


                    Object.assign(newHeaders, {

                        "Content-Type": "application/json", // content type of the body data in POST request
                        "Accept-Encoding": "gzip, deflate, br", // YouTube Response - br
                        "Accept": "application/json",
                        "X-Youtube-Bootstrap-Logged-In": false,

                    });

                    conf.headers = newHeaders;




                }

            }
            super(...args)

        }

    }

    return Request

})();

/*
*

    Authorization
    :
    "SAPISIDHASH xxxxxxxxxxxxxxxxxxxxx"
    Content-Type
    :
    "application/json"
    X-Goog-AuthUser
    :
    "0"
    X-Goog-PageId
    :
    "xxxxxxxxxxxxxxxxxxxxx"
    X-Goog-Visitor-Id
    :
    "xxxxxxxxxxxxxxxxxxxxx%3D%3D"
    X-Origin
    :
    "https://www.youtube.com"
    X-Youtube-Bootstrap-Logged-In
    :
    true
    X-Youtube-Client-Name
    :
    1
    X-Youtube-Client-Version
    :
    "2.20230629.06.00"

*/