// ==UserScript== // @description Redirects Youtube URLs to Invidio.us // @name Invidious Redirect // @namespace Backend // @downloadURL https://gist.github.com/m0n5t3r/b7c13265152bd8c997f2d22afb4932e7/raw/invidious-redirect.userscript.js // @include http://www.youtube.com/* // @include https://www.youtube.com/* // @include https://consent.youtube.com/* // @version 1.6 // @run-at document-start // @grant none // ==/UserScript== // prevent youtube content from loading; used to be a simple document.write, but // * at first, greasemonkey complained // * then google started redirecting everything to consent.youtube.com, which has CSP that requires trusted types var newDoc = document.implementation.createHTMLDocument (""), h1 = document.createElement("h1"); h1.innerText = "Redirecting, please wait..."; newDoc.body.appendChild(h1); document.replaceChild (document.importNode(newDoc.documentElement, true), document.documentElement); document.close(); const blacklist = [ "invidious.xyz", "invidious.site", "invidiou.site", "invidious.reallyancient.tech", ]; function find_parameter(name) { var result = null, tmp = []; location.search .substr(1) .split("&") .forEach((item) => { tmp = item.split("="); if (tmp[0] === name) result = decodeURIComponent(tmp[1]); }); return result; } // pick a random instance with a good health record fetch("https://api.invidious.io/instances.json?sort_by=type,health").then((response) => { response.json().then((instances) => { var filtered, domain, idx, a; filtered = instances.filter((i) => { var monitor = i[1].monitor; var type = i[1].type; if(type != "https") { return false; } if(blacklist.includes(i[0])) { return false; } if (monitor) { return monitor.dailyRatios[0].ratio == "100.00"; } // no monitoring data, let it through; might change later to filter in 2 steps and only // let unknown status instances through if no health info has been found return true; }).map((i) => { return i[0] }); idx = Math.floor(Math.random() * filtered.length); domain = filtered[idx]; if (/watch\?|channel|embed|shorts/.test(window.location.href) && window.location.href.indexOf('list=WL') < 0) { a = window.location.href.replace(/www\.youtube\.com/, domain).replace('/shorts/', '/watch?v='); window.location.replace(a); } // youtu.be and youtube.com seem to force-redirect to the consent thing lately, handle it here if(/consent\.youtube\.com/.test(window.location.href)) { a = find_parameter('continue').replace(/www.youtube.com/, domain).replace('/shorts/', '/watch?v='); window.location.replace(a); } }); });