diff --git a/src/scripts/ytContent.tsx b/src/scripts/ytContent.tsx index 4b24c2a..3e8cebc 100644 --- a/src/scripts/ytContent.tsx +++ b/src/scripts/ytContent.tsx @@ -246,97 +246,102 @@ import { getExtensionSettingsAsync, getSourcePlatfromSettingsFromHostname, getTa let urlHrefCache: string | null = null while (true) { await sleep(500) - - const url: URL = new URL(location.href) - const source = await getSourceByUrl(new URL(location.href)) - if (!source) continue - - try { - if (settings.redirect) { - const target = (await getTargetsBySources(source))[source.id] - if (!target) continue - if (url.href === urlHrefCache) continue - - const lbryURL = getLbryUrlByTarget(target) - - if (source.type === 'video') { - // As soon as video play is ready and start playing, pause it. - findVideoElementAwait(source).then((videoElement) => { - videoElement.addEventListener('play', () => videoElement.pause(), { once: true }) - videoElement.pause() - }) - } - - if (target.platform === targetPlatformSettings.app) { - if (document.hidden) await new Promise((resolve) => document.addEventListener('visibilitychange', resolve, { once: true })) - // Replace is being used so browser doesnt start an empty window - // Its not gonna be able to replace anyway, since its a LBRY Uri - location.replace(lbryURL) + const url: URL = new URL(location.href); + + await (async () => { + const source = await getSourceByUrl(new URL(location.href)) + if (!source) return + + try { + if (settings.redirect) { + const target = (await getTargetsBySources(source))[source.id] + if (!target) return + console.log(url.href, urlHrefCache) + if (url.href === urlHrefCache) return + + const lbryURL = getLbryUrlByTarget(target) + + if (source.type === 'video') { + // As soon as video play is ready and start playing, pause it. + findVideoElementAwait(source).then((videoElement) => { + videoElement.addEventListener('play', () => videoElement.pause(), { once: true }) + videoElement.pause() + }) + } + + if (target.platform === targetPlatformSettings.app) { + if (document.hidden) await new Promise((resolve) => document.addEventListener('visibilitychange', resolve, { once: true })) + // Replace is being used so browser doesnt start an empty window + // Its not gonna be able to replace anyway, since its a LBRY Uri + location.replace(lbryURL) + } + else { + console.log('open', lbryURL.href) + openNewTab(lbryURL, document.hasFocus()) + + if (window.history.length === 1) window.close() + else window.history.back() + } } else { - openNewTab(lbryURL, document.hasFocus()) - - if (window.history.length === 1) window.close() - else window.history.back() - } - } - else { - if (urlHrefCache !== url.href) updateButton(null) - let target = (await getTargetsBySources(source))[source.id] - - // There is no target found via API try to check Video Description for LBRY links. - if (!target) { - const linksContainer = - source.type === 'video' ? - document.querySelector(source.platform.htmlQueries.videoDescription) : - source.platform.htmlQueries.channelLinks ? document.querySelector(source.platform.htmlQueries.channelLinks) : null - - if (linksContainer) { - const anchors = Array.from(linksContainer.querySelectorAll('a')) - - for (const anchor of anchors) { - if (!anchor.href) continue - const url = new URL(anchor.href) - let lbryURL: URL | null = null - - // Extract real link from youtube's redirect link - if (source.platform === sourcePlatfromSettings['youtube.com']) { - if (!targetPlatforms.some(([key, platform]) => url.searchParams.get('q')?.startsWith(platform.domainPrefix))) continue - lbryURL = new URL(url.searchParams.get('q')!) - } - // Just directly use the link itself on other platforms - else { - if (!targetPlatforms.some(([key, platform]) => url.href.startsWith(platform.domainPrefix))) continue - lbryURL = new URL(url.href) - } - - if (lbryURL) { - target = { - lbryPathname: lbryURL.pathname.substring(1), - time: null, - type: lbryURL.pathname.substring(1).includes('/') ? 'video' : 'channel', - platform: targetPlatformSettings[settings.targetPlatform] + if (urlHrefCache !== url.href) updateButton(null) + let target = (await getTargetsBySources(source))[source.id] + + // There is no target found via API try to check Video Description for LBRY links. + if (!target) { + const linksContainer = + source.type === 'video' ? + document.querySelector(source.platform.htmlQueries.videoDescription) : + source.platform.htmlQueries.channelLinks ? document.querySelector(source.platform.htmlQueries.channelLinks) : null + + if (linksContainer) { + const anchors = Array.from(linksContainer.querySelectorAll('a')) + + for (const anchor of anchors) { + if (!anchor.href) continue + const url = new URL(anchor.href) + let lbryURL: URL | null = null + + // Extract real link from youtube's redirect link + if (source.platform === sourcePlatfromSettings['youtube.com']) { + if (!targetPlatforms.some(([key, platform]) => url.searchParams.get('q')?.startsWith(platform.domainPrefix))) continue + lbryURL = new URL(url.searchParams.get('q')!) + } + // Just directly use the link itself on other platforms + else { + if (!targetPlatforms.some(([key, platform]) => url.href.startsWith(platform.domainPrefix))) continue + lbryURL = new URL(url.href) + } + + if (lbryURL) { + target = { + lbryPathname: lbryURL.pathname.substring(1), + time: null, + type: lbryURL.pathname.substring(1).includes('/') ? 'video' : 'channel', + platform: targetPlatformSettings[settings.targetPlatform] + } + break } - break } } } - } - - if (!target) updateButton(null) - else { - // If target is a video target add timestampt to it - if (target.type === 'video') { - const videoElement = document.querySelector(source.platform.htmlQueries.videoPlayer) - if (videoElement) target.time = videoElement.currentTime > 3 && videoElement.currentTime < videoElement.duration - 1 ? videoElement.currentTime : null + + if (!target) updateButton(null) + else { + // If target is a video target add timestampt to it + if (target.type === 'video') { + const videoElement = document.querySelector(source.platform.htmlQueries.videoPlayer) + if (videoElement) target.time = videoElement.currentTime > 3 && videoElement.currentTime < videoElement.duration - 1 ? videoElement.currentTime : null + } + + updateButton({ target, source }) } - - updateButton({ target, source }) } + } catch (error) { + console.error(error) } - } catch (error) { - console.error(error) - } + })() + urlHrefCache = url.href }