mirror of
https://github.com/LBRYFoundation/Watch-on-LBRY.git
synced 2025-08-23 17:47:26 +00:00
🍙 more bug fixes
This commit is contained in:
parent
15f03b3bcc
commit
6af354de0c
4 changed files with 40 additions and 41 deletions
|
@ -68,6 +68,7 @@ export function Dialogs(params: { manager: ReturnType<typeof createDialogManager
|
||||||
{`
|
{`
|
||||||
.alert-dialog
|
.alert-dialog
|
||||||
{
|
{
|
||||||
|
position: fixed;
|
||||||
border: none;
|
border: none;
|
||||||
background: var(--color-dark);
|
background: var(--color-dark);
|
||||||
color: var(--color-light);
|
color: var(--color-light);
|
||||||
|
|
|
@ -1,25 +1,21 @@
|
||||||
// This should only work in background
|
// This should only work in background
|
||||||
|
|
||||||
let db: IDBDatabase | null = null
|
|
||||||
|
|
||||||
if (typeof chrome.extension === 'undefined') throw new Error("YT urlCache can only be accessed from extension windows and service-workers.")
|
if (typeof chrome.extension === 'undefined') throw new Error("YT urlCache can only be accessed from extension windows and service-workers.")
|
||||||
|
|
||||||
if (typeof self.indexedDB !== 'undefined') {
|
let db = new Promise<IDBDatabase>((resolve, reject) => {
|
||||||
const openRequest = indexedDB.open("yt-url-resolver-cache")
|
if (typeof self.indexedDB !== 'undefined') {
|
||||||
openRequest.addEventListener('upgradeneeded', () => openRequest.result.createObjectStore("store").createIndex("expireAt", "expireAt"))
|
const openRequest = indexedDB.open("yt-url-resolver-cache")
|
||||||
|
openRequest.addEventListener('upgradeneeded', () => openRequest.result.createObjectStore("store").createIndex("expireAt", "expireAt"))
|
||||||
// Delete Expired
|
openRequest.addEventListener('success', () => {
|
||||||
openRequest.addEventListener('success', () => {
|
resolve(openRequest.result)
|
||||||
db = openRequest.result
|
clearExpired()
|
||||||
clearExpired()
|
}, { once: true })
|
||||||
})
|
}
|
||||||
}
|
else reject(`IndexedDB not supported`)
|
||||||
else console.warn(`IndexedDB not supported`)
|
})
|
||||||
|
|
||||||
async function clearExpired() {
|
async function clearExpired() {
|
||||||
return new Promise<void>((resolve, reject) => {
|
return new Promise<void>(async (resolve, reject) => {
|
||||||
if (!db) throw new Error(`IDBDatabase not defined.`)
|
const transaction = (await db).transaction("store", "readwrite")
|
||||||
const transaction = db.transaction("store", "readwrite")
|
|
||||||
const range = IDBKeyRange.upperBound(new Date())
|
const range = IDBKeyRange.upperBound(new Date())
|
||||||
|
|
||||||
const expireAtCursorRequest = transaction.objectStore("store").index("expireAt").openCursor(range)
|
const expireAtCursorRequest = transaction.objectStore("store").index("expireAt").openCursor(range)
|
||||||
|
@ -40,8 +36,8 @@ async function clearExpired() {
|
||||||
}
|
}
|
||||||
|
|
||||||
async function clearAll() {
|
async function clearAll() {
|
||||||
return await new Promise<void>((resolve, reject) => {
|
return await new Promise<void>(async (resolve, reject) => {
|
||||||
const store = db?.transaction("store", "readwrite").objectStore("store")
|
const store = (await db).transaction("store", "readwrite").objectStore("store")
|
||||||
if (!store) return resolve()
|
if (!store) return resolve()
|
||||||
const request = store.clear()
|
const request = store.clear()
|
||||||
request.addEventListener('success', () => resolve())
|
request.addEventListener('success', () => resolve())
|
||||||
|
@ -50,8 +46,8 @@ async function clearAll() {
|
||||||
}
|
}
|
||||||
|
|
||||||
async function put(url: string | null, id: string): Promise<void> {
|
async function put(url: string | null, id: string): Promise<void> {
|
||||||
return await new Promise((resolve, reject) => {
|
return await new Promise(async (resolve, reject) => {
|
||||||
const store = db?.transaction("store", "readwrite").objectStore("store")
|
const store = (await db).transaction("store", "readwrite").objectStore("store")
|
||||||
if (!store) return resolve()
|
if (!store) return resolve()
|
||||||
const expireAt = !url ? new Date(Date.now() + 1 * 60 * 60 * 1000) : new Date(Date.now() + 15 * 24 * 60 * 60 * 1000)
|
const expireAt = !url ? new Date(Date.now() + 1 * 60 * 60 * 1000) : new Date(Date.now() + 15 * 24 * 60 * 60 * 1000)
|
||||||
const request = store.put({ value: url, expireAt }, id)
|
const request = store.put({ value: url, expireAt }, id)
|
||||||
|
@ -65,8 +61,8 @@ async function put(url: string | null, id: string): Promise<void> {
|
||||||
// null means there is cache of that id has no lbrypathname
|
// null means there is cache of that id has no lbrypathname
|
||||||
// undefined means there is no cache
|
// undefined means there is no cache
|
||||||
async function get(id: string): Promise<string | null | undefined> {
|
async function get(id: string): Promise<string | null | undefined> {
|
||||||
const response = (await new Promise((resolve, reject) => {
|
const response = (await new Promise(async (resolve, reject) => {
|
||||||
const store = db?.transaction("store", "readonly").objectStore("store")
|
const store = (await db).transaction("store", "readonly").objectStore("store")
|
||||||
if (!store) return reject(`Can't find object store.`)
|
if (!store) return reject(`Can't find object store.`)
|
||||||
|
|
||||||
const request = store.get(id)
|
const request = store.get(id)
|
||||||
|
|
|
@ -23,7 +23,7 @@ chrome.runtime.onMessage.addListener(({ method, data }, sender, sendResponse) =>
|
||||||
console.log('lbrypathname request', params, await promise)
|
console.log('lbrypathname request', params, await promise)
|
||||||
resolve(await promise)
|
resolve(await promise)
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
sendResponse(`error:${(error as any).toString()}`)
|
sendResponse(`error: ${(error as any).toString()}`)
|
||||||
console.error(error)
|
console.error(error)
|
||||||
}
|
}
|
||||||
finally {
|
finally {
|
||||||
|
|
|
@ -226,14 +226,16 @@ import { getExtensionSettingsAsync, getSourcePlatfromSettingsFromHostname, getTa
|
||||||
const response = await new Promise<string | null | 'error'>((resolve) => chrome.runtime.sendMessage({ method: 'resolveUrl', data: JSON.stringify(params) }, resolve))
|
const response = await new Promise<string | null | 'error'>((resolve) => chrome.runtime.sendMessage({ method: 'resolveUrl', data: JSON.stringify(params) }, resolve))
|
||||||
if (response?.startsWith('error:')) {
|
if (response?.startsWith('error:')) {
|
||||||
console.error("Background error on:", params)
|
console.error("Background error on:", params)
|
||||||
throw new Error(`Background error.${response ?? ''}`)
|
throw new Error(`Background error. ${response ?? ''}`)
|
||||||
}
|
}
|
||||||
|
console.log(response)
|
||||||
return response ? JSON.parse(response) : null
|
return response ? JSON.parse(response) : null
|
||||||
}
|
}
|
||||||
|
|
||||||
// Request new tab
|
// Request new tab
|
||||||
async function openNewTab(url: URL, active: boolean) {
|
async function openNewTab(url: URL, active: boolean) {
|
||||||
chrome.runtime.sendMessage({ method: 'openTab', data: JSON.stringify({ href: url.href, active }) })
|
if (!open(url.href, '_blank'))
|
||||||
|
chrome.runtime.sendMessage({ method: 'openTab', data: JSON.stringify({ href: url.href, active }) })
|
||||||
}
|
}
|
||||||
|
|
||||||
function getLbryUrlByTarget(target: Target) {
|
function getLbryUrlByTarget(target: Target) {
|
||||||
|
@ -246,20 +248,20 @@ 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 () => {
|
await (async () => {
|
||||||
const source = await getSourceByUrl(new URL(location.href))
|
const source = await getSourceByUrl(new URL(location.href))
|
||||||
if (!source) return
|
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) return
|
if (!target) return
|
||||||
if (url.href === urlHrefCache) return
|
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) => {
|
||||||
|
@ -267,7 +269,7 @@ import { getExtensionSettingsAsync, getSourcePlatfromSettingsFromHostname, getTa
|
||||||
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
|
||||||
|
@ -276,7 +278,7 @@ import { getExtensionSettingsAsync, getSourcePlatfromSettingsFromHostname, getTa
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
openNewTab(lbryURL, document.hasFocus())
|
openNewTab(lbryURL, document.hasFocus())
|
||||||
|
|
||||||
if (window.history.length === 1) window.close()
|
if (window.history.length === 1) window.close()
|
||||||
else window.history.back()
|
else window.history.back()
|
||||||
}
|
}
|
||||||
|
@ -284,22 +286,22 @@ import { getExtensionSettingsAsync, getSourcePlatfromSettingsFromHostname, getTa
|
||||||
else {
|
else {
|
||||||
if (urlHrefCache !== url.href) updateButton(null)
|
if (urlHrefCache !== url.href) updateButton(null)
|
||||||
let target = (await getTargetsBySources(source))[source.id]
|
let target = (await getTargetsBySources(source))[source.id]
|
||||||
|
|
||||||
// There is no target found via API try to check Video Description for LBRY links.
|
// There is no target found via API try to check Video Description for LBRY links.
|
||||||
if (!target) {
|
if (!target) {
|
||||||
const linksContainer =
|
const linksContainer =
|
||||||
source.type === 'video' ?
|
source.type === 'video' ?
|
||||||
document.querySelector(source.platform.htmlQueries.videoDescription) :
|
document.querySelector(source.platform.htmlQueries.videoDescription) :
|
||||||
source.platform.htmlQueries.channelLinks ? document.querySelector(source.platform.htmlQueries.channelLinks) : null
|
source.platform.htmlQueries.channelLinks ? document.querySelector(source.platform.htmlQueries.channelLinks) : null
|
||||||
|
|
||||||
if (linksContainer) {
|
if (linksContainer) {
|
||||||
const anchors = Array.from(linksContainer.querySelectorAll<HTMLAnchorElement>('a'))
|
const anchors = Array.from(linksContainer.querySelectorAll<HTMLAnchorElement>('a'))
|
||||||
|
|
||||||
for (const anchor of anchors) {
|
for (const anchor of anchors) {
|
||||||
if (!anchor.href) continue
|
if (!anchor.href) continue
|
||||||
const url = new URL(anchor.href)
|
const url = new URL(anchor.href)
|
||||||
let lbryURL: URL | null = null
|
let lbryURL: URL | null = null
|
||||||
|
|
||||||
// Extract real link from youtube's redirect link
|
// Extract real link from youtube's redirect link
|
||||||
if (source.platform === sourcePlatfromSettings['youtube.com']) {
|
if (source.platform === sourcePlatfromSettings['youtube.com']) {
|
||||||
if (!targetPlatforms.some(([key, platform]) => url.searchParams.get('q')?.startsWith(platform.domainPrefix))) continue
|
if (!targetPlatforms.some(([key, platform]) => url.searchParams.get('q')?.startsWith(platform.domainPrefix))) continue
|
||||||
|
@ -310,7 +312,7 @@ import { getExtensionSettingsAsync, getSourcePlatfromSettingsFromHostname, getTa
|
||||||
if (!targetPlatforms.some(([key, platform]) => url.href.startsWith(platform.domainPrefix))) continue
|
if (!targetPlatforms.some(([key, platform]) => url.href.startsWith(platform.domainPrefix))) continue
|
||||||
lbryURL = new URL(url.href)
|
lbryURL = new URL(url.href)
|
||||||
}
|
}
|
||||||
|
|
||||||
if (lbryURL) {
|
if (lbryURL) {
|
||||||
target = {
|
target = {
|
||||||
lbryPathname: lbryURL.pathname.substring(1),
|
lbryPathname: lbryURL.pathname.substring(1),
|
||||||
|
@ -323,7 +325,7 @@ import { getExtensionSettingsAsync, getSourcePlatfromSettingsFromHostname, getTa
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
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
|
||||||
|
@ -331,7 +333,7 @@ import { getExtensionSettingsAsync, getSourcePlatfromSettingsFromHostname, getTa
|
||||||
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 })
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Reference in a new issue