diff --git a/src/scripts/tabOnUpdated.ts b/src/scripts/tabOnUpdated.ts index cf67bfc..06e3644 100644 --- a/src/scripts/tabOnUpdated.ts +++ b/src/scripts/tabOnUpdated.ts @@ -39,36 +39,4 @@ async function ctxFromURL(href: string): Promise { await promise delete ctxFromURLOnGoingPromise[descriptor.id] return await promise -} - -// 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)); -}); +} \ No newline at end of file diff --git a/src/scripts/ytContent.tsx b/src/scripts/ytContent.tsx index 3281698..86e9f99 100644 --- a/src/scripts/ytContent.tsx +++ b/src/scripts/ytContent.tsx @@ -1,6 +1,7 @@ -import { getSourcePlatfromSettingsFromHostname, TargetPlatformName, targetPlatformSettings } from '../common/settings' +import { ExtensionSettings, getExtensionSettingsAsync, getSourcePlatfromSettingsFromHostname, TargetPlatformName, targetPlatformSettings } from '../common/settings' import type { UpdateContext } from '../scripts/tabOnUpdated' import { h, JSX, render } from 'preact' +import { YtIdResolverDescriptor, ytService } from '../common/yt' const sleep = (t: number) => new Promise(resolve => setTimeout(resolve, t)); @@ -159,21 +160,37 @@ function redirectTo({ targetPlatform, lbryPathname }: UpdateContext): void { findButtonMountPoint().then(() => updateButton(ctxCache)) findVideoElement().then(() => updateButton(ctxCache)) +async function onPageLoad() +{ + // Listen History.pushState + { + const originalPushState = history.pushState + history.pushState = function(...params) { onPushState(); return originalPushState(...params) } + } -/** Request UpdateContext from background */ -const requestCtxFromUrl = async (url: string) => await new Promise((resolve) => chrome.runtime.sendMessage({ url }, resolve)) + const settings = await getExtensionSettingsAsync('redirect', 'targetPlatform', 'urlResolver') + + // Listen Settings Change + chrome.storage.onChanged.addListener(async (changes, areaName) => { + if (areaName !== 'local') return; + Object.assign(settings, changes) + }); -/** Handle the location on load of the page */ -requestCtxFromUrl(location.href).then((ctx) => handleURLChange(ctx)) + async function updateByURL(url: URL) + { + if (url.pathname !== '/watch') return + const videoId = url.searchParams.get('v') + if (!videoId) return + const descriptor: YtIdResolverDescriptor = { id: videoId, type: 'video' } + const lbryPathname = (await ytService.resolveById([descriptor]))[0] + if (!lbryPathname) return + updateButton({ descriptor, lbryPathname, redirect: settings.redirect, targetPlatform: settings.targetPlatform }) + } -/* - * Gets messages from background script which relays tab update events. This is because there's no sensible way to detect - * history.pushState changes from a content script - */ -chrome.runtime.onMessage.addListener(async (ctx: UpdateContext) => handleURLChange(ctx)); + async function onPushState() + { + await updateByURL(new URL(location.href)) + } -/** On settings change */ -chrome.storage.onChanged.addListener(async (changes, areaName) => { - if (areaName !== 'local') return; - if (changes.targetPlatform) handleURLChange(await requestCtxFromUrl(location.href)) -}); \ No newline at end of file + await updateByURL(new URL(location.href)) +} \ No newline at end of file