Skip to content

Commit

Permalink
👽️ Fixes from changes to YTM API
Browse files Browse the repository at this point in the history
  • Loading branch information
NovusTheory committed Oct 11, 2023
1 parent f09bba8 commit 613a71a
Show file tree
Hide file tree
Showing 7 changed files with 56 additions and 45 deletions.
33 changes: 22 additions & 11 deletions src/integrations/volume-ratio/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,9 @@ export default class VolumeRatio implements IIntegration {
this.isEnabled = true;
if ((this.isEnabled && this.hasInjected) || this.ytmView === null) return;

this.ytmView.webContents.executeJavaScript(enableScript)
.catch((err) => { console.log(err); });
this.ytmView.webContents.executeJavaScript(enableScript).catch(err => {
console.log(err);
});

this.forceUpdateVolume();

Expand All @@ -36,7 +37,9 @@ export default class VolumeRatio implements IIntegration {
this.isEnabled = false;
if (!this.hasInjected) return;

this.ytmView.webContents.executeJavaScript(`
this.ytmView.webContents
.executeJavaScript(
`
{
if (typeof window.HTMLMediaElement_volume !== 'undefined' &&
typeof window.HTMLMediaElement_volume.get !== 'undefined' &&
Expand All @@ -54,23 +57,31 @@ export default class VolumeRatio implements IIntegration {
// Electron is not happy with whatever is returned, so we just give it an empty string.
'';
`)
.catch((err) => { err; });
`
)
.catch(err => {
err;
});

this.forceUpdateVolume();
this.hasInjected = false;
}

private forceUpdateVolume(): void {
this.ytmView.webContents.executeJavaScript(`
this.ytmView.webContents
.executeJavaScript(
`
{
let volume = document.querySelector("ytmusic-player-bar").playerApi_.getVolume();
document.querySelector("ytmusic-player-bar").playerApi_.setVolume(volume);
let volume = document.querySelector("ytmusic-player-bar").playerApi.getVolume();
document.querySelector("ytmusic-player-bar").playerApi.setVolume(volume);
document.querySelector("ytmusic-player-bar").store.dispatch({ type: 'SET_VOLUME', payload: volume });
volume;
}
`)
.catch((err) => { err; });
`
)
.catch(err => {
err;
});
}
}
}
38 changes: 19 additions & 19 deletions src/ytmview/preload.ts
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,7 @@ window.addEventListener("load", async () => {
const interval = setInterval(async () => {
const playerApiReady: boolean = await webFrame.executeJavaScript(`
{
document.querySelector("ytmusic-player-bar").playerApi_.isReady();
document.querySelector("ytmusic-player-bar").playerApi.isReady();
}
`);

Expand Down Expand Up @@ -237,14 +237,14 @@ window.addEventListener("load", async () => {
{
document.querySelector("ytmusic-popup-container").refitPopups_();
}
`)
`);
heightTransitionCount++;
if (heightTransitionCount >= 2) {
document.querySelector("ytmusic-player-bar").removeEventListener("transitionend", transitionEnd);
}
}
}
}
};
document.querySelector("ytmusic-player-bar").addEventListener("transitionend", transitionEnd);

document.dispatchEvent(
Expand All @@ -263,7 +263,7 @@ window.addEventListener("load", async () => {
} else {
webFrame.executeJavaScript(`
{
window.ytmd.sendVideoData(document.querySelector("ytmusic-player-bar").playerApi_.getPlayerResponse().videoDetails, document.querySelector("ytmusic-player-bar").playerApi_.getPlaylistId());
window.ytmd.sendVideoData(document.querySelector("ytmusic-player-bar").playerApi.getPlayerResponse().videoDetails, document.querySelector("ytmusic-player-bar").playerApi.getPlaylistId());
}
`);
}
Expand All @@ -279,7 +279,7 @@ window.addEventListener("load", async () => {
case "playPause": {
webFrame.executeJavaScript(`
{
document.querySelector("ytmusic-player-bar").playing_ ? document.querySelector("ytmusic-player-bar").playerApi_.pauseVideo() : document.querySelector("ytmusic-player-bar").playerApi_.playVideo();
document.querySelector("ytmusic-player-bar").playing ? document.querySelector("ytmusic-player-bar").playerApi.pauseVideo() : document.querySelector("ytmusic-player-bar").playerApi.playVideo();
}
`);
break;
Expand All @@ -288,7 +288,7 @@ window.addEventListener("load", async () => {
case "play": {
webFrame.executeJavaScript(`
{
document.querySelector("ytmusic-player-bar").playerApi_.playVideo();
document.querySelector("ytmusic-player-bar").playerApi.playVideo();
}
`);
break;
Expand All @@ -297,7 +297,7 @@ window.addEventListener("load", async () => {
case "pause": {
webFrame.executeJavaScript(`
{
document.querySelector("ytmusic-player-bar").playerApi_.pauseVideo();
document.querySelector("ytmusic-player-bar").playerApi.pauseVideo();
}
`);
break;
Expand All @@ -306,7 +306,7 @@ window.addEventListener("load", async () => {
case "next": {
webFrame.executeJavaScript(`
{
document.querySelector("ytmusic-player-bar").playerApi_.nextVideo();
document.querySelector("ytmusic-player-bar").playerApi.nextVideo();
}
`);
break;
Expand All @@ -315,7 +315,7 @@ window.addEventListener("load", async () => {
case "previous": {
webFrame.executeJavaScript(`
{
document.querySelector("ytmusic-player-bar").playerApi_.previousVideo();
document.querySelector("ytmusic-player-bar").playerApi.previousVideo();
}
`);
break;
Expand All @@ -334,7 +334,7 @@ window.addEventListener("load", async () => {
case "volumeUp": {
const currentVolumeUp: number = await webFrame.executeJavaScript(`
{
document.querySelector("ytmusic-player-bar").playerApi_.getVolume();
document.querySelector("ytmusic-player-bar").playerApi.getVolume();
}
`);

Expand All @@ -344,7 +344,7 @@ window.addEventListener("load", async () => {
}
webFrame.executeJavaScript(`
{
document.querySelector("ytmusic-player-bar").playerApi_.setVolume(${newVolumeUp});
document.querySelector("ytmusic-player-bar").playerApi.setVolume(${newVolumeUp});
document.querySelector("ytmusic-player-bar").store.dispatch({ type: 'SET_VOLUME', payload: ${newVolumeUp} });
}
`);
Expand All @@ -354,7 +354,7 @@ window.addEventListener("load", async () => {
case "volumeDown": {
const currentVolumeDown: number = await webFrame.executeJavaScript(`
{
document.querySelector("ytmusic-player-bar").playerApi_.getVolume();
document.querySelector("ytmusic-player-bar").playerApi.getVolume();
}
`);

Expand All @@ -364,7 +364,7 @@ window.addEventListener("load", async () => {
}
webFrame.executeJavaScript(`
{
document.querySelector("ytmusic-player-bar").playerApi_.setVolume(${newVolumeDown});
document.querySelector("ytmusic-player-bar").playerApi.setVolume(${newVolumeDown});
document.querySelector("ytmusic-player-bar").store.dispatch({ type: 'SET_VOLUME', payload: ${newVolumeDown} });
}
`);
Expand All @@ -380,7 +380,7 @@ window.addEventListener("load", async () => {

webFrame.executeJavaScript(`
{
document.querySelector("ytmusic-player-bar").playerApi_.setVolume(${valueInt});
document.querySelector("ytmusic-player-bar").playerApi.setVolume(${valueInt});
document.querySelector("ytmusic-player-bar").store.dispatch({ type: 'SET_VOLUME', payload: ${valueInt} });
}
`);
Expand All @@ -390,7 +390,7 @@ window.addEventListener("load", async () => {
case "mute":
webFrame.executeJavaScript(`
{
document.querySelector("ytmusic-player-bar").playerApi_.mute();
document.querySelector("ytmusic-player-bar").playerApi.mute();
document.querySelector("ytmusic-player-bar").store.dispatch({ type: 'SET_MUTED', payload: true });
}
`);
Expand All @@ -399,7 +399,7 @@ window.addEventListener("load", async () => {
case "unmute":
webFrame.executeJavaScript(`
{
document.querySelector("ytmusic-player-bar").playerApi_.unMute();
document.querySelector("ytmusic-player-bar").playerApi.unMute();
document.querySelector("ytmusic-player-bar").store.dispatch({ type: 'SET_MUTED', payload: false });
}
`);
Expand All @@ -416,15 +416,15 @@ window.addEventListener("load", async () => {
case "seekTo":
webFrame.executeJavaScript(`
{
document.querySelector("ytmusic-player-bar").playerApi_.seekTo(${value});
document.querySelector("ytmusic-player-bar").playerApi.seekTo(${value});
}
`);
break;

case "shuffle":
webFrame.executeJavaScript(`
{
document.querySelector("ytmusic-player-bar").queue_.shuffle();
document.querySelector("ytmusic-player-bar").queue.shuffle();
}
`);
break;
Expand Down
2 changes: 1 addition & 1 deletion src/ytmview/scripts/getplaylists.script.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
document.querySelector("ytmusic-player-bar"),
{
addToPlaylistEndpoint: {
videoId: document.querySelector("ytmusic-player-bar").playerApi_.getPlayerResponse().videoDetails.videoId
videoId: document.querySelector("ytmusic-player-bar").playerApi.getPlayerResponse().videoDetails.videoId
}
}
],
Expand Down
16 changes: 8 additions & 8 deletions src/ytmview/scripts/hookplayerapievents.script.js
Original file line number Diff line number Diff line change
@@ -1,22 +1,22 @@
{
document.querySelector("ytmusic-player-bar").playerApi_.addEventListener("onVideoProgress", progress => {
document.querySelector("ytmusic-player-bar").playerApi.addEventListener("onVideoProgress", progress => {
window.ytmd.sendVideoProgress(progress);
});
document.querySelector("ytmusic-player-bar").playerApi_.addEventListener("onStateChange", state => {
document.querySelector("ytmusic-player-bar").playerApi.addEventListener("onStateChange", state => {
window.ytmd.sendVideoState(state);
});
document.querySelector("ytmusic-player-bar").playerApi_.addEventListener("onVideoDataChange", event => {
document.querySelector("ytmusic-player-bar").playerApi.addEventListener("onVideoDataChange", event => {
if (event.playertype === 1 && (event.type === "dataloaded" || event.type === "dataupdated")) {
window.ytmd.sendVideoData(
document.querySelector("ytmusic-player-bar").playerApi_.getPlayerResponse().videoDetails,
document.querySelector("ytmusic-player-bar").playerApi_.getPlaylistId()
document.querySelector("ytmusic-player-bar").playerApi.getPlayerResponse().videoDetails,
document.querySelector("ytmusic-player-bar").playerApi.getPlaylistId()
);
}
});
document.querySelector("ytmusic-player-bar").playerApi_.addEventListener("onAdStart", () => {
document.querySelector("ytmusic-player-bar").playerApi.addEventListener("onAdStart", () => {
window.ytmd.sendAdState(true);
});
document.querySelector("ytmusic-player-bar").playerApi_.addEventListener("onAdEnd", () => {
document.querySelector("ytmusic-player-bar").playerApi.addEventListener("onAdEnd", () => {
window.ytmd.sendAdState(false);
});
document.querySelector("ytmusic-player-bar").store.subscribe(() => {
Expand All @@ -42,7 +42,7 @@
}
}

const videoId = document.querySelector("ytmusic-player-bar").playerApi_.getPlayerResponse()?.videoDetails?.videoId;
const videoId = document.querySelector("ytmusic-player-bar").playerApi.getPlayerResponse()?.videoDetails?.videoId;
const likeButtonData = document.querySelector("ytmusic-player-bar").querySelector("ytmusic-like-button-renderer").data;
const defaultLikeStatus = likeButtonData?.likeStatus ?? "UNKNOWN";

Expand Down
8 changes: 4 additions & 4 deletions src/ytmview/scripts/playerbarcontrols.script.js
Original file line number Diff line number Diff line change
Expand Up @@ -135,9 +135,9 @@
});
libraryButton.insertAdjacentElement("afterend", playlistButton);

document.querySelector("ytmusic-player-bar").playerApi_.addEventListener("onVideoDataChange", event => {
document.querySelector("ytmusic-player-bar").playerApi.addEventListener("onVideoDataChange", event => {
if (event.type === "dataloaded" && event.playertype === 1) {
currentVideoId = document.querySelector("ytmusic-player-bar").playerApi_.getPlayerResponse().videoDetails.videoId;
currentVideoId = document.querySelector("ytmusic-player-bar").playerApi.getPlayerResponse().videoDetails.videoId;
}
});

Expand Down Expand Up @@ -382,8 +382,8 @@
sleepTimerButton.classList.remove("active");
sleepTimerButton.setAttribute("title", "Sleep timer off");

if (document.querySelector("ytmusic-player-bar").playing_) {
document.querySelector("ytmusic-player-bar").playerApi_.pauseVideo();
if (document.querySelector("ytmusic-player-bar").playing) {
document.querySelector("ytmusic-player-bar").playerApi.pauseVideo();

document.body.dispatchEvent(
new CustomEvent("yt-action", {
Expand Down
2 changes: 1 addition & 1 deletion src/ytmview/scripts/toggledislike.script.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
const videoId = document.querySelector("ytmusic-player-bar").playerApi_.getPlayerResponse().videoDetails.videoId;
const videoId = document.querySelector("ytmusic-player-bar").playerApi.getPlayerResponse().videoDetails.videoId;
const likeButtonData = document.querySelector("ytmusic-player-bar").querySelector("ytmusic-like-button-renderer").data;

let dislikeServiceEndpoint = null;
Expand Down
2 changes: 1 addition & 1 deletion src/ytmview/scripts/togglelike.script.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
const videoId = document.querySelector("ytmusic-player-bar").playerApi_.getPlayerResponse().videoDetails.videoId;
const videoId = document.querySelector("ytmusic-player-bar").playerApi.getPlayerResponse().videoDetails.videoId;
const likeButtonData = document.querySelector("ytmusic-player-bar").querySelector("ytmusic-like-button-renderer").data;

let likeServiceEndpoint = null;
Expand Down

0 comments on commit 613a71a

Please sign in to comment.