From fcdc5cb77aec6f325a97048641c05bd3341d88ec Mon Sep 17 00:00:00 2001 From: Shiba <44804845+DeepDoge@users.noreply.github.com> Date: Tue, 9 Aug 2022 10:31:48 +0000 Subject: [PATCH] =?UTF-8?q?=F0=9F=8D=99=20opening=20new=20tabs=20handled?= =?UTF-8?q?=20by=20the=20background=20now=20-=20because=20firefox=20seem?= =?UTF-8?q?=20to=20block=20content=20script=20from=20openning=20new=20tabs?= =?UTF-8?q?=20in=20some=20cases?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- manifest.v2.json | 1 + manifest.v3.json | 3 ++- src/scripts/background.ts | 34 +++++++++++++++++++++------------- src/scripts/ytContent.tsx | 22 +++++++++++++++------- 4 files changed, 39 insertions(+), 21 deletions(-) diff --git a/manifest.v2.json b/manifest.v2.json index 2258b86..9489f9c 100644 --- a/manifest.v2.json +++ b/manifest.v2.json @@ -18,6 +18,7 @@ "https://odysee.com/", "https://madiator.com/", "https://finder.madiator.com/", + "tabs", "storage" ], "web_accessible_resources": [ diff --git a/manifest.v3.json b/manifest.v3.json index 5e848c5..25dee5b 100644 --- a/manifest.v3.json +++ b/manifest.v3.json @@ -8,7 +8,8 @@ "128": "assets/icons/wol/icon128.png" }, "permissions": [ - "storage" + "storage", + "tabs" ], "host_permissions": [ "https://www.youtube.com/", diff --git a/src/scripts/background.ts b/src/scripts/background.ts index a6e3f30..4bb72bd 100644 --- a/src/scripts/background.ts +++ b/src/scripts/background.ts @@ -2,23 +2,31 @@ import { resolveById } from "../modules/yt/urlResolve" const onGoingLbryPathnameRequest: Record> = {} -chrome.runtime.onMessage.addListener(({ json }, sender, sendResponse) => { +chrome.runtime.onMessage.addListener(({ method, data }, sender, sendResponse) => { function resolve(result: Awaited>) { sendResponse(JSON.stringify(result)) } (async () => { - try { - const params: Parameters = JSON.parse(json) - // Don't create a new Promise for same ID until on going one is over. - const promise = onGoingLbryPathnameRequest[json] ?? (onGoingLbryPathnameRequest[json] = resolveById(...params)) - console.log('lbrypathname request', params, await promise) - resolve(await promise) - } catch (error) { - sendResponse('error') - console.error(error) - } - finally { - delete onGoingLbryPathnameRequest[json] + + switch (method) { + case 'openTab': + chrome.tabs.create({ url: data }) + break + case 'resolveUrl': + try { + const params: Parameters = JSON.parse(data) + // Don't create a new Promise for same ID until on going one is over. + const promise = onGoingLbryPathnameRequest[data] ?? (onGoingLbryPathnameRequest[data] = resolveById(...params)) + console.log('lbrypathname request', params, await promise) + resolve(await promise) + } catch (error) { + sendResponse('error') + console.error(error) + } + finally { + delete onGoingLbryPathnameRequest[data] + } + break } })() diff --git a/src/scripts/ytContent.tsx b/src/scripts/ytContent.tsx index 1277ae1..521eb0d 100644 --- a/src/scripts/ytContent.tsx +++ b/src/scripts/ytContent.tsx @@ -222,7 +222,7 @@ 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({ json: JSON.stringify(params) }, resolve)) + const json = await new Promise((resolve) => chrome.runtime.sendMessage({ method: 'resolveUrl', data: JSON.stringify(params) }, resolve)) if (json === 'error') { console.error("Background error on:", params) throw new Error("Background error.") @@ -230,6 +230,11 @@ import { getExtensionSettingsAsync, getSourcePlatfromSettingsFromHostname, getTa return json ? JSON.parse(json) : null } + // Request new tab + async function openNewTab(url: URL) { + chrome.runtime.sendMessage({ method: 'openTab', data: url.href }) + } + function getLbryUrlByTarget(target: Target) { const url = new URL(`${target.platform.domainPrefix}${target.lbryPathname}`) if (target.time) url.searchParams.set('t', target.time.toFixed(0)) @@ -253,11 +258,13 @@ import { getExtensionSettingsAsync, getSourcePlatfromSettingsFromHostname, getTa const lbryURL = getLbryUrlByTarget(target) - // As soon as video play is ready and start playing, pause it. - findVideoElementAwait(source).then((videoElement) => { - videoElement.addEventListener('play', () => videoElement.pause(), { once: true }) - videoElement.pause() - }) + if (source.type === 'video') { + // As soon as video play is ready and start playing, pause it. + findVideoElementAwait(source).then((videoElement) => { + videoElement.addEventListener('play', () => videoElement.pause(), { once: true }) + videoElement.pause() + }) + } if (target.platform === targetPlatformSettings.app) { if (document.hidden) await new Promise((resolve) => document.addEventListener('visibilitychange', resolve, { once: true })) @@ -266,7 +273,8 @@ import { getExtensionSettingsAsync, getSourcePlatfromSettingsFromHostname, getTa location.replace(lbryURL) } else { - open(lbryURL, '_blank') + openNewTab(lbryURL) + if (window.history.length === 1) window.close() else window.history.back() }