Merge pull request #125 from DeepDoge/master

Made yt video pause, since we use new tab now
This commit is contained in:
kodxana 2022-07-08 23:24:19 +02:00 committed by GitHub
commit 1f29fc7c29
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -49,7 +49,11 @@ import { getExtensionSettingsAsync, getSourcePlatfromSettingsFromHostname, getTa
fontSize: '14px', fontSize: '14px',
textDecoration: 'none', textDecoration: 'none',
...target.platform.button.style?.button, ...target.platform.button.style?.button,
}}> }}
onClick={() => findVideoElementAwait().then((videoElement) => {
videoElement.pause()
})}
>
<img src={target.platform.button.icon} height={16} <img src={target.platform.button.icon} height={16}
style={{ transform: 'scale(1.5)', ...target.platform.button.style?.icon }} /> style={{ transform: 'scale(1.5)', ...target.platform.button.style?.icon }} />
<span>{target.platform.button.text}</span> <span>{target.platform.button.text}</span>
@ -137,7 +141,11 @@ import { getExtensionSettingsAsync, getSourcePlatfromSettingsFromHostname, getTa
// We should get this from background, so the caching works and we don't get errors in the future if yt decides to impliment CORS // We should get this from background, so the caching works and we don't get errors in the future if yt decides to impliment CORS
async function requestResolveById(...params: Parameters<typeof resolveById>): ReturnType<typeof resolveById> { async function requestResolveById(...params: Parameters<typeof resolveById>): ReturnType<typeof resolveById> {
const json = await new Promise<string | null | 'error'>((resolve) => chrome.runtime.sendMessage({ json: JSON.stringify(params) }, resolve)) const json = await new Promise<string | null | 'error'>((resolve) => chrome.runtime.sendMessage({ json: JSON.stringify(params) }, resolve))
if (json === 'error') throw new Error("Background error.") if (json === 'error')
{
console.error("Background error on:", params)
throw new Error("Background error.")
}
return json ? JSON.parse(json) : null return json ? JSON.parse(json) : null
} }
@ -152,72 +160,77 @@ import { getExtensionSettingsAsync, getSourcePlatfromSettingsFromHostname, getTa
while (true) { while (true) {
await sleep(500) await sleep(500)
const url = new URL(location.href); const url: URL = (urlCache?.href === location.href) ? urlCache : new URL(location.href);
let target = (await getTargetsByURL(url))[url.href] try {
if (settings.redirect) {
if (settings.redirect) { const target = (await getTargetsByURL(url))[url.href]
if (!target) continue if (!target) continue
if (url === urlCache) continue if (url === urlCache) continue
const lbryURL = getLbryUrlByTarget(target) const lbryURL = getLbryUrlByTarget(target)
findVideoElementAwait().then((videoElement) => { findVideoElementAwait().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 }))
// Its not gonna be able to replace anyway // Its not gonna be able to replace anyway
// This was empty window doesnt stay open // This was empty window doesnt stay open
location.replace(lbryURL) location.replace(lbryURL)
}
else {
open(lbryURL, '_blank')
if (window.history.length === 1) window.close()
else window.history.back()
}
} }
else { else {
open(lbryURL, '_blank') if (urlCache !== url) updateButton(null)
if (window.history.length === 1) window.close() let target = (await getTargetsByURL(url))[url.href]
else window.history.back() if (!target) {
} const descriptionElement = document.querySelector(sourcePlatform.htmlQueries.videoDescription)
} if (descriptionElement) {
else { const anchors = Array.from(descriptionElement.querySelectorAll<HTMLAnchorElement>('a'))
if (!target) {
const descriptionElement = document.querySelector(sourcePlatform.htmlQueries.videoDescription) for (const anchor of anchors) {
if (descriptionElement) { if (!anchor.href) continue
const anchors = Array.from(descriptionElement.querySelectorAll<HTMLAnchorElement>('a')) const url = new URL(anchor.href)
let lbryURL: URL | null = null
for (const anchor of anchors) { if (sourcePlatform === 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')!)
if (sourcePlatform === sourcePlatfromSettings['youtube.com']) { }
if (!targetPlatforms.some(([key, platform]) => url.searchParams.get('q')?.startsWith(platform.domainPrefix))) continue else {
lbryURL = new URL(url.searchParams.get('q')!) if (!targetPlatforms.some(([key, platform]) => url.href.startsWith(platform.domainPrefix))) continue
} lbryURL = new URL(url.href)
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),
if (lbryURL) { time: null,
target = { type: 'video',
lbryPathname: lbryURL.pathname.substring(1), platform: targetPlatformSettings[settings.targetPlatform]
time: null, }
type: 'video', break
platform: targetPlatformSettings[settings.targetPlatform]
} }
break
} }
} }
} }
if (target?.type === 'video') {
const videoElement = document.querySelector<HTMLVideoElement>(sourcePlatform.htmlQueries.videoPlayer)
if (videoElement) target.time = videoElement.currentTime > 3 && videoElement.currentTime < videoElement.duration - 1 ? videoElement.currentTime : null
}
// We run it anyway with null target to hide the button
updateButton(target)
} }
} catch (error) {
if (target) { console.error(error)
const videoElement = document.querySelector<HTMLVideoElement>(sourcePlatform.htmlQueries.videoPlayer)
if (videoElement) target.time = videoElement.currentTime > 3 && videoElement.currentTime < videoElement.duration - 1 ? videoElement.currentTime : null
}
// We run it anyway with null target to hide the button
updateButton(target)
} }
urlCache = url urlCache = url
} }