mirror of
https://github.com/LBRYFoundation/Watch-on-LBRY.git
synced 2025-08-23 17:47:26 +00:00
🍙 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:
parent
73147fdbcd
commit
fcdc5cb77a
4 changed files with 39 additions and 21 deletions
|
@ -18,6 +18,7 @@
|
||||||
"https://odysee.com/",
|
"https://odysee.com/",
|
||||||
"https://madiator.com/",
|
"https://madiator.com/",
|
||||||
"https://finder.madiator.com/",
|
"https://finder.madiator.com/",
|
||||||
|
"tabs",
|
||||||
"storage"
|
"storage"
|
||||||
],
|
],
|
||||||
"web_accessible_resources": [
|
"web_accessible_resources": [
|
||||||
|
|
|
@ -8,7 +8,8 @@
|
||||||
"128": "assets/icons/wol/icon128.png"
|
"128": "assets/icons/wol/icon128.png"
|
||||||
},
|
},
|
||||||
"permissions": [
|
"permissions": [
|
||||||
"storage"
|
"storage",
|
||||||
|
"tabs"
|
||||||
],
|
],
|
||||||
"host_permissions": [
|
"host_permissions": [
|
||||||
"https://www.youtube.com/",
|
"https://www.youtube.com/",
|
||||||
|
|
|
@ -2,23 +2,31 @@ import { resolveById } from "../modules/yt/urlResolve"
|
||||||
|
|
||||||
const onGoingLbryPathnameRequest: Record<string, ReturnType<typeof resolveById>> = {}
|
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>>) {
|
function resolve(result: Awaited<ReturnType<typeof resolveById>>) {
|
||||||
sendResponse(JSON.stringify(result))
|
sendResponse(JSON.stringify(result))
|
||||||
}
|
}
|
||||||
(async () => {
|
(async () => {
|
||||||
try {
|
|
||||||
const params: Parameters<typeof resolveById> = JSON.parse(json)
|
switch (method) {
|
||||||
// Don't create a new Promise for same ID until on going one is over.
|
case 'openTab':
|
||||||
const promise = onGoingLbryPathnameRequest[json] ?? (onGoingLbryPathnameRequest[json] = resolveById(...params))
|
chrome.tabs.create({ url: data })
|
||||||
console.log('lbrypathname request', params, await promise)
|
break
|
||||||
resolve(await promise)
|
case 'resolveUrl':
|
||||||
} catch (error) {
|
try {
|
||||||
sendResponse('error')
|
const params: Parameters<typeof resolveById> = JSON.parse(data)
|
||||||
console.error(error)
|
// Don't create a new Promise for same ID until on going one is over.
|
||||||
}
|
const promise = onGoingLbryPathnameRequest[data] ?? (onGoingLbryPathnameRequest[data] = resolveById(...params))
|
||||||
finally {
|
console.log('lbrypathname request', params, await promise)
|
||||||
delete onGoingLbryPathnameRequest[json]
|
resolve(await promise)
|
||||||
|
} catch (error) {
|
||||||
|
sendResponse('error')
|
||||||
|
console.error(error)
|
||||||
|
}
|
||||||
|
finally {
|
||||||
|
delete onGoingLbryPathnameRequest[data]
|
||||||
|
}
|
||||||
|
break
|
||||||
}
|
}
|
||||||
})()
|
})()
|
||||||
|
|
||||||
|
|
|
@ -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
|
// 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> {
|
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') {
|
if (json === 'error') {
|
||||||
console.error("Background error on:", params)
|
console.error("Background error on:", params)
|
||||||
throw new Error("Background error.")
|
throw new Error("Background error.")
|
||||||
|
@ -230,6 +230,11 @@ import { getExtensionSettingsAsync, getSourcePlatfromSettingsFromHostname, getTa
|
||||||
return json ? JSON.parse(json) : null
|
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) {
|
function getLbryUrlByTarget(target: Target) {
|
||||||
const url = new URL(`${target.platform.domainPrefix}${target.lbryPathname}`)
|
const url = new URL(`${target.platform.domainPrefix}${target.lbryPathname}`)
|
||||||
if (target.time) url.searchParams.set('t', target.time.toFixed(0))
|
if (target.time) url.searchParams.set('t', target.time.toFixed(0))
|
||||||
|
@ -253,11 +258,13 @@ import { getExtensionSettingsAsync, getSourcePlatfromSettingsFromHostname, getTa
|
||||||
|
|
||||||
const lbryURL = getLbryUrlByTarget(target)
|
const lbryURL = getLbryUrlByTarget(target)
|
||||||
|
|
||||||
// As soon as video play is ready and start playing, pause it.
|
if (source.type === 'video') {
|
||||||
findVideoElementAwait(source).then((videoElement) => {
|
// As soon as video play is ready and start playing, pause it.
|
||||||
videoElement.addEventListener('play', () => videoElement.pause(), { once: true })
|
findVideoElementAwait(source).then((videoElement) => {
|
||||||
videoElement.pause()
|
videoElement.addEventListener('play', () => videoElement.pause(), { once: true })
|
||||||
})
|
videoElement.pause()
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
if (target.platform === targetPlatformSettings.app) {
|
if (target.platform === targetPlatformSettings.app) {
|
||||||
if (document.hidden) await new Promise((resolve) => document.addEventListener('visibilitychange', resolve, { once: true }))
|
if (document.hidden) await new Promise((resolve) => document.addEventListener('visibilitychange', resolve, { once: true }))
|
||||||
|
@ -266,7 +273,8 @@ import { getExtensionSettingsAsync, getSourcePlatfromSettingsFromHostname, getTa
|
||||||
location.replace(lbryURL)
|
location.replace(lbryURL)
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
open(lbryURL, '_blank')
|
openNewTab(lbryURL)
|
||||||
|
|
||||||
if (window.history.length === 1) window.close()
|
if (window.history.length === 1) window.close()
|
||||||
else window.history.back()
|
else window.history.back()
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Reference in a new issue