🍙 opening new tabs handled by the background now

- because firefox seem to block content script from openning new tabs in some cases
This commit is contained in:
Shiba 2022-08-09 10:31:48 +00:00
parent 73147fdbcd
commit fcdc5cb77a
4 changed files with 39 additions and 21 deletions

View file

@ -18,6 +18,7 @@
"https://odysee.com/",
"https://madiator.com/",
"https://finder.madiator.com/",
"tabs",
"storage"
],
"web_accessible_resources": [

View file

@ -8,7 +8,8 @@
"128": "assets/icons/wol/icon128.png"
},
"permissions": [
"storage"
"storage",
"tabs"
],
"host_permissions": [
"https://www.youtube.com/",

View file

@ -2,15 +2,21 @@ import { resolveById } from "../modules/yt/urlResolve"
const onGoingLbryPathnameRequest: Record<string, ReturnType<typeof resolveById>> = {}
chrome.runtime.onMessage.addListener(({ json }, sender, sendResponse) => {
chrome.runtime.onMessage.addListener(({ method, data }, sender, sendResponse) => {
function resolve(result: Awaited<ReturnType<typeof resolveById>>) {
sendResponse(JSON.stringify(result))
}
(async () => {
switch (method) {
case 'openTab':
chrome.tabs.create({ url: data })
break
case 'resolveUrl':
try {
const params: Parameters<typeof resolveById> = JSON.parse(json)
const params: Parameters<typeof resolveById> = JSON.parse(data)
// Don't create a new Promise for same ID until on going one is over.
const promise = onGoingLbryPathnameRequest[json] ?? (onGoingLbryPathnameRequest[json] = resolveById(...params))
const promise = onGoingLbryPathnameRequest[data] ?? (onGoingLbryPathnameRequest[data] = resolveById(...params))
console.log('lbrypathname request', params, await promise)
resolve(await promise)
} catch (error) {
@ -18,7 +24,9 @@ chrome.runtime.onMessage.addListener(({ json }, sender, sendResponse) => {
console.error(error)
}
finally {
delete onGoingLbryPathnameRequest[json]
delete onGoingLbryPathnameRequest[data]
}
break
}
})()

View file

@ -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<typeof resolveById>): ReturnType<typeof resolveById> {
const json = await new Promise<string | null | 'error'>((resolve) => chrome.runtime.sendMessage({ json: JSON.stringify(params) }, resolve))
const json = await new Promise<string | null | 'error'>((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)
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()
}