diff --git a/src/assets/javascripts/services.js b/src/assets/javascripts/services.js new file mode 100644 index 0000000..cf193e3 --- /dev/null +++ b/src/assets/javascripts/services.js @@ -0,0 +1,105 @@ +window.browser = window.browser || window.chrome + +import utils from "./utils.js" + +let config + +function getConfig() { + return new Promise(async resolve => { + fetch("/config/config.json") + .then(response => response.text()) + .then(data => { + config = JSON.parse(data) + }) + resolve() + }) +} + +let redirects = {} +let disabled, curNetwork, networkFallback, redirectType + +function init() { + return new Promise(async resolve => { + browser.storage.local.get(["network", "networkFallback"], r => { + curNetwork = r.network + networkFallback = r.networkFallback + }) + //cur = current + getConfig() + for (service in config.services) { + redirects = {} + browser.storage.local.get([`disable${camelCase(service)}`, `${service}Redirects`, `${service}RedirectType,`, `${service}Frontend`], r => { + disabled = r["disable" + camelCase(service)] + redirects = r[service + "Redirects"] + frontend = r[service + "Frontend"] + }) + for (frontend in config[service].frontends) { + redirects[frontend] = {} + for (network in config.networks) { + browser.storage.local.get([`${frontend}${camelCase(network)}RedirectsChecks`, `${frontend}${camelCase(network)}CustomRedirects`], r => { + redirects[frontend][network] = [...r[frontend + camelCase(network) + "RedirectsChecks"], ...r[frontend + camelCase(network) + "CustomRedirects"]] + }) + } + } + } + resolve() + }) +} + +init() +browser.storage.onChanged.addListener(init) + +function redirect(url, type, initiator) { + if (url.pathname == "/") return + for (curService in config.services) { + if (disabled && !disableOverride) continue + if (initiator && (all().includes(initiator.origin) || targets.includes(initiator.host))) continue + //if (!targets.some(rx => rx.test(url.href))) continue + if (!target.test(url)) continue + if (type != redirectType && type != "both") continue + let instanceList = redirects[frontend][curNetwork] + if (instanceList.length === 0 && networkFallback) instanceList = redirects[frontend].clearnet + if (instanceList.length === 0) return + const randomInstance = utils.getRandomInstance(instanceList) + return `${randomInstance}${url.pathname}${url.search}` + } +} + +function initDefaults() { + return new Promise(resolve => { + fetch("/instances/data.json") + .then(response => response.text()) + .then(data => { + let dataJson = JSON.parse(data) + redirects = dataJson + browser.storage.local.get(["cloudflareBlackList", "authenticateBlackList", "offlineBlackList"], async r => { + for (service in config.services) { + for (frontend in service.frontends) { + for (const instance of [...r.cloudflareBlackList, ...r.authenticateBlackList, ...r.offlineBlackList]) { + let i = redirects[frontend]["clearnet"].indexOf(instance) + if (i > -1) redirects[frontend]["clearnet"].splice(i, 1) + } + browser.storage.local.set({ + ["disable" + camelCase(service)]: false, + [service + "Redirects"]: redirects, + [service + "RedirectType"]: "both", + }) + for (frontend in service.frontends) { + for (protocol in config.protocols) { + browser.storage.local.set({ + [frontend + camelCase(protocol) + "RedirectsChecks"]: [...redirects[frontend][protocol]], + [frontend + camelCase(protocol) + "CustomRedirects"]: [], + }) + } + } + ;() => resolve() + } + } + }) + }) + }) +} + +export default { + redirect, +} diff --git a/src/config/config.json b/src/config/config.json index 9051d11..db99177 100644 --- a/src/config/config.json +++ b/src/config/config.json @@ -1,437 +1,437 @@ -{ - "networks": { - "clearnet": { - "url": "org", - "name": "Clearnet" - }, - "tor": { - "url": "onion", - "name": "Tor" - }, - "i2p": { - "url": "i2p", - "name": "I2P" - }, - "loki": { - "url": "loki", - "name": "Lokinet" - } - }, - "services": { - "youtube": { - "frontends": { - "invidious": { - "preferences": { - "cookies": ["PREFS"] - } - }, - "piped": { - "preferences": { - "localstorage": [ - "bufferGoal", - "comments", - "disableLBRY", - "enabledCodecs", - "hl", - "homepage", - "instance", - "listen", - "minimizeDescription", - "playerAutoPlay", - "proxyLBRY", - "quality", - "region", - "selectedSkip", - "sponsorblock", - "theme", - "volume", - "watchHistory" - ] - } - }, - "pipedMaterial": { - "preferences": { - "localstorage": ["PREFERENCES"] - } - }, - "cloudtube": { - "preferences": { - "token": "token", - "fetchEndpoint": "/api/settings", - "setEndpoint": "/settings" - } - } - }, - "singleInstanceFrontends": ["freetube", "yatte"], - "targets": [ - "^https?:\\/{2}(www\\.|music\\.|m\\.|)youtube.com(\\/.*|$)", - "^https?:\\/{2}img\\.youtube.com\\/vi\\/.*\\/..*", - "^https?:\\/{2}(i|s)\\.ytimg.com\\/vi\\/.*\\/..*", - "^https?:\\/{2}(www\\.|music\\.|)youtube.com\\/watch?v=..*", - "^https?:\\/{2}youtu\\.be\\/..*", - "^https?:\\/{2}(www\\.|)(youtube|youtube-nocookie)\\.com\\/embed\\/..*" - ], - "name": "Youtube", - "defaults": { - "disableYoutube": false, - "enableYoutubeCustomSettings": false, - "onlyEmbeddedVideo": "both", - "youtubeFrontend": "invidious", - "youtubeEmbedFrontend": "invidious" - }, - "imageType": "png" - }, - "youtubeMusic": { - "frontends": { - "beatbump": {}, - "hyperpipe": {} - }, - "targets": ["^https?:\\/{2}music\\.youtube\\.com(\\/.*|$)"], - "name": "YT Music", - "defaults": { - "disableYoutubeMusic": false, - "youtubeMusicFrontend": "beatbump" - }, - "imageType": "png" - }, - "twitter": { - "frontends": { - "nitter": { - "preferences": { - "cookies": [ - "theme", - "infiniteScroll", - "stickyProfile", - "bidiSupport", - "hideTweetStats", - "hideBanner", - "hidePins", - "hideReplies", - "squareAvatars", - "mp4Playback", - "hlsPlayback", - "proxyVideos", - "muteVideos", - "autoplayGifs", - "replaceInstagram", - "replaceReddit", - "replaceTwitter", - "replaceYouTube" - ] - } - } - }, - "targets": ["^https?:\\/{2}(www\\.|mobile\\.|)twitter\\.com", "^https?:\\/{2}(pbs\\.|video\\.|)twimg\\.com", "^https?:\\/{2}platform\\.twitter\\.com/embed", "^https?:\\/{2}t\\.co"], - "name": "Twitter", - "defaults": { - "disableTwitter": false, - "twitterRedirectType": "both" - }, - "imageType": "png" - }, - "instagram": { - "frontends": { - "bibliogram": { - "preferences": { - "token": "token", - "fetchEndpoint": "/settings.json", - "setEndpoint": "/applysettings" - } - } - }, - "targets": ["^https?:\\/{2}(www\\.)?instagram\\.com"], - "name": "Instagram", - "defaults": { - "disableInstagram": false - }, - "imageType": "png" - }, - "tiktok": { - "frontends": { - "proxiTok": {} - }, - "targets": ["^https?:\\/{2}(www\\.|)tiktok\\.com.*"], - "name": "TikTok", - "defaults": { - "disableTiktok": false - }, - "imageType": "png" - }, - "reddit": { - "frontends": { - "libreddit": { - "preferences": { - "cookies": ["theme", "front_page", "layout", "wide", "post_sort", "comment_sort", "show_nsfw", "autoplay_videos", "use_hls", "hide_hls_notification", "subscriptions", "filters"] - } - }, - "teddit": { - "preferences": { - "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" - ] - } - } - }, - "targets": ["^https?:\\/{2}(www\\.|old\\.|np\\.|new\\.|amp\\.|)reddit\\.com", "^https?:\\/{2}(i\\.|preview\\.)redd\\.it"], - "name": "Reddit", - "defaults": { - "disableReddit": false, - "redditFrontend": "libreddit" - }, - "imageType": "png" - }, - "imgur": { - "frontends": { - "rimgo": { - "preferences": {} - } - }, - "targets": ["^https?:\\/{2}([im]\\.)?imgur\\.(com|io)(\\/|$)"], - "name": "Imgur", - "defaults": { "disableImgur": false }, - "imageType": "png" - }, - "wikipedia": { - "frontends": { - "wikiless": { - "preferences": { - "cookies": ["theme", "default_lang"] - } - } - }, - "targets": ["^https?:\\/{2}([a-z]+\\.)*wikipedia\\.org"], - "name": "Wikipedia", - "defaults": { "disableWikipedia": true }, - "imageType": "svg" - }, - "medium": { - "frontends": { - "scribe": { - "preferences": {} - } - }, - "targets": [ - "(?:.*\\.)*(? { @@ -86,6 +88,7 @@ browser.webRequest.onBeforeRequest.addListener( return null } + /* let newUrl = youtubeMusicHelper.redirect(url, details.type) if (!newUrl) newUrl = youtubeHelper.redirect(url, details.type, initiator) if (!newUrl) newUrl = twitterHelper.redirect(url, details.type, initiator) @@ -104,6 +107,8 @@ browser.webRequest.onBeforeRequest.addListener( 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