aboutsummaryrefslogtreecommitdiffstats
path: root/scripts-greasemonkey/invidious_changer.user.js
blob: aaa9ff67a953d718922ac65e986d430f71ecaa25 (plain)
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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
// ==UserScript==
// @name        Invidious Instance Changer
// @namespace   InvidiousChanger
// @author      Cyberdevil
// @license     GPL version 3 or any later version::: https://www.gnu.org/licenses/gpl-3.0.html
// @description Menu UI Invidious instances changer
// @homepageURL https://libregit.spks.xyz/heckyel/book/src/branch/master/scripts-greasemonkey
// @include     https://*
// @version     0.0.3
// @grant       GM.setValue
// @grant       GM.getValue
// @grant       GM.xmlhttpRequest
// ==/UserScript==

// Console Style - Debug
let consoleCSS = 'background: #000; color: #00FF00; padding: 0px 7px; border: 1px solid #00FF00; line-height: 16px;';

// BETWEEN HERE and ..
if (GM_getValue('invidious.instances.isUpdating') === true) { return; }

const instancesStr = GM_getValue('invidious.instances');
if (!instancesStr){ // First time fetching instances list.
  updateInstances();
  return;
}

let instances = instancesStr.split(';');
if (instances.indexOf(window.location.host) < 0) { return; }
// .. BETWEEN HERE is run on every request, so keep it minimal.


let lastUpdated = GM_getValue('invidious.instances.lastUpdate');
let diffHours = (Date.now() - lastUpdated) / (1000 * 60 * 60);
//let diffMins = (Date.now() - lastUpdated) / (1000 * 60);

// Update instances
if (diffHours > 24) {
  updateInstances();
}

function updateInstances() {
  GM_setValue('invidious.instances.isUpdating', true);
  GM_xmlhttpRequest ({
    method: 'GET',
    url   : 'https://instances.invidio.us/instances.json',
    onload: function (request) {
      if(request.readyState == 4) {
	if(request.status == 200) {
	  processJson(JSON.parse(request.responseText));
	  saveInstances();
	}
	GM_setValue('invidious.instances.isUpdating', false);
      }
    }
  });
}

function processJson(json) {
  let newList = [];
  for (i in json) {
    if (json[i][1]['type'] != 'https') {
      continue;
    }
    if (!json[i][1]['monitor']) {
      continue;
    }
    if( json[i][1]['monitor']['statusClass'] != 'success') {
      continue;
    }

    newList.push(json[i][1]['uri']);
    console.log(json[i][0]);
  }
  instances = newList;
}

function saveInstances() {
  let str = '';
  for (i in instances) {
    let url = new URL(instances[i]);
    str = str.concat(url.host).concat(";");
    console.log(url.host);
  }
  str = str.substr(0,str.length-1)
  console.log(str);
  GM_setValue('invidious.instances.lastUpdate', Date.now());
  GM_setValue('invidious.instances', str);
}

function getRandomInstance() {
  let newInstances = instances
  newInstances.splice (newInstances.indexOf(parsedUrl.host), 1); // remove current instance.
  let random = Math.floor(Math.random() * newInstances.length)
  return newInstances[Math.floor(Math.random() * newInstances.length)];
}

function generateRandomUrl(args) {
  return generateUrl(getRandomInstance(), args);
}

function generateUrl(host, args) {
  let url = "https://".concat(host.concat("/"));
  url = url.concat(args);
  return url;
}

// --- UI stuff
const parsedUrl = new URL(window.location);
// -- UI methods
function go() {
  // Change instance
  const args = parsedUrl.pathname.substr(1, parsedUrl.pathname.length).concat(parsedUrl.search);
  const url = generateUrl(instanceSelect.item(instanceSelect.selectedIndex).text, args);
  replaceUrl(url);
}

function random() {
  // Change to random instance
  const args = parsedUrl.pathname.substr(1, parsedUrl.pathname.length).concat(parsedUrl.search);
  const url = generateRandomUrl(args);
  replaceUrl(url);
}

function replaceUrl(url) {
  window.location.replace(url);
}

// -- UI elements
// - Main div
let mainDiv = document.createElement("div");
mainDiv.style.width = "200px";
mainDiv.style.position = "fixed";
mainDiv.style.top = 0;

// - Combobox
let instanceSelect = document.createElement("select");
instanceSelect.style.color = "#fff";
instanceSelect.style.background = "#232323";
instanceSelect.style.border = "1px solid #a0a0a0";
for (i in instances) {
  if (instances[i] == parsedUrl.host) {
    // Skip current instance
    continue;
  }
  let option = document.createElement("option");
  option.text = instances[i];
  instanceSelect.add(option);
}

// - Go button
let goButton = document.createElement("input");
goButton.style.color = "#fff";
goButton.style.background = " #232323";
goButton.style.border = "1px solid #a0a0a0";
goButton.setAttribute("type", "button");
goButton.setAttribute("value", "Go");
goButton.addEventListener("click", go, false);

// - Random button
let randomButton = document.createElement("input");
randomButton.style.color = "#fff";
randomButton.style.background = " #232323";
randomButton.style.border = "1px solid #a0a0a0";
randomButton.setAttribute("type", "button");
randomButton.setAttribute("value", "Random");
randomButton.addEventListener("click", random, false);

// - Add stuff
mainDiv.appendChild(instanceSelect);
mainDiv.appendChild(goButton);
mainDiv.appendChild(randomButton);
document.body.appendChild(mainDiv);

function statuscheck(){
  // Console Feedback
  console.log("%cUSERSCRIPT | " + GM_info.script.name + " " + GM_info.script.version + " | successfully initialized", consoleCSS);
}

statuscheck();