Youtube Error fix

Forces YouTube links to load with a real page navigation instead of SPA routing.

Tento skript by neměl být instalován přímo. Jedná se o knihovnu, kterou by měly jiné skripty využívat pomocí meta příkazu // @require https://update.sleazyfork.org/scripts/579644/1834066/Youtube%20Error%20fix.js

K instalaci tototo skriptu si budete muset nainstalovat rozšíření jako Tampermonkey, Greasemonkey nebo Violentmonkey.

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

K instalaci tohoto skriptu si budete muset nainstalovat rozšíření jako Tampermonkey nebo Violentmonkey.

K instalaci tohoto skriptu si budete muset nainstalovat rozšíření jako Tampermonkey nebo Userscripts.

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

K instalaci tohoto skriptu si budete muset nainstalovat manažer uživatelských skriptů.

(Už mám manažer uživatelských skriptů, nechte mě ho nainstalovat!)

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.

(Už mám manažer uživatelských stylů, nechte mě ho nainstalovat!)

// ==UserScript==
// @name         Youtube Error fix
// @version      1.0
// @description  Forces YouTube links to load with a real page navigation instead of SPA routing.
// @match        https://www.youtube.com/*
// @grant        none
// ==/UserScript==

(function () {
  'use strict';

  function isYouTubeLink(url) {
    try {
      const u = new URL(url, location.href);
      return u.hostname.endsWith('youtube.com') || u.hostname.endsWith('youtu.be');
    } catch {
      return false;
    }
  }

  function hardNavigate(url) {
    if (!url) return;
    location.href = url;
  }

  document.addEventListener(
    'click',
    (e) => {
      const a = e.target.closest && e.target.closest('a[href]');
      if (!a) return;

      const url = a.href;
      if (!isYouTubeLink(url)) return;

      // Let modified clicks behave normally
      if (e.metaKey || e.ctrlKey || e.shiftKey || e.altKey || e.button !== 0) return;

      e.preventDefault();
      e.stopPropagation();
      hardNavigate(url);
    },
    true
  );

  // Also catch YouTube's SPA-style navigation attempts
  window.addEventListener('yt-navigate-start', (e) => {
    const url = e && e.detail && e.detail.url;
    if (url && isYouTubeLink(url)) {
      hardNavigate(url);
    }
  });
})();