mirror of
https://github.com/th-ch/youtube-music.git
synced 2026-01-14 03:41:46 +00:00
fix(music-together): fix profile issue
This commit is contained in:
@ -21,6 +21,8 @@ import { createSettingPopup } from './ui/setting';
|
|||||||
import settingHTML from './templates/setting.html?raw';
|
import settingHTML from './templates/setting.html?raw';
|
||||||
import style from './style.css?inline';
|
import style from './style.css?inline';
|
||||||
|
|
||||||
|
import { waitForElement } from '@/utils/wait-for-element';
|
||||||
|
|
||||||
import type { YoutubePlayer } from '@/types/youtube-player';
|
import type { YoutubePlayer } from '@/types/youtube-player';
|
||||||
import type { RendererContext } from '@/types/contexts';
|
import type { RendererContext } from '@/types/contexts';
|
||||||
import type { VideoDataChanged } from '@/types/video-data-changed';
|
import type { VideoDataChanged } from '@/types/video-data-changed';
|
||||||
@ -595,19 +597,22 @@ export default createPlugin<
|
|||||||
this.elements.spinner.setAttribute('hidden', '');
|
this.elements.spinner.setAttribute('hidden', '');
|
||||||
},
|
},
|
||||||
|
|
||||||
initMyProfile() {
|
async initMyProfile() {
|
||||||
const accountButton = document.querySelector<
|
const accountButton = await waitForElement<HTMLElement>(
|
||||||
HTMLElement & {
|
'#right-content > ytmusic-settings-button *:where(tp-yt-paper-icon-button,yt-icon-button,.ytmusic-settings-button)',
|
||||||
onButtonTap: () => void;
|
{
|
||||||
}
|
maxRetry: 10000,
|
||||||
>('ytmusic-settings-button');
|
},
|
||||||
|
);
|
||||||
|
|
||||||
accountButton?.onButtonTap();
|
accountButton?.click();
|
||||||
setTimeout(() => {
|
setTimeout(async () => {
|
||||||
accountButton?.onButtonTap();
|
const renderer = await waitForElement<HTMLElement & { data: unknown }>(
|
||||||
const renderer = document.querySelector<
|
'ytd-active-account-header-renderer',
|
||||||
HTMLElement & { data: unknown }
|
{
|
||||||
>('ytd-active-account-header-renderer');
|
maxRetry: 10000,
|
||||||
|
},
|
||||||
|
);
|
||||||
if (!accountButton || !renderer) {
|
if (!accountButton || !renderer) {
|
||||||
console.warn('Music Together: Cannot find account');
|
console.warn('Music Together: Cannot find account');
|
||||||
this.me = getDefaultProfile(this.connection?.id ?? '');
|
this.me = getDefaultProfile(this.connection?.id ?? '');
|
||||||
@ -628,6 +633,7 @@ export default createPlugin<
|
|||||||
this.popups.guest.setProfile(this.me.thumbnail);
|
this.popups.guest.setProfile(this.me.thumbnail);
|
||||||
this.popups.setting.setProfile(this.me.thumbnail);
|
this.popups.setting.setProfile(this.me.thumbnail);
|
||||||
}
|
}
|
||||||
|
accountButton?.click(); // close menu
|
||||||
}, 0);
|
}, 0);
|
||||||
},
|
},
|
||||||
/* hooks */
|
/* hooks */
|
||||||
|
|||||||
@ -1,13 +1,30 @@
|
|||||||
export const waitForElement = <T extends Element>(
|
export const waitForElement = <T extends Element>(
|
||||||
selector: string,
|
selector: string,
|
||||||
|
options: {
|
||||||
|
maxRetry?: number;
|
||||||
|
retryInterval?: number;
|
||||||
|
} = {
|
||||||
|
maxRetry: -1,
|
||||||
|
retryInterval: 100,
|
||||||
|
},
|
||||||
): Promise<T> => {
|
): Promise<T> => {
|
||||||
return new Promise<T>((resolve) => {
|
return new Promise<T>((resolve) => {
|
||||||
|
let retryCount = 0;
|
||||||
|
const maxRetry = options.maxRetry ?? -1;
|
||||||
|
const retryInterval = options.retryInterval ?? 100;
|
||||||
const interval = setInterval(() => {
|
const interval = setInterval(() => {
|
||||||
|
if (maxRetry > 0 && retryCount >= maxRetry) {
|
||||||
|
clearInterval(interval);
|
||||||
|
return;
|
||||||
|
}
|
||||||
const elem = document.querySelector<T>(selector);
|
const elem = document.querySelector<T>(selector);
|
||||||
if (!elem) return;
|
if (!elem) {
|
||||||
|
retryCount++;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
clearInterval(interval);
|
clearInterval(interval);
|
||||||
resolve(elem);
|
resolve(elem);
|
||||||
}, 100 /* ms */);
|
}, retryInterval /* ms */);
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|||||||
Reference in New Issue
Block a user