Mycc commited on
Commit
08b2eef
·
1 Parent(s): f10648e

Update Invidious Redirect.user.js

Browse files
Files changed (1) hide show
  1. Invidious Redirect.user.js +42 -7
Invidious Redirect.user.js CHANGED
@@ -12,7 +12,7 @@
12
  // ==/UserScript==
13
 
14
  /* CONFIG */
15
- const instances = ["https://inv.citw.lgbt", "https://yewtu.be", "https://iv.nboeck.de", "https://invidious.asir.dev", "http://valheim.by"];
16
 
17
  /* SCRIPT START */
18
  let url = new URL(window.location);
@@ -20,16 +20,51 @@ let videoID = url.searchParams.get("v");
20
  let time = url.searchParams.get("t");
21
 
22
  if (!videoID) {
23
- let path = url.pathname.substring(1);
24
- let parts = path.split('/');
25
- videoID = parts[parts.length - 1];
26
  }
27
 
28
- let invidiousInstance = instances[Math.floor(Math.random() * instances.length)];
29
  let redirectURL = new URL(`${invidiousInstance}/watch?v=${videoID}`);
30
 
31
  if (time) {
32
- redirectURL.searchParams.set("t", time);
33
  }
34
 
35
- window.location = redirectURL.toString();
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
12
  // ==/UserScript==
13
 
14
  /* CONFIG */
15
+ const instances = ["https://yewtu.be","https://inv.citw.lgbt", "https://iv.nboeck.de", "https://invidious.asir.dev", "http://valheim.by"];
16
 
17
  /* SCRIPT START */
18
  let url = new URL(window.location);
 
20
  let time = url.searchParams.get("t");
21
 
22
  if (!videoID) {
23
+ let path = url.pathname.substring(1);
24
+ let parts = path.split('/');
25
+ videoID = parts[parts.length - 1];
26
  }
27
 
28
+ let invidiousInstance = instances[0]; // Use the first instance as the default
29
  let redirectURL = new URL(`${invidiousInstance}/watch?v=${videoID}`);
30
 
31
  if (time) {
32
+ redirectURL.searchParams.set("t", time);
33
  }
34
 
35
+ // Check if the default instance is available
36
+ fetch(invidiousInstance, { method: 'HEAD', mode: 'no-cors' })
37
+ .then(response => {
38
+ if (!response.ok) {
39
+ // If the default instance is not available, try the next one
40
+ for (let i = 1; i < instances.length; i++) {
41
+ invidiousInstance = instances[i];
42
+ redirectURL = new URL(`${invidiousInstance}/watch?v=${videoID}`);
43
+
44
+ if (time) {
45
+ redirectURL.searchParams.set("t", time);
46
+ }
47
+
48
+ window.location = redirectURL.toString();
49
+ break;
50
+ }
51
+ } else {
52
+ // If the default instance is available, use it
53
+ window.location = redirectURL.toString();
54
+ }
55
+ })
56
+ .catch(() => {
57
+ // If the fetch request fails, try the next instance
58
+ for (let i = 1; i < instances.length; i++) {
59
+ invidiousInstance = instances[i];
60
+ redirectURL = new URL(`${invidiousInstance}/watch?v=${videoID}`);
61
+
62
+ if (time) {
63
+ redirectURL.searchParams.set("t", time);
64
+ }
65
+
66
+ window.location = redirectURL.toString();
67
+ break;
68
+ }
69
+ });
70
+