Skip to content

Commit

Permalink
fix(synced-lyric): fix timestamp
Browse files Browse the repository at this point in the history
- Close #2323
- Close #2379
  • Loading branch information
JellyBrick committed Sep 7, 2024
1 parent 5b004ee commit a4992ba
Showing 1 changed file with 6 additions and 20 deletions.
26 changes: 6 additions & 20 deletions src/plugins/synced-lyrics/renderer/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,9 @@ export let _ytAPI: YoutubePlayer | null = null;

export const renderer = createRenderer<{
observerCallback: MutationCallback;
hasAddedEvents: boolean;
observer?: MutationObserver;
videoDataChange: () => Promise<void>;
progressCallback: (evt: Event) => void;
updateTimestampInterval?: NodeJS.Timeout | string | number;
}, SyncedLyricsPluginConfig>({
onConfigChange(newConfig) {
setConfig(newConfig);
Expand Down Expand Up @@ -50,15 +49,12 @@ export const renderer = createRenderer<{
await this.videoDataChange();
},

hasAddedEvents: false,

async videoDataChange() {
if (!this.hasAddedEvents) {
const video = document.querySelector('video');

video?.addEventListener('timeupdate', this.progressCallback);

if (video) this.hasAddedEvents = true;
if (!this.updateTimestampInterval) {
this.updateTimestampInterval = setInterval(
() => setCurrentTime((_ytAPI?.getCurrentTime() ?? 0) * 1000),
100,
);
}

this.observer ??= new MutationObserver(
Expand All @@ -73,16 +69,6 @@ export const renderer = createRenderer<{
header.removeAttribute('disabled');
},

progressCallback(evt: Event) {
switch (evt.type) {
case 'timeupdate': {
const video = evt.target as HTMLVideoElement;
setCurrentTime(video.currentTime * 1000);
break;
}
}
},

async start(ctx: RendererContext<SyncedLyricsPluginConfig>) {
setConfig(await ctx.getConfig());

Expand Down

0 comments on commit a4992ba

Please sign in to comment.