diff --git a/src/assets/javascripts/helpers/youtube/youtube.js b/src/assets/javascripts/helpers/youtube/youtube.js index e3ba0f3..5731599 100644 --- a/src/assets/javascripts/helpers/youtube/youtube.js +++ b/src/assets/javascripts/helpers/youtube/youtube.js @@ -197,16 +197,63 @@ function setPersistInvidiousPrefs(val) { console.log("persistInvidiousPrefs: ", persistInvidiousPrefs) } + +let alwaysusePreferred; +const getAlwaysusePreferred = () => alwaysusePreferred; +function setAlwaysusePreferred(val) { + alwaysusePreferred = val; + browser.storage.sync.set({ alwaysusePreferred }) + console.log("alwaysusePreferred: ", alwaysusePreferred) +} + let invidiousHostNames = () => redirects.invidious.normal.map(link => new URL(link).host); let pipedHostNames = () => redirects.piped.normal.map(link => new URL(link).host); -function isYoutube(url) { - if (frontend == 'invidious') - return (targets.some((rx) => rx.test(url.href)) | pipedHostNames().includes(url.host)); - if (frontend == 'piped') - return (targets.some((rx) => rx.test(url.href)) | invidiousHostNames().includes(url.host)); +function isYoutube(url, initiator) { + if (disableYoutube) + return null; + + if ( + initiator && + ( + redirects.invidious.normal.includes(initiator.origin) || + redirects.piped.normal.includes(initiator.origin) || + targets.includes(initiator.host) + ) + ) + return null; + + if (url.pathname.match(/iframe_api/) || url.pathname.match(/www-widgetapi/)) return null; // Don't redirect YouTube Player API. + + let pipedInstancesList = [...pipedRedirectsChecks, ...pipedCustomRedirects]; + let invidiousInstancesList = [...invidiousRedirectsChecks, ...invidiousCustomRedirects]; + let isTargets = targets.some((rx) => rx.test(url.href)); + let protocolHost = `${url.protocol}//${url.host}`; + + let isInvidious = redirects.invidious.normal.includes(protocolHost); + if (isInvidious) + for (const iterator of invidiousInstancesList) + if (iterator.indexOf(protocolHost) === 0) isInvidious = false; + + let isPiped = redirects.piped.normal.includes(protocolHost); + if (isPiped) + for (const iterator of pipedInstancesList) + if (iterator.indexOf(protocolHost) === 0) isPiped = false; + + if (frontend == 'invidious') { + if (alwaysusePreferred) + return isTargets | isPiped | isInvidious; + else + return isTargets | isPiped; + } + if (frontend == 'piped') { + if (alwaysusePreferred) + return isTargets | isPiped | isInvidious; + else + return isTargets | isInvidious; + } else - return targets.some((rx) => rx.test(url.href)) + return isTargets } async function init() { @@ -229,6 +276,7 @@ async function init() { "invidiousCustomRedirects", "pipedRedirectsChecks", "pipedCustomRedirects", + "alwaysusePreferred", ], (result) => { if (result.youtubeRedirects) redirects = result.youtubeRedirects; @@ -253,6 +301,8 @@ async function init() { persistInvidiousPrefs = result.persistInvidiousPrefs ?? false; + alwaysusePreferred = result.alwaysusePreferred ?? true; + resolve(); }); @@ -267,27 +317,17 @@ function invidiousInitCookies(tabId) { }); } -function redirect(url, initiator, type) { - if (disableYoutube) - return null; - - if ( - initiator && - ( - redirects.invidious.normal.includes(initiator.origin) || - redirects.piped.normal.includes(initiator.origin) || - targets.includes(initiator.host) - ) - ) - return null; - - if (url.pathname.match(/iframe_api/) || url.pathname.match(/www-widgetapi/)) return null; // Don't redirect YouTube Player API. +function redirect(url, type) { if (frontend == 'freeTube' && type === "main_frame") return `freetube://${url}`; else if (frontend == 'invidious') { + let instancesList = [...invidiousRedirectsChecks, ...invidiousCustomRedirects]; + if (instancesList.length === 0) return null; + let randomInstance = commonHelper.getRandomInstance(instancesList); + if (invidiousOnlyEmbeddedVideo && type !== "sub_frame") return null; if (invidiousAlwaysProxy != "DEFAULT") url.searchParams.append("local", invidiousAlwaysProxy); @@ -298,35 +338,21 @@ function redirect(url, initiator, type) { if (invidiousSubtitles.trim() != '') url.searchParams.append("subtitles", invidiousSubtitles); if (invidiousAutoplay != "DEFAULT") url.searchParams.append("autoplay", invidiousAutoplay); - let instancesList = [...invidiousRedirectsChecks, ...invidiousCustomRedirects]; - - if (instancesList.length === 0) - return null; - - let randomInstance = commonHelper.getRandomInstance(instancesList) - return `${randomInstance}${url.pathname.replace("/shorts/", "/watch?v=")}${url.search}`; } else if (frontend == 'piped') { - if (invidiousOnlyEmbeddedVideo && type !== "sub_frame") return null; - let instancesList = [...pipedRedirectsChecks, ...pipedCustomRedirects]; - if (instancesList.length === 0) return null; + let randomInstance = commonHelper.getRandomInstance(instancesList); - let randomInstance = commonHelper.getRandomInstance(instancesList) + if (invidiousOnlyEmbeddedVideo && type !== "sub_frame") return null; if (invidiousTheme != "DEFAULT") url.searchParams.append("theme", invidiousTheme); if (invidiousVolume != "--") url.searchParams.append("volume", invidiousVolume / 100); if (invidiousAutoplay != "DEFAULT") url.searchParams.append("playerAutoPlay", invidiousAutoplay); - if (url.hostname.endsWith("youtube.com") || url.hostname.endsWith("youtube-nocookie.com") || invidiousHostNames().includes(url.hostname)) - return `${randomInstance}${url.pathname.replace("/shorts/", "/watch?v=")}${url.search}`; - - if (url.hostname.endsWith("youtu.be") && url.pathname.length > 1) - return `${randomInstance}/watch?v=${url.pathname.substring(1)}`; - + return `${randomInstance}${url.pathname.replace("/shorts/", "/watch?v=")}${url.search}`; } return 'CANCEL'; } @@ -387,5 +413,8 @@ export default { getPipedCustomRedirects, setPipedCustomRedirects, + getAlwaysusePreferred, + setAlwaysusePreferred, + init, }; diff --git a/src/pages/background/background.js b/src/pages/background/background.js index 76ef849..8add9c3 100644 --- a/src/pages/background/background.js +++ b/src/pages/background/background.js @@ -47,7 +47,7 @@ browser.webRequest.onBeforeRequest.addListener( if (exceptionsHelper.isException(url)) newUrl = null; - else if (youtubeHelper.isYoutube(url)) newUrl = youtubeHelper.redirect(url, initiator, details.type) + else if (youtubeHelper.isYoutube(url, initiator)) newUrl = youtubeHelper.redirect(url, initiator, details.type) else if (twitterHelper.isTwitter(url)) newUrl = twitterHelper.redirect(url, initiator); diff --git a/src/pages/options/youtube/youtube.html b/src/pages/options/youtube/youtube.html index 767780f..b21e515 100644 --- a/src/pages/options/youtube/youtube.html +++ b/src/pages/options/youtube/youtube.html @@ -2,269 +2,271 @@ - - - - - - LibRedirect options: Twitter + + + + + + LibRedirect options: Twitter - + d="M19,5v14H5V5H19 M19,3H5C3.9,3,3,3.9,3,5v14c0,1.1,0.9,2,2,2h14c1.1,0,2-0.9,2-2V5C21,3.9,20.1,3,19,3L19,3z" /> + + + + + Medium + + -
+
+
+

Enable

+ +
+ +
+

Frontend

+ +
+ +
+ +
+
+

Only Redirect Embedded-Video

+ +
+ +
+

Always use Preffered Instances

+ +
+ +
+

Volume: 50%

+ + +
+ +
+

Theme

+ +
+ +
+

Autoplay Video

+ +
+
+ +
+
+

Player Style

+ +
+ +
+

Always proxy videos

+ +
+ +
+

Video Quality

+ +
+ +
+

Subtitles

+ +
+ +
+

Persist preferences (as cookie)

+ +
+ +
+ +
+

Default Instances

+
+
+
+
+
+

Custom Instances

+
+
-

Enable

- + +
+
+
+
+ + +
+
+ +
+

Default Instances

+
+
+
+
+
+

Custom Instances

+
+
-

Frontend

- + +
+
+
+
-
+
-
-
-

Only Redirect Embedded-Video

- -
- -
-

Volume: 50%

- - -
- -
-

Theme

- -
- -
-

Autoplay Video

- -
-
- -
-
-

Player Style

- -
- -
-

Always proxy videos

- -
- -
-

Video Quality

- -
- -
-

Subtitles

- -
- -
-

Persist preferences (as cookie)

- -
- -
- -
-

Default Instances

-
-
-
-
-
-

Custom Instances

-
-
-
- - -
-
-
- -
- - -
-
- -
-

Default Instances

-
-
-
-
-
-

Custom Instances

-
-
-
- - -
-
-
-
- -
- - - - - - + + + + + \ No newline at end of file diff --git a/src/pages/options/youtube/youtube.js b/src/pages/options/youtube/youtube.js index a007203..79330f4 100644 --- a/src/pages/options/youtube/youtube.js +++ b/src/pages/options/youtube/youtube.js @@ -6,6 +6,7 @@ let youtubeFrontendElement = document.getElementById("youtube-frontend"); let invidiousDivElement = document.getElementById("invidious") let pipedDivElement = document.getElementById("piped") let invidiousPipedDivElement = document.getElementById("invidious-piped") + function changeFrontendsSettings(frontend) { if (frontend == 'piped') { invidiousPipedDivElement.style.display = 'block' @@ -57,7 +58,6 @@ invidiousClearVolumeElement.addEventListener("click", } ); - let invidiousAutoplayElement = document.getElementById("invidious-autoplay"); invidiousAutoplayElement.addEventListener("change", (event) => youtubeHelper.setInvidiousAutoplay(event.target.options[invidiousAutoplayElement.selectedIndex].value) @@ -68,12 +68,18 @@ invidiousOnlyEmbeddedVideoElement.addEventListener("change", (event) => youtubeHelper.setInvidiousOnlyEmbeddedVideo(event.target.checked) ); +let alwaysUsePreferredElement = document.getElementById("always-use-preferred") +alwaysUsePreferredElement.addEventListener("change", + (event) => youtubeHelper.setAlwaysusePreferred(event.target.checked) +); + youtubeHelper.init().then(() => { disableYoutubeElement.checked = !youtubeHelper.getDisableYoutube(); invidiousThemeElement.checked = youtubeHelper.getInvidiousTheme(); invidiousVolumeElement.value = youtubeHelper.getInvidiousVolume(); invidiousVolumeValueElement.textContent = `${youtubeHelper.getInvidiousVolume()}%`; invidiousOnlyEmbeddedVideoElement.checked = youtubeHelper.getInvidiousOnlyEmbeddedVideo(); + alwaysUsePreferredElement.checked = youtubeHelper.getAlwaysusePreferred(); invidiousAutoplayElement.checked = youtubeHelper.getInvidiousAutoplay(); let frontend = youtubeHelper.getFrontend(); youtubeFrontendElement.value = frontend;