Skip to content

Instantly share code, notes, and snippets.

@m0n5t3r
Last active December 11, 2023 05:28
Show Gist options
  • Save m0n5t3r/b7c13265152bd8c997f2d22afb4932e7 to your computer and use it in GitHub Desktop.
Save m0n5t3r/b7c13265152bd8c997f2d22afb4932e7 to your computer and use it in GitHub Desktop.
Random invidious redirect (fetches list from api.invidious.io, attempts to keep 100% healthy ones, picks random instance from the list; tested with violentmonkey, greasemonkey (firefox) and tampermonkey (brave)
// ==UserScript==
// @description Redirects Youtube URLs to Invidio.us
// @name Invidious Redirect
// @namespace Backend
// @downloadURL https://gist.githubusercontent.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);
}
});
});
@Mennaruuk
Copy link

New PSA from Invidious project:

"invidio.us is now deprecated, invidious.io is the new domain.

The invidious-redirect website ( invidio.us ) is now https://redirect.invidious.io/

The instance API ( instances.invidio.us ) is now https://api.invidious.io/

The uptimerobot page ( uptime.invidio.us ) is now https://uptime.invidious.io/

https://git.invidious.io/ redirects to the Github page of the project: https://github.com/iv-org/invidious/

https://docs.invidious.io/ redirects to the documentation website of the project: https://github.com/iv-org/documentation/

https://invidious.io/ will become in the future a "project presentation" page (like https://searx.me/ ), we will use a markdown framework like Hugo.

Projects using something from invidio.us need to move to the new domain."

@m0n5t3r
Copy link
Author

m0n5t3r commented Jan 26, 2021

updated, thanks for letting me know

@Mennaruuk
Copy link

You're welcome!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment