about summary refs log tree commit diff stats
path: root/src
diff options
context:
space:
mode:
authorHygna <hygna@proton.me>2022-10-04 19:10:07 +0100
committerHygna <hygna@proton.me>2022-10-04 19:10:07 +0100
commitc87bbba349ced5cbc5d9559fbf8af88f9557ad24 (patch)
treec8289fd3f245ee365310731f36b66be5405acd39 /src
parentMerge branch 'optimizations' (diff)
downloadlibredirect-c87bbba349ced5cbc5d9559fbf8af88f9557ad24.zip
Ported 87d68cb662
Diffstat (limited to 'src')
-rw-r--r--src/assets/javascripts/frontend.js143
-rw-r--r--src/assets/javascripts/services.js18
-rw-r--r--src/pages/background/background.js314
3 files changed, 47 insertions, 428 deletions
diff --git a/src/assets/javascripts/frontend.js b/src/assets/javascripts/frontend.js
deleted file mode 100644
index ab71cc0d..00000000
--- a/src/assets/javascripts/frontend.js
+++ /dev/null
@@ -1,143 +0,0 @@
-class FrontEnd {
-	constructor({ enable, frontends, frontend, redirect }) {
-		this.redirects = {}
-		this.enable = enable
-		this.frontend = frontend
-		this.protocol = "normal"
-		this.protocolFallback = true
-		fetch("/instances/data.json")
-			.then(response => response.text())
-			.then(async data => {
-				data = JSON.parse(data)
-				fetch("/instances/blacklist.json")
-					.then(response => response.text())
-					.then(async blackList => {
-						blackList = JSON.parse(blackList)
-						for (const frontend in frontends) {
-							this.redirects[frontend] = {}
-
-							this.redirects[frontend].cookies = [...frontends[frontend].cookies]
-
-							for (const protocol in data[frontend]) {
-								this.redirects[frontend][protocol] = {}
-
-								this.redirects[frontend][protocol].all = [...data[frontend][protocol]]
-
-								this.redirects[frontend][protocol].custom = []
-
-								this.redirects[frontend][protocol].checked = [...data[frontend][protocol]]
-								for (const instance of blackList.cloudflare) {
-									const a = this.redirects[frontend][protocol].checked.indexOf(instance)
-									if (a > -1) this.redirects[frontend][protocol].checked.splice(a, 1)
-								}
-								for (const instance of blackList.offline) {
-									const a = this.redirects[frontend][protocol].checked.indexOf(instance)
-									if (a > -1) this.redirects[frontend][protocol].checked.splice(a, 1)
-								}
-							}
-						}
-					})
-			})
-		this.unifyCookies = from =>
-			new Promise(async resolve => {
-				await init()
-				const protocolHost = utils.protocolHost(from)
-				const list = [...this.redirects[this.frontend][this.protocol]]
-				if (![...list.checked, ...list.custom].includes(protocolHost)) {
-					resolve()
-					return
-				}
-				for (const cookie of this.redirects[this.frontend].cookies) {
-					await utils.copyCookie(frontend, protocolHost, [...list.checked, list.custom], cookie)
-				}
-				resolve(true)
-			})
-
-		this.switchInstance = (url, disableOverride) => {
-			if (!this.enable && !disableOverride) return
-
-			const protocolHost = utils.protocolHost(url)
-
-			const list = [...this.redirects[this.frontend][this.protocol]]
-			if (!list.all.includes(protocolHost)) return
-
-			let userList = [...list.checked, ...list.custom]
-			if (userList.length === 0 && this.protocolFallback) userList = [...list.normal.all]
-
-			const i = userList.indexOf(protocolHost)
-			if (i > -1) userList.splice(i, 1)
-			if (userList.length === 0) return
-
-			const randomInstance = utils.getRandomInstance(userList)
-			return `${randomInstance}${url.pathname}${url.search}`
-		}
-
-		this.redirect = (url, type, initiator, disableOverride) => {
-			const result = redirect(url, type, initiator, disableOverride)
-			if (result == "BYPASSTAB") return "BYPASSTAB"
-			if (result) {
-				const list = [...this.redirects[this.frontend][this.protocol]]
-				let userList = [...list.checked, ...list.custom]
-				const randomInstance = utils.getRandomInstance(userList)
-				return `${randomInstance}${result.pathname}${result.search}`
-			}
-		}
-
-		let init = () => new Promise(async resolve => {})
-	}
-}
-
-let Reddit = new FrontEnd({
-	enable: true,
-	frontends: {
-		libreddit: { cookies: ["theme", "front_page", "layout", "wide", "post_sort", "comment_sort", "show_nsfw", "autoplay_videos", "use_hls", "hide_hls_notification", "subscriptions", "filters"] },
-		teddit: {
-			cookies: [
-				"collapse_child_comments",
-				"domain_instagram",
-				"domain_twitter",
-				"domain_youtube",
-				"flairs",
-				"highlight_controversial",
-				"nsfw_enabled",
-				"post_media_max_height",
-				"show_upvoted_percentage",
-				"show_upvotes",
-				"theme",
-				"videos_muted",
-			],
-		},
-	},
-	frontend: "libreddit",
-	redirect: (url, type, initiator, disableOverride) => {
-		if (this.enable && !disableOverride) return
-
-		const targets = [/^https?:\/{2}(www\.|old\.|np\.|new\.|amp\.|)reddit\.com/, /^https?:\/{2}(i\.|preview\.)redd\.it/]
-		if (!targets.some(rx => rx.test(url.href))) return
-
-		if (initiator && all().includes(initiator.origin)) return "BYPASSTAB"
-		if (!["main_frame", "xmlhttprequest", "other", "image", "media"].includes(type)) return
-
-		const bypassPaths = /\/(gallery\/poll\/rpan\/settings\/topics)/
-		if (url.pathname.match(bypassPaths)) return
-
-		const protocolHost = utils.protocolHost(url)
-
-		if (url.host === "i.redd.it") {
-			if (this.frontend == "libreddit") return `${protocolHost}/img${url.pathname}${url.search}`
-			if (this.frontend == "teddit") return `${protocolHost}/pics/w:null_${url.pathname.substring(1)}${url.search}`
-		} else if (url.host === "redd.it") {
-			// https://redd.it/foo => https://libredd.it/comments/foo
-			if (this.frontend == "libreddit" && !url.pathname.match(/^\/+[^\/]+\/+[^\/]/)) return `${protocolHost}/comments${url.pathname}${url.search}`
-			// https://redd.it/foo => https://teddit.net/comments/foo
-			if (this.frontend == "teddit" && !url.pathname.match(/^\/+[^\/]+\/+[^\/]/)) return `${protocolHost}/comments${url.pathname}${url.search}`
-		} else if (url.host === "preview.redd.it") {
-			if (this.frontend == "libreddit") return `${protocolHost}/preview/pre${url.pathname}${url.search}`
-			if (this.frontend == "teddit") return
-		} else {
-			return `${url.href}`
-		}
-	},
-})
-
-export default {}
diff --git a/src/assets/javascripts/services.js b/src/assets/javascripts/services.js
index afd7906f..eed1e6c6 100644
--- a/src/assets/javascripts/services.js
+++ b/src/assets/javascripts/services.js
@@ -140,13 +140,23 @@ function redirect(url, type, initiator) {
 		case "send":

 			return randomInstance

 		case "nitter":

+			let search = new URLSearchParams(url.search)

+

+			search.delete("ref_src")

+			search.delete("ref_url")

+

+			search = search.toString()

+			if (search !== "") search = `?${search}`

+

 			if (url.host.split(".")[0] === "pbs" || url.host.split(".")[0] === "video") {

 				const [, id, format, extra] = url.search.match(/(.*)\?format=(.*)&(.*)/)

 				const query = encodeURIComponent(`${id}.${format}?${extra}`)

-				return `${randomInstance}/pic${url.pathname}${query}`

-			} else if (url.pathname.split("/").includes("tweets")) return `${randomInstance}${url.pathname.replace("/tweets", "")}${url.search}`

-			else if (url.host == "t.co") return `${randomInstance}/t.co${url.pathname}`

-			else return `${randomInstance}${url.pathname}${url.search}`

+				return `${randomInstance}/pic${search}${query}`

+			}

+

+			if (url.pathname.split("/").includes("tweets")) return `${randomInstance}${url.pathname.replace("/tweets", "")}${search}`

+			if (url.host == "t.co") return `${randomInstance}/t.co${url.pathname}`

+			return `${randomInstance}${url.pathname}${search}`

 		case "yattee":

 			return url.href.replace(/^https?:\/{2}/, "yattee://")

 		case "freetube":

diff --git a/src/pages/background/background.js b/src/pages/background/background.js
index 7186144b..cad08580 100644
--- a/src/pages/background/background.js
+++ b/src/pages/background/background.js
@@ -1,68 +1,25 @@
-<<<<<<< HEAD
 "use strict"
 
 import generalHelper from "../../assets/javascripts/general.js"
 import utils from "../../assets/javascripts/utils.js"
-
-import youtubeHelper from "../../assets/javascripts/youtube/youtube.js"
-import youtubeMusicHelper from "../../assets/javascripts/youtubeMusic.js"
-import twitterHelper from "../../assets/javascripts/twitter.js"
-import instagramHelper from "../../assets/javascripts/instagram.js"
-import redditHelper from "../../assets/javascripts/reddit.js"
-import searchHelper from "../../assets/javascripts/search.js"
-import translateHelper from "../../assets/javascripts/translate/translate.js"
-import mapsHelper from "../../assets/javascripts/maps.js"
-import wikipediaHelper from "../../assets/javascripts/wikipedia.js"
-import mediumHelper from "../../assets/javascripts/medium.js"
-import quoraHelper from "../../assets/javascripts/quora.js"
-import libremdbHelper from "../../assets/javascripts/imdb.js"
-import reutersHelper from "../../assets/javascripts/reuters.js"
-import imgurHelper from "../../assets/javascripts/imgur.js"
-import tiktokHelper from "../../assets/javascripts/tiktok.js"
-import sendTargetsHelper from "../../assets/javascripts/sendTargets.js"
-import peertubeHelper from "../../assets/javascripts/peertube.js"
-import lbryHelper from "../../assets/javascripts/lbry.js"
-
-import frontend from "../../assets/javascripts/frontend.js"
+import servicesHelper from "../../assets/javascripts/services.js"
 
 window.browser = window.browser || window.chrome
 
-browser.runtime.onInstalled.addListener(details => {
-	function initDefaults() {
+function initDefaults() {
+	browser.storage.local.clear(() => {
 		fetch("/instances/blacklist.json")
 			.then(response => response.text())
 			.then(async data => {
-				browser.storage.local.clear(() => {
-					browser.storage.local.set({ cloudflareBlackList: JSON.parse(data).cloudflare }, () => {
-						browser.storage.local.set({ authenticateBlackList: JSON.parse(data).authenticate }, () => {
-							browser.storage.local.set({ offlineBlackList: JSON.parse(data).offline }, () => {
-								generalHelper.initDefaults()
-								youtubeHelper.initDefaults()
-								youtubeMusicHelper.initDefaults()
-								twitterHelper.initDefaults()
-								instagramHelper.initDefaults()
-								mapsHelper.initDefaults()
-								searchHelper.initDefaults()
-								translateHelper.initDefaults()
-								mediumHelper.initDefaults()
-								quoraHelper.initDefaults()
-								libremdbHelper.initDefaults()
-								reutersHelper.initDefaults()
-								redditHelper.initDefaults()
-								wikipediaHelper.initDefaults()
-								imgurHelper.initDefaults()
-								tiktokHelper.initDefaults()
-								sendTargetsHelper.initDefaults()
-								peertubeHelper.initDefaults()
-								lbryHelper.initDefaults()
-							})
-						})
-					})
+				browser.storage.local.set({ blacklists: JSON.parse(data) }, async () => {
+					await generalHelper.initDefaults()
+					await servicesHelper.initDefaults()
 				})
 			})
-	}
-	if (details.reason == "install") initDefaults()
+	})
+}
 
+browser.runtime.onInstalled.addListener(details => {
 	// if (details.reason == 'install' || (details.reason == "update" && details.previousVersion != browser.runtime.getManifest().version)) {
 	//   if (details.reason == "update")
 	//     browser.storage.local.get(null, r => {
@@ -74,6 +31,25 @@ browser.runtime.onInstalled.addListener(details => {
 	//     })
 	//   else initDefaults();
 	// }
+	switch (details.reason) {
+		case "install":
+			initDefaults()
+			break
+		case "update":
+			fetch("/instances/blacklist.json")
+				.then(response => response.text())
+				.then(async data => {
+					browser.storage.local.set({ blacklists: JSON.parse(data) }, async () => {
+						switch (details.previousVersion) {
+							case "2.2.1":
+								await generalHelper.initDefaults()
+								await servicesHelper.initDefaults()
+								await servicesHelper.upgradeOptions()
+								break
+						}
+					})
+				})
+	}
 })
 
 let BYPASSTABs = []
@@ -89,24 +65,7 @@ browser.webRequest.onBeforeRequest.addListener(
 			return null
 		}
 
-		let newUrl = youtubeMusicHelper.redirect(url, details.type)
-		if (!newUrl) newUrl = youtubeHelper.redirect(url, details.type, details.tabId, initiator)
-		if (!newUrl) newUrl = twitterHelper.redirect(url, details.type, initiator)
-		if (!newUrl) newUrl = instagramHelper.redirect(url, details.type, initiator)
-		if (!newUrl) newUrl = mapsHelper.redirect(url, initiator)
-		if (!newUrl) newUrl = redditHelper.redirect(url, details.type, initiator)
-		if (!newUrl) newUrl = mediumHelper.redirect(url, details.type, initiator)
-		if (!newUrl) newUrl = quoraHelper.redirect(url, details.type, initiator)
-		if (!newUrl) newUrl = libremdbHelper.redirect(url, details.type, initiator)
-		if (!newUrl) newUrl = reutersHelper.redirect(url, details.type, initiator)
-		if (!newUrl) newUrl = imgurHelper.redirect(url, details.type, initiator)
-		if (!newUrl) newUrl = tiktokHelper.redirect(url, details.type, initiator)
-		if (!newUrl) newUrl = sendTargetsHelper.redirect(url, details.type, initiator)
-		if (!newUrl) newUrl = peertubeHelper.redirect(url, details.type, initiator)
-		if (!newUrl) newUrl = lbryHelper.redirect(url, details.type, initiator)
-		if (!newUrl) newUrl = translateHelper.redirect(url)
-		if (!newUrl) newUrl = searchHelper.redirect(url)
-		if (!newUrl) newUrl = wikipediaHelper.redirect(url)
+		let newUrl = servicesHelper.redirect(url, details.type, initiator)
 
 		if (details.frameAncestors && details.frameAncestors.length > 0 && generalHelper.isException(new URL(details.frameAncestors[0].url))) newUrl = null
 
@@ -140,6 +99,7 @@ browser.tabs.onRemoved.addListener(tabId => {
 	}
 })
 
+/*
 browser.webRequest.onHeadersReceived.addListener(
 	e => {
 		let response = youtubeHelper.removeXFrameOptions(e)
@@ -149,23 +109,10 @@ browser.webRequest.onHeadersReceived.addListener(
 	{ urls: ["<all_urls>"] },
 	["blocking", "responseHeaders"]
 )
+*/
 
 async function redirectOfflineInstance(url, tabId) {
-	let newUrl = await youtubeHelper.switchInstance(url, true)
-	if (!newUrl) newUrl = await twitterHelper.switchInstance(url, true)
-	if (!newUrl) newUrl = await instagramHelper.switchInstance(url, true)
-	if (!newUrl) newUrl = await redditHelper.switchInstance(url, true)
-	if (!newUrl) newUrl = await searchHelper.switchInstance(url, true)
-	if (!newUrl) newUrl = await translateHelper.switchInstance(url, true)
-	if (!newUrl) newUrl = await mediumHelper.switchInstance(url, true)
-	if (!newUrl) newUrl = await quoraHelper.switchInstance(url, true)
-	if (!newUrl) newUrl = await libremdbHelper.switchInstance(url, true)
-	if (!newUrl) newUrl = await tiktokHelper.switchInstance(url, true)
-	if (!newUrl) newUrl = await imgurHelper.switchInstance(url, true)
-	if (!newUrl) newUrl = await wikipediaHelper.switchInstance(url, true)
-	if (!newUrl) newUrl = await peertubeHelper.switchInstance(url, true)
-	if (!newUrl) newUrl = await lbryHelper.switchInstance(url, true)
-	if (!newUrl) newUrl = await youtubeMusicHelper.switchInstance(url, true)
+	let newUrl = await servicesHelper.switchInstance(url, true)
 
 	if (newUrl) {
 		if (counter >= 5) {
@@ -182,7 +129,7 @@ async function redirectOfflineInstance(url, tabId) {
 let counter = 0
 
 function isAutoRedirect() {
-	return new Promise(resolve => browser.storage.local.get("autoRedirect", r => resolve(r.autoRedirect == true)))
+	return new Promise(resolve => browser.storage.local.get("options", r => resolve(r.options.autoRedirect == true)))
 }
 
 browser.webRequest.onResponseStarted.addListener(
@@ -242,198 +189,3 @@ browser.runtime.onMessage.addListener((message, sender, sendResponse) => {
 	if (message.function === "unify") utils.unify(false).then(r => sendResponse({ response: r }))
 	return true
 })
-
-browser.storage.local.set({ version: browser.runtime.getManifest().version })
-=======
-"use strict"

-

-import generalHelper from "../../assets/javascripts/general.js"

-import utils from "../../assets/javascripts/utils.js"

-import servicesHelper from "../../assets/javascripts/services.js"

-

-window.browser = window.browser || window.chrome

-

-function initDefaults() {

-	browser.storage.local.clear(() => {

-		fetch("/instances/blacklist.json")

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

-			.then(async data => {

-				browser.storage.local.set({ blacklists: JSON.parse(data) }, async () => {

-					await generalHelper.initDefaults()

-					await servicesHelper.initDefaults()

-				})

-			})

-	})

-}

-

-browser.runtime.onInstalled.addListener(details => {

-	// if (details.reason == 'install' || (details.reason == "update" && details.previousVersion != browser.runtime.getManifest().version)) {

-	//   if (details.reason == "update")

-	//     browser.storage.local.get(null, r => {

-	//       if (r.theme) {

-	//         const old = encodeURIComponent(JSON.stringify(r))

-	//         browser.tabs.create({ url: browser.runtime.getURL(`/pages/background/reset_warning.html?data=${old}`) });

-	//       }

-	//       initDefaults();

-	//     })

-	//   else initDefaults();

-	// }

-	switch (details.reason) {

-		case "install":

-			initDefaults()

-			break

-		case "update":

-			fetch("/instances/blacklist.json")

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

-				.then(async data => {

-					browser.storage.local.set({ blacklists: JSON.parse(data) }, async () => {

-						switch (details.previousVersion) {

-							case "2.2.1":

-								await generalHelper.initDefaults()

-								await servicesHelper.initDefaults()

-								await servicesHelper.upgradeOptions()

-								break

-						}

-					})

-				})

-	}

-})

-

-let BYPASSTABs = []

-browser.webRequest.onBeforeRequest.addListener(

-	details => {

-		const url = new URL(details.url)

-		if (new RegExp(/^chrome-extension:\/{2}.*\/instances\/.*.json$/).test(url.href) && details.type == "xmlhttprequest") return

-		let initiator

-		try {

-			if (details.originUrl) initiator = new URL(details.originUrl)

-			else if (details.initiator) initiator = new URL(details.initiator)

-		} catch {

-			return null

-		}

-

-		let newUrl = servicesHelper.redirect(url, details.type, initiator)

-

-		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

-			}

-			console.info("Redirecting", url.href, "=>", newUrl)

-			return { redirectUrl: newUrl }

-		}

-		return null

-	},

-	{ urls: ["<all_urls>"] },

-	["blocking"]

-)

-

-browser.tabs.onRemoved.addListener(tabId => {

-	const i = BYPASSTABs.indexOf(tabId)

-	if (i > -1) {

-		BYPASSTABs.splice(i, 1)

-		console.log("Removed BYPASSTABs", tabId)

-	}

-})

-

-/*

-browser.webRequest.onHeadersReceived.addListener(

-	e => {

-		let response = youtubeHelper.removeXFrameOptions(e)

-		if (!response) response = twitterHelper.removeXFrameOptions(e)

-		return response

-	},

-	{ urls: ["<all_urls>"] },

-	["blocking", "responseHeaders"]

-)

-*/

-

-async function redirectOfflineInstance(url, tabId) {

-	let newUrl = await servicesHelper.switchInstance(url, true)

-

-	if (newUrl) {

-		if (counter >= 5) {

-			browser.tabs.update(tabId, {

-				url: `/pages/errors/instance_offline.html?url=${encodeURIComponent(newUrl)}`,

-			})

-			counter = 0

-		} else {

-			browser.tabs.update(tabId, { url: newUrl })

-			counter++

-		}

-	}

-}

-let counter = 0

-

-function isAutoRedirect() {

-	return new Promise(resolve => browser.storage.local.get("options", r => resolve(r.options.autoRedirect == true)))

-}

-

-browser.webRequest.onResponseStarted.addListener(

-	async details => {

-		if (!(await isAutoRedirect())) return null

-		if (details.type == "main_frame" && details.statusCode >= 500) redirectOfflineInstance(new URL(details.url), details.tabId)

-	},

-	{ urls: ["<all_urls>"] }

-)

-

-browser.webRequest.onErrorOccurred.addListener(

-	async details => {

-		if (!(await isAutoRedirect())) return

-		if (details.type == "main_frame") redirectOfflineInstance(new URL(details.url), details.tabId)

-	},

-	{ urls: ["<all_urls>"] }

-)

-

-browser.commands.onCommand.addListener(command => {

-	if (command === "switchInstance") utils.switchInstance()

-	else if (command == "copyRaw") utils.copyRaw()

-	else if (command == "unify") utils.unify()

-})

-

-browser.contextMenus.create({

-	id: "settings",

-	title: browser.i18n.getMessage("Settings"),

-	contexts: ["browser_action"],

-})

-

-browser.contextMenus.create({

-	id: "switchInstance",

-	title: browser.i18n.getMessage("switchInstance"),

-	contexts: ["browser_action"],

-})

-

-browser.contextMenus.create({

-	id: "copyRaw",

-	title: browser.i18n.getMessage("copyRaw"),

-	contexts: ["browser_action"],

-})

-

-browser.contextMenus.create({

-	id: "unify",

-	title: browser.i18n.getMessage("unifySettings"),

-	contexts: ["browser_action"],

-})

-

-browser.contextMenus.onClicked.addListener(info => {

-	if (info.menuItemId == "switchInstance") utils.switchInstance()

-	else if (info.menuItemId == "settings") browser.runtime.openOptionsPage()

-	else if (info.menuItemId == "copyRaw") utils.copyRaw()

-	else if (info.menuItemId == "unify") utils.unify()

-})

-

-browser.runtime.onMessage.addListener((message, sender, sendResponse) => {

-	if (message.function === "unify") utils.unify(false).then(r => sendResponse({ response: r }))

-	return true

-})

->>>>>>> optimizations