mirror of
https://github.com/th-ch/youtube-music.git
synced 2026-01-15 20:31:46 +00:00
Merge pull request #584 from Araxeus/fix-lyrics
fix various lyrics issues
This commit is contained in:
@ -3,20 +3,34 @@ const is = require("electron-is");
|
|||||||
|
|
||||||
module.exports = () => {
|
module.exports = () => {
|
||||||
ipcRenderer.on("update-song-info", (_, extractedSongInfo) => {
|
ipcRenderer.on("update-song-info", (_, extractedSongInfo) => {
|
||||||
const lyricsTab = document.querySelector('tp-yt-paper-tab[tabindex="-1"]');
|
const tabList = document.querySelectorAll("tp-yt-paper-tab");
|
||||||
|
const tabs = {
|
||||||
|
upNext: tabList[0],
|
||||||
|
lyrics: tabList[1],
|
||||||
|
discover: tabList[2],
|
||||||
|
}
|
||||||
|
|
||||||
// Check if disabled
|
// Check if disabled
|
||||||
if (!lyricsTab || !lyricsTab.hasAttribute("disabled")) {
|
if (!tabs.lyrics?.hasAttribute("disabled")) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
let hasLyrics = true;
|
||||||
|
|
||||||
const html = ipcRenderer.sendSync(
|
const html = ipcRenderer.sendSync(
|
||||||
"search-genius-lyrics",
|
"search-genius-lyrics",
|
||||||
extractedSongInfo
|
extractedSongInfo
|
||||||
);
|
);
|
||||||
if (!html) {
|
if (!html) {
|
||||||
|
// Delete previous lyrics if tab is open and couldn't get new lyrics
|
||||||
|
checkLyricsContainer(() => {
|
||||||
|
hasLyrics = false;
|
||||||
|
setTabsOnclick(undefined);
|
||||||
|
});
|
||||||
return;
|
return;
|
||||||
} else if (is.dev()) {
|
}
|
||||||
|
|
||||||
|
if (is.dev()) {
|
||||||
console.log("Fetched lyrics from Genius");
|
console.log("Fetched lyrics from Genius");
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -35,27 +49,16 @@ module.exports = () => {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
lyricsTab.removeAttribute("disabled");
|
enableLyricsTab();
|
||||||
lyricsTab.removeAttribute("aria-disabled");
|
|
||||||
document.querySelector("tp-yt-paper-tab").onclick = () => {
|
|
||||||
lyricsTab.removeAttribute("disabled");
|
|
||||||
lyricsTab.removeAttribute("aria-disabled");
|
|
||||||
};
|
|
||||||
|
|
||||||
lyricsTab.onclick = () => {
|
setTabsOnclick(enableLyricsTab);
|
||||||
|
|
||||||
|
checkLyricsContainer();
|
||||||
|
|
||||||
|
tabs.lyrics.onclick = () => {
|
||||||
const tabContainer = document.querySelector("ytmusic-tab-renderer");
|
const tabContainer = document.querySelector("ytmusic-tab-renderer");
|
||||||
const observer = new MutationObserver((_, observer) => {
|
const observer = new MutationObserver((_, observer) => {
|
||||||
const lyricsContainer = document.querySelector(
|
checkLyricsContainer(() => observer.disconnect());
|
||||||
'[page-type="MUSIC_PAGE_TYPE_TRACK_LYRICS"] > ytmusic-message-renderer'
|
|
||||||
);
|
|
||||||
if (lyricsContainer) {
|
|
||||||
lyricsContainer.innerHTML = `<div id="contents" class="style-scope ytmusic-section-list-renderer genius-lyrics">
|
|
||||||
${lyrics}
|
|
||||||
|
|
||||||
<yt-formatted-string class="footer style-scope ytmusic-description-shelf-renderer">Source : Genius</yt-formatted-string>
|
|
||||||
</div>`;
|
|
||||||
observer.disconnect();
|
|
||||||
}
|
|
||||||
});
|
});
|
||||||
observer.observe(tabContainer, {
|
observer.observe(tabContainer, {
|
||||||
attributes: true,
|
attributes: true,
|
||||||
@ -63,5 +66,41 @@ module.exports = () => {
|
|||||||
subtree: true,
|
subtree: true,
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
|
function checkLyricsContainer(callback = () => {}) {
|
||||||
|
const lyricsContainer = document.querySelector(
|
||||||
|
'[page-type="MUSIC_PAGE_TYPE_TRACK_LYRICS"] > ytmusic-message-renderer'
|
||||||
|
);
|
||||||
|
if (lyricsContainer) {
|
||||||
|
callback();
|
||||||
|
setLyrics(lyricsContainer)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
function setLyrics(lyricsContainer) {
|
||||||
|
lyricsContainer.innerHTML =
|
||||||
|
`<div id="contents" class="style-scope ytmusic-section-list-renderer description ytmusic-description-shelf-renderer genius-lyrics">
|
||||||
|
${hasLyrics ? lyrics : 'Could not retrieve lyrics from genius'}
|
||||||
|
|
||||||
|
</div>
|
||||||
|
<yt-formatted-string class="footer style-scope ytmusic-description-shelf-renderer" style="align-self: baseline"></yt-formatted-string>`;
|
||||||
|
if (hasLyrics) {
|
||||||
|
lyricsContainer.querySelector('.footer').textContent = 'Source: Genius';
|
||||||
|
enableLyricsTab();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
function setTabsOnclick(callback) {
|
||||||
|
for (tab of [tabs.upNext, tabs.discover]) {
|
||||||
|
if (tab) {
|
||||||
|
tab.onclick = callback;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
function enableLyricsTab() {
|
||||||
|
tabs.lyrics.removeAttribute("disabled");
|
||||||
|
tabs.lyrics.removeAttribute("aria-disabled");
|
||||||
|
}
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|||||||
@ -6,8 +6,7 @@
|
|||||||
text-decoration: none;
|
text-decoration: none;
|
||||||
}
|
}
|
||||||
|
|
||||||
#contents.genius-lyrics {
|
.description {
|
||||||
font-size: 1vw;
|
font-size: 1.1vw !important;
|
||||||
opacity: 0.9;
|
text-align: center !important;
|
||||||
text-align: center;
|
|
||||||
}
|
}
|
||||||
|
|||||||
@ -112,13 +112,12 @@ const suffixesToRemove = [
|
|||||||
" - topic",
|
" - topic",
|
||||||
"vevo",
|
"vevo",
|
||||||
" (performance video)",
|
" (performance video)",
|
||||||
" (official music video)",
|
|
||||||
" (official video)",
|
|
||||||
" (clip officiel)",
|
" (clip officiel)",
|
||||||
];
|
];
|
||||||
|
|
||||||
function cleanupName(name) {
|
function cleanupName(name) {
|
||||||
if (!name) return name;
|
if (!name) return name;
|
||||||
|
name = name.replace(/\((?:official)?[ ]?(?:music)?[ ]?(?:lyric[s]?)?[ ]?(?:video)?\)$/i, '')
|
||||||
const lowCaseName = name.toLowerCase();
|
const lowCaseName = name.toLowerCase();
|
||||||
for (const suffix of suffixesToRemove) {
|
for (const suffix of suffixesToRemove) {
|
||||||
if (lowCaseName.endsWith(suffix)) {
|
if (lowCaseName.endsWith(suffix)) {
|
||||||
|
|||||||
Reference in New Issue
Block a user