|
1 | 1 | (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; |
19 | 4 | const style = document.createElement("style"); |
20 | 5 | style.innerHTML = ` |
21 | 6 | .main-nowPlayingBar-lyricsButton[data-testid="lyrics-button"] { |
|
27 | 12 | `; |
28 | 13 | style.classList.add("lyrics-plus:visual:playbar-button"); |
29 | 14 |
|
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 | | - |
39 | 15 | function setPlaybarButton() { |
40 | 16 | document.head.appendChild(style); |
41 | | - button.register(); |
| 17 | + button?.register(); |
42 | 18 | } |
43 | 19 |
|
44 | 20 | function removePlaybarButton() { |
45 | 21 | style.remove(); |
46 | | - button.deregister(); |
| 22 | + button?.deregister(); |
47 | 23 | } |
| 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(); |
48 | 75 | })(); |
0 commit comments