about summary refs log tree commit diff stats
path: root/src/pages
diff options
context:
space:
mode:
authorManeraKai <manerakai@protonmail.com>2024-08-26 15:33:25 +0300
committerManeraKai <manerakai@protonmail.com>2024-08-26 15:33:25 +0300
commitdd4b741e4ab7d6892cbc4a4316c73d92ef48efed (patch)
tree6b0e0e5aeb62b60509f784c65316f1192beadb3c /src/pages
parentAdded support for subdirectory https://github.com/libredirect/browser_extensi... (diff)
downloadlibredirect-dd4b741e4ab7d6892cbc4a4316c73d92ef48efed.zip
Added Auto Pick Instance https://github.com/libredirect/browser_extension/issues/949
Diffstat (limited to 'src/pages')
-rw-r--r--src/pages/icons/AutoPickIcon.svelte11
-rw-r--r--src/pages/options_src/Services/Instances.svelte37
2 files changed, 44 insertions, 4 deletions
diff --git a/src/pages/icons/AutoPickIcon.svelte b/src/pages/icons/AutoPickIcon.svelte
new file mode 100644
index 00000000..86adfa28
--- /dev/null
+++ b/src/pages/icons/AutoPickIcon.svelte
@@ -0,0 +1,11 @@
+<svg
+  {...$$restProps}
+  xmlns="http://www.w3.org/2000/svg"
+  height="20px"
+  viewBox="0 -960 960 960"
+  width="20px"
+  fill="currentColor"
+  ><path
+    d="M144-144v-178l342-342-54-53 51-51 72 72 110.22-110.22q4.45-4.45 11.11-7.11Q683-816 691-816t15 2.5q7 2.5 12 7.5l87 88q4.55 5.83 7.27 12.64 2.73 6.8 2.73 14.58t-2.66 14.44q-2.67 6.66-7.11 11.1L696-556l72 73-51 51-54-54-341 342H144Zm72-72h76l320-320-75-76-321 320v76Zm424-385 90-91-38-39-91 90 39 40Zm0 0-39-40 39 40Z"
+  /></svg
+>
diff --git a/src/pages/options_src/Services/Instances.svelte b/src/pages/options_src/Services/Instances.svelte
index 3b870c4b..32425b80 100644
--- a/src/pages/options_src/Services/Instances.svelte
+++ b/src/pages/options_src/Services/Instances.svelte
@@ -5,6 +5,7 @@
   import AddIcon from "../../icons/AddIcon.svelte"
   import { options, config } from "../stores"
   import PingIcon from "../../icons/PingIcon.svelte"
+  import AutoPickIcon from "../../icons/AutoPickIcon.svelte"
   import Row from "../../components/Row.svelte"
   import Input from "../../components/Input.svelte"
   import Label from "../../components/Label.svelte"
@@ -36,10 +37,9 @@
     allInstances = []
     if (_options[selectedFrontend]) allInstances.push(..._options[selectedFrontend])
     if (redirects && redirects[selectedFrontend]) {
-      for (const network in redirects[selectedFrontend]) {
-        allInstances.push(...redirects[selectedFrontend][network])
-      }
+      allInstances.push(...redirects[selectedFrontend]["clearnet"])
     }
+    allInstances = [...new Set(allInstances)]
   }
 
   let pingCache
@@ -64,6 +64,31 @@
       pingCache[instance] = colorTime(time)
     }
   }
+  function randomInstances(n) {
+    let instances = []
+    for (let i = 0; i < n; i++) {
+      instances.push(redirects[selectedFrontend]["clearnet"][Math.floor(Math.random() * allInstances.length)])
+    }
+    return instances
+  }
+
+  async function autoPickInstance() {
+    const instances = randomInstances(5)
+    const myInstancesCache = []
+    for (const instance of instances) {
+      pingCache[instance] = { color: "lightblue", value: "pinging..." }
+      const time = await utils.ping(instance)
+      pingCache[instance] = colorTime(time)
+      myInstancesCache.push([instance, time])
+    }
+    myInstancesCache.sort(function (a, b) {
+      return a[1] - b[1]
+    })
+
+    _options[selectedFrontend].push(myInstancesCache[0][0])
+    options.set(_options)
+  }
+
   function colorTime(time) {
     let value
     let color
@@ -102,11 +127,15 @@
 {#if serviceConf.frontends[selectedFrontend].instanceList && redirects && blacklist}
   <hr />
 
-  <div class="ping">
+  <div>
     <Button on:click={pingInstances}>
       <PingIcon class="margin margin_{document.body.dir}" />
       {browser.i18n.getMessage("pingInstances") || "Ping Instances"}
     </Button>
+    <Button on:click={autoPickInstance}>
+      <AutoPickIcon class="margin margin_{document.body.dir}" />
+      {browser.i18n.getMessage("autoPickInstance") || "Auto Pick Instance"}
+    </Button>
   </div>
 
   <Row>