From b26d540a4a944fdf9f6251d5e88223cbda31657f Mon Sep 17 00:00:00 2001 From: Shiba <44804845+DeepDoge@users.noreply.github.com> Date: Tue, 9 Aug 2022 16:42:51 +0000 Subject: [PATCH] =?UTF-8?q?=F0=9F=8D=A3=20bug=20fixes?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/scripts/background.ts | 7 +++++-- src/scripts/ytContent.tsx | 25 +++++++++++++------------ 2 files changed, 18 insertions(+), 14 deletions(-) diff --git a/src/scripts/background.ts b/src/scripts/background.ts index 4bb72bd..139b5b4 100644 --- a/src/scripts/background.ts +++ b/src/scripts/background.ts @@ -10,7 +10,10 @@ chrome.runtime.onMessage.addListener(({ method, data }, sender, sendResponse) => switch (method) { case 'openTab': - chrome.tabs.create({ url: data }) + { + const { href, active }: { href: string, active: boolean } = JSON.parse(data) + chrome.tabs.create({ url: href, active }) + } break case 'resolveUrl': try { @@ -20,7 +23,7 @@ chrome.runtime.onMessage.addListener(({ method, data }, sender, sendResponse) => console.log('lbrypathname request', params, await promise) resolve(await promise) } catch (error) { - sendResponse('error') + sendResponse(`error:${(error as any).toString()}`) console.error(error) } finally { diff --git a/src/scripts/ytContent.tsx b/src/scripts/ytContent.tsx index 521eb0d..4b24c2a 100644 --- a/src/scripts/ytContent.tsx +++ b/src/scripts/ytContent.tsx @@ -26,6 +26,7 @@ import { getExtensionSettingsAsync, getSourcePlatfromSettingsFromHostname, getTa chrome.storage.onChanged.addListener(async (changes, areaName) => { if (areaName !== 'local') return Object.assign(settings, Object.fromEntries(Object.entries(changes).map(([key, change]) => [key, change.newValue]))) + if (settings.redirect) updateButton(null) }) const buttonMountPoint = document.createElement('div') @@ -222,17 +223,17 @@ import { getExtensionSettingsAsync, getSourcePlatfromSettingsFromHostname, getTa } // 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 requestResolveById(...params: Parameters): ReturnType { - const json = await new Promise((resolve) => chrome.runtime.sendMessage({ method: 'resolveUrl', data: JSON.stringify(params) }, resolve)) - if (json === 'error') { + const response = await new Promise((resolve) => chrome.runtime.sendMessage({ method: 'resolveUrl', data: JSON.stringify(params) }, resolve)) + if (response?.startsWith('error:')) { console.error("Background error on:", params) - throw new Error("Background error.") + throw new Error(`Background error.${response ?? ''}`) } - return json ? JSON.parse(json) : null + return response ? JSON.parse(response) : null } // Request new tab - async function openNewTab(url: URL) { - chrome.runtime.sendMessage({ method: 'openTab', data: url.href }) + async function openNewTab(url: URL, active: boolean) { + chrome.runtime.sendMessage({ method: 'openTab', data: JSON.stringify({ href: url.href, active }) }) } function getLbryUrlByTarget(target: Target) { @@ -242,11 +243,11 @@ import { getExtensionSettingsAsync, getSourcePlatfromSettingsFromHostname, getTa return url } - let urlCache: URL | null = null + let urlHrefCache: string | null = null while (true) { await sleep(500) - const url: URL = (urlCache?.href === location.href) ? urlCache : new URL(location.href) + const url: URL = new URL(location.href) const source = await getSourceByUrl(new URL(location.href)) if (!source) continue @@ -254,7 +255,7 @@ import { getExtensionSettingsAsync, getSourcePlatfromSettingsFromHostname, getTa if (settings.redirect) { const target = (await getTargetsBySources(source))[source.id] if (!target) continue - if (url === urlCache) continue + if (url.href === urlHrefCache) continue const lbryURL = getLbryUrlByTarget(target) @@ -273,14 +274,14 @@ import { getExtensionSettingsAsync, getSourcePlatfromSettingsFromHostname, getTa location.replace(lbryURL) } else { - openNewTab(lbryURL) + openNewTab(lbryURL, document.hasFocus()) if (window.history.length === 1) window.close() else window.history.back() } } else { - if (urlCache !== url) updateButton(null) + if (urlHrefCache !== url.href) updateButton(null) let target = (await getTargetsBySources(source))[source.id] // There is no target found via API try to check Video Description for LBRY links. @@ -336,7 +337,7 @@ import { getExtensionSettingsAsync, getSourcePlatfromSettingsFromHostname, getTa } catch (error) { console.error(error) } - urlCache = url + urlHrefCache = url.href } })() \ No newline at end of file