mirror of
https://github.com/th-ch/youtube-music.git
synced 2026-01-10 10:11:46 +00:00
fix: improve menu detector
This commit is contained in:
@ -13,7 +13,6 @@ import { DownloadButton } from './templates/download';
|
||||
import type { RendererContext } from '@/types/contexts';
|
||||
import type { DownloaderPluginConfig } from './index';
|
||||
|
||||
let menu: HTMLElement | null = null;
|
||||
let download: () => void;
|
||||
|
||||
const [downloadButtonText, setDownloadButtonText] = createSignal<string>('');
|
||||
@ -21,14 +20,10 @@ const [downloadButtonText, setDownloadButtonText] = createSignal<string>('');
|
||||
let buttonContainer: HTMLDivElement | null = null;
|
||||
|
||||
const menuObserver = new MutationObserver(() => {
|
||||
if (!menu) {
|
||||
menu = getSongMenu();
|
||||
if (!menu) {
|
||||
return;
|
||||
}
|
||||
}
|
||||
const menu = getSongMenu();
|
||||
|
||||
if (
|
||||
!menu ||
|
||||
menu.contains(buttonContainer) ||
|
||||
!isMusicOrVideoTrack() ||
|
||||
!buttonContainer
|
||||
|
||||
@ -3,7 +3,10 @@ import keyEventAreEqual from 'keyboardevents-areequal';
|
||||
import { render } from 'solid-js/web';
|
||||
|
||||
import { getSongMenu } from '@/providers/dom-elements';
|
||||
import { isMusicOrVideoTrack } from '@/plugins/utils/renderer/check';
|
||||
import {
|
||||
isMusicOrVideoTrack,
|
||||
isPlayerMenu,
|
||||
} from '@/plugins/utils/renderer/check';
|
||||
|
||||
import { t } from '@/i18n';
|
||||
|
||||
@ -152,7 +155,12 @@ export const onPlayerApiReady = async (
|
||||
const observer = new MutationObserver(() => {
|
||||
const menu = getSongMenu();
|
||||
|
||||
if (menu?.contains(pipButtonContainer) || !isMusicOrVideoTrack()) {
|
||||
console.log(isPlayerMenu(menu));
|
||||
if (
|
||||
menu?.contains(pipButtonContainer) ||
|
||||
!isMusicOrVideoTrack() ||
|
||||
!isPlayerMenu(menu)
|
||||
) {
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
@ -7,7 +7,10 @@ import { getSongMenu } from '@/providers/dom-elements';
|
||||
import { PlaybackSpeedSlider } from './components/slider';
|
||||
import { t } from '@/i18n';
|
||||
|
||||
import { isMusicOrVideoTrack } from '@/plugins/utils/renderer/check';
|
||||
import {
|
||||
isMusicOrVideoTrack,
|
||||
isPlayerMenu,
|
||||
} from '@/plugins/utils/renderer/check';
|
||||
|
||||
const MIN_PLAYBACK_SPEED = 0.07;
|
||||
const MAX_PLAYBACK_SPEED = 16;
|
||||
@ -83,7 +86,12 @@ export const onPlayerApiReady = () => {
|
||||
const observer = new MutationObserver(() => {
|
||||
const menu = getSongMenu();
|
||||
|
||||
if (menu && !menu.contains(sliderContainer) && isMusicOrVideoTrack()) {
|
||||
if (
|
||||
menu &&
|
||||
!menu.contains(sliderContainer) &&
|
||||
isMusicOrVideoTrack() &&
|
||||
isPlayerMenu(menu)
|
||||
) {
|
||||
menu.prepend(sliderContainer);
|
||||
}
|
||||
});
|
||||
|
||||
@ -1,20 +1,39 @@
|
||||
export const isMusicOrVideoTrack = () => {
|
||||
let menuUrl = document.querySelector<HTMLAnchorElement>(
|
||||
'tp-yt-paper-listbox [tabindex="0"] #navigation-endpoint',
|
||||
)?.href;
|
||||
|
||||
if (!menuUrl?.includes('watch?')) {
|
||||
menuUrl = undefined;
|
||||
// check for podcast
|
||||
for (const it of document.querySelectorAll(
|
||||
'tp-yt-paper-listbox [tabindex="-1"] #navigation-endpoint',
|
||||
)) {
|
||||
if (it.getAttribute('href')?.includes('podcast/')) {
|
||||
menuUrl = it.getAttribute('href')!;
|
||||
break;
|
||||
}
|
||||
for (const menuSelector of document.querySelectorAll<
|
||||
HTMLAnchorElement & {
|
||||
data: {
|
||||
watchEndpoint: {
|
||||
videoId: string;
|
||||
};
|
||||
addToPlaylistEndpoint: {
|
||||
videoId: string;
|
||||
};
|
||||
clickTrackingParams: string;
|
||||
};
|
||||
}
|
||||
>('tp-yt-paper-listbox #navigation-endpoint')) {
|
||||
if (
|
||||
menuSelector?.data?.addToPlaylistEndpoint?.videoId ||
|
||||
menuSelector?.data?.watchEndpoint?.videoId
|
||||
) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
return !!menuUrl;
|
||||
return false;
|
||||
};
|
||||
|
||||
export const isPlayerMenu = (menu?: HTMLElement | null) => {
|
||||
return (
|
||||
menu?.parentElement as
|
||||
| (HTMLElement & {
|
||||
ytEventForwardingBehavior: {
|
||||
forwarder_: {
|
||||
eventSink: HTMLElement;
|
||||
};
|
||||
};
|
||||
})
|
||||
| null
|
||||
)?.ytEventForwardingBehavior?.forwarder_?.eventSink?.matches(
|
||||
'ytmusic-menu-renderer.ytmusic-player-bar',
|
||||
);
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user