Fix for companion use on Android

This commit is contained in:
WardPearce
2025-08-19 18:10:26 +12:00
parent f2cfa0305b
commit c67080bf55
2 changed files with 29 additions and 12 deletions
+24 -11
View File
@@ -1,22 +1,37 @@
export async function dashManifestDomainInclusion(manifestUrl: string): Promise<string> {
/* If on android, must manually format dash manifest to include URL. */
const response = await fetch(manifestUrl, {
// Must fetch the headers of the request
// using our custom android proxy logic
const respIvg = await fetch(manifestUrl, {
method: 'GET',
redirect: 'follow'
headers: {
__custom_return: 'json-headers',
__redirect: 'manual'
}
});
if (!response.ok) {
throw Error('Unable to fetch manifest');
if (!respIvg.ok) {
throw Error('Unable to make request to Invidious');
}
const finalUrl = response.url;
const manifestText = await response.text();
const ivgHeaders = await respIvg.json();
// If location isn't present, then use base manifest URL.
const companionUrl = 'location' in ivgHeaders ? ivgHeaders['location'] : manifestUrl;
const respCompanion = await fetch(companionUrl, {
method: 'GET'
});
if (!respCompanion.ok) {
throw Error('Unable to make request to Companion');
}
const manifestText = await respCompanion.text();
const parser = new DOMParser();
const xmlDoc = parser.parseFromString(manifestText, 'application/xml');
// Find all BaseURL elements
const baseUrlElements = xmlDoc.getElementsByTagName('BaseURL');
for (let i = 0; i < baseUrlElements.length; i++) {
@@ -24,8 +39,8 @@ export async function dashManifestDomainInclusion(manifestUrl: string): Promise<
const baseUrlValue = baseUrlElement.textContent;
if (baseUrlValue && (baseUrlValue.startsWith('/') || !baseUrlValue.includes('://'))) {
const finalUrlObj = new URL(finalUrl);
const baseDomain = `${finalUrlObj.protocol}//${finalUrlObj.host}`;
const companionUrlObj = new URL(companionUrl);
const baseDomain = `${companionUrlObj.protocol}//${companionUrlObj.host}`;
const resolvedUrl = new URL(baseUrlValue, baseDomain).href;
baseUrlElement.textContent = resolvedUrl;
}
@@ -34,7 +49,5 @@ export async function dashManifestDomainInclusion(manifestUrl: string): Promise<
const serializer = new XMLSerializer();
const updatedManifest = serializer.serializeToString(xmlDoc);
console.log(updatedManifest);
return `data:application/dash+xml;base64,${btoa(updatedManifest)}`;
}
@@ -401,7 +401,11 @@
dashUrl += '?local=true';
}
if (Capacitor.getPlatform() === 'android' && $playerProxyVideosStore) {
if (
Capacitor.getPlatform() === 'android' &&
$playerProxyVideosStore &&
!data.video.fallbackPatch
) {
const manifest = await dashManifestDomainInclusion(dashUrl);
await player.load(manifest, await getLastPlayPos());
} else {