From 6afbe6ac92c835eaeeb28190f07f9fa5092c9672 Mon Sep 17 00:00:00 2001
From: Kyryl <91323838+kirizdev@users.noreply.github.com>
Date: Mon, 13 Feb 2023 18:46:42 +0000
Subject: Refactor createList function
---
 src/pages/options/index.js | 125 +++++++++++++++++++++++----------------------
 1 file changed, 63 insertions(+), 62 deletions(-)
(limited to 'src/pages')
diff --git a/src/pages/options/index.js b/src/pages/options/index.js
index f122f3fc..3f37d829 100644
--- a/src/pages/options/index.js
+++ b/src/pages/options/index.js
@@ -181,68 +181,69 @@ async function processCustomInstances(frontend, document) {
 }
 
 function createList(frontend, networks, document, redirects, blacklist) {
-	for (const network in networks) {
-		if (redirects[frontend]) {
-			if (redirects[frontend][network].length > 0) {
-				document.getElementById(frontend).getElementsByClassName("custom-instance")[0].placeholder = redirects[frontend].clearnet[0]
-				document.getElementById(frontend)
-					.getElementsByClassName(network)[0]
-					.getElementsByClassName("checklist")[0]
-					.innerHTML = [
-						`
-						
${utils.camelCase(network)}
-					`,
-						...redirects[frontend][network]
-							.sort((a, b) =>
-								(blacklist.cloudflare.includes(a) && !blacklist.cloudflare.includes(b))
-							)
-							.map(x => {
-								const cloudflare = blacklist.cloudflare.includes(x) ?
-									` 
-							 		cloudflare
-								` : ""
-
-								const warnings = [cloudflare].join(" ")
-								return `
-										
-											${x}${warnings}
-										
-										
-						  			
 `
-							}),
-						'
'
-					].join("\n
\n")
-
-				for (const x of redirects[frontend][network]) {
-					document.getElementById(frontend)
-						.getElementsByClassName(network)[0]
-						.getElementsByClassName("checklist")[0]
-						.getElementsByClassName(`add-${x}`)[0]
-						.addEventListener("click", async () => {
-							let options = await utils.getOptions()
-							let customInstances = options[frontend]
-							if (!customInstances.includes(x)) {
-								customInstances.push(x)
-								options = await utils.getOptions()
-								options[frontend] = customInstances
-								browser.storage.local.set({ options }, () => {
-									calcCustomInstances(frontend)
-								})
-							}
-						})
-				}
-			}
-		} else {
-			document.getElementById(frontend).getElementsByClassName(network)[0].getElementsByClassName("checklist")[0].innerHTML =
-				`No instances found.
`
-			break
-		}
+    for (const network in networks) {
+        const checklist = document.getElementById(frontend)
+            .getElementsByClassName(network)[0]
+            .getElementsByClassName("checklist")[0]
 
-	}
+        if (!redirects[frontend]) {
+            checklist.innerHTML = 'No instances found.
'
+            break
+        }
+        const instances = redirects[frontend][network]
+        if (!instances || instances.length === 0) continue
+
+        document.getElementById(frontend)
+            .getElementsByClassName("custom-instance")[0]
+            .placeholder = redirects[frontend].clearnet[0]
+
+        const sortedInstances = instances
+            .sort((a, b) => (blacklist.cloudflare.includes(a) && !blacklist.cloudflare.includes(b)))
+
+        const content = sortedInstances
+            .map(x => {
+                const cloudflare = blacklist.cloudflare.includes(x) ?
+                    `
+                        cloudflare
+                    ` : ""
+
+                const warnings = [cloudflare].join(" ")
+                return `
+                            
+                                ${x}${warnings}
+                            
+                            
+                        
 `
+            })
+
+        checklist.innerHTML = [
+            `
+                
${utils.camelCase(network)}
+            `,
+            ...content,
+            "
"
+        ].join("\n
\n")
+
+        for (const instance of instances) {
+            checklist.getElementsByClassName(`add-${instance}`)[0]
+                .addEventListener("click", async () => {
+                    let options = await utils.getOptions()
+                    let customInstances = options[frontend]
+                    if (!customInstances.includes(instance)) {
+                        customInstances.push(instance)
+                        options = await utils.getOptions()
+                        options[frontend] = customInstances
+                        browser.storage.local.set({options}, () => {
+                            calcCustomInstances(frontend)
+                        })
+                    }
+                })
+        }
+    }
 }
 
 const r = window.location.href.match(/#(.*)/)
@@ -288,4 +289,4 @@ async function ping(frontend) {
 		span.innerHTML = `${text}`
 
 	}
-}
\ No newline at end of file
+}
-- 
cgit 1.4.1