about summary refs log tree commit diff stats
path: root/src/assets
diff options
context:
space:
mode:
authorYour Name <you@example.com>2024-03-12 16:27:18 +0100
committerYour Name <you@example.com>2024-03-12 16:27:18 +0100
commit1f8e0d00e13fcd5d26f7e58499c534509ecc44ae (patch)
treecde4a9b6004129a24a765fc46168c24f0a99fbbb /src/assets
parent2.8.2 => 2.8.3 (diff)
downloadlibredirect-1f8e0d00e13fcd5d26f7e58499c534509ecc44ae.zip
Move rewrite logic to own function
Diffstat (limited to 'src/assets')
-rw-r--r--src/assets/javascripts/services.js119
1 files changed, 65 insertions, 54 deletions
diff --git a/src/assets/javascripts/services.js b/src/assets/javascripts/services.js
index 1a9c3f29..b23295db 100644
--- a/src/assets/javascripts/services.js
+++ b/src/assets/javascripts/services.js
@@ -62,61 +62,13 @@ async function redirectAsync(url, type, initiator, forceRedirection) {
 }
 
 /**
- * @param {URL} url
- * @param {string} type
- * @param {URL} initiator
- * @param {boolean} forceRedirection
- * @returns {string | undefined}
+ * @param url
+ * @param frontend
+ * @param randomInstance
+ * @returns {undefined|string}
  */
-function redirect(url, type, initiator, forceRedirection, incognito) {
-	if (type != "main_frame" && type != "sub_frame" && type != "image") return
-	let randomInstance
-	let frontend
-	if (!forceRedirection && options.redirectOnlyInIncognito == true && !incognito) return
-	for (const service in config.services) {
-		if (!forceRedirection && !options[service].enabled) continue
-
-		frontend = options[service].frontend
-
-
-		if (config.services[service].frontends[frontend].desktopApp && type != "main_frame" && options[service].redirectType != "main_frame")
-			frontend = options[service].embedFrontend
-
-
-		if (!regexArray(service, url, config, frontend)) {
-			frontend = null
-			continue
-		}
-
-		if (
-			config.services[service].embeddable &&
-			type != options[service].redirectType && options[service].redirectType != "both"
-		) {
-			if (options[service].unsupportedUrls == 'block') return 'CANCEL'
-			return
-		}
-
-		let instanceList = options[frontend]
-		if (instanceList === undefined) break
-		if (instanceList.length === 0) return null
-
-		if (
-			initiator
-			&&
-			instanceList.includes(initiator.origin)
-		) {
-			if (type != "main_frame") return null
-			else return "BYPASSTAB"
-		}
-
-		randomInstance = utils.getRandomInstance(instanceList)
-		if (config.services[service].frontends[frontend].localhost && options[service].instance == "localhost") {
-			randomInstance = `http://${frontend}.localhost:8080`
-		}
-		break
-	}
-	if (!frontend) return
-
+function rewrite(url, frontend, randomInstance){
+	if (!frontend || !randomInstance) return
 	switch (frontend) {
 		case "hyperpipe": {
 			return `${randomInstance}${url.pathname}${url.search}`.replace(/\/search\?q=.*/, searchQuery => searchQuery.replace("?q=", "/"))
@@ -559,6 +511,65 @@ function redirect(url, type, initiator, forceRedirection, incognito) {
 
 /**
  * @param {URL} url
+ * @param {string} type
+ * @param {URL} initiator
+ * @param {boolean} forceRedirection
+ * @returns {string | undefined}
+ */
+function redirect(url, type, initiator, forceRedirection, incognito) {
+	if (type != "main_frame" && type != "sub_frame" && type != "image") return
+	let randomInstance
+	let frontend
+	if (!forceRedirection && options.redirectOnlyInIncognito == true && !incognito) return
+	for (const service in config.services) {
+		if (!forceRedirection && !options[service].enabled) continue
+
+		frontend = options[service].frontend
+
+
+		if (config.services[service].frontends[frontend].desktopApp && type != "main_frame" && options[service].redirectType != "main_frame")
+			frontend = options[service].embedFrontend
+
+
+		if (!regexArray(service, url, config, frontend)) {
+			frontend = null
+			continue
+		}
+
+		if (
+			config.services[service].embeddable &&
+			type != options[service].redirectType && options[service].redirectType != "both"
+		) {
+			if (options[service].unsupportedUrls == 'block') return 'CANCEL'
+			return
+		}
+
+		let instanceList = options[frontend]
+		if (instanceList === undefined) break
+		if (instanceList.length === 0) return null
+
+		if (
+			initiator
+			&&
+			instanceList.includes(initiator.origin)
+		) {
+			if (type != "main_frame") return null
+			else return "BYPASSTAB"
+		}
+
+		randomInstance = utils.getRandomInstance(instanceList)
+		if (config.services[service].frontends[frontend].localhost && options[service].instance == "localhost") {
+			randomInstance = `http://${frontend}.localhost:8080`
+		}
+		break
+	}
+	if (!frontend) return
+
+	return rewrite(url, frontend, randomInstance)
+}
+
+/**
+ * @param {URL} url
  * @param {*} returnFrontend
  */
 function computeService(url, returnFrontend) {