mirror of
https://github.com/LBRYFoundation/Watch-on-LBRY.git
synced 2025-08-23 17:47:26 +00:00
- Told browser to not to cache the api request - Removed manual memory cache - Implimented caching using indexedDB - Cache time is 1 day
67 lines
2.7 KiB
TypeScript
67 lines
2.7 KiB
TypeScript
import { appRedirectUrl, parseProtocolUrl } from '../common/lbry-url'
|
|
import { getExtensionSettingsAsync, getSourcePlatfromSettingsFromHostname, TargetPlatformName } from '../common/settings'
|
|
import { YtIdResolverDescriptor, ytService } from '../common/yt'
|
|
export interface UpdateContext {
|
|
descriptor: YtIdResolverDescriptor
|
|
/** LBRY URL fragment */
|
|
lbryPathname: string
|
|
redirect: boolean
|
|
targetPlatform: TargetPlatformName
|
|
}
|
|
|
|
async function resolveYT(descriptor: YtIdResolverDescriptor) {
|
|
const lbryProtocolUrl: string | null = await ytService.resolveById([descriptor]).then(a => a[0]);
|
|
const segments = parseProtocolUrl(lbryProtocolUrl || '', { encode: true });
|
|
if (segments.length === 0) return;
|
|
return segments.join('/');
|
|
}
|
|
|
|
async function ctxFromURL(href: string): Promise<UpdateContext | void> {
|
|
if (!href) return;
|
|
|
|
const url = new URL(href);
|
|
if (!getSourcePlatfromSettingsFromHostname(url.hostname)) return
|
|
if (!(url.pathname.startsWith('/watch') || url.pathname.startsWith('/channel'))) return
|
|
|
|
const descriptor = ytService.getId(href);
|
|
if (!descriptor) return; // couldn't get the ID, so we're done
|
|
|
|
// NOTE: API call cached by resolveYT method automatically
|
|
const res = await resolveYT(descriptor);
|
|
if (!res) return; // couldn't find it on lbry, so we're done
|
|
|
|
const { redirect, targetPlatform } = await getExtensionSettingsAsync('redirect', 'targetPlatform');
|
|
return { descriptor, lbryPathname: res, redirect, targetPlatform };
|
|
}
|
|
|
|
// handles lbry.tv -> lbry app redirect
|
|
chrome.tabs.onUpdated.addListener(async (tabId, changeInfo, { url: tabUrl }) => {
|
|
const { redirect, targetPlatform } = await getExtensionSettingsAsync('redirect', 'targetPlatform');
|
|
if (!redirect || targetPlatform !== 'app' || !changeInfo.url || !tabUrl?.startsWith('https://odysee.com/')) return;
|
|
|
|
const url = appRedirectUrl(tabUrl, { encode: true });
|
|
if (!url) return;
|
|
chrome.tabs.update(tabId, { url });
|
|
alert('Opened link in LBRY App!'); // Better for UX since sometimes LBRY App doesn't take focus, if that is fixed, this can be removed
|
|
chrome.tabs.executeScript(tabId, {
|
|
code: `if (window.history.length === 1) {
|
|
window.close();
|
|
} else {
|
|
window.history.back();
|
|
}
|
|
document.querySelectorAll('video').forEach(v => v.pause());
|
|
`
|
|
});
|
|
});
|
|
|
|
chrome.runtime.onMessage.addListener(({ url }: { url: string }, sender, sendResponse) => {
|
|
ctxFromURL(url).then(ctx => {
|
|
sendResponse(ctx);
|
|
})
|
|
return true;
|
|
})
|
|
|
|
// relay youtube link changes to the content script
|
|
chrome.tabs.onUpdated.addListener((tabId, changeInfo, { url }) => {
|
|
if (url) ctxFromURL(url).then(ctx => chrome.tabs.sendMessage(tabId, ctx));
|
|
});
|