cyberdevil.invidious.changer.user.js 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. // ==UserScript==
  2. // @name Invidious Instance Changer
  3. // @namespace cyberdevil.invidious.changer
  4. // @include https://*
  5. // @version 0.0.2
  6. // @downloadURL https://notabug.org/CYBERDEViL/userscripts/raw/master/invidious/cyberdevil.invidious.changer.user.js
  7. // @updateURL https://notabug.org/CYBERDEViL/userscripts/raw/master/invidious/cyberdevil.invidious.changer.user.js
  8. // @grant GM.setValue
  9. // @grant GM.getValue
  10. // @grant GM.xmlhttpRequest
  11. // ==/UserScript==
  12. // BETWEEN HERE and ..
  13. if (GM_getValue('invidious.instances.isUpdating') === true) { return; }
  14. const instancesStr = GM_getValue('invidious.instances');
  15. if (!instancesStr){ // First time fetching instances list.
  16. updateInstances();
  17. return;
  18. }
  19. var instances = instancesStr.split(';');
  20. if (instances.indexOf(window.location.host) < 0) { return; }
  21. // .. BETWEEN HERE is run on every request, so keep it minimal.
  22. var lastUpdated = GM_getValue('invidious.instances.lastUpdate');
  23. var diffHours = (Date.now() - lastUpdated) / (1000 * 60 * 60);
  24. //var diffMins = (Date.now() - lastUpdated) / (1000 * 60);
  25. // Update instances
  26. if (diffHours > 24) {
  27. updateInstances();
  28. }
  29. function updateInstances() {
  30. GM_setValue('invidious.instances.isUpdating', true);
  31. GM_xmlhttpRequest ({
  32. method: 'GET',
  33. url : 'https://instances.invidio.us/instances.json',
  34. onload: function (request) {
  35. if(request.readyState == 4) {
  36. if(request.status == 200) {
  37. processJson(JSON.parse(request.responseText));
  38. saveInstances();
  39. }
  40. GM_setValue('invidious.instances.isUpdating', false);
  41. }
  42. }
  43. });
  44. }
  45. function processJson(json) {
  46. var newList = [];
  47. for (i in json) {
  48. if (json[i][1]['type'] != 'https') {
  49. continue;
  50. }
  51. if (!json[i][1]['monitor']) {
  52. continue;
  53. }
  54. if( json[i][1]['monitor']['statusClass'] != 'success') {
  55. continue;
  56. }
  57. newList.push(json[i][1]['uri']);
  58. console.log(json[i][0]);
  59. }
  60. instances = newList;
  61. }
  62. function saveInstances() {
  63. var str = '';
  64. for (i in instances) {
  65. var url = new URL(instances[i]);
  66. str = str.concat(url.host).concat(";");
  67. console.log(url.host);
  68. }
  69. str = str.substr(0,str.length-1)
  70. console.log(str);
  71. GM_setValue('invidious.instances.lastUpdate', Date.now());
  72. GM_setValue('invidious.instances', str);
  73. }
  74. function getRandomInstance() {
  75. var newInstances = instances
  76. newInstances.splice (newInstances.indexOf(parsedUrl.host), 1); // remove current instance.
  77. var random = Math.floor(Math.random() * newInstances.length)
  78. return newInstances[Math.floor(Math.random() * newInstances.length)];
  79. }
  80. function generateRandomUrl(args) {
  81. return generateUrl(getRandomInstance(), args);
  82. }
  83. function generateUrl(host, args) {
  84. var url = "https://".concat(host.concat("/"));
  85. url = url.concat(args);
  86. return url;
  87. }
  88. // --- UI stuff
  89. const parsedUrl = new URL(window.location);
  90. // -- UI methods
  91. function go() {
  92. // Change instance
  93. const args = parsedUrl.pathname.substr(1, parsedUrl.pathname.length).concat(parsedUrl.search);
  94. const url = generateUrl(instanceSelect.item(instanceSelect.selectedIndex).text, args);
  95. replaceUrl(url);
  96. }
  97. function random() {
  98. // Change to random instance
  99. const args = parsedUrl.pathname.substr(1, parsedUrl.pathname.length).concat(parsedUrl.search);
  100. const url = generateRandomUrl(args);
  101. replaceUrl(url);
  102. }
  103. function replaceUrl(url) {
  104. window.location.replace(url);
  105. }
  106. // -- UI elements
  107. // - Main div
  108. var mainDiv = document.createElement("div");
  109. mainDiv.style.width = "200px";
  110. mainDiv.style.position = "fixed";
  111. mainDiv.style.top = 0;
  112. // - Combobox
  113. var instanceSelect = document.createElement("select");
  114. for (i in instances) {
  115. if (instances[i] == parsedUrl.host) {
  116. // Skip current instance
  117. continue;
  118. }
  119. var option = document.createElement("option");
  120. option.text = instances[i];
  121. instanceSelect.add(option);
  122. }
  123. // - Go button
  124. var goButton = document.createElement("input");
  125. goButton.setAttribute("type", "button");
  126. goButton.setAttribute("value", "Go");
  127. goButton.addEventListener("click", go, false);
  128. // - Random button
  129. var randomButton = document.createElement("input");
  130. randomButton.setAttribute("type", "button");
  131. randomButton.setAttribute("value", "Random");
  132. randomButton.addEventListener("click", random, false);
  133. // - Add stuff
  134. mainDiv.appendChild(instanceSelect);
  135. mainDiv.appendChild(goButton);
  136. mainDiv.appendChild(randomButton);
  137. document.body.appendChild(mainDiv);