🎉 欢迎访问GreasyFork.Org 镜像站!本镜像站由公众号【爱吃馍】搭建,用于分享脚本。联系邮箱📮

Greasy fork 爱吃馍镜像

Greasy Fork is available in English.

YouTube: Simulate Direct Link to Bypass Anti-Adblock

Bypasses YouTube's anti-adblock by making in-site navigations appear as direct links without reloading the page (bypasses some anti-adblock features)

Aby zainstalować ten skrypt, wymagana jest instalacje jednego z następujących rozszerzeń: Tampermonkey, Greasemonkey lub Violentmonkey.

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

Aby zainstalować ten skrypt, wymagana jest instalacje jednego z następujących rozszerzeń: Tampermonkey, Violentmonkey.

Aby zainstalować ten skrypt, wymagana będzie instalacja rozszerzenia Tampermonkey lub Userscripts.

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

Aby zainstalować ten skrypt, musisz zainstalować rozszerzenie menedżera skryptów użytkownika.

(Mam już menedżera skryptów użytkownika, pozwól mi to zainstalować!)

🚀 安装遇到问题?关注公众号获取帮助

公众号二维码

扫码关注【爱吃馍】

回复【脚本】获取最新教程和防失联地址

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.

Będziesz musiał zainstalować rozszerzenie menedżera stylów użytkownika, aby zainstalować ten styl.

Będziesz musiał zainstalować rozszerzenie menedżera stylów użytkownika, aby zainstalować ten styl.

Musisz zainstalować rozszerzenie menedżera stylów użytkownika, aby zainstalować ten styl.

(Mam już menedżera stylów użytkownika, pozwól mi to zainstalować!)

🚀 安装遇到问题?关注公众号获取帮助

公众号二维码

扫码关注【爱吃馍】

回复【脚本】获取最新教程和防失联地址

// ==UserScript==
// @name         YouTube: Simulate Direct Link to Bypass Anti-Adblock
// @namespace    github.com/annaroblox
// @version      2.0
// @description  Bypasses YouTube's anti-adblock by making in-site navigations appear as direct links without reloading the page (bypasses some anti-adblock features)
// @author       annaroblox
// @match        https://www.youtube.com/*
// @run-at       document-start
// @grant        none
// @license      Public Domain
// ==/UserScript==

(function () {
    'use strict';

    // A Map to track which video IDs have been "fixed" in the current session.
    // Using a Map is slightly cleaner than sessionStorage for session-only data.
    const fixedVideos = new Map();

    // Store the original fetch function.
    const originalFetch = window.fetch;

    // Override the global fetch function.
    window.fetch = function (input, init) {
        // Create a URL object from the fetch input.
        const url = (typeof input === 'string') ? new URL(input, window.location.origin) : new URL(input.url, window.location.origin);

        // We only care about requests to the '/youtubei/v1/player' endpoint,
        // which is responsible for loading the video player and its data.
        if (url.pathname.includes('/youtubei/v1/player')) {
            const currentUrl = new URL(location.href);
            const videoId = currentUrl.searchParams.get('v');

            // Proceed only if we're on a watch page and haven't fixed this video yet.
            if (currentUrl.pathname === '/watch' && videoId && !fixedVideos.has(videoId)) {

                console.info('YT Anti-Adblock Bypass: Simulating direct link for video:', videoId);

                // Mark this video as fixed for this session.
                fixedVideos.set(videoId, true);

                // The key modification: An in-site navigation (clicking a thumbnail) sends a `Referer`
                // header pointing to the previous YouTube page. A direct visit (e.g., from a bookmark or
                // external link) often has no referrer or a non-YouTube one.
                // By removing the header, we make the request look like a direct visit.

                // Create a mutable copy of the headers.
                const newHeaders = new Headers(init ? init.headers : {});

                // Remove the Referer header.
                newHeaders.delete('Referer');

                // Create a new init object to avoid modifying the original one passed to the function.
                const newInit = { ...init, headers: newHeaders };

                // Call the original fetch with the modified headers.
                return originalFetch.call(this, input, newInit);
            }
        }

        // For all other requests, use the original fetch function without modification.
        return originalFetch.apply(this, arguments);
    };
})();