-
Notifications
You must be signed in to change notification settings - Fork 0
/
preload.js
96 lines (81 loc) · 2.89 KB
/
preload.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
const { ipcRenderer } = require("electron");
window.addEventListener("DOMContentLoaded", () => {
function checkTitle() {
return document.title === "YouTube Music";
}
function checkLike() {
const likeButton = document.querySelector(".middle-controls-buttons .like");
return likeButton && likeButton.getAttribute("aria-pressed") == "true";
}
function checkTrackSelection() {
const playPauseButton = document.querySelector(".play-pause-button");
const isTrackSelected = !!playPauseButton;
ipcRenderer.send("update-thumbar-buttons", { isTrackSelected });
return isTrackSelected;
}
function togglePlayPause() {
const progressBar = document.querySelector("#progress-bar");
if (progressBar) {
const progressValue = progressBar.getAttribute("aria-valuetext");
if (progressValue.includes("NaN")) {
const myDiv = document.querySelector(".ytmusic-play-button-renderer");
myDiv.click();
} else {
const playPauseButton = document.querySelector(".play-pause-button");
if (playPauseButton) {
playPauseButton.click();
}
}
}
}
const observer = new MutationObserver(() => {
const isYouTubeMusic = checkTitle();
ipcRenderer.send("update-thumbar-buttons", {
isTrackSelected: !isYouTubeMusic,
});
ipcRenderer.send("update-play-pause", !isYouTubeMusic); // Изменяем состояние play/pause
});
observer.observe(document.querySelector("title"), {
childList: true,
subtree: true,
});
const likeButton = document.querySelector(".middle-controls-buttons .like");
const observer2 = new MutationObserver(() => {
const isLiked = checkLike();
ipcRenderer.send("update-like-button", !isLiked);
});
observer2.observe(likeButton, {
attributes: true,
attributeFilter: ["aria-pressed"],
});
ipcRenderer.on("like-track", () => {
const likeButtonElement = document.querySelector(
".middle-controls-buttons .like button"
);
if (likeButtonElement) {
const isLiked = checkLike();
likeButtonElement.click();
ipcRenderer.send("update-like-button", isLiked);
}
});
ipcRenderer.on("media-play-pause", togglePlayPause);
ipcRenderer.on("media-next-track", () => {
const nextButton = document.querySelector(".next-button");
if (nextButton && !nextButton.disabled) {
nextButton.click();
}
});
ipcRenderer.on("media-previous-track", () => {
const previousButton = document.querySelector(".previous-button");
if (previousButton && !previousButton.disabled) {
previousButton.click();
}
});
ipcRenderer.on("media-radio-track", () => {
document.querySelector(".middle-controls-buttons #button-shape").click();
document.querySelector(".middle-controls-buttons #button-shape").click();
setTimeout(() => {
document.getElementById("navigation-endpoint").click();
}, 500);
});
});