🍣 bug fixes

This commit is contained in:
Shiba 2022-08-09 16:49:45 +00:00
parent 619d66d6aa
commit 4295dbe1fe

View file

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