invidious-redirect/src/main.js

82 lines
2.3 KiB
JavaScript
Raw Normal View History

2020-08-02 06:18:40 +00:00
"use strict";
;(() => {
const q = s => document.querySelector(s)
const qa = s => document.querySelectorAll(s)
function createElement(tag, properties = {}, children = []) {
const e = document.createElement(tag)
for (const key of Object.keys(properties)) {
e[key] = properties[key]
}
for (const child of children) {
e.appendChild(child)
}
return e
}
2020-08-02 10:09:13 +00:00
function shuffle(array) {
for (let i = 0; i < array.length; i++) {
let j = Math.floor(Math.random() * (array.length-i)) + i
;[array[i], array[j]] = [array[j], array[i]]
}
return array
}
function request(url, callback) {
const xhr = new XMLHttpRequest()
xhr.addEventListener("readystatechange", () => {
if (xhr.readyState === 4) {
if (xhr.status === 200) {
callback(null, JSON.parse(xhr.response))
2020-08-07 08:10:12 +00:00
}
}
2020-08-07 08:10:12 +00:00
})
xhr.open("GET", url)
xhr.send()
2020-08-07 08:10:12 +00:00
}
2020-08-02 06:18:40 +00:00
const destinationPath = window.location.href.slice(window.location.origin.length)
q("#watch-on-youtube").href = "https://www.youtube.com" + destinationPath
for (const e of qa("[data-loading-message]")) {
e.textContent = e.getAttribute("data-loading-message")
}
2020-08-07 08:10:12 +00:00
request("https://instances.invidio.us/instances.json?sort_by=type,health",
/** @param {[string, {monitor: any, flag: string, region: string, stats: any, type: string, uri: string}][]} root */ (err, root) => {
2020-08-02 10:09:13 +00:00
shuffle(root)
root.map(entry => {
const healthKnown = !!entry[1].monitor
return {
name: entry[0],
details: entry[1],
2020-08-07 08:10:12 +00:00
health: +(healthKnown ? entry[1].monitor.dailyRatios[0].ratio : 95),
2020-08-02 10:09:13 +00:00
healthKnown
2020-08-02 06:18:40 +00:00
}
2020-08-02 10:09:13 +00:00
}).filter(entry => {
2020-08-02 10:15:06 +00:00
return entry.details.type === "https" && entry.health > 0
2020-08-02 10:09:13 +00:00
}).sort((a, b) => {
return b.health - a.health
}).forEach(entry => {
let target = entry.details.uri.replace(/\/*$/, "") + destinationPath
const healthUnknown = entry.healthKnown ? "" : "health-unknown "
const health = entry.healthKnown ? entry.health.toFixed(0) : "(unknown)"
2020-08-02 06:18:40 +00:00
q("#instances-tbody").appendChild(
createElement("tr", {}, [
2020-08-02 10:09:13 +00:00
createElement("td", {textContent: entry.name}),
2020-08-02 06:18:40 +00:00
createElement("td", {className: "column-center "+healthUnknown, textContent: health}),
createElement("td", {className: "column-center"}, [
createElement("a", {href: target, textContent: "Go →"})
])
])
)
})
for (const e of qa(".loading")) {
e.remove()
}
2020-08-02 06:18:40 +00:00
})
})()