about summary refs log tree commit diff stats
path: root/src/assets/javascripts/utils.js
diff options
context:
space:
mode:
authorBenedikt Peetz <benedikt.peetz@b-peetz.de>2025-01-29 17:19:04 +0100
committerBenedikt Peetz <benedikt.peetz@b-peetz.de>2025-01-29 17:19:04 +0100
commit0cfcd80f3a715a77821aa83e0c89245ec2f53eec (patch)
treeaec2aea5c24c15f287bc05e695b60d45a26e6dc8 /src/assets/javascripts/utils.js
parentchore(merge): Merge remote-tracking branch 'origin/master' (diff)
parentAdded Structables, and made it the default https://codeberg.org/LibRedirect/b... (diff)
downloadlibredirect-0cfcd80f3a715a77821aa83e0c89245ec2f53eec.zip
Merge remote-tracking branch 'origin/master'
Diffstat (limited to 'src/assets/javascripts/utils.js')
-rw-r--r--src/assets/javascripts/utils.js20
1 files changed, 19 insertions, 1 deletions
diff --git a/src/assets/javascripts/utils.js b/src/assets/javascripts/utils.js
index e5b8ba46..f360a15b 100644
--- a/src/assets/javascripts/utils.js
+++ b/src/assets/javascripts/utils.js
@@ -31,7 +31,8 @@ function protocolHost(url) {
   if (url.pathname == "/TekstoLibre/" && url.host.endsWith("github.io"))
     return `${url.protocol}//${url.host}${url.pathname.slice(0, -1)}`
 
-  return `${url.protocol}//${url.host}${url.pathname}`
+  const pathname = url.pathname != "/" ? url.pathname : ""
+  return `${url.protocol}//${url.host}${pathname}`
 }
 
 /**
@@ -221,6 +222,22 @@ export function randomInstances(clearnet, n) {
   }
   return instances
 }
+
+async function autoPickInstance(clearnet, url) {
+  if (url) {
+    const i = clearnet.findIndex(instance => url.href.startsWith(instance))
+    if (i >= 0) clearnet.splice(i, 1)
+  }
+  const random = randomInstances(clearnet, 5)
+  const pings = await Promise.all([
+    ...random.map(async instance => {
+      return [instance, await ping(instance)]
+    }),
+  ])
+  pings.sort((a, b) => a[1] - b[1])
+  return pings[0][0]
+}
+
 export function style(options, window) {
   const vars = cssVariables(options, window)
   return `--text: ${vars.text};
@@ -276,4 +293,5 @@ export default {
   convertMapCentre,
   randomInstances,
   style,
+  autoPickInstance,
 }