diff options
author | Jesús <heckyel@hyperbola.info> | 2020-07-01 16:04:09 -0500 |
---|---|---|
committer | Jesús <heckyel@hyperbola.info> | 2020-07-01 16:04:09 -0500 |
commit | 3a095e75869ade51e01c30a7169cf1d7892b8aa7 (patch) | |
tree | 46265a5d25184dd28d04ef07a81ecaaf7a2f74a6 | |
parent | cd760a698314e3b19ce3f78410daaf17c121fb9b (diff) | |
download | book-3a095e75869ade51e01c30a7169cf1d7892b8aa7.tar.lz book-3a095e75869ade51e01c30a7169cf1d7892b8aa7.tar.xz book-3a095e75869ade51e01c30a7169cf1d7892b8aa7.zip |
scripts-greasemonkey: add Invidious Instance Changer
-rw-r--r-- | scripts-greasemonkey/invidious_changer.user.js | 179 |
1 files changed, 179 insertions, 0 deletions
diff --git a/scripts-greasemonkey/invidious_changer.user.js b/scripts-greasemonkey/invidious_changer.user.js new file mode 100644 index 0000000..aaa9ff6 --- /dev/null +++ b/scripts-greasemonkey/invidious_changer.user.js @@ -0,0 +1,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(); |