🍣 New button on the video player

This commit is contained in:
Shiba 2022-07-24 23:18:34 +00:00
parent a6472799a1
commit 29c9b2829a
2 changed files with 92 additions and 21 deletions

View file

@ -28,8 +28,11 @@ import { getExtensionSettingsAsync, getSourcePlatfromSettingsFromHostname, getTa
Object.assign(settings, Object.fromEntries(Object.entries(changes).map(([key, change]) => [key, change.newValue]))) Object.assign(settings, Object.fromEntries(Object.entries(changes).map(([key, change]) => [key, change.newValue])))
}) })
const mountPoint = document.createElement('div') const buttonMountPoint = document.createElement('div')
mountPoint.style.display = 'flex' buttonMountPoint.style.display = 'flex'
const playerButtonMountPoint = document.createElement('div')
playerButtonMountPoint.style.display = 'flex'
function WatchOnLbryButton({ source, target }: { source?: Source, target?: Target }) { function WatchOnLbryButton({ source, target }: { source?: Source, target?: Target }) {
if (!target || !source) return null if (!target || !source) return null
@ -60,19 +63,65 @@ import { getExtensionSettingsAsync, getSourcePlatfromSettingsFromHostname, getTa
> >
<img src={target.platform.button.icon} height={16} <img src={target.platform.button.icon} height={16}
style={{ transform: 'scale(1.5)', ...target.platform.button.style?.icon }} /> style={{ transform: 'scale(1.5)', ...target.platform.button.style?.icon }} />
<span>{target.type === 'channel' ? 'Channel on' : 'Watch on'} {target.platform.displayName}</span> <span>{target.type === 'channel' ? 'Channel on' : 'Watch on'} {target.platform.button.platformNameText}</span>
</a>
</div>
}
function WatchOnLbryPlayerButton({ source, target }: { source?: Source, target?: Target }) {
if (!target || !source) return null
const url = getLbryUrlByTarget(target)
return <div style={{ display: 'flex', justifyContent: 'center', flexDirection: 'column' }}>
<a href={`${url.href}`} target={target.platform === targetPlatformSettings.app ? '' : '_blank'} role='button'
style={{
display: 'flex',
alignItems: 'center',
justifyContent: 'center',
gap: '12px',
fontWeight: 'bold',
border: '0',
color: 'whitesmoke',
marginRight: '10px',
fontSize: '14px',
textDecoration: 'none',
...target.platform.button.style?.button,
}}
onClick={() => findVideoElementAwait(source).then((videoElement) => {
videoElement.pause()
})}
>
<img src={target.platform.button.icon} height={16}
style={{ transform: 'scale(1.5)', ...target.platform.button.style?.icon }} />
<span>{target.type === 'channel' ? 'Channel on' : 'Watch on'} {target.platform.button.platformNameText}</span>
</a> </a>
</div> </div>
} }
function updateButton(params: { source: Source, target: Target } | null): void { function updateButton(params: { source: Source, target: Target } | null): void {
if (!params) return render(<WatchOnLbryButton />, mountPoint) if (!params) {
render(<WatchOnLbryButton />, buttonMountPoint)
render(<WatchOnLbryPlayerButton />, playerButtonMountPoint)
return
}
const mountBefore = document.querySelector(params.source.platform.htmlQueries.mountButtonBefore[params.source.type]) const mountPlayerButtonBefore = params.source.platform.htmlQueries.mountPoints.mountPlayerButtonBefore ?
if (!mountBefore) return render(<WatchOnLbryButton />, mountPoint) document.querySelector(params.source.platform.htmlQueries.mountPoints.mountPlayerButtonBefore) :
null
if (!mountPlayerButtonBefore) render(<WatchOnLbryPlayerButton />, playerButtonMountPoint)
else {
if (mountPlayerButtonBefore.previousSibling !== playerButtonMountPoint)
mountPlayerButtonBefore.parentElement?.insertBefore(playerButtonMountPoint, mountPlayerButtonBefore)
render(<WatchOnLbryPlayerButton target={params.target} source={params.source} />, playerButtonMountPoint)
}
mountBefore.parentElement?.insertBefore(mountPoint, mountBefore) const mountButtonBefore = document.querySelector(params.source.platform.htmlQueries.mountPoints.mountButtonBefore[params.source.type])
render(<WatchOnLbryButton target={params.target} source={params.source} />, mountPoint) if (!mountButtonBefore) render(<WatchOnLbryButton />, playerButtonMountPoint)
else {
if (mountButtonBefore.previousSibling !== buttonMountPoint)
mountButtonBefore.parentElement?.insertBefore(buttonMountPoint, mountButtonBefore)
render(<WatchOnLbryButton target={params.target} source={params.source} />, buttonMountPoint)
}
} }
async function findVideoElementAwait(source: Source) { async function findVideoElementAwait(source: Source) {
@ -205,9 +254,15 @@ import { getExtensionSettingsAsync, getSourcePlatfromSettingsFromHostname, getTa
// There is no target found via API try to check Video Description for LBRY links. // There is no target found via API try to check Video Description for LBRY links.
if (!target) { if (!target) {
const descriptionElement = document.querySelector(source.platform.htmlQueries.videoDescription) const linksContainer =
if (descriptionElement) { source.type === 'video' ?
const anchors = Array.from(descriptionElement.querySelectorAll<HTMLAnchorElement>('a')) document.querySelector(source.platform.htmlQueries.videoDescription) :
source.platform.htmlQueries.channelLinks ? document.querySelector(source.platform.htmlQueries.channelLinks) : null
console.log(linksContainer)
if (linksContainer) {
const anchors = Array.from(linksContainer.querySelectorAll<HTMLAnchorElement>('a'))
for (const anchor of anchors) { for (const anchor of anchors) {
if (!anchor.href) continue if (!anchor.href) continue

View file

@ -60,6 +60,7 @@ const targetPlatform = (o: {
displayName: string displayName: string
theme: string theme: string
button: { button: {
platformNameText: string,
icon: string icon: string
style?: style?:
{ {
@ -79,6 +80,7 @@ export const targetPlatformSettings = {
displayName: 'Madiator.com', displayName: 'Madiator.com',
theme: 'linear-gradient(130deg, #499375, #43889d)', theme: 'linear-gradient(130deg, #499375, #43889d)',
button: { button: {
platformNameText: '',
icon: chrome.runtime.getURL('assets/icons/lbry/madiator-logo.svg'), icon: chrome.runtime.getURL('assets/icons/lbry/madiator-logo.svg'),
style: { style: {
button: { flexDirection: 'row-reverse' }, button: { flexDirection: 'row-reverse' },
@ -91,6 +93,7 @@ export const targetPlatformSettings = {
displayName: 'Odysee', displayName: 'Odysee',
theme: 'linear-gradient(130deg, #c63d59, #f77937)', theme: 'linear-gradient(130deg, #c63d59, #f77937)',
button: { button: {
platformNameText: 'Odysee',
icon: chrome.runtime.getURL('assets/icons/lbry/odysee-logo.svg') icon: chrome.runtime.getURL('assets/icons/lbry/odysee-logo.svg')
} }
}), }),
@ -99,6 +102,7 @@ export const targetPlatformSettings = {
displayName: 'LBRY App', displayName: 'LBRY App',
theme: 'linear-gradient(130deg, #499375, #43889d)', theme: 'linear-gradient(130deg, #499375, #43889d)',
button: { button: {
platformNameText: 'LBRY',
icon: chrome.runtime.getURL('assets/icons/lbry/lbry-logo.svg') icon: chrome.runtime.getURL('assets/icons/lbry/lbry-logo.svg')
} }
}), }),
@ -109,9 +113,13 @@ export const targetPlatformSettings = {
const sourcePlatform = (o: { const sourcePlatform = (o: {
hostnames: string[] hostnames: string[]
htmlQueries: { htmlQueries: {
mountButtonBefore: Record<ResolveUrlTypes, string>, mountPoints: {
mountButtonBefore: Record<ResolveUrlTypes, string>,
mountPlayerButtonBefore: string | null,
}
videoPlayer: string, videoPlayer: string,
videoDescription: string videoDescription: string
channelLinks: string | null
} }
}) => o }) => o
export type SourcePlatform = ReturnType<typeof sourcePlatform> export type SourcePlatform = ReturnType<typeof sourcePlatform>
@ -126,24 +134,32 @@ export const sourcePlatfromSettings = {
"youtube.com": sourcePlatform({ "youtube.com": sourcePlatform({
hostnames: ['www.youtube.com'], hostnames: ['www.youtube.com'],
htmlQueries: { htmlQueries: {
mountButtonBefore: { mountPoints: {
video: 'ytd-video-owner-renderer~#subscribe-button', mountButtonBefore: {
channel: '#channel-header-container #buttons' video: 'ytd-video-owner-renderer~#subscribe-button',
channel: '#channel-header-container #buttons'
},
mountPlayerButtonBefore: 'ytd-player .ytp-right-controls',
}, },
videoPlayer: '#ytd-player video', videoPlayer: '#ytd-player video',
videoDescription: 'ytd-video-secondary-info-renderer #description' videoDescription: 'ytd-video-secondary-info-renderer #description',
channelLinks: '#channel-header #links-holder'
} }
}), }),
"yewtu.be": sourcePlatform({ "yewtu.be": sourcePlatform({
hostnames: ['yewtu.be', 'vid.puffyan.us', 'invidio.xamh.de', 'invidious.kavin.rocks'], hostnames: ['yewtu.be', 'vid.puffyan.us', 'invidio.xamh.de', 'invidious.kavin.rocks'],
htmlQueries: { htmlQueries: {
mountButtonBefore: mountPoints: {
{ mountButtonBefore:
video: '#watch-on-youtube', {
channel: '#subscribe' video: '#watch-on-youtube',
channel: '#subscribe'
},
mountPlayerButtonBefore: null,
}, },
videoPlayer: '#player-container video', videoPlayer: '#player-container video',
videoDescription: '#descriptionWrapper' videoDescription: '#descriptionWrapper',
channelLinks: null
} }
}) })
} }