about summary refs log tree commit diff stats
diff options
context:
space:
mode:
authorHygna <hygna@proton.me>2022-10-08 16:33:39 +0100
committerHygna <hygna@proton.me>2022-10-08 16:33:39 +0100
commit9560cfc3e793d9fcb4fd8184c3d83fc0f4958f67 (patch)
tree19111b6029f6d11d544f450fa108d5bd4d0b5b13
parentAdded context menu to toggle redirects for a certain tab (diff)
downloadlibredirect-9560cfc3e793d9fcb4fd8184c3d83fc0f4958f67.zip
Fixed bugs present in the previous commit
-rw-r--r--src/_locales/en/messages.json2
-rw-r--r--src/assets/javascripts/services.js5
-rw-r--r--src/config/config.json61
-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
-rw-r--r--src/pages/stylesheets/styles.css5
7 files changed, 114 insertions, 107 deletions
diff --git a/src/_locales/en/messages.json b/src/_locales/en/messages.json
index 003a878e..b8dccda3 100644
--- a/src/_locales/en/messages.json
+++ b/src/_locales/en/messages.json
@@ -211,7 +211,7 @@
     "lbryDesktop": {
         "message": "LBRY Desktop"
     },
-    "bypassTab": {
+    "toggleTab": {
         "message": "Toggle redirects in this tab",
         "description": "Used in context menus when right clicking on a page/tab"
     }
diff --git a/src/assets/javascripts/services.js b/src/assets/javascripts/services.js
index 7e0a76a4..6614bf92 100644
--- a/src/assets/javascripts/services.js
+++ b/src/assets/javascripts/services.js
@@ -60,12 +60,12 @@ function regexArray(service, url, config) {
 	return false

 }

 

-function redirect(url, type, initiator) {

+function redirect(url, type, initiator, forceRedirection) {

 	if (type != "main_frame" && type != "sub_frame") return

 	let randomInstance

 	let frontend

 	for (const service in config.services) {

-		if (!options[service].enabled) continue

+		if (!forceRedirection && !options[service].enabled) continue

 		if (config.services[service].embeddable && type != options[service].redirectType && options[service].redirectType != "both") continue

 		if (!config.services[service].embeddable && type != "main_frame") continue

 		// let targets = new RegExp(config.services[service].targets.join("|"), "i")

@@ -407,6 +407,7 @@ function redirect(url, type, initiator) {
 }

 

 function computeService(url, returnFrontend) {

+	console.log(url)

 	return new Promise(resolve => {

 		fetch("/config/config.json")

 			.then(response => response.text())

diff --git a/src/config/config.json b/src/config/config.json
index 97168263..642d90ee 100644
--- a/src/config/config.json
+++ b/src/config/config.json
@@ -188,7 +188,9 @@
 			},

 			"targets": ["^https?:\\/{2}(www\\.)?instagram\\.com\\/p\\/"],

 			"name": "Instagram",

-			"options": { "enabled": true },

+			"options": {

+				"enabled": true

+			},

 			"imageType": "png",

 			"embeddable": false,

 			"url": "https://instagram.com"

@@ -205,7 +207,9 @@
 			},

 			"targets": ["^https?:\\/{2}(www\\.|)tiktok\\.com.*"],

 			"name": "TikTok",

-			"options": { "enabled": true },

+			"options": {

+				"enabled": true

+			},

 			"imageType": "png",

 			"embeddable": false,

 			"url": "https://tiktok.com"

@@ -223,6 +227,7 @@
 					"preferences": {

 						"cookies": [

 							"collapse_child_comments",

+							"default_comment_sort",

 							"domain_instagram",

 							"domain_twitter",

 							"domain_youtube",

@@ -230,8 +235,11 @@
 							"highlight_controversial",

 							"nsfw_enabled",

 							"post_media_max_height",

+							"prefer_frontpage",

+							"show_large_gallery_images",

 							"show_upvoted_percentage",

 							"show_upvotes",

+							"subbed_subreddits",

 							"theme",

 							"videos_muted"

 						]

@@ -280,7 +288,9 @@
 			},

 			"targets": ["^https?:\\/{2}(?:[a-z]+\\.)*wikipedia\\.org"],

 			"name": "Wikipedia",

-			"options": { "enabled": false },

+			"options": {

+				"enabled": false

+			},

 			"imageType": "svg",

 			"embeddable": false,

 			"url": "https://wikipedia.org"

@@ -313,7 +323,9 @@
 				"^writingcooperative\\.com"

 			],

 			"name": "Medium",

-			"options": { "enabled": true },

+			"options": {

+				"enabled": true

+			},

 			"imageType": "svgMono",

 			"embeddable": false,

 			"url": "https://medium.com"

@@ -330,7 +342,9 @@
 			},

 			"targets": ["^https?:\\/{2}([a-z]+\\.)*quora\\.com.*"],

 			"name": "Quora",

-			"options": { "enabled": true },

+			"options": {

+				"enabled": true

+			},

 			"imageType": "png",

 			"embeddable": false,

 			"url": "https://quora.com"

@@ -345,9 +359,11 @@
 					"instanceList": true

 				}

 			},

-			"targets": ["^https?:\\/{2}(?:www\\.|)imdb\\.com.*"],

+			"targets": ["^https?:\\/{2}(?:www\\.|)imdb\\.com\\/title"],

 			"name": "IMDb",

-			"options": { "enabled": false },

+			"options": {

+				"enabled": true

+			},

 			"imageType": "svg",

 			"embeddable": false,

 			"url": "https://imdb.com"

@@ -361,7 +377,9 @@
 			},

 			"targets": ["^https?:\\/{2}(www\\.|)reuters\\.com.*"],

 			"name": "Reuters",

-			"options": { "enabled": false },

+			"options": {

+				"enabled": false

+			},

 			"imageType": "svg",

 			"embeddable": false,

 			"url": "https://reuters.com"

@@ -375,7 +393,9 @@
 			},

 			"targets": ["^https?:\\/{2}(?:[a-zA-Z0-9]+\\.)?fandom\\.com(?=(?:\\/wiki)|(?:\\/?$))"],

 			"name": "Fandom",

-			"options": { "enabled": true },

+			"options": {

+				"enabled": true

+			},

 			"imageType": "svg",

 			"embeddable": false,

 			"url": "https://fandom.com"

@@ -389,7 +409,9 @@
 			},

 			"targets": "datajson",

 			"name": "PeerTube",

-			"options": { "enabled": false },

+			"options": {

+				"enabled": false

+			},

 			"imageType": "svg",

 			"embeddable": false,

 			"url": "https://search.joinpeertube.org"

@@ -414,7 +436,7 @@
 			"targets": ["^https?:\\/{2}odysee\\.com", "^https?:\\/{2}lbry\\.tv"],

 			"name": "LBRY",

 			"options": {

-				"enabled": false,

+				"enabled": true,

 				"frontend": "librarian",

 				"redirectType": "both",

 				"embedFrontend": "librarian"

@@ -455,6 +477,7 @@
 						"cookies": [

 							"autocomplete",

 							"categories",

+							"center_alignment",

 							"disabled_engines",

 							"disabled_plugins",

 							"doi_resolver",

@@ -561,15 +584,23 @@
 			},

 			"targets": ["^https?:\\/{2}send\\.libredirect\\.invalid", "^https?:\\/{2}send\\.firefox\\.com\\/?$", "^https?:\\/{2}sendfiles\\.online\\/?$"],

 			"name": "Send Files",

-			"options": { "enabled": true },

+			"options": {

+				"enabled": true

+			},

 			"imageType": "svgMono",

 			"embeddable": false,

 			"url": "https://send.libredirect.invalid"

 		}

 	},

 	"blacklist": {

-		"cloudflare": { "color": "red" },

-		"authenticate": { "color": "orange" },

-		"offline": { "color": "grey" }

+		"cloudflare": {

+			"color": "red"

+		},

+		"authenticate": {

+			"color": "orange"

+		},

+		"offline": {

+			"color": "grey"

+		}

 	}

 }

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"
diff --git a/src/pages/stylesheets/styles.css b/src/pages/stylesheets/styles.css
index eb599656..072b5d5b 100644
--- a/src/pages/stylesheets/styles.css
+++ b/src/pages/stylesheets/styles.css
@@ -477,3 +477,8 @@ input:disabled {
 	opacity: 0.6;
 	cursor: not-allowed;
 }
+
+div.about a {
+	width: 500px;
+	display: inline-block;
+}