mirror of
https://github.com/LBRYFoundation/Watch-on-LBRY.git
synced 2025-08-23 17:47:26 +00:00
Merge pull request #101 from DeepDoge/master
Button wasn't being updated when we change target platform
This commit is contained in:
commit
27207e0f9c
3 changed files with 31 additions and 16 deletions
|
@ -52,7 +52,9 @@ async function put(url: string | null, id: string): Promise<void> {
|
||||||
return await new Promise((resolve, reject) => {
|
return await new Promise((resolve, reject) => {
|
||||||
const store = db?.transaction("store", "readwrite").objectStore("store")
|
const store = db?.transaction("store", "readwrite").objectStore("store")
|
||||||
if (!store) return resolve()
|
if (!store) return resolve()
|
||||||
const request = store.put({ value: url, expireAt: !url ? new Date(Date.now() + 1 * 60 * 60 * 1000) : new Date(Date.now() + 15 * 24 * 60 * 60 * 1000) }, id)
|
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)
|
||||||
|
console.log('caching', id, url, 'until:', expireAt)
|
||||||
request.addEventListener('success', () => resolve())
|
request.addEventListener('success', () => resolve())
|
||||||
request.addEventListener('error', () => reject(request.error))
|
request.addEventListener('error', () => reject(request.error))
|
||||||
})
|
})
|
||||||
|
@ -76,6 +78,7 @@ async function get(id: string): Promise<string | null | undefined> {
|
||||||
await clearExpired()
|
await clearExpired()
|
||||||
return undefined
|
return undefined
|
||||||
}
|
}
|
||||||
|
console.log('cache found', id, response.value)
|
||||||
return response.value
|
return response.value
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -1,23 +1,35 @@
|
||||||
import { parseProtocolUrl } from '../common/lbry-url'
|
|
||||||
import { resolveById, YtIdResolverDescriptor } from '../common/yt/urlResolve'
|
import { resolveById, YtIdResolverDescriptor } from '../common/yt/urlResolve'
|
||||||
|
|
||||||
async function resolveYT(descriptor: YtIdResolverDescriptor) {
|
async function resolveYT(descriptor: YtIdResolverDescriptor) {
|
||||||
const lbryProtocolUrl: string | null = await resolveById([descriptor]).then(a => a[0])
|
const lbryProtocolUrl: string | null = (await resolveById([descriptor]).then(a => a[0])) ?? null
|
||||||
const segments = parseProtocolUrl(lbryProtocolUrl || '', { encode: true })
|
if (!lbryProtocolUrl) return null
|
||||||
if (segments.length === 0) return
|
return lbryProtocolUrl.replaceAll('#', ':')
|
||||||
return segments.join('/')
|
/* const segments = parseProtocolUrl(lbryProtocolUrl || '', { encode: true })
|
||||||
|
if (segments.length === 0) throw new Error()
|
||||||
|
return segments.join('/') */
|
||||||
}
|
}
|
||||||
|
|
||||||
const onGoingLbryPathnameRequest: Record<string, Promise<string | void>> = {}
|
const onGoingLbryPathnameRequest: Record<string, Promise<string | null>> = {}
|
||||||
async function lbryPathnameFromVideoId(videoId: string): Promise<string | void> {
|
async function lbryPathnameFromVideoId(videoId: string): Promise<string | null> {
|
||||||
// Don't create a new Promise for same ID until on going one is over.
|
// Don't create a new Promise for same ID until on going one is over.
|
||||||
|
try {
|
||||||
const promise = onGoingLbryPathnameRequest[videoId] ?? (onGoingLbryPathnameRequest[videoId] = resolveYT({ id: videoId, type: 'video' }))
|
const promise = onGoingLbryPathnameRequest[videoId] ?? (onGoingLbryPathnameRequest[videoId] = resolveYT({ id: videoId, type: 'video' }))
|
||||||
await promise
|
console.log('lbrypathname request', videoId, await promise)
|
||||||
delete onGoingLbryPathnameRequest[videoId]
|
|
||||||
return await promise
|
return await promise
|
||||||
|
} catch (error) {
|
||||||
|
throw error
|
||||||
|
}
|
||||||
|
finally {
|
||||||
|
delete onGoingLbryPathnameRequest[videoId]
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
chrome.runtime.onMessage.addListener(({ videoId }: { videoId: string }, sender, sendResponse) => {
|
chrome.runtime.onMessage.addListener(({ videoId }: { videoId: string }, sender, sendResponse) => {
|
||||||
lbryPathnameFromVideoId(videoId).then((lbryPathname) => sendResponse(lbryPathname)).catch((err) => sendResponse(err))
|
lbryPathnameFromVideoId(videoId).then((lbryPathname) => sendResponse(lbryPathname)).catch((err) =>
|
||||||
|
{
|
||||||
|
sendResponse('error')
|
||||||
|
console.error(err)
|
||||||
|
})
|
||||||
return true
|
return true
|
||||||
})
|
})
|
||||||
|
|
||||||
|
|
|
@ -80,8 +80,8 @@ async function findVideoElement() {
|
||||||
|
|
||||||
// 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 requestLbryPathname(videoId: string) {
|
async function requestLbryPathname(videoId: string) {
|
||||||
const response = await new Promise<string | null | Error>((resolve) => chrome.runtime.sendMessage({ videoId }, resolve))
|
const response = await new Promise<string | null | 'error'>((resolve) => chrome.runtime.sendMessage({ videoId }, resolve))
|
||||||
if (response instanceof Error) throw response
|
if (response === 'error') throw new Error("Background error.")
|
||||||
return response
|
return response
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -95,6 +95,7 @@ async function requestLbryPathname(videoId: string) {
|
||||||
if (areaName !== 'local') return
|
if (areaName !== 'local') return
|
||||||
Object.assign(settings, Object.fromEntries(Object.entries(changes).map(([key, change]) => [key, change.newValue])))
|
Object.assign(settings, Object.fromEntries(Object.entries(changes).map(([key, change]) => [key, change.newValue])))
|
||||||
if (changes.redirect) await onModeChange()
|
if (changes.redirect) await onModeChange()
|
||||||
|
await updater()
|
||||||
})
|
})
|
||||||
|
|
||||||
/*
|
/*
|
||||||
|
@ -174,7 +175,6 @@ async function requestLbryPathname(videoId: string) {
|
||||||
updateButton(mountPoint, target)
|
updateButton(mountPoint, target)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
await updater()
|
await updater()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Add table
Reference in a new issue