diff --git a/src/scripts/ytContent.tsx b/src/scripts/ytContent.tsx
index 91d3ba9..e4294cc 100644
--- a/src/scripts/ytContent.tsx
+++ b/src/scripts/ytContent.tsx
@@ -49,7 +49,11 @@ import { getExtensionSettingsAsync, getSourcePlatfromSettingsFromHostname, getTa
fontSize: '14px',
textDecoration: 'none',
...target.platform.button.style?.button,
- }}>
+ }}
+ onClick={() => findVideoElementAwait().then((videoElement) => {
+ videoElement.pause()
+ })}
+ >
{target.platform.button.text}
@@ -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
async function requestResolveById(...params: Parameters): ReturnType {
const json = await new Promise((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
}
@@ -152,72 +160,77 @@ import { getExtensionSettingsAsync, getSourcePlatfromSettingsFromHostname, getTa
while (true) {
await sleep(500)
- const url = new URL(location.href);
- let target = (await getTargetsByURL(url))[url.href]
-
- if (settings.redirect) {
- if (!target) continue
- if (url === urlCache) continue
-
- const lbryURL = getLbryUrlByTarget(target)
-
- findVideoElementAwait().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 }))
- // Its not gonna be able to replace anyway
- // This was empty window doesnt stay open
- location.replace(lbryURL)
+ const url: URL = (urlCache?.href === location.href) ? urlCache : new URL(location.href);
+ try {
+ if (settings.redirect) {
+ const target = (await getTargetsByURL(url))[url.href]
+ if (!target) continue
+ if (url === urlCache) continue
+
+ const lbryURL = getLbryUrlByTarget(target)
+
+ findVideoElementAwait().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 }))
+ // Its not gonna be able to replace anyway
+ // This was empty window doesnt stay open
+ location.replace(lbryURL)
+ }
+ else {
+ open(lbryURL, '_blank')
+ if (window.history.length === 1) window.close()
+ else window.history.back()
+ }
}
else {
- open(lbryURL, '_blank')
- if (window.history.length === 1) window.close()
- else window.history.back()
- }
- }
- else {
- if (!target) {
- const descriptionElement = document.querySelector(sourcePlatform.htmlQueries.videoDescription)
- if (descriptionElement) {
- const anchors = Array.from(descriptionElement.querySelectorAll('a'))
-
- for (const anchor of anchors) {
- const url = new URL(anchor.href)
- let lbryURL: URL | null = null
- if (sourcePlatform === sourcePlatfromSettings['youtube.com']) {
- if (!targetPlatforms.some(([key, platform]) => url.searchParams.get('q')?.startsWith(platform.domainPrefix))) continue
- lbryURL = new URL(url.searchParams.get('q')!)
- }
- 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: 'video',
- platform: targetPlatformSettings[settings.targetPlatform]
+ if (urlCache !== url) updateButton(null)
+ let target = (await getTargetsByURL(url))[url.href]
+ if (!target) {
+ const descriptionElement = document.querySelector(sourcePlatform.htmlQueries.videoDescription)
+ if (descriptionElement) {
+ const anchors = Array.from(descriptionElement.querySelectorAll('a'))
+
+ for (const anchor of anchors) {
+ if (!anchor.href) continue
+ const url = new URL(anchor.href)
+ let lbryURL: URL | null = null
+ if (sourcePlatform === sourcePlatfromSettings['youtube.com']) {
+ if (!targetPlatforms.some(([key, platform]) => url.searchParams.get('q')?.startsWith(platform.domainPrefix))) continue
+ lbryURL = new URL(url.searchParams.get('q')!)
+ }
+ 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: 'video',
+ platform: targetPlatformSettings[settings.targetPlatform]
+ }
+ break
}
- break
}
}
}
+
+ if (target?.type === 'video') {
+ const videoElement = document.querySelector(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)
}
-
- if (target) {
- const videoElement = document.querySelector(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) {
+ console.error(error)
}
-
urlCache = url
}