about summary refs log tree commit diff stats
path: root/src/pages/background
diff options
context:
space:
mode:
Diffstat (limited to 'src/pages/background')
-rw-r--r--src/pages/background/background.js82
-rw-r--r--src/pages/background/reset_warning.html59
-rw-r--r--src/pages/background/reset_warning.js7
3 files changed, 59 insertions, 89 deletions
diff --git a/src/pages/background/background.js b/src/pages/background/background.js
index f53d7b97..54a80011 100644
--- a/src/pages/background/background.js
+++ b/src/pages/background/background.js
@@ -20,6 +20,7 @@ function initDefaults() {
 }
 
 browser.runtime.onInstalled.addListener(details => {
+	if (details.previousVersion != browser.runtime.getManifest().version) {
 	switch (details.reason) {
 		case "install":
 			initDefaults()
@@ -42,9 +43,11 @@ browser.runtime.onInstalled.addListener(details => {
 					})
 				})
 	}
+	}
 })
 
-let BYPASSTABs = []
+let tabIdRedirects = {}
+// true == Always redirect, false == Never redirect, null/undefined == follow options for services
 browser.webRequest.onBeforeRequest.addListener(
 	details => {
 		const url = new URL(details.url)
@@ -57,23 +60,23 @@ browser.webRequest.onBeforeRequest.addListener(
 			return null
 		}
 
-		let newUrl = servicesHelper.redirect(url, details.type, initiator)
+		if (tabIdRedirects[details.tabId] == false) return null
+		let newUrl = servicesHelper.redirect(url, details.type, initiator, tabIdRedirects[details.tabId])
 
 		if (details.frameAncestors && details.frameAncestors.length > 0 && generalHelper.isException(new URL(details.frameAncestors[0].url))) newUrl = null
 
 		if (generalHelper.isException(url)) newUrl = "BYPASSTAB"
-		if (BYPASSTABs.includes(details.tabId)) newUrl = null
 
 		if (newUrl) {
 			if (newUrl === "CANCEL") {
 				console.log(`Canceled ${url}`)
 				return { cancel: true }
 			}
-			// if (newUrl === "BYPASSTAB") {
-			// 	console.log(`Bypassed ${details.tabId} ${url}`)
-			// 	if (!BYPASSTABs.includes(details.tabId)) BYPASSTABs.push(details.tabId)
-			// 	return null
-			// }
+			if (newUrl === "BYPASSTAB") {
+				console.log(`Bypassed ${details.tabId} ${url}`)
+				if (tabIdRedirects[details.tabId] != false) tabIdRedirects[details.tabId] = false
+				return null
+			}
 			console.info("Redirecting", url.href, "=>", newUrl)
 			return { redirectUrl: newUrl }
 		}
@@ -84,10 +87,9 @@ browser.webRequest.onBeforeRequest.addListener(
 )
 
 browser.tabs.onRemoved.addListener(tabId => {
-	const i = BYPASSTABs.indexOf(tabId)
-	if (i > -1) {
-		BYPASSTABs.splice(i, 1)
-		console.log("Removed BYPASSTABs", tabId)
+	if (tabIdRedirects[tabId] != undefined) {
+		delete tabIdRedirects[tabId]
+		console.log("Removed tab " + tabId + " from tabIdRedirects")
 	}
 })
 
@@ -159,11 +161,30 @@ browser.contextMenus.create({
 })
 
 browser.contextMenus.create({
-	id: "bypassTab",
-	title: browser.i18n.getMessage("bypassTab"),
+	id: "toggleTab",
+	title: browser.i18n.getMessage("toggleTab"),
 	contexts: ["page", "tab"],
 })
 
+function handleToggleTab(tab) {
+	return new Promise(async resolve => {
+	console.log(tabIdRedirects[tab.id])
+		switch (tabIdRedirects[tab.id]) {
+			case false:
+				console.log("kj")
+				const newUrl = await servicesHelper.reverse(tab.url, true)
+				if (newUrl) browser.tabs.update(tab.id, { url: newUrl })
+				resolve()
+				return
+			case true:
+				console.log("lsad")
+				browser.tabs.reload(tab.id)
+				resolve()
+				return
+		}
+	})
+}
+
 browser.contextMenus.onClicked.addListener((info, tab) => {
 	return new Promise(async resolve => {
 		switch (info.menuItemId) {
@@ -183,18 +204,33 @@ browser.contextMenus.onClicked.addListener((info, tab) => {
 				utils.unify()
 				resolve()
 				return
-			case "bypassTab":
-				if (!BYPASSTABs.includes(tab.id)) {
-					BYPASSTABs.push(tab.id)
-					let newUrl = await servicesHelper.reverse(tab.url, true)
-					if (newUrl) browser.tabs.update(tab.id, { url: newUrl })
+			case "toggleTab":
+				if (tabIdRedirects[tab.id] != undefined) {
+					tabIdRedirects[tab.id] = !tabIdRedirects[tab.id]
+					await handleToggleTab(tab)
 					resolve()
 					return
 				} else {
-					BYPASSTABs.splice(BYPASSTABs.indexOf(tab.id), 1)
-					browser.tabs.reload(tab.id)
-					resolve()
-					return
+					console.log("n")
+					const url = new URL(tab.url)
+					const service = await servicesHelper.computeService(url)
+					console.log(service)
+					if (service) {
+						console.log("h")
+						browser.storage.local.get("options", async r => {
+							console.log(r.options[service])
+							if (r.options[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
+					}
 				}
 		}
 	})
diff --git a/src/pages/background/reset_warning.html b/src/pages/background/reset_warning.html
deleted file mode 100644
index f1881ed8..00000000
--- a/src/pages/background/reset_warning.html
+++ /dev/null
@@ -1,59 +0,0 @@
-<!DOCTYPE html>
-<html lang="en">
-	<head>
-		<meta charset="UTF-8" />
-		<meta http-equiv="X-UA-Compatible" content="IE=edge" />
-		<meta name="viewport" content="width=device-width, initial-scale=1" />
-		<title data-localise="__MSG_instanceIsOff__">Reset Warning</title>
-		<link href="../stylesheets/styles.css" rel="stylesheet" />
-		<style>
-			body {
-				margin: 0;
-				padding: 0;
-				height: 100vh;
-				width: 100vw;
-				flex-wrap: wrap;
-				justify-content: center;
-				align-items: center;
-				font-size: 30px;
-				display: flex;
-			}
-
-			div {
-				width: 80%;
-			}
-
-			div.logo {
-				display: flex;
-			}
-
-			img {
-				width: 90px;
-				height: auto;
-			}
-		</style>
-	</head>
-
-	<body>
-		<div>
-			<div class="logo">
-				<img src="/assets/images/libredirect.svg" alt="LibRedirect icon" />
-				<h1>LibRedirect</h1>
-			</div>
-
-			<p data-localise="__MSG_instanceOffline__">All settings have been reset as they're incompatible with the previous version.</p>
-			<p>Sorry for the inconvenience, but we're going in a fast development process and can't support nor convert older settings. It will reach a stable plateau though.</p>
-
-			<a id="export-settings" class="button button-inline">
-				<svg xmlns="http://www.w3.org/2000/svg" height="24px" viewBox="0 0 24 24" width="24px" fill="currentColor">
-					<path
-						d="M10.09 15.59L11.5 17l5-5-5-5-1.41 1.41L12.67 11H3v2h9.67l-2.58 2.59zM19 3H5c-1.11 0-2 .9-2 2v4h2V5h14v14H5v-4H3v4c0 1.1.89 2 2 2h14c1.1 0 2-.9 2-2V5c0-1.1-.9-2-2-2z"
-					></path>
-				</svg>
-				&nbsp;
-				<x data-localise="__MSG_exportSettings__">Export Settings</x>
-			</a>
-		</div>
-	</body>
-	<script src="reset_warning.js"></script>
-</html>
diff --git a/src/pages/background/reset_warning.js b/src/pages/background/reset_warning.js
deleted file mode 100644
index 9ce49c9a..00000000
--- a/src/pages/background/reset_warning.js
+++ /dev/null
@@ -1,7 +0,0 @@
-let params = new URLSearchParams(location.search)
-
-const resultString = JSON.stringify(JSON.parse(params.get("data")), null, "  ")
-
-let exportSettingsElement = document.getElementById("export-settings")
-exportSettingsElement.href = "data:application/json;base64," + btoa(resultString)
-exportSettingsElement.download = "libredirect-settings.json"