🔥 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" },
await importPrivateKey(privateKey),
Buffer.from(data)
))
)).toString('base64')
}

View file

@ -4,16 +4,16 @@ export interface ExtensionSettings {
redirect: boolean
targetPlatform: TargetPlatformName
urlResolver: YTUrlResolverName
publicKey?: string,
privateKey?: string
publicKey: string | null,
privateKey: string | null
}
export const DEFAULT_SETTINGS: ExtensionSettings = {
redirect: true,
targetPlatform: 'odysee',
urlResolver: 'odyseeApi'
urlResolver: 'odyseeApi',
privateKey: null,
publicKey: null
}
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
*
* @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) {
const [state, dispatch] = useReducer((state, nstate: Partial<ExtensionSettings>) => ({ ...state, ...nstate }), initial)
export function useSettings(defaultSettings: ExtensionSettings) {
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
useEffect(() => {
const changeListener = (changes: Record<string, chrome.storage.StorageChange>, areaName: string) => {
if (areaName !== 'local') return
const changeSet = Object.keys(changes)
.filter(k => Object.keys(initial).includes(k))
.map(k => [k, changes[k].newValue])
if (changeSet.length === 0) return // no changes; no use dispatching
dispatch(Object.fromEntries(changeSet))
const changeEntries = Object.keys(changes).filter((key) => settingsKeys.includes(key)).map((key) => [key, changes[key].newValue])
if (changeEntries.length === 0) return // no changes; no use dispatching
dispatch(Object.fromEntries(changeEntries))
}
if (!state.privateKey || !state.publicKey) generateKeys().then((keys) => {
setSetting('publicKey', keys.publicKey)
setSetting('privateKey', keys.privateKey)
})
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)
}, [])

View file

@ -36,6 +36,7 @@ export async function resolveById(params: Paramaters, progressCallback?: (progre
// No cache found
return item
}))).filter((o) => o) as Paramaters
console.log(params)
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' })
if (apiResponse.ok) {
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
await LbryPathnameCache.put(lbryUrl, id)
await LbryPathnameCache.put(lbryUrl, item.id)
if (lbryUrl) results[id] = { id: lbryUrl, type: 'channel' }
}
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' }
if (lbryUrl) results[item.id] = { id: lbryUrl, type: item.type }
}
}

View file

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

View file

@ -19,7 +19,6 @@ function WatchOnLbryPopup() {
let [clearingCache, updateClearingCache] = useState(() => false)
return <div className='container'>
{ }
<section>
<label className='radio-label'>Enable Redirection:</label>
<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 { setSetting } from '../common/useSettings'
/** Reset settings to default value and update the browser badge text */
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
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
if (invalidEntries.length > 0) {
@ -15,6 +17,12 @@ async function initSettings() {
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' })
}