// ==UserScript== // @name Invidious Redirect // @description Redirect YouTube video link // @namespace // @match *://youtube.com/watch* // @match *://*.youtube.com/watch* // @match *://youtu.be/* // @run-at document-start // @grant none // @version 1.05 // @author Mycc // ==/UserScript== /* CONFIG */ const instances = ["https://inv.citw.lgbt","https://yewtu.be", "https://iv.nboeck.de", "https://invidious.asir.dev", "http://valheim.by"]; /* SCRIPT START */ let url = new URL(window.location); let videoID = url.searchParams.get("v"); let time = url.searchParams.get("t"); if (!videoID) { let path = url.pathname.substring(1); let parts = path.split('/'); videoID = parts[parts.length - 1]; } let invidiousInstance = instances[0]; // Use the first instance as the default let redirectURL = new URL(`${invidiousInstance}/watch?v=${videoID}`); if (time) { redirectURL.searchParams.set("t", time); } // Check if the default instance is available fetch(invidiousInstance, { method: 'HEAD', mode: 'no-cors' }) .then(response => { if (!response.ok) { // If the default instance is not available, try the next one for (let i = 1; i < instances.length; i++) { invidiousInstance = instances[i]; redirectURL = new URL(`${invidiousInstance}/watch?v=${videoID}`); if (time) { redirectURL.searchParams.set("t", time); } window.location = redirectURL.toString(); break; } } else { // If the default instance is available, use it window.location = redirectURL.toString(); } }) .catch(() => { // If the fetch request fails, try the next instance for (let i = 1; i < instances.length; i++) { invidiousInstance = instances[i]; redirectURL = new URL(`${invidiousInstance}/watch?v=${videoID}`); if (time) { redirectURL.searchParams.set("t", time); } window.location = redirectURL.toString(); break; } });