Youtube Error fix

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

Цей скрипт не слід встановлювати безпосередньо. Це - бібліотека для інших скриптів для включення в мета директиву // @require https://update.sleazyfork.org/scripts/579644/1834066/Youtube%20Error%20fix.js

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.

(У мене вже є менеджер скриптів, дайте мені встановити його!)

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         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);
    }
  });
})();