File size: 1,956 Bytes
41f8001
 
 
 
 
 
 
 
cc2a1ff
 
41f8001
 
 
 
c7d8507
41f8001
 
 
 
 
 
 
08b2eef
 
 
41f8001
 
08b2eef
41f8001
 
 
08b2eef
41f8001
 
08b2eef
 
 
43e9138
 
 
 
 
08b2eef
43e9138
 
 
08b2eef
43e9138
 
 
 
 
 
 
08b2eef
 
43e9138
 
 
 
08b2eef
43e9138
 
 
08b2eef
43e9138
 
 
cc2a1ff
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
// ==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;
  }
 });