mirror of
https://github.com/LBRYFoundation/Watch-on-LBRY.git
synced 2025-08-23 17:47:26 +00:00
Update ytContent.tsx
Turns out YouTube doesn't destroy the `HTMLVideoElement` once its created, so no need to check if its destroyed
This commit is contained in:
parent
c7a839573a
commit
8c4f3e60e0
1 changed files with 24 additions and 32 deletions
|
@ -41,6 +41,8 @@ async function resolveYT(descriptor: YTDescriptor) {
|
|||
return segments.join('/');
|
||||
}
|
||||
|
||||
|
||||
|
||||
/** Compute the URL and determine whether or not a redirect should be performed. Delegates the redirect to callbacks. */
|
||||
async function handleURLChange(ctx: UpdateContext, { onRedirect, onURL }: UpdaterOptions): Promise<void> {
|
||||
if (onURL) onURL(ctx);
|
||||
|
@ -61,6 +63,7 @@ async function findMountPoint(): Promise<HTMLDivElement | void> {
|
|||
const div = document.createElement('div');
|
||||
div.style.display = 'flex';
|
||||
ownerBar.insertAdjacentElement('afterend', div);
|
||||
|
||||
return div;
|
||||
}
|
||||
|
||||
|
@ -96,13 +99,26 @@ function WatchOnLbryButton({ redirect = 'app', url: pathname, time }: { redirect
|
|||
|
||||
const mountPointPromise = findMountPoint();
|
||||
|
||||
|
||||
let ctxCache: UpdateContext | undefined
|
||||
const handle = (ctx: UpdateContext) => (ctxCache = ctx) && ctx.url && handleURLChange(ctx, {
|
||||
async onURL({ descriptor: { type }, url, redirect }) {
|
||||
const mountPoint = await mountPointPromise;
|
||||
if (type !== 'video' || !mountPoint) return;
|
||||
render(<WatchOnLbryButton url={url} redirect={redirect} />, mountPoint);
|
||||
},
|
||||
onRedirect({ redirect, url }) {
|
||||
const domain = redirectDomains[redirect];
|
||||
if (redirect === 'app') return openApp(domain.prefix + url);
|
||||
location.replace(domain.prefix + url);
|
||||
},
|
||||
});
|
||||
|
||||
{(async () => {
|
||||
let videoElement: HTMLVideoElement | null = null;
|
||||
let renderingButton = false
|
||||
|
||||
while(!(videoElement = document.querySelector('video'))) await sleep(200)
|
||||
|
||||
const handleTimeChange = () => {
|
||||
if (renderingButton) return
|
||||
if (!videoElement) return
|
||||
|
@ -115,33 +131,9 @@ let ctxCache: UpdateContext | undefined
|
|||
.then(() => renderingButton = false)
|
||||
}
|
||||
|
||||
while (true) {
|
||||
await sleep(200)
|
||||
if (!videoElement) {
|
||||
videoElement = document.querySelector('video')
|
||||
if (videoElement) videoElement.addEventListener('timeupdate', handleTimeChange)
|
||||
}
|
||||
else if (!videoElement.parentElement) {
|
||||
videoElement.removeEventListener('timeupdate', handleTimeChange)
|
||||
videoElement = null
|
||||
}
|
||||
}
|
||||
videoElement.addEventListener('timeupdate', handleTimeChange)
|
||||
})()}
|
||||
|
||||
|
||||
const handle = (ctx: UpdateContext) => (ctxCache = ctx) && ctx.url && handleURLChange(ctx, {
|
||||
async onURL({ descriptor: { type }, url, redirect }) {
|
||||
const mountPoint = await mountPointPromise;
|
||||
if (type !== 'video' || !mountPoint) return;
|
||||
render(<WatchOnLbryButton url={url} redirect={redirect} />, mountPoint);
|
||||
},
|
||||
onRedirect({ redirect, url }) {
|
||||
const domain = redirectDomains[redirect];
|
||||
if (redirect === 'app') return openApp(domain.prefix + url);
|
||||
location.replace(domain.prefix + url);
|
||||
},
|
||||
});
|
||||
|
||||
// handle the location on load of the page
|
||||
chrome.runtime.sendMessage({ url: location.href }, async (ctx: UpdateContext) => handle(ctx));
|
||||
|
||||
|
|
Loading…
Add table
Reference in a new issue