🔥 bug fixes

This commit is contained in:
Shiba 2022-04-15 13:37:37 +00:00
parent 651ca5b4dc
commit 450ca8cef6
7 changed files with 30 additions and 32 deletions

View file

@ -52,5 +52,5 @@ export async function sign(data: string, privateKey: string) {
{ name: "RSASSA-PKCS1-v1_5" }, { name: "RSASSA-PKCS1-v1_5" },
await importPrivateKey(privateKey), await importPrivateKey(privateKey),
Buffer.from(data) Buffer.from(data)
)) )).toString('base64')
} }

View file

@ -4,16 +4,16 @@ export interface ExtensionSettings {
redirect: boolean redirect: boolean
targetPlatform: TargetPlatformName targetPlatform: TargetPlatformName
urlResolver: YTUrlResolverName urlResolver: YTUrlResolverName
publicKey?: string, publicKey: string | null,
privateKey?: string privateKey: string | null
} }
export const DEFAULT_SETTINGS: ExtensionSettings = { export const DEFAULT_SETTINGS: ExtensionSettings = {
redirect: true, redirect: true,
targetPlatform: 'odysee', targetPlatform: 'odysee',
urlResolver: 'odyseeApi' urlResolver: 'odyseeApi',
privateKey: null,
publicKey: null
} }
export function getExtensionSettingsAsync(): Promise<ExtensionSettings> { export function getExtensionSettingsAsync(): Promise<ExtensionSettings> {

View file

@ -5,28 +5,22 @@ import { DEFAULT_SETTINGS, ExtensionSettings } from './settings'
/** /**
* A hook to read the settings from local storage * A hook to read the settings from local storage
* *
* @param initial the default value. Must have all relevant keys present and should not change * @param defaultSettings the default value. Must have all relevant keys present and should not change
*/ */
export function useSettings(initial: ExtensionSettings) { export function useSettings(defaultSettings: ExtensionSettings) {
const [state, dispatch] = useReducer((state, nstate: Partial<ExtensionSettings>) => ({ ...state, ...nstate }), initial) const [state, dispatch] = useReducer((state, nstate: Partial<ExtensionSettings>) => ({ ...state, ...nstate }), defaultSettings)
const settingsKeys = Object.keys(defaultSettings)
// register change listeners, gets current values, and cleans up the listeners on unload // register change listeners, gets current values, and cleans up the listeners on unload
useEffect(() => { useEffect(() => {
const changeListener = (changes: Record<string, chrome.storage.StorageChange>, areaName: string) => { const changeListener = (changes: Record<string, chrome.storage.StorageChange>, areaName: string) => {
if (areaName !== 'local') return if (areaName !== 'local') return
const changeSet = Object.keys(changes) const changeEntries = Object.keys(changes).filter((key) => settingsKeys.includes(key)).map((key) => [key, changes[key].newValue])
.filter(k => Object.keys(initial).includes(k)) if (changeEntries.length === 0) return // no changes; no use dispatching
.map(k => [k, changes[k].newValue]) dispatch(Object.fromEntries(changeEntries))
if (changeSet.length === 0) return // no changes; no use dispatching
dispatch(Object.fromEntries(changeSet))
} }
if (!state.privateKey || !state.publicKey) generateKeys().then((keys) => {
setSetting('publicKey', keys.publicKey)
setSetting('privateKey', keys.privateKey)
})
chrome.storage.onChanged.addListener(changeListener) chrome.storage.onChanged.addListener(changeListener)
chrome.storage.local.get(Object.keys(initial), o => dispatch(o as Partial<ExtensionSettings>)) chrome.storage.local.get(settingsKeys, async (settings) => dispatch(settings))
return () => chrome.storage.onChanged.removeListener(changeListener) return () => chrome.storage.onChanged.removeListener(changeListener)
}, []) }, [])

View file

@ -36,6 +36,7 @@ export async function resolveById(params: Paramaters, progressCallback?: (progre
// No cache found // No cache found
return item return item
}))).filter((o) => o) as Paramaters }))).filter((o) => o) as Paramaters
console.log(params)
if (params.length === 0) return results if (params.length === 0) return results
@ -51,17 +52,13 @@ export async function resolveById(params: Paramaters, progressCallback?: (progre
const apiResponse = await fetch(url.toString(), { cache: 'no-store' }) const apiResponse = await fetch(url.toString(), { cache: 'no-store' })
if (apiResponse.ok) { if (apiResponse.ok) {
const response: ApiResponse = await apiResponse.json() const response: ApiResponse = await apiResponse.json()
for (const [id, lbryUrl] of Object.entries(response.channels ?? {})) { for (const item of params)
{
const lbryUrl = ((item.type === 'channel' ? response.channels : response.videos) ?? {})[item.id] ?? null
// we cache it no matter if its null or not // we cache it no matter if its null or not
await LbryPathnameCache.put(lbryUrl, id) await LbryPathnameCache.put(lbryUrl, item.id)
if (lbryUrl) results[id] = { id: lbryUrl, type: 'channel' } if (lbryUrl) results[item.id] = { id: lbryUrl, type: item.type }
}
for (const [id, lbryUrl] of Object.entries(response.videos ?? {})) {
// we cache it no matter if its null or not
await LbryPathnameCache.put(lbryUrl, id)
if (lbryUrl) results[id] = { id: lbryUrl, type: 'video' }
} }
} }

View file

@ -34,7 +34,7 @@
"scripts/storageSetup.js", "scripts/storageSetup.js",
"scripts/background.js" "scripts/background.js"
], ],
"persistent": false "persistent": true
}, },
"browser_action": { "browser_action": {
"default_title": "Watch on LBRY", "default_title": "Watch on LBRY",

View file

@ -19,7 +19,6 @@ function WatchOnLbryPopup() {
let [clearingCache, updateClearingCache] = useState(() => false) let [clearingCache, updateClearingCache] = useState(() => false)
return <div className='container'> return <div className='container'>
{ }
<section> <section>
<label className='radio-label'>Enable Redirection:</label> <label className='radio-label'>Enable Redirection:</label>
<ButtonRadio value={redirect ? 'YES' : 'NO'} options={['YES', 'NO']} <ButtonRadio value={redirect ? 'YES' : 'NO'} options={['YES', 'NO']}

View file

@ -1,4 +1,6 @@
import { generateKeys } from '../common/crypto'
import { DEFAULT_SETTINGS, ExtensionSettings, getExtensionSettingsAsync } from '../common/settings' import { DEFAULT_SETTINGS, ExtensionSettings, getExtensionSettingsAsync } from '../common/settings'
import { setSetting } from '../common/useSettings'
/** Reset settings to default value and update the browser badge text */ /** Reset settings to default value and update the browser badge text */
async function initSettings() { async function initSettings() {
@ -6,7 +8,7 @@ async function initSettings() {
// get all the values that aren't set and use them as a change set // get all the values that aren't set and use them as a change set
const invalidEntries = (Object.entries(DEFAULT_SETTINGS) as Array<[keyof ExtensionSettings, ExtensionSettings[keyof ExtensionSettings]]>) const invalidEntries = (Object.entries(DEFAULT_SETTINGS) as Array<[keyof ExtensionSettings, ExtensionSettings[keyof ExtensionSettings]]>)
.filter(([k]) => settings[k] === null || settings[k] === undefined) .filter(([k]) => settings[k] === undefined || settings[k] === null)
// fix our local var and set it in storage for later // fix our local var and set it in storage for later
if (invalidEntries.length > 0) { if (invalidEntries.length > 0) {
@ -15,6 +17,12 @@ async function initSettings() {
chrome.storage.local.set(changeSet) chrome.storage.local.set(changeSet)
} }
if (!settings.privateKey || !settings.publicKey)
await generateKeys().then((keys) => {
setSetting('publicKey', keys.publicKey)
setSetting('privateKey', keys.privateKey)
})
chrome.browserAction.setBadgeText({ text: settings.redirect ? 'ON' : 'OFF' }) chrome.browserAction.setBadgeText({ text: settings.redirect ? 'ON' : 'OFF' })
} }