// ==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();