diff --git a/src/common/crypto.ts b/src/common/crypto.ts index 2b80a45..d210897 100644 --- a/src/common/crypto.ts +++ b/src/common/crypto.ts @@ -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') } \ No newline at end of file diff --git a/src/common/settings.ts b/src/common/settings.ts index e19c13f..2e5e991 100644 --- a/src/common/settings.ts +++ b/src/common/settings.ts @@ -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 { diff --git a/src/common/useSettings.ts b/src/common/useSettings.ts index 62339b2..8199f35 100644 --- a/src/common/useSettings.ts +++ b/src/common/useSettings.ts @@ -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) => ({ ...state, ...nstate }), initial) +export function useSettings(defaultSettings: ExtensionSettings) { + const [state, dispatch] = useReducer((state, nstate: Partial) => ({ ...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, 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)) + chrome.storage.local.get(settingsKeys, async (settings) => dispatch(settings)) return () => chrome.storage.onChanged.removeListener(changeListener) }, []) diff --git a/src/common/yt/urlResolve.ts b/src/common/yt/urlResolve.ts index 8c87784..c83835f 100644 --- a/src/common/yt/urlResolve.ts +++ b/src/common/yt/urlResolve.ts @@ -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 } } } diff --git a/src/manifest.json b/src/manifest.json index d150e80..e4ca076 100644 --- a/src/manifest.json +++ b/src/manifest.json @@ -34,7 +34,7 @@ "scripts/storageSetup.js", "scripts/background.js" ], - "persistent": false + "persistent": true }, "browser_action": { "default_title": "Watch on LBRY", diff --git a/src/popup/popup.tsx b/src/popup/popup.tsx index e550e46..9c63644 100644 --- a/src/popup/popup.tsx +++ b/src/popup/popup.tsx @@ -19,7 +19,6 @@ function WatchOnLbryPopup() { let [clearingCache, updateClearingCache] = useState(() => false) return
- { }
) - .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' }) }