about summary refs log tree commit diff stats
path: root/src
diff options
context:
space:
mode:
authorManeraKai <manerakai@protonmail.com>2024-05-21 02:05:37 +0300
committerManeraKai <manerakai@protonmail.com>2024-05-21 02:05:37 +0300
commit462011d2c30b1e0b6d60295c85216545c41a4a22 (patch)
tree8332736d16662d5715a8620b3affc66d05fa9af3 /src
parentAdded LightTube https://github.com/libredirect/browser_extension/issues/913 (diff)
downloadlibredirect-462011d2c30b1e0b6d60295c85216545c41a4a22.zip
Cleaning code
Diffstat (limited to 'src')
-rw-r--r--src/assets/javascripts/services.js26
-rw-r--r--src/assets/javascripts/utils.js52
-rw-r--r--src/pages/background/background.js61
-rw-r--r--src/pages/options/index.js86
-rw-r--r--src/pages/options/init.js53
-rw-r--r--src/pages/options/widgets/general.js16
-rw-r--r--src/pages/popup/popup.js34
7 files changed, 118 insertions, 210 deletions
diff --git a/src/assets/javascripts/services.js b/src/assets/javascripts/services.js
index 567ffc84..6801d54b 100644
--- a/src/assets/javascripts/services.js
+++ b/src/assets/javascripts/services.js
@@ -792,25 +792,21 @@ function processUpdate() {
 
 /**
  * @param {URL} url
- * @param {boolean} test
  */
-async function copyRaw(url, test) {
+async function copyRaw(url) {
 	const newUrl = await reverse(url)
 	if (newUrl) {
-		if (!test) {
-			if (!isChrome) {
-				navigator.clipboard.writeText(newUrl)
-			} else {
-				var copyFrom = document.createElement("textarea");
-				copyFrom.textContent = newUrl;
-				document.body.appendChild(copyFrom);
-				copyFrom.select()
-				document.execCommand('copy')
-				copyFrom.blur();
-				document.body.removeChild(copyFrom);
-			}
+		if (!isChrome) {
+			navigator.clipboard.writeText(newUrl)
+		} else {
+			var copyFrom = document.createElement("textarea");
+			copyFrom.textContent = newUrl;
+			document.body.appendChild(copyFrom);
+			copyFrom.select()
+			document.execCommand('copy')
+			copyFrom.blur();
+			document.body.removeChild(copyFrom);
 		}
-		return newUrl
 	}
 }
 
diff --git a/src/assets/javascripts/utils.js b/src/assets/javascripts/utils.js
index af972d22..d28f9701 100644
--- a/src/assets/javascripts/utils.js
+++ b/src/assets/javascripts/utils.js
@@ -78,19 +78,11 @@ function getConfig() {
  * @returns {Promise<Object.<string, Option | string[]>>}
  */
 function getOptions() {
-	return new Promise(resolve =>
-		browser.storage.local.get("options", r => {
-			resolve(r.options)
-		})
-	)
+	return new Promise(resolve => browser.storage.local.get("options", r => resolve(r.options)))
 }
 
 function getPingCache() {
-	return new Promise(resolve =>
-		browser.storage.local.get("pingCache", r => {
-			resolve(r.pingCache ?? {})
-		})
-	)
+	return new Promise(resolve => browser.storage.local.get("pingCache", r => resolve(r.pingCache ?? {})))
 }
 
 function getBlacklist(options) {
@@ -98,26 +90,15 @@ function getBlacklist(options) {
 		let url
 		if (options.fetchInstances == 'github') url = 'https://raw.githubusercontent.com/libredirect/instances/main/blacklist.json'
 		else if (options.fetchInstances == 'codeberg') url = 'https://codeberg.org/LibRedirect/instances/raw/branch/main/blacklist.json'
-		else {
-			resolve('disabled')
-			return
-		}
+		else return resolve('disabled')
 		const http = new XMLHttpRequest()
 		http.open("GET", url, true)
 		http.onreadystatechange = () => {
-			if (http.status === 200 && http.readyState == XMLHttpRequest.DONE) {
+			if (http.status === 200 && http.readyState == XMLHttpRequest.DONE)
 				resolve(JSON.parse(http.responseText))
-				return
-			}
-		}
-		http.onerror = () => {
-			resolve()
-			return
-		}
-		http.ontimeout = () => {
-			resolve()
-			return
 		}
+		http.onerror = () => resolve()
+		http.ontimeout = () => resolve()
 		http.send(null)
 	})
 }
@@ -127,26 +108,15 @@ function getList(options) {
 		let url
 		if (options.fetchInstances == 'github') url = 'https://raw.githubusercontent.com/libredirect/instances/main/data.json'
 		else if (options.fetchInstances == 'codeberg') url = 'https://codeberg.org/LibRedirect/instances/raw/branch/main/data.json'
-		else {
-			resolve('disabled')
-			return
-		}
+		else return resolve('disabled')
 		const http = new XMLHttpRequest()
 		http.open("GET", url, true)
 		http.onreadystatechange = () => {
-			if (http.status === 200 && http.readyState == XMLHttpRequest.DONE) {
-				resolve(JSON.parse(http.responseText))
-				return
-			}
-		}
-		http.onerror = () => {
-			resolve()
-			return
-		}
-		http.ontimeout = () => {
-			resolve()
-			return
+			if (http.status === 200 && http.readyState == XMLHttpRequest.DONE)
+				return resolve(JSON.parse(http.responseText))
 		}
+		http.onerror = () => resolve()
+		http.ontimeout = () => resolve()
 		http.send(null)
 	})
 }
diff --git a/src/pages/background/background.js b/src/pages/background/background.js
index 0754f8a5..0c4d0923 100644
--- a/src/pages/background/background.js
+++ b/src/pages/background/background.js
@@ -92,15 +92,15 @@ browser.commands.onCommand.addListener(async command => {
 	browser.tabs.query({ active: true, currentWindow: true }, async tabs => {
 		const url = new URL(tabs[0].url)
 		switch (command) {
-			case "switchInstance":
+			case "switchInstance": {
 				const newUrl = await servicesHelper.switchInstance(url)
 				if (newUrl) browser.tabs.update({ url: newUrl })
 				break
-			case "copyRaw": {
+			}
+			case "copyRaw":
 				servicesHelper.copyRaw(url)
 				break
-			}
-			case "redirect": {
+			case "redirect":
 				browser.tabs.query({ active: true, currentWindow: true }, async tabs => {
 					if (tabs[0].url) {
 						const url = new URL(tabs[0].url)
@@ -113,8 +113,7 @@ browser.commands.onCommand.addListener(async command => {
 					}
 				})
 				break
-			}
-			case "reverse": {
+			case "reverse":
 				browser.tabs.query({ active: true, currentWindow: true }, async tabs => {
 					if (tabs[0].url) {
 						const url = new URL(tabs[0].url)
@@ -127,7 +126,6 @@ browser.commands.onCommand.addListener(async command => {
 					}
 				})
 				break
-			}
 		}
 	})
 })
@@ -164,11 +162,10 @@ browser.contextMenus.onClicked.addListener(async (info) => {
 			if (newUrl) browser.tabs.update({ url: newUrl })
 			return
 		}
-		case 'settingsTab': {
+		case 'settingsTab':
 			browser.runtime.openOptionsPage()
 			return
-		}
-		case 'copyReverseTab': {
+		case 'copyReverseTab':
 			browser.tabs.query({ active: true, currentWindow: true }, async tabs => {
 				if (tabs[0].url) {
 					const url = new URL(tabs[0].url)
@@ -176,8 +173,7 @@ browser.contextMenus.onClicked.addListener(async (info) => {
 				}
 			})
 			return
-		}
-		case 'reverseTab': {
+		case 'reverseTab':
 			browser.tabs.query({ active: true, currentWindow: true }, async tabs => {
 				if (tabs[0].url) {
 					const url = new URL(tabs[0].url)
@@ -190,8 +186,7 @@ browser.contextMenus.onClicked.addListener(async (info) => {
 				}
 			})
 			return
-		}
-		case 'redirectTab': {
+		case 'redirectTab':
 			browser.tabs.query({ active: true, currentWindow: true }, async tabs => {
 				if (tabs[0].url) {
 					const url = new URL(tabs[0].url)
@@ -204,8 +199,6 @@ browser.contextMenus.onClicked.addListener(async (info) => {
 				}
 			})
 			return
-		}
-
 		case 'copyReverseLink': {
 			const url = new URL(info.linkUrl)
 			await servicesHelper.copyRaw(url)
@@ -238,7 +231,6 @@ browser.contextMenus.onClicked.addListener(async (info) => {
 			}
 			return
 		}
-
 		case 'bypassLink':
 		case 'bypassLinkInNewTab': {
 			const url = new URL(info.linkUrl)
@@ -253,17 +245,14 @@ browser.contextMenus.onClicked.addListener(async (info) => {
 			}
 			return
 		}
-
-		case 'copyReverseBookmark': {
+		case 'copyReverseBookmark':
 			browser.bookmarks.get(info.bookmarkId, bookmarks => {
 				const url = new URL(bookmarks[0].url)
 				servicesHelper.copyRaw(url)
 			});
 			return
-		}
-
 		case 'redirectBookmark':
-		case 'redirectBookmarkInNewTab': {
+		case 'redirectBookmarkInNewTab':
 			browser.bookmarks.get(info.bookmarkId, bookmarks => {
 				const url = new URL(bookmarks[0].url)
 				const newUrl = servicesHelper.redirect(url, "main_frame", null, true)
@@ -273,9 +262,8 @@ browser.contextMenus.onClicked.addListener(async (info) => {
 				}
 			})
 			return
-		}
 		case 'reverseBookmark':
-		case 'reverseBookmarkInNewTab': {
+		case 'reverseBookmarkInNewTab':
 			browser.bookmarks.get(info.bookmarkId, async bookmarks => {
 				const url = new URL(bookmarks[0].url)
 				const newUrl = await servicesHelper.reverse(url)
@@ -292,24 +280,17 @@ browser.contextMenus.onClicked.addListener(async (info) => {
 				}
 			})
 			return
-		}
-
 		case 'bypassBookmark':
-		case 'bypassBookmarkInNewTab': {
+		case 'bypassBookmarkInNewTab':
 			browser.bookmarks.get(info.bookmarkId, async bookmarks => {
 				const url = new URL(bookmarks[0].url)
 				if (info.menuItemId == "bypassBookmark") {
-					browser.tabs.update({ url: url.href }, tab => {
-						tabIdRedirects[tab.id] = false
-					})
+					browser.tabs.update({ url: url.href }, tab => tabIdRedirects[tab.id] = false)
 				} else {
-					browser.tabs.create({ url: url.href }, tab => {
-						tabIdRedirects[tab.id] = false
-					})
+					browser.tabs.create({ url: url.href }, tab => tabIdRedirects[tab.id] = false)
 				}
 				return
 			})
-		}
 	}
 })
 
@@ -319,11 +300,7 @@ browser.runtime.onMessage.addListener((request, sender, sendResponse) => {
 			if (tabs[0].url) {
 				const url = new URL(tabs[0].url)
 				const newUrl = await servicesHelper.reverse(url)
-				if (newUrl) {
-					browser.tabs.update(tabs[0].id, { url: newUrl }, () => {
-						tabIdRedirects[tabs[0].id] = false
-					})
-				}
+				if (newUrl) browser.tabs.update(tabs[0].id, { url: newUrl }, () => tabIdRedirects[tabs[0].id] = false)
 			}
 		})
 	}
@@ -332,11 +309,7 @@ browser.runtime.onMessage.addListener((request, sender, sendResponse) => {
 			if (tabs[0].url) {
 				const url = new URL(tabs[0].url)
 				const newUrl = servicesHelper.redirect(url, "main_frame", null, true)
-				if (newUrl) {
-					browser.tabs.update(tabs[0].id, { url: newUrl }, () => {
-						tabIdRedirects[tabs[0].id] = true
-					})
-				}
+				if (newUrl) browser.tabs.update(tabs[0].id, { url: newUrl }, () => tabIdRedirects[tabs[0].id] = true)
 			}
 		})
 	}
diff --git a/src/pages/options/index.js b/src/pages/options/index.js
index 9274023f..0066df0d 100644
--- a/src/pages/options/index.js
+++ b/src/pages/options/index.js
@@ -129,9 +129,7 @@ async function loadPage(path) {
 
 	if (path != 'general') {
 		const service = path;
-
 		divs[service] = {}
-
 		for (const option in config.services[service].options) {
 			divs[service][option] = document.getElementById(`${service}-${option}`)
 			if (typeof config.services[service].options[option] == "boolean") divs[service][option].checked = options[service][option]
@@ -146,46 +144,39 @@ async function loadPage(path) {
 				changeFrontendsSettings(service)
 			})
 		}
-
 		changeFrontendsSettings(service)
 
-
-
-
-		!async function () {
-			blacklist = await utils.getBlacklist(options)
-			redirects = await utils.getList(options)
-
-			for (const frontend in config.services[service].frontends) {
-				if (config.services[service].frontends[frontend].instanceList) {
-					if (redirects == 'disabled' || blacklist == 'disabled') {
-						document.getElementById(frontend).getElementsByClassName('clearnet')[0].style.display = 'none'
-						document.getElementById(frontend).getElementsByClassName('ping')[0].style.display = 'none'
-					}
-					else if (!redirects || !blacklist) {
-						document.getElementById(frontend)
-							.getElementsByClassName('clearnet')[0]
-							.getElementsByClassName("checklist")[0]
-							.getElementsByClassName('loading')[0]
-							.innerHTML = 'Could not fetch instances.'
-					}
-					else {
-						createList(frontend)
-					}
+		blacklist = await utils.getBlacklist(options)
+		redirects = await utils.getList(options)
+		for (const frontend in config.services[service].frontends) {
+			if (config.services[service].frontends[frontend].instanceList) {
+				if (redirects == 'disabled' || blacklist == 'disabled') {
+					document.getElementById(frontend).getElementsByClassName('clearnet')[0].style.display = 'none'
+					document.getElementById(frontend).getElementsByClassName('ping')[0].style.display = 'none'
+				}
+				else if (!redirects || !blacklist) {
+					document.getElementById(frontend)
+						.getElementsByClassName('clearnet')[0]
+						.getElementsByClassName("checklist")[0]
+						.getElementsByClassName('loading')[0]
+						.innerHTML = 'Could not fetch instances.'
+				}
+				else {
+					createList(frontend)
 				}
 			}
+		}
 
-			for (const frontend in config.services[service].frontends) {
-				if (config.services[service].frontends[frontend].instanceList) {
-					processCustomInstances(frontend)
-					document.getElementById(`ping-${frontend}`).addEventListener("click", async () => {
-						document.getElementById(`ping-${frontend}`).getElementsByTagName('x')[0].innerHTML = "Pinging..."
-						await ping(frontend)
-						document.getElementById(`ping-${frontend}`).getElementsByTagName('x')[0].innerHTML = "Ping instances"
-					})
-				}
+		for (const frontend in config.services[service].frontends) {
+			if (config.services[service].frontends[frontend].instanceList) {
+				processCustomInstances(frontend)
+				document.getElementById(`ping-${frontend}`).addEventListener("click", async () => {
+					document.getElementById(`ping-${frontend}`).getElementsByTagName('x')[0].innerHTML = "Pinging..."
+					await ping(frontend)
+					document.getElementById(`ping-${frontend}`).getElementsByTagName('x')[0].innerHTML = "Ping instances"
+				})
 			}
-		}()
+		}
 	}
 }
 
@@ -197,11 +188,10 @@ async function calcCustomInstances(frontend) {
 	document.getElementById(frontend).getElementsByClassName("custom-checklist")[0].innerHTML = customInstances
 		.map(
 			x => {
-				let time = pingCache[x]
-				let timeText = ""
+				const time = pingCache[x]
 				if (time) {
 					const { color, text } = processTime(time)
-					timeText = `<span class="ping" style="color:${color};">${text}</span>`
+					var timeText = `<span class="ping" style="color:${color};">${text}</span>`
 				}
 				const custom = isCustomInstance(frontend, x) ? "" : `<span>custom</span>`
 				return `<div>
@@ -226,8 +216,6 @@ async function calcCustomInstances(frontend) {
 			options = await utils.getOptions()
 			options[frontend] = customInstances
 			browser.storage.local.set({ options }, async () => {
-				blacklist = await utils.getBlacklist(options)
-				redirects = await utils.getList(options)
 				calcCustomInstances(frontend)
 				createList(frontend)
 			})
@@ -242,9 +230,8 @@ async function processCustomInstances(frontend) {
 		let options = await utils.getOptions()
 		let customInstances = options[frontend]
 		let frontendCustomInstanceInput = document.getElementById(frontend).getElementsByClassName("custom-instance")[0]
-		let url
 		try {
-			url = new URL(frontendCustomInstanceInput.value)
+			var url = new URL(frontendCustomInstanceInput.value)
 		} catch (error) {
 			return
 		}
@@ -270,9 +257,7 @@ async function createList(frontend) {
 	const pingCache = await utils.getPingCache()
 	const options = await utils.getOptions()
 	for (const network in config.networks) {
-		const checklist = document.getElementById(frontend)
-			.getElementsByClassName(network)[0]
-			.getElementsByClassName("checklist")[0]
+		const checklist = document.getElementById(frontend).getElementsByClassName(network)[0].getElementsByClassName("checklist")[0]
 
 		if (!redirects[frontend]) {
 			checklist.innerHTML = '<div class="block block-option">No instances found.</div>'
@@ -282,13 +267,10 @@ async function createList(frontend) {
 		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))
+		document.getElementById(frontend).getElementsByClassName("custom-instance")[0].placeholder = redirects[frontend].clearnet[0]
 
-		const content = sortedInstances
+		instances.sort((a, b) => blacklist.cloudflare.includes(a) && !blacklist.cloudflare.includes(b))
+		const content = instances
 			.map(x => {
 				const cloudflare = blacklist.cloudflare.includes(x) ?
 					`<a target="_blank" href="https://libredirect.github.io/docs.html#instances">
diff --git a/src/pages/options/init.js b/src/pages/options/init.js
index f88c9ef9..baf3eebe 100644
--- a/src/pages/options/init.js
+++ b/src/pages/options/init.js
@@ -8,40 +8,37 @@ if (!(await utils.getOptions())) {
 	await servicesHelper.initDefaults()
 }
 
-function changeTheme() {
-	return new Promise(async resolve => {
-		switch ((await utils.getOptions()).theme) {
-			case "dark":
-				document.body.classList.add("dark-theme")
-				document.body.classList.remove("light-theme")
-				for (const element of document.body.getElementsByClassName('dark')) {
-					element.style.display = 'none';
-				}
-				break
-			case "light":
+async function changeTheme() {
+	switch ((await utils.getOptions()).theme) {
+		case "dark":
+			document.body.classList.add("dark-theme")
+			document.body.classList.remove("light-theme")
+			for (const element of document.body.getElementsByClassName('dark')) {
+				element.style.display = 'none';
+			}
+			break
+		case "light":
+			document.body.classList.add("light-theme")
+			document.body.classList.remove("dark-theme")
+			for (const element of document.body.getElementsByClassName('light')) {
+				element.style.display = 'none';
+			}
+			break
+		default:
+			if (matchMedia("(prefers-color-scheme: light)").matches) {
 				document.body.classList.add("light-theme")
 				document.body.classList.remove("dark-theme")
 				for (const element of document.body.getElementsByClassName('light')) {
 					element.style.display = 'none';
 				}
-				break
-			default:
-				if (matchMedia("(prefers-color-scheme: light)").matches) {
-					document.body.classList.add("light-theme")
-					document.body.classList.remove("dark-theme")
-					for (const element of document.body.getElementsByClassName('light')) {
-						element.style.display = 'none';
-					}
-				} else {
-					document.body.classList.add("dark-theme")
-					document.body.classList.remove("light-theme")
-					for (const element of document.body.getElementsByClassName('dark')) {
-						element.style.display = 'none';
-					}
+			} else {
+				document.body.classList.add("dark-theme")
+				document.body.classList.remove("light-theme")
+				for (const element of document.body.getElementsByClassName('dark')) {
+					element.style.display = 'none';
 				}
-		}
-		resolve()
-	})
+			}
+	}
 }
 
 changeTheme()
diff --git a/src/pages/options/widgets/general.js b/src/pages/options/widgets/general.js
index 64172287..30a8a0c8 100644
--- a/src/pages/options/widgets/general.js
+++ b/src/pages/options/widgets/general.js
@@ -8,12 +8,16 @@ const isChrome = browser.runtime.getBrowserInfo === undefined
 
 async function setOption(option, type, event) {
 	let options = await utils.getOptions()
-	if (type == "select") {
-		options[option] = event.target.options[event.target.options.selectedIndex].value
-	} else if (type == "checkbox") {
-		options[option] = event.target.checked
-	} else if (type == "range") {
-		options[option] = event.target.value
+	switch (type) {
+		case "select":
+			options[option] = event.target.options[event.target.options.selectedIndex].value
+			break;
+		case "checkbox":
+			options[option] = event.target.checked
+			break;
+		case "range":
+			options[option] = event.target.value
+			break;
 	}
 	browser.storage.local.set({ options })
 }
diff --git a/src/pages/popup/popup.js b/src/pages/popup/popup.js
index cfb7be27..485ec0e0 100644
--- a/src/pages/popup/popup.js
+++ b/src/pages/popup/popup.js
@@ -11,13 +11,10 @@ await browser.runtime.getPlatformInfo(r => {
 	switch (r.os) {
 		case "fuchsia":
 		case "ios":
-		case "android": {
+		case "android":
 			document.getElementsByTagName("html")[0].classList.add("mobile")
-		}
 	}
-}
-)
-
+})
 
 const allSites = document.getElementById("all_sites")
 const currSite = document.getElementById("current_site")
@@ -66,12 +63,10 @@ for (const service in config.services) {
 }
 
 browser.tabs.query({ active: true, currentWindow: true }, async tabs => {
-	let url;
-
 	// Set visibility of control buttons
 	if (tabs[0].url) {
 		const hr = document.getElementById("hr")
-		url = new URL(tabs[0].url)
+		var url = new URL(tabs[0].url)
 		servicesHelper.switchInstance(url).then(r => {
 			if (r) {
 				document.getElementById("change_instance_div").style.display = ""
@@ -81,31 +76,22 @@ browser.tabs.query({ active: true, currentWindow: true }, async tabs => {
 				)
 			}
 		})
-		servicesHelper.copyRaw(url, true).then(r => {
-			if (r) {
-				document.getElementById("copy_original_div").style.display = ""
-				hr.style.display = ""
-				document.getElementById("copy_original").addEventListener("click", () =>
-					servicesHelper.copyRaw(url)
-				)
-			}
-		})
 		servicesHelper.reverse(url).then(r => {
 			if (r) {
-				document.getElementById("redirect_to_original_div").style.display = ""
 				hr.style.display = ""
-				document.getElementById("redirect_to_original").addEventListener("click", () =>
-					browser.runtime.sendMessage("reverseTab")
-				)
+
+				document.getElementById("copy_original_div").style.display = ""
+				document.getElementById("copy_original").addEventListener("click", () => servicesHelper.copyRaw(url))
+
+				document.getElementById("redirect_to_original_div").style.display = ""
+				document.getElementById("redirect_to_original").addEventListener("click", () => browser.runtime.sendMessage("reverseTab"))
 			}
 		})
 		servicesHelper.redirectAsync(url, "main_frame", null, true).then(r => {
 			if (r) {
 				document.getElementById("redirect_div").style.display = ""
 				hr.style.display = ""
-				document.getElementById("redirect").addEventListener("click", () =>
-					browser.runtime.sendMessage("redirectTab")
-				)
+				document.getElementById("redirect").addEventListener("click", () => browser.runtime.sendMessage("redirectTab"))
 			}
 		})
 	}