Added feature request #35

This commit is contained in:
ManeraKai 2022-02-14 22:32:30 +03:00
parent 7b54087a63
commit 2a1c3ea554
No known key found for this signature in database
GPG Key ID: 5ABC31FFD562E337
4 changed files with 326 additions and 289 deletions

View File

@ -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 targets.some((rx) => rx.test(url.href))
return isTargets | isPiped;
}
if (frontend == 'piped') {
if (alwaysusePreferred)
return isTargets | isPiped | isInvidious;
else
return isTargets | isInvidious;
}
else
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 'CANCEL';
}
@ -387,5 +413,8 @@ export default {
getPipedCustomRedirects,
setPipedCustomRedirects,
getAlwaysusePreferred,
setAlwaysusePreferred,
init,
};

View File

@ -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);

View File

@ -131,12 +131,16 @@
<input id="only-embed" type="checkbox" checked />
</div>
<div class="some-block option-block">
<h4>Always use Preffered Instances</h4>
<input id="always-use-preferred" type="checkbox" checked />
</div>
<div class="some-block option-block">
<h4>Volume: <span id="volume-value">50%</span></h4>
<input id="invidious-volume" name="invidious-volume" type="range" min="0" max="100" step="1" />
<button type="button" class="default" id="clear-invidious-volume">
<svg xmlns="http://www.w3.org/2000/svg" height="24px" viewBox="0 0 24 24" width="24px"
fill="currentColor">
<svg xmlns="http://www.w3.org/2000/svg" height="24px" viewBox="0 0 24 24" width="24px" fill="currentColor">
<path d="M0 0h24v24H0V0z" fill="none" />
<path
d="M19 6.41L17.59 5 12 10.59 6.41 5 5 6.41 10.59 12 5 17.59 6.41 19 12 13.41 17.59 19 19 17.59 13.41 12 19 6.41z" />
@ -218,8 +222,7 @@
<div class="some-block option-block">
<input id="invidious-custom-instance" placeholder="https://invidious.com" type="url" />
<button type="submit" class="add" id="invidious-add-instance">
<svg xmlns="http://www.w3.org/2000/svg" height="20px" viewBox="0 0 24 24" width="20px"
fill="currentColor">
<svg xmlns="http://www.w3.org/2000/svg" height="20px" viewBox="0 0 24 24" width="20px" fill="currentColor">
<path d="M0 0h24v24H0V0z" fill="none" />
<path d="M19 13h-6v6h-2v-6H5v-2h6V5h2v6h6v2z" />
</svg>
@ -247,8 +250,7 @@
<div class="some-block option-block">
<input id="piped-custom-instance" placeholder="https://piped.com" type="url" />
<button type="submit" class="add" id="piped-add-instance">
<svg xmlns="http://www.w3.org/2000/svg" height="20px" viewBox="0 0 24 24" width="20px"
fill="currentColor">
<svg xmlns="http://www.w3.org/2000/svg" height="20px" viewBox="0 0 24 24" width="20px" fill="currentColor">
<path d="M0 0h24v24H0V0z" fill="none" />
<path d="M19 13h-6v6h-2v-6H5v-2h6V5h2v6h6v2z" />
</svg>

View File

@ -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;