Invidious Instance Switcher

Adds a link with a random public Invidious alternative to easily switch Invidious instances.

  1. // ==UserScript==
  2. // @name Invidious Instance Switcher
  3. // @namespace Violentmonkey Scripts
  4. // @match https://invidious.snopyta.org/*
  5. // @match https://invidious.13ad.de/*
  6. // @match https://invidious.kavin.rocks/*
  7. // @grant none
  8. // @version 1.1
  9. // @author BErnd14
  10. // @description Adds a link with a random public Invidious alternative to easily switch Invidious instances.
  11. // ==/UserScript==
  12.  
  13. var instances = ["https://invidious.snopyta.org",
  14. "https://invidious.13ad.de",
  15. "https://invidious.kavin.rocks"];
  16.  
  17.  
  18. function shuffleArray(array) {
  19. for (let i = array.length - 1; i > 0; i--) {
  20. const j = Math.floor(Math.random() * (i + 1));
  21. [array[i], array[j]] = [array[j], array[i]];
  22. }
  23. }
  24.  
  25.  
  26. function getIndex(array) {
  27. for (var i = 0; i < instances.length; i++) {
  28. if (window.location.href.includes(instances[i])) {
  29. return i;
  30. }
  31. }
  32. return 0;
  33. }
  34.  
  35. shuffleArray(instances);
  36.  
  37. var found_at_index = getIndex(instances);
  38. var next_instance = (found_at_index + 1) % instances.length;
  39.  
  40.  
  41. if (!window.location.pathname.includes("/embed/")) {
  42. var instance_switch = document.createElement("a");
  43. instance_switch.setAttribute('href', instances[next_instance] + window.location.pathname + window.location.search);
  44. instance_switch.innerHTML = "Next instance (" + instances[next_instance] + ")";
  45. document.body.prepend(instance_switch);
  46. }
  47.