about summary refs log tree commit diff stats
path: root/src/pages
diff options
context:
space:
mode:
authorManeraKai <manerakai@protonmail.com>2023-01-22 20:44:36 +0300
committerManeraKai <manerakai@protonmail.com>2023-01-22 20:44:40 +0300
commit610d2da76111154cf61011bb92acb0b5b56d5a40 (patch)
tree82a2cdf6c313a56778b41a8b610a21b369dee6c6 /src/pages
parentAdded plus button next to public instances (diff)
downloadlibredirect-610d2da76111154cf61011bb92acb0b5b56d5a40.zip
Fixed bugs
Diffstat (limited to 'src/pages')
-rw-r--r--src/pages/background/background.js101
-rw-r--r--src/pages/options/widgets/general.js117
-rw-r--r--src/pages/popup/popup.js6
3 files changed, 104 insertions, 120 deletions
diff --git a/src/pages/background/background.js b/src/pages/background/background.js
index 5c6ec521..dbc788f2 100644
--- a/src/pages/background/background.js
+++ b/src/pages/background/background.js
@@ -18,8 +18,8 @@ browser.runtime.onInstalled.addListener(async details => {
 			await servicesHelper.upgradeOptions()
 			// await servicesHelper.processUpdate()
 		}
+		browser.runtime.openOptionsPage()
 	}
-	browser.runtime.openOptionsPage()
 })
 
 let tabIdRedirects = {}
@@ -41,7 +41,12 @@ browser.webRequest.onBeforeRequest.addListener(
 
 		if (details.frameAncestors && details.frameAncestors.length > 0 && generalHelper.isException(new URL(details.frameAncestors[0].url))) newUrl = null
 
-		if (generalHelper.isException(url)) newUrl = "BYPASSTAB"
+		if (generalHelper.isException(url)) {
+			if (details.type == "main_frame")
+				newUrl = "BYPASSTAB"
+			else
+				return null
+		}
 
 		if (newUrl) {
 			if (newUrl === "CANCEL") {
@@ -69,8 +74,11 @@ browser.tabs.onRemoved.addListener(tabId => {
 	}
 })
 
-browser.commands.onCommand.addListener(command => {
-	if (command === "switchInstance") servicesHelper.switchInstance()
+browser.commands.onCommand.addListener(async command => {
+	if (command === "switchInstance") {
+		const newUrl = await servicesHelper.switchInstance()
+		if (newUrl) browser.tabs.update({ url: newUrl })
+	}
 	else if (command == "copyRaw") servicesHelper.copyRaw()
 })
 
@@ -93,71 +101,42 @@ browser.contextMenus.create({
 })
 
 browser.contextMenus.create({
+	id: "redirectToOriginal",
+	title: 'Redirect to original',
+	contexts: ["browser_action"],
+})
+
+browser.contextMenus.create({
 	id: "redirectLink",
 	title: browser.i18n.getMessage("redirectLink"),
 	contexts: ["link"],
 })
 
-function handleToggleTab(tab) {
-	return new Promise(async resolve => {
-		switch (tabIdRedirects[tab.id]) {
-			case false:
-				const newUrl = await servicesHelper.reverse(tab.url, true)
-				if (newUrl) browser.tabs.update(tab.id, { url: newUrl })
-				resolve()
-				return
-			case true:
-				browser.tabs.reload(tab.id)
-				resolve()
-				return
-		}
-	})
-}
-
 browser.contextMenus.onClicked.addListener((info, tab) => {
 	return new Promise(async resolve => {
-		switch (info.menuItemId) {
-			case "switchInstance":
-				servicesHelper.switchInstance()
-				resolve()
-				return
-			case "settings":
-				browser.runtime.openOptionsPage()
-				resolve()
-				return
-			case "copyRaw":
-				servicesHelper.copyRaw()
-				resolve()
-				return
-			case "toggleTab":
-				if (tabIdRedirects[tab.id] != undefined) {
-					tabIdRedirects[tab.id] = !tabIdRedirects[tab.id]
-					await handleToggleTab(tab)
-					resolve()
-					return
-				} else {
-					const url = new URL(tab.url)
-					const service = await servicesHelper.computeService(url)
-					if (service) {
-						if ((await utils.getOptions())[service].enabled) tabIdRedirects[tab.id] = false
-						else tabIdRedirects[tab.id] = true
-						await handleToggleTab(tab)
-						resolve()
-						return
-					} else {
-						tabIdRedirects[tab.id] = false
-						await handleToggleTab(tab)
-						resolve()
-						return
-					}
-				}
-			case "redirectLink":
-				const tmpUrl = new URL(info.linkUrl)
-				const newUrl = servicesHelper.redirect(tmpUrl, "main_frame", null, true)
-				if (newUrl) browser.tabs.create({ url: newUrl })
-				resolve()
-				return
+		if (info.menuItemId == 'switchInstance') {
+			servicesHelper.switchInstance()
+		}
+		else if (info.menuItemId == 'settings') {
+			browser.runtime.openOptionsPage()
+		}
+		else if (info.menuItemId == 'copyRaw') {
+			servicesHelper.copyRaw()
+		}
+		else if (info.menuItemId == 'redirectToOriginal') {
+			const newUrl = await servicesHelper.reverse(tab.url)
+			if (newUrl) {
+				tabIdRedirects[tab.id] = false
+				browser.tabs.update(tab.id, { url: newUrl })
+			}
+		}
+		else if (info.menuItemId == 'redirectLink') {
+			const url = new URL(info.linkUrl)
+			const newUrl = servicesHelper.redirect(url, "main_frame", null, true)
+			if (newUrl) browser.tabs.create({ url: newUrl })
 		}
+		resolve()
+		return
 	})
 })
 
diff --git a/src/pages/options/widgets/general.js b/src/pages/options/widgets/general.js
index e89ce173..09c222a2 100644
--- a/src/pages/options/widgets/general.js
+++ b/src/pages/options/widgets/general.js
@@ -95,25 +95,27 @@ for (const service in config.services) {
 	})
 }
 
-!async function () {
-	const options = await utils.getOptions()
-	themeElement.value = options.theme
-
-	instanceTypeElement.addEventListener("change", event => {
-		instanceType = event.target.options[instanceTypeElement.selectedIndex].value
-		if (instanceType == "url") {
-			nameCustomInstanceInput.setAttribute("type", "url")
-			nameCustomInstanceInput.setAttribute("placeholder", "https://www.google.com")
-		} else if (instanceType == "regex") {
-			nameCustomInstanceInput.setAttribute("type", "text")
-			nameCustomInstanceInput.setAttribute("placeholder", "https?://(www.|)youtube.com/")
-		}
-	})
-	let exceptionsCustomInstances = options.exceptions
-	function calcExceptionsCustomInstances() {
-		document.getElementById("exceptions-custom-checklist").innerHTML = [...exceptionsCustomInstances.url, ...exceptionsCustomInstances.regex]
-			.map(
-				x => `<div>
+
+let options = await utils.getOptions()
+themeElement.value = options.theme
+for (const service in config.services) document.getElementById(service).checked = options.popupServices.includes(service)
+
+instanceTypeElement.addEventListener("change", event => {
+	instanceType = event.target.options[instanceTypeElement.selectedIndex].value
+	if (instanceType == "url") {
+		nameCustomInstanceInput.setAttribute("type", "url")
+		nameCustomInstanceInput.setAttribute("placeholder", "https://www.google.com")
+	} else if (instanceType == "regex") {
+		nameCustomInstanceInput.setAttribute("type", "text")
+		nameCustomInstanceInput.setAttribute("placeholder", "https?://(www.|)youtube.com/")
+	}
+})
+
+let exceptionsCustomInstances = options.exceptions
+function calcExceptionsCustomInstances() {
+	document.getElementById("exceptions-custom-checklist").innerHTML = [...exceptionsCustomInstances.url, ...exceptionsCustomInstances.regex]
+		.map(
+			x => `<div>
                       ${x}
                       <button class="add" id="clear-${x}">
                         <svg xmlns="https://www.w3.org/2000/svg" height="20px" viewBox="0 0 24 24" width="20px"
@@ -123,46 +125,47 @@ for (const service in config.services) {
                       </button>
                     </div>
                     <hr>`
-			)
-			.join("\n")
-
-		for (const x of [...exceptionsCustomInstances.url, ...exceptionsCustomInstances.regex]) {
-			document.getElementById(`clear-${x}`).addEventListener("click", () => {
-				let index
-				index = exceptionsCustomInstances.url.indexOf(x)
-				if (index > -1) exceptionsCustomInstances.url.splice(index, 1)
-				else {
-					index = exceptionsCustomInstances.regex.indexOf(x)
-					if (index > -1) exceptionsCustomInstances.regex.splice(index, 1)
-				}
-				options.exceptions = exceptionsCustomInstances
-				browser.storage.local.set({ options })
-				calcExceptionsCustomInstances()
-			})
-		}
-	}
-	calcExceptionsCustomInstances()
-	document.getElementById("custom-exceptions-instance-form").addEventListener("submit", event => {
-		event.preventDefault()
-
-		let val
-		if (instanceType == "url") {
-			if (nameCustomInstanceInput.validity.valid) {
-				let url = new URL(nameCustomInstanceInput.value)
-				val = `${url.protocol}//${url.host}`
-				if (!exceptionsCustomInstances.url.includes(val)) exceptionsCustomInstances.url.push(val)
+		)
+		.join("\n")
+
+	for (const x of [...exceptionsCustomInstances.url, ...exceptionsCustomInstances.regex]) {
+		document.getElementById(`clear-${x}`).addEventListener("click", async () => {
+			let index
+			index = exceptionsCustomInstances.url.indexOf(x)
+			if (index > -1) exceptionsCustomInstances.url.splice(index, 1)
+			else {
+				index = exceptionsCustomInstances.regex.indexOf(x)
+				if (index > -1) exceptionsCustomInstances.regex.splice(index, 1)
 			}
-		} else if (instanceType == "regex") {
-			val = nameCustomInstanceInput.value
-			if (val.trim() != "" && !exceptionsCustomInstances.regex.includes(val)) exceptionsCustomInstances.regex.push(val)
-		}
-		if (val) {
+			options = await utils.getOptions()
 			options.exceptions = exceptionsCustomInstances
 			browser.storage.local.set({ options })
-			nameCustomInstanceInput.value = ""
+			calcExceptionsCustomInstances()
+		})
+	}
+}
+calcExceptionsCustomInstances()
+document.getElementById("custom-exceptions-instance-form").addEventListener("submit", async event => {
+	event.preventDefault()
+
+	let val
+	if (instanceType == "url") {
+		if (nameCustomInstanceInput.validity.valid) {
+			val = nameCustomInstanceInput.value
+			if (!exceptionsCustomInstances.url.includes(val)) exceptionsCustomInstances.url.push(val)
 		}
-		calcExceptionsCustomInstances()
-	})
+	} else if (instanceType == "regex") {
+		val = nameCustomInstanceInput.value
+		if (val.trim() != "" && !exceptionsCustomInstances.regex.includes(val)) exceptionsCustomInstances.regex.push(val)
+	}
+	if (val) {
+		options = await utils.getOptions()
+		options.exceptions = exceptionsCustomInstances
+		console.log(options.exceptions)
+		browser.storage.local.set({ options }, () =>
+			nameCustomInstanceInput.value = ""
+		)
 
-	for (const service in config.services) document.getElementById(service).checked = options.popupServices.includes(service)
-}
\ No newline at end of file
+	}
+	calcExceptionsCustomInstances()
+})
diff --git a/src/pages/popup/popup.js b/src/pages/popup/popup.js
index 4cd59ebe..56c0c3cc 100644
--- a/src/pages/popup/popup.js
+++ b/src/pages/popup/popup.js
@@ -9,9 +9,11 @@ let config,
 
 config = await utils.getConfig()
 
-servicesHelper.switchInstance(true).then(r => {
+servicesHelper.switchInstance().then(r => {
 	if (!r) document.getElementById("change_instance_div").style.display = "none"
-	else document.getElementById("change_instance").addEventListener("click", () => servicesHelper.switchInstance(false))
+	else document.getElementById("change_instance").addEventListener("click", async () => {
+		browser.tabs.update({ url: await servicesHelper.switchInstance() })
+	})
 })
 
 servicesHelper.copyRaw(true).then(r => {