From f586207c433ef3c80b46c684bcc145c570cb347c Mon Sep 17 00:00:00 2001 From: ManeraKai Date: Sun, 17 Sep 2023 22:49:27 +0300 Subject: Added cache for ping https://github.com/libredirect/browser_extension/issues/773 --- src/assets/javascripts/utils.js | 9 ++++ src/pages/options/index.js | 93 ++++++++++++++++++++++++++--------------- 2 files changed, 68 insertions(+), 34 deletions(-) diff --git a/src/assets/javascripts/utils.js b/src/assets/javascripts/utils.js index 6644f8ed..e85b1115 100644 --- a/src/assets/javascripts/utils.js +++ b/src/assets/javascripts/utils.js @@ -32,6 +32,14 @@ function getOptions() { ) } +function getPingCache() { + return new Promise(resolve => + browser.storage.local.get("pingCache", r => { + resolve(r.pingCache ?? {}) + }) + ) +} + function getBlacklist(options) { return new Promise(resolve => { let url @@ -148,5 +156,6 @@ export default { camelCase, getConfig, getOptions, + getPingCache, ping, } diff --git a/src/pages/options/index.js b/src/pages/options/index.js index 2861203a..1c2a9c90 100644 --- a/src/pages/options/index.js +++ b/src/pages/options/index.js @@ -182,21 +182,30 @@ async function loadPage(path) { async function calcCustomInstances(frontend) { let options = await utils.getOptions() let customInstances = options[frontend] + const pingCache = await utils.getPingCache() + document.getElementById(frontend).getElementsByClassName("custom-checklist")[0].innerHTML = customInstances .map( - x => ` -
- - ${x} - - -
-
` - ) + x => { + let time = pingCache[x] + let timeText = "" + if (time) { + const { color, text } = processTime(time) + timeText = `${text}` + } + return `
+ + ${x} + ${timeText} + + +
+
` + }) .join("\n") for (const item of customInstances) { document.getElementById(frontend).getElementsByClassName(`clear-${item}`)[0].addEventListener("click", async () => { @@ -243,6 +252,8 @@ async function processCustomInstances(frontend, document) { } async function createList(frontend, networks, document, redirects, blacklist) { + const pingCache = await utils.getPingCache() + const options = await utils.getOptions() for (const network in networks) { const checklist = document.getElementById(frontend) .getElementsByClassName(network)[0] @@ -261,7 +272,6 @@ async function createList(frontend, networks, document, redirects, blacklist) { .placeholder = redirects[frontend].clearnet[0] const sortedInstances = instances.sort((a, b) => blacklist.cloudflare.includes(a) && !blacklist.cloudflare.includes(b)) - const options = await utils.getOptions() const content = sortedInstances .map(x => { @@ -270,9 +280,16 @@ async function createList(frontend, networks, document, redirects, blacklist) { cloudflare ` : "" + let time = pingCache[x] + let timeText = "" + if (time) { + const { color, text } = processTime(time) + timeText = `${text}` + } + const chosen = options[frontend].includes(x) ? `chosen` : "" - const warnings = [cloudflare, chosen].join(" ") + const warnings = [cloudflare, timeText, chosen].join(" ") return `
${x} @@ -320,6 +337,7 @@ async function ping(frontend) { ...document.getElementById(frontend).getElementsByClassName('clearnet')[0].getElementsByTagName('x') ] + let pingCache = await utils.getPingCache() for (const element of instanceElements) { let span = element.getElementsByClassName('ping')[0] if (!span) span = document.createElement('span') @@ -328,25 +346,32 @@ async function ping(frontend) { element.appendChild(span) const href = element.getElementsByTagName('a')[0].href - let time = await utils.ping(href) - - let color - let text - - if (time < 5000) { - text = `${time}ms` - if (time <= 1000) color = "green" - else if (time <= 2000) color = "orange" - } - else if (time >= 5000) { - color = "red" - if (time == 5000) text = "5000ms+" - if (time > 5000) text = `Error: ${time - 5000}` - } - else { - color = "red" - text = 'Server not found' - } + const time = await utils.ping(href) + const { color, text } = processTime(time) span.innerHTML = `${text}` + pingCache[element.getElementsByTagName('a')[0].innerHTML] = time + browser.storage.local.set({ pingCache }) + } +} + +function processTime(time) { + let text + let color + if (time < 5000) { + text = `${time}ms` + if (time <= 1000) color = "green" + else if (time <= 2000) color = "orange" + } + else if (time >= 5000) { + color = "red" + if (time == 5000) text = "5000ms+" + if (time > 5000) text = `Error: ${time - 5000}` + } + else { + color = "red" + text = 'Server not found' + } + return { + color, text } } \ No newline at end of file -- cgit 1.4.1