-
Notifications
You must be signed in to change notification settings - Fork 602
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 multiple songInfo calls #439
Conversation
Would probably be better at this point to get all the data from the same place instead of maybe something like `onXHR request -> check if XHR.details.title !== document.querySelector(".title.ytmusic-player-bar")?.textContent -> get and send all info from DOM instead of XHR also am I missing something or songUrl wasn't changed? it should come from the same place as the title to avoid sync issues songInfo.url = win.webContents.getURL().split("&")[0]; // or something else from dom (I actually was planning to do all this #334 (comment) but you beat me to it 😅) another way to do this would be to use the youtube video api (but that requires injecting script into page and listeners since it needs page context to work - so probably not the most efficient idea lol) |
|
i believe that this should fix the problem for wring title and then we can upgrade for a better way of getting the info |
Sorry my bad but that isn't the most reliable way to get the video url since current page url isn't guaranteed to be the video url (if you browse the app it changes the url) Needs to be changed to something like this: // song-info.js
{...
const currentSrc = win.webContents.getURL().split("&")[0];
songInfo.url = currentSrc.includes('/watch?v=') ?
currentSrc :
getSongUrl(songInfo.imageSrc) ||
data?.microformat?.microformatDataRenderer?.urlCanonical;
...}
const getSongUrl = (imgSrc) => {
const regexResult = imgSrc?.match(/\/vi\/(.*)\//);
return regexResult?.length >= 2 ?
"https://music.youtube.com/watch?v=" + regexResult[1] :
'';
} This checks if currentURL is actually a video url -> if not then grab video id from thumbnail -> if no thumbnail then just use old data from XHR thumbnail at the playerbar is usually something like (this getSongUrl method will also not always work, if the page is too small and the thumbnail isn't rendered, then the imgSrc will be 'undefined' - and thats why there are 3 fallbacks for this. but anyways this piece of code was tested and should work correctly this time 😋) |
Here is an idea for progress updates straight from the browser: (untested but should theoretically work) In front.jsconst { ipcRenderer } = require("electron");
ipcRenderer.once("observe-progressbar", async () => {
const progressObserver = new MutationObserver(mutations => {
mutations.forEach(mutation => {
if (mutation.type === 'attributes' && mutation.attributeName === 'value') {
ipcRenderer.send('progress-change', mutation.target.value)
}
});
});
progressObserver.observe($('#progress-bar'), { attributeFilter: ["value"] })
}); In back.jsconst { ipcMain } = require("electron");
const secToMilisec = t => Math.round(Number(t) * 1000)
const watchingProgress = false;
const data = {
cover_url: '',
title: '',
artists: '',
status: '',
progress: '',
duration: '',
album_url: ''
};
module.exports = async (win) => {
registerCallback(async (songInfo) => {
if (songInfo.title?.length === 0 && songInfo.artist?.length === 0) {
return;
}
if (!watchingProgress) {
win.webContents.send("observe-progressbar");
watchingProgress = true;
}
data.duration = secToMilisec(songInfo.songDuration)
data.progress = secToMilisec(songInfo.elapsedSeconds)
data.cover_url = songInfo.imageSrc
data.album_url = songInfo.imageSrc
data.title = songInfo.title
data.artists = [songInfo.artist]
data.status = !songInfo.isPaused ? 'Playing' : 'Paused'
await post(data);
})
ipcMain.on("progress-change", (_event, async newProgress => {
data.progress = secToMilisec(newProgress)
await post(data);
}));
} |
i'll code this tomorrow, so expect one or twot days commit for that |
seems to be a valid approach, i'll try this with the update for url song |
I have found a better and cleaner way for getting all the songInfo stuff and opened a new PR, see #443 Could you rename this PR and delete all changes to songInfo.js and leave only the tuna stuff please? 😄 |
if this PR (#443) fixes the problem i can close this PR entirely and reopen only for progress bar |
#443 does fix the problem You can test it yourself 😅 |
pr closed, i'll wait #443 |
@mesmerx you wanna add the live updates yourself? or shall I just add this to my ever expanding list of pr's? 😝 |
in the daily usage of tuna plugin that i created i get that sometimes the songs info are mixed with the currently one and the next one
debuging i get that the handleData are receiving or some reason the info for the currently and the next one, so i fix that setting functions to get currently info for what i can get from document itself
i set some verification for edge cases without info and a song-info crash that happens too