mirror of
https://github.com/LBRYFoundation/Watch-on-LBRY.git
synced 2025-08-23 17:47:26 +00:00
- Added more types, so when there is an error it's more visible. - Default setting was using `lbry.tv` which doesn't exists anymore, so i made it odysee. - Changed `redirect` value name in the `LbrySettings` to `platform` which makes more sense to this version. - Changed `url` in UpdateContext to `pathname`. Using `url` for the full URL. - Rewrote most of the `ytContent.tsx` so the timestamp feature doesnt look like a patch.
67 lines
2.6 KiB
TypeScript
67 lines
2.6 KiB
TypeScript
import { appRedirectUrl, parseProtocolUrl } from '../common/lbry-url'
|
|
import { getSettingsAsync, PlatformName } from '../common/settings'
|
|
import { YTDescriptor, ytService } from '../common/yt'
|
|
export interface UpdateContext {
|
|
descriptor: YTDescriptor
|
|
/** LBRY URL fragment */
|
|
pathname: string
|
|
enabled: boolean
|
|
platform: PlatformName
|
|
}
|
|
|
|
async function resolveYT(descriptor: YTDescriptor) {
|
|
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('/');
|
|
}
|
|
|
|
const pathnameCache: Record<string, string | undefined> = {};
|
|
|
|
async function ctxFromURL(url: string): Promise<UpdateContext | void> {
|
|
if (!url || !(url.startsWith('https://www.youtube.com/watch?v=') || url.startsWith('https://www.youtube.com/channel/'))) return;
|
|
url = new URL(url).href;
|
|
const { enabled, platform } = await getSettingsAsync('enabled', 'platform');
|
|
const descriptor = ytService.getId(url);
|
|
if (!descriptor) return; // couldn't get the ID, so we're done
|
|
|
|
const res = url in pathnameCache ? pathnameCache[url] : await resolveYT(descriptor);
|
|
pathnameCache[url] = res;
|
|
if (!res) return; // couldn't find it on lbry, so we're done
|
|
|
|
return { descriptor, pathname: res, enabled, platform };
|
|
}
|
|
|
|
// handles lbry.tv -> lbry app redirect
|
|
chrome.tabs.onUpdated.addListener(async (tabId, changeInfo, { url: tabUrl }) => {
|
|
const { enabled, platform } = await getSettingsAsync('enabled', 'platform');
|
|
if (!enabled || platform !== '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 (!changeInfo.url || !url || !(url.startsWith('https://www.youtube.com/watch?v=') || url.startsWith('https://www.youtube.com/channel/'))) return;
|
|
|
|
ctxFromURL(url).then(ctx => chrome.tabs.sendMessage(tabId, ctx));
|
|
});
|