Skip to content

Commit eae7100

Browse files
committed
fix(lyrics-plus): prevent crash when clicking lyrics button
1 parent a4910e6 commit eae7100

File tree

2 files changed

+56
-29
lines changed

2 files changed

+56
-29
lines changed
Lines changed: 55 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,6 @@
11
(function PlaybarButton() {
2-
if (!Spicetify.Platform.History) {
3-
setTimeout(PlaybarButton, 300);
4-
return;
5-
}
6-
7-
const button = new Spicetify.Playbar.Button(
8-
"Lyrics Plus",
9-
`<svg role="img" height="16" width="16" aria-hidden="true" viewBox="0 0 16 16" data-encore-id="icon" fill="currentColor"><path d="M13.426 2.574a2.831 2.831 0 0 0-4.797 1.55l3.247 3.247a2.831 2.831 0 0 0 1.55-4.797zM10.5 8.118l-2.619-2.62A63303.13 63303.13 0 0 0 4.74 9.075L2.065 12.12a1.287 1.287 0 0 0 1.816 1.816l3.06-2.688 3.56-3.129zM7.12 4.094a4.331 4.331 0 1 1 4.786 4.786l-3.974 3.493-3.06 2.689a2.787 2.787 0 0 1-3.933-3.933l2.676-3.045 3.505-3.99z"></path></svg>`,
10-
() =>
11-
Spicetify.Platform.History.location.pathname !== "/lyrics-plus"
12-
? Spicetify.Platform.History.push("/lyrics-plus")
13-
: Spicetify.Platform.History.goBack(),
14-
false,
15-
Spicetify.Platform.History.location.pathname === "/lyrics-plus",
16-
false
17-
);
18-
2+
let retryCount = 0;
3+
let button;
194
const style = document.createElement("style");
205
style.innerHTML = `
216
.main-nowPlayingBar-lyricsButton[data-testid="lyrics-button"] {
@@ -27,22 +12,64 @@
2712
`;
2813
style.classList.add("lyrics-plus:visual:playbar-button");
2914

30-
if (Spicetify.LocalStorage.get("lyrics-plus:visual:playbar-button") === "true") setPlaybarButton();
31-
window.addEventListener("lyrics-plus", (event) => {
32-
if (event.detail?.name === "playbar-button") event.detail.value ? setPlaybarButton() : removePlaybarButton();
33-
});
34-
35-
Spicetify.Platform.History.listen((location) => {
36-
button.active = location.pathname === "/lyrics-plus";
37-
});
38-
3915
function setPlaybarButton() {
4016
document.head.appendChild(style);
41-
button.register();
17+
button?.register();
4218
}
4319

4420
function removePlaybarButton() {
4521
style.remove();
46-
button.deregister();
22+
button?.deregister();
4723
}
24+
25+
const checkHistory = () => {
26+
if (retryCount > 100) return; // Stop after ~30 seconds
27+
if (!Spicetify.Platform.History) {
28+
retryCount++;
29+
setTimeout(checkHistory, 300);
30+
return;
31+
}
32+
init();
33+
};
34+
35+
function init() {
36+
const history = Spicetify.Platform.History;
37+
if (!history._lyricsPlusPatched) {
38+
const originalPush = history.push;
39+
history.push = (path, state) => {
40+
if (typeof path === "string" && path.includes("lyrics-plus")) {
41+
path = { pathname: path, state: { ...state, cinemaState: null } };
42+
return originalPush.apply(history, [path]);
43+
} else if (typeof path === "object" && path.pathname && path.pathname.includes("lyrics-plus")) {
44+
path.state = { ...path.state, cinemaState: null };
45+
}
46+
return originalPush.apply(history, [path, state]);
47+
};
48+
history._lyricsPlusPatched = true;
49+
}
50+
51+
button = new Spicetify.Playbar.Button(
52+
"Lyrics Plus",
53+
`<svg role="img" height="16" width="16" aria-hidden="true" viewBox="0 0 16 16" data-encore-id="icon" fill="currentColor"><path d="M13.426 2.574a2.831 2.831 0 0 0-4.797 1.55l3.247 3.247a2.831 2.831 0 0 0 1.55-4.797zM10.5 8.118l-2.619-2.62A63303.13 63303.13 0 0 0 4.74 9.075L2.065 12.12a1.287 1.287 0 0 0 1.816 1.816l3.06-2.688 3.56-3.129zM7.12 4.094a4.331 4.331 0 1 1 4.786 4.786l-3.974 3.493-3.06 2.689a2.787 2.787 0 0 1-3.933-3.933l2.676-3.045 3.505-3.99z"></path></svg>`,
54+
() =>
55+
Spicetify.Platform.History.location.pathname !== "/lyrics-plus"
56+
? Spicetify.Platform.History.push({ pathname: "/lyrics-plus", state: { cinemaState: null } })
57+
: Spicetify.Platform.History.goBack(),
58+
false,
59+
Spicetify.Platform.History.location.pathname === "/lyrics-plus",
60+
false
61+
);
62+
63+
history.listen((location) => {
64+
button.active = location.pathname === "/lyrics-plus";
65+
});
66+
67+
if (Spicetify.LocalStorage.get("lyrics-plus:visual:playbar-button") === "true") setPlaybarButton();
68+
69+
window.addEventListener("lyrics-plus", (event) => {
70+
if (event.detail?.name === "playbar-button") event.detail.value ? setPlaybarButton() : removePlaybarButton();
71+
});
72+
}
73+
74+
checkHistory();
4875
})();

jsHelper/spicetifyWrapper.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2350,7 +2350,7 @@ Spicetify.Playbar = (() => {
23502350
this.element = document.createElement("button");
23512351
this.element.classList.add("main-genericButton-button");
23522352
this.iconElement = document.createElement("span");
2353-
this.iconElement.classList.add("Wrapper-sm-only", "Wrapper-small-only");
2353+
this.iconElement.classList.add("e-91000-button__icon-wrapper");
23542354
this.element.appendChild(this.iconElement);
23552355
this.icon = icon;
23562356
this.onClick = onClick;

0 commit comments

Comments
 (0)