about summary refs log tree commit diff stats
path: root/src/assets/javascripts/youtubeMusic.js
diff options
context:
space:
mode:
Diffstat (limited to 'src/assets/javascripts/youtubeMusic.js')
-rw-r--r--src/assets/javascripts/youtubeMusic.js364
1 files changed, 276 insertions, 88 deletions
diff --git a/src/assets/javascripts/youtubeMusic.js b/src/assets/javascripts/youtubeMusic.js
index 46caf60e..3ff9a174 100644
--- a/src/assets/javascripts/youtubeMusic.js
+++ b/src/assets/javascripts/youtubeMusic.js
@@ -1,61 +1,192 @@
-"use strict";
-
-import utils from './utils.js'
-
-window.browser = window.browser || window.chrome;
-
-const targets = [
-    /^https?:\/{2}music\.youtube\.com(\/.*|$)/,
-];
-let redirects = {
-    "beatbump": {
-        "normal": [
-            "https://beatbump.ml"
-        ],
-        "tor": [],
-        "i2p": [],
-        "loki": []
-    },
-};
-
-let
-    disableYoutubeMusic,
-    protocol,
-    protocolFallback,
-    beatbumpNormalRedirectsChecks,
-    beatbumpNormalCustomRedirects,
-    beatbumpTorCustomRedirects,
-    beatbumpI2pCustomRedirects,
-    beatbumpLokiCustomRedirects;
+"use strict"
+
+import utils from "./utils.js"
+
+window.browser = window.browser || window.chrome
+
+const targets = [/^https?:\/{2}music\.youtube\.com(\/.*|$)/]
+
+const frontends = new Array("beatbump", "hyperpipe")
+const protocols = new Array("normal", "tor", "i2p", "loki")
+
+let redirects = {}
+
+for (let i = 0; i < frontends.length; i++) {
+	redirects[frontends[i]] = {}
+	for (let x = 0; x < protocols.length; x++) {
+		redirects[frontends[i]][protocols[x]] = []
+	}
+}
+
+function setRedirects(val) {
+	browser.storage.local.get("cloudflareBlackList", r => {
+		redirects = val
+		beatbumpNormalRedirectsChecks = [...redirects.beatbump.normal]
+		hyperpipeNormalRedirectsChecks = [...redirects.hyperpipe.normal]
+		for (const instance of r.cloudflareBlackList) {
+			const a = beatbumpNormalRedirectsChecks.indexOf(instance)
+			if (a > -1) beatbumpNormalRedirectsChecks.splice(a, 1)
+
+			const b = hyperpipeNormalRedirectsChecks.indexOf(instance)
+			if (b > -1) hyperpipeNormalRedirectsChecks.splice(b, 1)
+		}
+		browser.storage.local.set({
+			youtubeMusicRedirects: redirects,
+			beatbumpNormalRedirectsChecks,
+			hyperpipeNormalRedirectsChecks,
+		})
+	})
+}
+
+let disableYoutubeMusic,
+	youtubeMusicFrontend,
+	youtubeMusicRedirects,
+	protocol,
+	protocolFallback,
+	beatbumpNormalRedirectsChecks,
+	beatbumpNormalCustomRedirects,
+	beatbumpTorRedirectsChecks,
+	beatbumpTorCustomRedirects,
+	beatbumpI2pRedirectsChecks,
+	beatbumpI2pCustomRedirects,
+	beatbumpLokiRedirectsChecks,
+	beatbumpLokiCustomRedirects,
+	hyperpipeNormalRedirectsChecks,
+	hyperpipeNormalCustomRedirects,
+	hyperpipeTorRedirectsChecks,
+	hyperpipeTorCustomRedirects,
+	hyperpipeI2pRedirectsChecks,
+	hyperpipeI2pCustomRedirects,
+	hyperpipeLokiRedirectsChecks,
+	hyperpipeLokiCustomRedirects
 
 function init() {
-    browser.storage.local.get(
-        [
-            "disableYoutubeMusic",
-            "protocol",
-            "protocolFallback",
-            "beatbumpNormalRedirectsChecks",
-            "beatbumpNormalCustomRedirects",
-            "beatbumpTorCustomRedirects",
-            "beatbumpI2pCustomRedirects",
-            "beatbumpLokiCustomRedirects"
-        ],
-        r => {
-            disableYoutubeMusic = r.disableYoutubeMusic;
-            protocol = r.protocol;
-            protocolFallback = r.protocolFallback;
-            beatbumpNormalRedirectsChecks = r.beatbumpNormalRedirectsChecks;
-            beatbumpNormalCustomRedirects = r.beatbumpNormalCustomRedirects;
-            beatbumpTorCustomRedirects = r.beatbumpTorCustomRedirects;
-            beatbumpI2pCustomRedirects = r.beatbumpI2pCustomRedirects;
-            beatbumpLokiCustomRedirects = r.beatbumpLokiCustomRedirects;
-        }
-    )
+	return new Promise(async resolve => {
+		browser.storage.local.get(
+			[
+				"disableYoutubeMusic",
+				"youtubeMusicFrontend",
+				"youtubeMusicRedirects",
+				"protocol",
+				"protocolFallback",
+				"beatbumpNormalRedirectsChecks",
+				"beatbumpNormalCustomRedirects",
+				"beatbumpTorRedirectsChecks",
+				"beatbumpTorCustomRedirects",
+				"beatbumpI2pRedirectsChecks",
+				"beatbumpI2pCustomRedirects",
+				"beatbumpLokiRedirectsChecks",
+				"beatbumpLokiCustomRedirects",
+				"hyperpipeNormalRedirectsChecks",
+				"hyperpipeNormalCustomRedirects",
+				"hyperpipeTorRedirectsChecks",
+				"hyperpipeTorCustomRedirects",
+				"hyperpipeI2pRedirectsChecks",
+				"hyperpipeI2pCustomRedirects",
+				"hyperpipeLokiRedirectsChecks",
+				"hyperpipeLokiCustomRedirects",
+			],
+			r => {
+				disableYoutubeMusic = r.disableYoutubeMusic
+				youtubeMusicFrontend = r.youtubeMusicFrontend
+				youtubeMusicRedirects = r.youtubeMusicRedirects
+				protocol = r.protocol
+				protocolFallback = r.protocolFallback
+				beatbumpNormalRedirectsChecks = r.beatbumpNormalRedirectsChecks
+				beatbumpNormalCustomRedirects = r.beatbumpNormalCustomRedirects
+				beatbumpTorRedirectsChecks = r.beatbumpTorRedirectsChecks
+				beatbumpTorCustomRedirects = r.beatbumpTorCustomRedirects
+				beatbumpI2pRedirectsChecks = r.beatbumpI2pRedirectsChecks
+				beatbumpI2pCustomRedirects = r.beatbumpI2pCustomRedirects
+				beatbumpLokiRedirectsChecks = r.beatbumpLokiRedirectsChecks
+				beatbumpLokiCustomRedirects = r.beatbumpLokiCustomRedirects
+				hyperpipeNormalRedirectsChecks = r.hyperpipeNormalRedirectsChecks
+				hyperpipeNormalCustomRedirects = r.hyperpipeNormalCustomRedirects
+				hyperpipeTorRedirectsChecks = r.hyperpipeTorRedirectsChecks
+				hyperpipeTorCustomRedirects = r.hyperpipeTorCustomRedirects
+				hyperpipeI2pRedirectsChecks = r.hyperpipeI2pRedirectsChecks
+				hyperpipeI2pCustomRedirects = r.hyperpipeI2pCustomRedirects
+				hyperpipeLokiRedirectsChecks = r.hyperpipeLokiRedirectsChecks
+				hyperpipeLokiCustomRedirects = r.hyperpipeLokiCustomRedirects
+				resolve()
+			}
+		)
+	})
 }
 
-init();
+init()
 browser.storage.onChanged.addListener(init)
 
+function all() {
+	return [
+		...beatbumpNormalRedirectsChecks,
+		...beatbumpNormalCustomRedirects,
+		...beatbumpTorRedirectsChecks,
+		...beatbumpTorCustomRedirects,
+		...beatbumpI2pRedirectsChecks,
+		...beatbumpI2pCustomRedirects,
+		...beatbumpLokiRedirectsChecks,
+		...beatbumpLokiCustomRedirects,
+		...hyperpipeNormalRedirectsChecks,
+		...hyperpipeNormalCustomRedirects,
+		...hyperpipeTorRedirectsChecks,
+		...hyperpipeTorCustomRedirects,
+		...hyperpipeI2pRedirectsChecks,
+		...hyperpipeI2pCustomRedirects,
+		...hyperpipeLokiRedirectsChecks,
+		...hyperpipeLokiCustomRedirects,
+	]
+}
+
+function getInstanceList() {
+	let tmpList = []
+	switch (youtubeMusicFrontend) {
+		case "beatbump":
+			switch (protocol) {
+				case "loki":
+					tmpList = [...beatbumpLokiRedirectsChecks, ...beatbumpLokiCustomRedirects]
+					break
+				case "i2p":
+					tmpList = [...beatbumpI2pRedirectsChecks, ...beatbumpI2pCustomRedirects]
+					break
+				case "tor":
+					tmpList = [...beatbumpTorRedirectsChecks, ...beatbumpTorCustomRedirects]
+			}
+			if ((tmpList.length === 0 && protocolFallback) || protocol == "normal") {
+				tmpList = [...beatbumpNormalRedirectsChecks, ...beatbumpNormalCustomRedirects]
+			}
+			break
+		case "hyperpipe":
+			switch (protocol) {
+				case "loki":
+					tmpList = [...hyperpipeLokiRedirectsChecks, ...hyperpipeLokiCustomRedirects]
+					break
+				case "i2p":
+					tmpList = [...hyperpipeI2pRedirectsChecks, ...hyperpipeI2pCustomRedirects]
+					break
+				case "tor":
+					tmpList = [...hyperpipeTorRedirectsChecks, ...hyperpipeTorCustomRedirects]
+			}
+			if ((tmpList.length === 0 && protocolFallback) || protocol == "normal") {
+				tmpList = [...hyperpipeNormalRedirectsChecks, ...hyperpipeNormalCustomRedirects]
+			}
+	}
+	return tmpList
+}
+
+function getUrl(randomInstance, url) {
+	switch (youtubeMusicFrontend) {
+		case "beatbump":
+			return `${randomInstance}${url.pathname}${url.search}`
+				.replace("/watch?v=", "/listen?id=")
+				.replace("/channel/", "/artist/")
+				.replace("/playlist?list=", "/playlist/VL")
+				.replace(/\/search\?q=.*/, searchQuery => searchQuery.replace("?q=", "/") + "?filter=EgWKAQIIAWoKEAMQBBAKEAkQBQ%3D%3D")
+		case "hyperpipe":
+			return `${randomInstance}${url.pathname}${url.search}`.replace(/\/search\?q=.*/, searchQuery => searchQuery.replace("?q=", "/"))
+	}
+}
+
 /* 
 Video
 https://music.youtube.com/watch?v=_PkGiKBW-DA&list=RDAMVM_PkGiKBW-DA
@@ -88,48 +219,105 @@ https://music.youtube.com/search?q=test
 https://beatbump.ml/search/test?filter=EgWKAQIIAWoKEAMQBBAKEAkQBQ%3D%3D
 
 */
-function redirect(url, disableOverride) {
-    if (disableYoutubeMusic && !disableOverride) return;
-    if (!targets.some(rx => rx.test(url.href))) return;
-
-    let instancesList = [];
-    if (protocol == 'loki') instancesList = [...beatbumpLokiCustomRedirects];
-    else if (protocol == 'i2p') instancesList = [...beatbumpI2pCustomRedirects];
-    else if (protocol == 'tor') instancesList = [...beatbumpTorCustomRedirects];
-    if ((instancesList.length === 0 && protocolFallback) || protocol == 'normal') {
-        instancesList = [...beatbumpNormalRedirectsChecks, ...beatbumpNormalCustomRedirects];
-    }
-    if (instancesList.length === 0) return;
-    const randomInstance = utils.getRandomInstance(instancesList);
-    return `${randomInstance}${url.pathname}${url.search}`
-        .replace("/watch?v=", "/listen?id=")
-        .replace("/channel/", "/artist/")
-        .replace("/playlist?list=", "/playlist/VL")
-        .replace(/\/search\?q=.*/, searchQuery => searchQuery.replace("?q=", "/") + "?filter=EgWKAQIIAWoKEAMQBBAKEAkQBQ%3D%3D");
+function redirect(url, type, initiator, disableOverride) {
+	if (disableYoutubeMusic && !disableOverride) return
+	if (!targets.some(rx => rx.test(url.href))) return
+
+	let instancesList = getInstanceList()
+
+	if (instancesList.length === 0) return
+	const randomInstance = utils.getRandomInstance(instancesList)
+	return getUrl(randomInstance, url)
+}
+
+function switchInstance(url, disableOverride) {
+	return new Promise(async resolve => {
+		await init()
+		if (disableYoutubeMusic && !disableOverride) {
+			resolve()
+			return
+		}
+		const protocolHost = utils.protocolHost(url)
+		if (!all().includes(protocolHost)) {
+			resolve()
+			return
+		}
+
+		let instancesList = getInstanceList()
+
+		const i = instancesList.indexOf(protocolHost)
+		if (i > -1) instancesList.splice(i, 1)
+		if (instancesList.length === 0) {
+			resolve()
+			return
+		}
+
+		const randomInstance = utils.getRandomInstance(instancesList)
+		return getUrl(randomInstance, url)
+	})
 }
 
-async function initDefaults() {
-    return new Promise(resolve =>
-        browser.storage.local.set({
-            disableYoutubeMusic: true,
-            youtubeMusicRedirects: redirects,
+function initDefaults() {
+	return new Promise(resolve => {
+		fetch("/instances/data.json")
+			.then(response => response.text())
+			.then(async data => {
+				let dataJson = JSON.parse(data)
+				for (let i = 0; i < frontends.length; i++) {
+					redirects[frontends[i]] = dataJson[frontends[i]]
+				}
+				browser.storage.local.get("cloudflareBlackList", async r => {
+					beatbumpNormalRedirectsChecks = [...redirects.beatbump.normal]
+					hyperpipeNormalRedirectsChecks = [...redirects.hyperpipe.normal]
+					for (const instance of r.cloudflareBlackList) {
+						let i
 
-            beatbumpNormalRedirectsChecks: [...redirects.beatbump.normal],
-            beatbumpNormalCustomRedirects: [],
+						i = beatbumpNormalRedirectsChecks.indexOf(instance)
+						if (i > -1) beatbumpNormalRedirectsChecks.splice(i, 1)
 
-            beatbumpTorRedirectsChecks: [...redirects.beatbump.tor],
-            beatbumpTorCustomRedirects: [],
+						i = hyperpipeNormalRedirectsChecks.indexOf(instance)
+						if (i > -1) hyperpipeNormalRedirectsChecks.splice(i, 1)
+					}
+					browser.storage.local.set(
+						{
+							disableYoutubeMusic: false,
+							youtubeMusicFrontend: "hyperpipe",
+							youtubeMusicRedirects: redirects,
 
-            beatbumpI2pRedirectsChecks: [...redirects.beatbump.i2p],
-            beatbumpI2pCustomRedirects: [],
+							beatbumpNormalRedirectsChecks: beatbumpNormalRedirectsChecks,
+							beatbumpNormalCustomRedirects: [],
 
-            beatbumpLokiRedirectsChecks: [...redirects.beatbump.loki],
-            beatbumpLokiCustomRedirects: []
-        }, () => resolve())
-    )
+							beatbumpTorRedirectsChecks: [...redirects.beatbump.tor],
+							beatbumpTorCustomRedirects: [],
+
+							beatbumpI2pRedirectsChecks: [...redirects.beatbump.i2p],
+							beatbumpI2pCustomRedirects: [],
+
+							beatbumpLokiRedirectsChecks: [...redirects.beatbump.loki],
+							beatbumpLokiCustomRedirects: [],
+
+							hyperpipeNormalRedirectsChecks: hyperpipeNormalRedirectsChecks,
+							hyperpipeNormalCustomRedirects: [],
+
+							hyperpipeTorRedirectsChecks: [...redirects.hyperpipe.tor],
+							hyperpipeTorCustomRedirects: [],
+
+							hyperpipeI2pRedirectsChecks: [...redirects.hyperpipe.i2p],
+							hyperpipeI2pCustomRedirects: [],
+
+							hyperpipeLokiRedirectsChecks: [...redirects.hyperpipe.loki],
+							hyperpipeLokiCustomRedirects: [],
+						},
+						() => resolve()
+					)
+				})
+			})
+	})
 }
 
 export default {
-    redirect,
-    initDefaults,
-};
+	setRedirects,
+	switchInstance,
+	redirect,
+	initDefaults,
+}