From 4c69fa7e292a3896ef5f4daf1e6dea3c9bd0b446 Mon Sep 17 00:00:00 2001 From: BobIsMyManager Date: Mon, 1 Aug 2022 13:31:16 +0100 Subject: [PATCH] Latency threshold, LBRY/Odysee -> LBRY Closes https://github.com/libredirect/libredirect/pull/405 --- .../images/{imgur-icon.png => imgur.png} | Bin src/assets/javascripts/general.js | 1 + src/assets/javascripts/utils.js | 63 ++++++++++++++---- src/pages/options/index.html | 15 +++-- src/pages/options/index.pug | 2 +- src/pages/options/widgets/general.pug | 10 ++- src/pages/popup/popup.html | 4 +- src/pages/popup/popup.pug | 2 +- 8 files changed, 74 insertions(+), 23 deletions(-) rename src/assets/images/{imgur-icon.png => imgur.png} (100%) diff --git a/src/assets/images/imgur-icon.png b/src/assets/images/imgur.png similarity index 100% rename from src/assets/images/imgur-icon.png rename to src/assets/images/imgur.png diff --git a/src/assets/javascripts/general.js b/src/assets/javascripts/general.js index 0b34c14..113dc6f 100644 --- a/src/assets/javascripts/general.js +++ b/src/assets/javascripts/general.js @@ -32,6 +32,7 @@ async function initDefaults() { firstPartyIsolate: false, protocol: "normal", protocolFallback: true, + latencyThreshold: 1000 }, () => resolve() ) diff --git a/src/assets/javascripts/utils.js b/src/assets/javascripts/utils.js index 9a7d37b..52848de 100644 --- a/src/assets/javascripts/utils.js +++ b/src/assets/javascripts/utils.js @@ -247,34 +247,65 @@ async function processDefaultCustomInstances(target, name, protocol, document) { }) } -async function ping(href) { +function ping(href) { return new Promise(async resolve => { + let average = 0 + let time + for (let i = 0; i < 3; i++) { + time = await pingOnce(href) + if (i == 0) continue + if (time >= 5000) { + resolve(time) + return + } + average += time + } + average = parseInt(average / 3) + resolve(average) + }) +} + +function pingOnce(href) { + return new Promise(async resolve => { + let started let http = new XMLHttpRequest() - http.open("GET", `${href}?_=${new Date().getTime()}`, /*async*/ true) http.timeout = 5000 - let started = new Date().getTime() + http.ontimeout = () => resolve(5000) + http.onerror = () => resolve() http.onreadystatechange = () => { if (http.readyState == 2) { if (http.status == 200) { let ended = new Date().getTime() http.abort() resolve(ended - started) - } else resolve(5000 + http.status) + } else { + resolve(5000 + http.status) + } } + } - http.ontimeout = () => resolve(5000) - http.onerror = () => resolve() - try { - http.send(null) - } catch (exception) { - resolve() - } + http.open("GET", `${href}?_=${new Date().getTime()}`, true) + started = new Date().getTime() + http.send(null) }) } + -async function testLatency(element, instances) { +async function testLatency(element, instances, frontend) { return new Promise(async resolve => { let myList = {} + let latencyThreshold + let redirectsChecks = [] + browser.storage.local.get( + [ + "latencyThreshold", + `${frontend}NormalRedirectsChecks` + ], + r => { + latencyThreshold = r.latencyThreshold + redirectsChecks = r[`${frontend}NormalRedirectsChecks`] + } + ) for (const href of instances) await ping(href).then(time => { if (time) { @@ -284,6 +315,12 @@ async function testLatency(element, instances) { else if (time <= 2000) color = "orange" else color = "red" + if (time > latencyThreshold) { + redirectsChecks.splice(redirectsChecks.indexOf(href), 1) + } + + browser.storage.local.set({ [`${frontend}NormalRedirectsChecks`]: redirectsChecks }) + let text if (time == 5000) text = "5000ms+" else if (time > 5000) text = `ERROR: ${time - 5000}` @@ -487,7 +524,7 @@ function latency(name, frontend, document, location) { let redirects = r[key] const oldHtml = latencyLabel.innerHTML latencyLabel.innerHTML = "..." - testLatency(latencyLabel, redirects[frontend].normal).then(r => { + testLatency(latencyLabel, redirects[frontend].normal, frontend).then(r => { browser.storage.local.set({ [`${frontend}Latency`]: r }) latencyLabel.innerHTML = oldHtml processDefaultCustomInstances(name, frontend, "normal", document) diff --git a/src/pages/options/index.html b/src/pages/options/index.html index a868ad3..afad0bf 100644 --- a/src/pages/options/index.html +++ b/src/pages/options/index.html @@ -21,7 +21,7 @@
Instagram
TikTok
Reddit
-
Imgur
+
Imgur
Wikipedia
@@ -34,7 +34,7 @@ - +
@@ -94,6 +94,13 @@

+
+
+

Latency Threshold

+ + +
+

@@ -173,7 +180,7 @@
-
+
Imgur
@@ -254,7 +261,7 @@
- LBRY/Odysee + LBRY
diff --git a/src/pages/options/index.pug b/src/pages/options/index.pug index 553f918..3711fda 100644 --- a/src/pages/options/index.pug +++ b/src/pages/options/index.pug @@ -33,4 +33,4 @@ html#elementToShowWithJavaScript(lang="en") include ./widgets/sendTargets.pug include ./widgets/about.pug - script(type="module" src="./index.js") \ No newline at end of file + script(type="module" src="./index.js") diff --git a/src/pages/options/widgets/general.pug b/src/pages/options/widgets/general.pug index e04e9e2..e523d5b 100644 --- a/src/pages/options/widgets/general.pug +++ b/src/pages/options/widgets/general.pug @@ -31,6 +31,12 @@ section#general_page.option-block h4(data-localise="__MSG_autoRedirect__") input#auto-redirect(type="checkbox") + form + .some-block.option-block + h4(data-localise="__MSG_latencyThreshold") Latency Threshold + output#latency-output(for="latencyInput" name="latencyOutput") + input#latency-input(type="range" min="50" max="5000" value="1000" name="latencyInput" step="50") + .some-block.option-block h4(data-localise="__MSG_exceptions__") @@ -117,7 +123,7 @@ section#general_page.option-block div div - img(src="../../../assets/images/imgur-icon.png") + img(src="../../../assets/images/imgur.png") x(data-localise="__MSG_imgur__") Imgur input#imgur(type="checkbox") @@ -190,7 +196,7 @@ section#general_page.option-block div div img(src="../../../assets/images/lbry-icon.png") - x(data-localise="__MSG_lbry__") LBRY/Odysee + x(data-localise="__MSG_lbry__") LBRY input#lbry(type="checkbox") div diff --git a/src/pages/popup/popup.html b/src/pages/popup/popup.html index 7493ecb..25f70f4 100644 --- a/src/pages/popup/popup.html +++ b/src/pages/popup/popup.html @@ -28,7 +28,7 @@

TikTok

-
+ @@ -122,7 +122,7 @@

TikTok

-
+ diff --git a/src/pages/popup/popup.pug b/src/pages/popup/popup.pug index cd4d371..7da687c 100644 --- a/src/pages/popup/popup.pug +++ b/src/pages/popup/popup.pug @@ -33,7 +33,7 @@ mixin services .imgur.some-block a.title(href="https://imgur.com") - img(src="../../assets/images/imgur-icon.png") + img(src="../../assets/images/imgur.png") h4(data-localise="__MSG_imgur__") Imgur input.disable-imgur(type="checkbox")