Skip to content

Commit 53f1fd9

Browse files
cyfung1031cvzi
authored andcommitted
fix getYoutubeMainVideo() not [src] during song changing
1 parent 26c0d6a commit 53f1fd9

File tree

1 file changed

+19
-3
lines changed

1 file changed

+19
-3
lines changed

YoutubeMusicGeniusLyrics.user.js

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -321,11 +321,23 @@ function addLyrics (force, beLessSpecific) {
321321
songTitle = genius.f.cleanUpSongTitle(songTitle)
322322

323323
const video = getYoutubeMainVideo()
324+
console.log('debug: Youtube Music Genius Lyrics - getYoutubeMainVideo()', video)
324325
const musicIsPlaying = video && !video.paused
325326
genius.f.loadLyrics(force, beLessSpecific, songTitle, songArtistsArr, musicIsPlaying)
326327
}
327328

328329
function getYoutubeMainVideo () {
330+
const activeMedia_ = activeMedia
331+
if (activeMedia_) {
332+
const moviePlayer = activeMedia_.closest('#movie_player')
333+
const mediaList = moviePlayer ? moviePlayer.querySelectorAll('audio, video') : null
334+
if (mediaList && mediaList.length === 1 && mediaList[0] === activeMedia_) {
335+
return activeMedia_
336+
}
337+
if (activeMedia_.classList.contains('html5-main-video')) {
338+
return activeMedia_
339+
}
340+
}
329341
let video = document.querySelector('#movie_player video[src]')
330342
if (video !== null) {
331343
return video
@@ -674,7 +686,7 @@ const getNodeHTML = (e) => {
674686
}
675687
return ''
676688
}
677-
689+
let activeMedia = null
678690
async function setupMain () {
679691
let resizeRequested = false
680692
lyricsWidth = await GM.getValue('lyricswidth', '40%')
@@ -686,7 +698,7 @@ async function setupMain () {
686698
if (lastNodeString === nodeString) return
687699
lastNodeString = nodeString
688700
if (nodeString.length > 1 && songInfoNodes.isSongQueuedOrPlaying) {
689-
console.log('debug: Youtube Music Genius Lyrics', songInfoNodes, nodeString)
701+
console.log('debug: Youtube Music Genius Lyrics - Song Info', songInfoNodes, nodeString)
690702
if (genius.option.autoShow) {
691703
addLyrics()
692704
} else {
@@ -705,13 +717,17 @@ async function setupMain () {
705717
if (titleNode) {
706718
mutationObserver.observe(titleNode, { attributes: true, childList: true, subtree: true, characterData: true, attributeFilter: ['media-changed-at', 'title'] })
707719
titleNode.setAttribute('media-changed-at', Date.now())
720+
} else {
721+
activeMedia = null
708722
}
709723
}
710724

711725
const onMediaChanged = (evt) => {
712-
if (!(evt?.target instanceof HTMLMediaElement)) return
726+
const target = evt?.target
727+
if (!(target instanceof HTMLMediaElement)) return
713728
if (runid > 1e9) runid = 9
714729
const runid_ = ++runid
730+
activeMedia = target
715731
Promise.resolve(runid_).then(onMediaChanged_).catch(console.warn)
716732
}
717733

0 commit comments

Comments
 (0)