Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix various lyrics issues #584

Merged
merged 11 commits into from
Feb 9, 2022
81 changes: 60 additions & 21 deletions plugins/lyrics-genius/front.js
Original file line number Diff line number Diff line change
@@ -3,20 +3,34 @@ const is = require("electron-is");

module.exports = () => {
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
if (!lyricsTab || !lyricsTab.hasAttribute("disabled")) {
if (!tabs.lyrics?.hasAttribute("disabled")) {
return;
}

let hasLyrics = true;

const html = ipcRenderer.sendSync(
"search-genius-lyrics",
extractedSongInfo
);
if (!html) {
// Delete previous lyrics if tab is open and couldn't get new lyrics
checkLyricsContainer(() => {
hasLyrics = false;
setTabsOnclick(undefined);
});
return;
} else if (is.dev()) {
}

if (is.dev()) {
console.log("Fetched lyrics from Genius");
}

@@ -35,33 +49,58 @@ module.exports = () => {
return;
}

lyricsTab.removeAttribute("disabled");
lyricsTab.removeAttribute("aria-disabled");
document.querySelector("tp-yt-paper-tab").onclick = () => {
lyricsTab.removeAttribute("disabled");
lyricsTab.removeAttribute("aria-disabled");
};
enableLyricsTab();

setTabsOnclick(enableLyricsTab);

checkLyricsContainer();

lyricsTab.onclick = () => {
tabs.lyrics.onclick = () => {
const tabContainer = document.querySelector("ytmusic-tab-renderer");
const observer = new MutationObserver((_, observer) => {
const lyricsContainer = document.querySelector(
'[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&nbsp;: Genius</yt-formatted-string>
</div>`;
observer.disconnect();
}
checkLyricsContainer(() => observer.disconnect());
});
observer.observe(tabContainer, {
attributes: true,
childList: 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]) {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit to avoid a warning about tab:

Suggested change
for (tab of [tabs.upNext, tabs.discover]) {
for (const tab of [tabs.upNext, tabs.discover]) {

if (tab) {
tab.onclick = callback;
}
}
}

function enableLyricsTab() {
tabs.lyrics.removeAttribute("disabled");
tabs.lyrics.removeAttribute("aria-disabled");
}
});
};
7 changes: 3 additions & 4 deletions plugins/lyrics-genius/style.css
Original file line number Diff line number Diff line change
@@ -6,8 +6,7 @@
text-decoration: none;
}

#contents.genius-lyrics {
font-size: 1vw;
opacity: 0.9;
text-align: center;
.description {
font-size: 1.1vw !important;
text-align: center !important;
}
3 changes: 1 addition & 2 deletions providers/song-info.js
Original file line number Diff line number Diff line change
@@ -112,13 +112,12 @@ const suffixesToRemove = [
" - topic",
"vevo",
" (performance video)",
" (official music video)",
" (official video)",
" (clip officiel)",
];

function cleanupName(name) {
if (!name) return name;
name = name.replace(/\((?:official)?[ ]?(?:music)?[ ]?(?:lyric[s]?)?[ ]?(?:video)?\)$/i, '')
const lowCaseName = name.toLowerCase();
for (const suffix of suffixesToRemove) {
if (lowCaseName.endsWith(suffix)) {