From 39ee72324560acb7823d545f29e90a2d1daee5c0 Mon Sep 17 00:00:00 2001 From: ManeraKai Date: Wed, 23 Feb 2022 23:45:22 +0300 Subject: [PATCH] Added the base code for Beatbump #30 --- .../javascripts/helpers/youtubeMusic.js | 117 ++++++++++++++++++ src/pages/background/background.js | 14 +++ 2 files changed, 131 insertions(+) create mode 100644 src/assets/javascripts/helpers/youtubeMusic.js diff --git a/src/assets/javascripts/helpers/youtubeMusic.js b/src/assets/javascripts/helpers/youtubeMusic.js new file mode 100644 index 0000000..9455601 --- /dev/null +++ b/src/assets/javascripts/helpers/youtubeMusic.js @@ -0,0 +1,117 @@ +"use strict"; + +import commonHelper from './common.js' + +window.browser = window.browser || window.chrome; + +const targets = [ + /https?:\/\/music\.youtube\.com(\/.*|$)/, +]; +let redirects = { + "beatbump": { + "normal": [ + "https://beatbump.ml" + ], + "tor": [] + }, +}; + +const getRedirects = () => redirects; + +function getCustomRedirects() { + return { + "beatbump": { + "normal": [...beatbumpNormalRedirectsChecks, ...beatbumpNormalCustomRedirects] + }, + }; +}; + +function setYoutubeMusicRedirects(val) { + redirects.beatbump = val; + browser.storage.local.set({ youtubeMusicRedirects: redirects }) + console.log("youtubeMusicRedirects: ", val) +} + +let beatbumpNormalRedirectsChecks; +const getBeatbumpNormalRedirectsChecks = () => beatbumpNormalRedirectsChecks; +function setBeatbumpNormalRedirectsChecks(val) { + beatbumpNormalRedirectsChecks = val; + browser.storage.local.set({ beatbumpNormalRedirectsChecks }) + console.log("beatbumpNormalRedirectsChecks: ", val) +} + +let beatbumpNormalCustomRedirects = []; +const getBeatbumpNormalCustomRedirects = () => beatbumpNormalCustomRedirects; +function setBeatbumpNormalCustomRedirects(val) { + beatbumpNormalCustomRedirects = val; + browser.storage.local.set({ beatbumpNormalCustomRedirects }) + console.log("beatbumpNormalCustomRedirects: ", val) +} + +let disable; +const getDisable = () => disable; +function setDisable(val) { + disable = val; + browser.storage.local.set({ disableYoutubeMusic: disable }) + console.log("disableYoutubeMusic: ", disable) +} + +function isYoutubeMusic(url, initiator) { + if (disable) return false + return targets.some((rx) => rx.test(url.href)); +} + +function redirect(url, type) { + let instancesList = [...beatbumpNormalRedirectsChecks, ...beatbumpNormalCustomRedirects]; + if (instancesList.length === 0) return null; + let randomInstance = commonHelper.getRandomInstance(instancesList); + + return `${randomInstance}${url.pathname}${url.search}`.replace("/watch?v=", "/listen?id="); +} + +async function init() { + return new Promise((resolve) => { + console.log("Init music") + browser.storage.local.get( + [ + "disableYoutubeMusic", + "youtubeMusicRedirects", + + "beatbumpNormalRedirectsChecks", + "beatbumpNormalCustomRedirects", + + "youtubeMusicProtocol", + ], + (result) => { + disable = result.disableYoutubeMusic ?? false; + + if (result.youtubeMusicRedirects) redirects = result.youtubeMusicRedirects; + + beatbumpNormalRedirectsChecks = result.beatbumpNormalRedirectsChecks ?? [...redirects.beatbump.normal]; + console.log("beatbumpNormalRedirectsChecks", beatbumpNormalRedirectsChecks) + beatbumpNormalCustomRedirects = result.beatbumpNormalCustomRedirects ?? []; + + resolve(); + }); + }); +} + +export default { + getRedirects, + getCustomRedirects, + setYoutubeMusicRedirects, + + redirect, + isYoutubeMusic, + + getDisable, + setDisable, + + getBeatbumpNormalRedirectsChecks, + setBeatbumpNormalRedirectsChecks, + + getBeatbumpNormalCustomRedirects, + setBeatbumpNormalCustomRedirects, + + init, +}; diff --git a/src/pages/background/background.js b/src/pages/background/background.js index dced348..06c08c2 100644 --- a/src/pages/background/background.js +++ b/src/pages/background/background.js @@ -12,11 +12,13 @@ import mediumHelper from "../../assets/javascripts/helpers/medium.js"; import imgurHelper from "../../assets/javascripts/helpers/imgur.js"; import tiktokHelper from "../../assets/javascripts/helpers/tiktok.js"; import exceptionsHelper from "../../assets/javascripts/helpers/exceptions.js"; +import youtubeMusicHelper from "../../assets/javascripts/helpers/youtubeMusic.js"; window.browser = window.browser || window.chrome; async function wholeInit() { youtubeHelper.init() + youtubeMusicHelper.init() twitterHelper.init() instagramHelper.init() mapsHelper.init() @@ -47,6 +49,8 @@ browser.webRequest.onBeforeRequest.addListener( if (exceptionsHelper.isException(url, initiator)) newUrl = null; + else if (youtubeMusicHelper.isYoutubeMusic(url, initiator)) newUrl = youtubeMusicHelper.redirect(url, details.type) + else if (youtubeHelper.isYoutube(url, initiator)) newUrl = youtubeHelper.redirect(url, details.type) else if (twitterHelper.isTwitter(url, initiator)) newUrl = twitterHelper.redirect(url); @@ -156,18 +160,28 @@ function getMightyList() { return [ ...youtubeHelper.getCustomRedirects().invidious.normal, ...youtubeHelper.getCustomRedirects().piped.normal, + ...twitterHelper.getCustomRedirects().nitter.normal, + + ...youtubeMusicHelper.getCustomRedirects().beatbump.normal, + ...instagramHelper.getCustomRedirects().bibliogram.normal, + ...redditHelper.getCustomRedirects().libreddit.normal, ...redditHelper.getCustomRedirects().teddit.normal, redditHelper.getCustomRedirects().desktop, redditHelper.getCustomRedirects().mobile, + ...searchHelper.getCustomRedirects().searx.normal, ...searchHelper.getCustomRedirects().whoogle.normal, + ...translateHelper.getCustomRedirects().simplyTranslate.normal, ...translateHelper.getCustomRedirects().lingva.normal, + ...mediumHelper.getCustomRedirects().scribe.normal, + ...imgurHelper.getCustomRedirects().rimgo.normal, + ...wikipediaHelper.getCustomRedirects().wikiless.normal ]; }