Forces YouTube links to load with a real page navigation instead of SPA routing.
此腳本不應該直接安裝,它是一個供其他腳本使用的函式庫。欲使用本函式庫,請在腳本 metadata 寫上: // @require https://update.sleazyfork.org/scripts/579644/1834066/Youtube%20Error%20fix.js
// ==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);
}
});
})();