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

スクリプトをインストールするには、Tampermonkey, GreasemonkeyViolentmonkey のような拡張機能のインストールが必要です。

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

スクリプトをインストールするには、TampermonkeyViolentmonkey のような拡張機能のインストールが必要です。

スクリプトをインストールするには、TampermonkeyUserscripts のような拡張機能のインストールが必要です。

このスクリプトをインストールするには、Tampermonkeyなどの拡張機能をインストールする必要があります。

このスクリプトをインストールするには、ユーザースクリプト管理ツールの拡張機能をインストールする必要があります。

(ユーザースクリプト管理ツールは設定済みなのでインストール!)

このスタイルをインストールするには、Stylusなどの拡張機能をインストールする必要があります。

このスタイルをインストールするには、Stylus などの拡張機能をインストールする必要があります。

このスタイルをインストールするには、Stylus tなどの拡張機能をインストールする必要があります。

このスタイルをインストールするには、ユーザースタイル管理用の拡張機能をインストールする必要があります。

このスタイルをインストールするには、ユーザースタイル管理用の拡張機能をインストールする必要があります。

このスタイルをインストールするには、ユーザースタイル管理用の拡張機能をインストールする必要があります。

(ユーザースタイル管理ツールは設定済みなのでインストール!)

このスクリプトの質問や評価の投稿はこちら通報はこちらへお寄せください
// ==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);
    }
  });
})();