diff --git a/dist/LuckyLP.js b/dist/LuckyLP.js index b6d42a6..b62aaa3 100644 --- a/dist/LuckyLP.js +++ b/dist/LuckyLP.js @@ -1,2 +1,2 @@ -!async function(){for(;!Spicetify.React||!Spicetify.ReactDOM;)await new Promise(t=>setTimeout(t,10));var t,a,i,o,e,n,m,l,s;t=()=>` - `,a=()=>{return"/"===Spicetify.Platform.History.location.pathname},i=()=>{return Spicetify.Platform.History.location.pathname.startsWith("/album/")},o=async()=>{try{var t,a,i,o=await Spicetify.CosmosAsync.get("https://api.spotify.com/v1/me/albums?limit=1");return o&&null!=o&&o.total?(t=o.total,e=t,a=Math.floor(Math.random()*(e+1)),i=(await Spicetify.CosmosAsync.get("https://api.spotify.com/v1/me/albums?limit=1&offset="+(a-1))).items[0],Spicetify.Platform.History.push("/album/"+i.album.id),i):!1}catch(t){return!1}var e},e=async()=>{Spicetify.showNotification("Choosing random album...");var t,a=await o();!1!==a?(t=a.album.artists.map(t=>t.name).join(","),Spicetify.showNotification(`Chosen album: ${a.album.name} by `+t)):Spicetify.showNotification("Random album cannot be chosen.",!0)},n=()=>{(a()||i()?m:l)()},m=()=>{document.getElementById("random-saved-album-btn")||(new Spicetify.Topbar.Button("Get Random Saved Album",`${t}`,e).element.id="random-saved-album-btn")},l=()=>{var t=document.getElementById("random-saved-album-btn");t&&t.remove()},s=async function(){for(var t;null==Spicetify||!Spicetify.showNotification||null==(t=null==Spicetify?void 0:Spicetify.Platform)||!t.History;)await new Promise(t=>setTimeout(t,500));n(),Spicetify.Platform.History.listen(()=>{n()})},(async()=>{await s()})()}(); \ No newline at end of file +!async function(){for(;!Spicetify.React||!Spicetify.ReactDOM;)await new Promise(t=>setTimeout(t,10));var t,a,i,e,o,n,m,s,l;t=()=>` + `,a=t=>"/"===t,i=t=>t.startsWith("/album/"),e=async()=>{try{var t,a=await(await Spicetify.CosmosAsync.get("https://api.spotify.com/v1/me/albums?limit=1")).total;return a?(t=await(async t=>{t=Math.floor(Math.random()*(t+1));return(await Spicetify.CosmosAsync.get("https://api.spotify.com/v1/me/albums?limit=1&offset="+(t-1))).items[0]})(a),Spicetify.Platform.History.push("/album/"+t.album.id),t):!1}catch(t){return!1}},o=async()=>{Spicetify.showNotification("Choosing random album...");var t,a=await e();!1!==a?(t=a.album.artists.map(t=>t.name).join(","),Spicetify.showNotification(`Chosen album: ${a.album.name} by `+t)):Spicetify.showNotification("Random album cannot be chosen.",!0)},n=t=>{(a(t.pathname)||i(t.pathname)?m:s)()},m=()=>{document.getElementById("random-saved-album-btn")||(new Spicetify.Topbar.Button("Get Random Saved Album",`${t}`,o).element.id="random-saved-album-btn")},s=()=>{var t=document.getElementById("random-saved-album-btn");t&&t.remove()},l=async function(){for(var t;!(null!=Spicetify&&Spicetify.showNotification||null!=(t=null==Spicetify?void 0:Spicetify.Platform)&&t.History||null!=Spicetify&&Spicetify.CosmosAsync);)await new Promise(t=>setTimeout(t,500));n(Spicetify.Platform.History.location),Spicetify.Platform.History.listen(t=>{n(t)})},(async()=>{await l()})()}(); \ No newline at end of file diff --git a/package-lock.json b/package-lock.json index bd1eace..61cb302 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "lucky-lp", - "version": "2.2.0", + "version": "2.2.1", "lockfileVersion": 3, "requires": true, "packages": { "": { "name": "lucky-lp", - "version": "2.2.0", + "version": "2.2.1", "license": "MIT", "dependencies": { "prettier": "^3.2.4" diff --git a/package.json b/package.json index efb7146..d4adad1 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "lucky-lp", - "version": "2.2.0", + "version": "2.2.1", "private": true, "scripts": { "build": "spicetify-creator", diff --git a/src/app.ts b/src/app.ts index f8e8273..94e0616 100644 --- a/src/app.ts +++ b/src/app.ts @@ -1,16 +1,20 @@ import { toggleButton } from "./button"; async function main() { - while (!Spicetify?.showNotification || !Spicetify?.Platform?.History) { + while ( + !Spicetify?.showNotification && + !Spicetify?.Platform?.History && + !Spicetify?.CosmosAsync + ) { await new Promise((resolve) => setTimeout(resolve, 500)); } // Show button on app load - toggleButton(); + toggleButton(Spicetify.Platform.History.location); // Show button depending on navigation - Spicetify.Platform.History.listen(() => { - toggleButton(); + Spicetify.Platform.History.listen((location: Location) => { + toggleButton(location); }); } diff --git a/src/button.ts b/src/button.ts index a5dd1ab..a86ff74 100644 --- a/src/button.ts +++ b/src/button.ts @@ -1,8 +1,8 @@ import ShuffleIcon from "./shuffleIcon"; -import { handleClick, isAlbumPage, isHomePage } from "./util"; +import { handleClick, isAlbumPage, isHomePage } from "./utils"; -export const toggleButton = () => { - if (isHomePage() || isAlbumPage()) { +export const toggleButton = (location: Location) => { + if (isHomePage(location.pathname) || isAlbumPage(location.pathname)) { addButton(); } else { removeButton(); diff --git a/src/services.ts b/src/services.ts new file mode 100644 index 0000000..54752c3 --- /dev/null +++ b/src/services.ts @@ -0,0 +1,19 @@ +import { IAlbum, ISavedAlbums } from "./types/api"; + +export const getAlbumsCount = async (): Promise => { + const result: ISavedAlbums = await Spicetify.CosmosAsync.get( + "https://api.spotify.com/v1/me/albums?limit=1", + ); + + return result.total; +}; + +export const getRandomAlbum = async (total: number): Promise => { + const randomIndex = Math.floor(Math.random() * (total + 1)); + + const randomResult: ISavedAlbums = await Spicetify.CosmosAsync.get( + `https://api.spotify.com/v1/me/albums?limit=1&offset=${randomIndex - 1}`, + ); + + return randomResult.items[0]; +}; diff --git a/src/util.ts b/src/utils.ts similarity index 51% rename from src/util.ts rename to src/utils.ts index b612bef..fd053c2 100644 --- a/src/util.ts +++ b/src/utils.ts @@ -1,31 +1,20 @@ import { IAlbum, ISavedAlbums } from "./types/api"; +import { getAlbumsCount, getRandomAlbum } from "./services"; -export const isHomePage = () => { - const pathname = Spicetify.Platform.History.location.pathname; +export const isHomePage = (pathname: string) => { return pathname === "/"; }; -export const isAlbumPage = () => { - const pathname = Spicetify.Platform.History.location.pathname; +export const isAlbumPage = (pathname: string) => { return pathname.startsWith("/album/"); }; -const getRandomAlbumIndex = (total: number): number => - Math.floor(Math.random() * (total + 1)); - const getRandomSavedAlbum = async (): Promise => { try { - const result: ISavedAlbums = await Spicetify.CosmosAsync.get( - "https://api.spotify.com/v1/me/albums?limit=1", - ); + const albumsCount = await getAlbumsCount(); - if (result && result?.total) { - const albumsCount = result.total; - const randomIndex = getRandomAlbumIndex(albumsCount); - const randomResult: ISavedAlbums = await Spicetify.CosmosAsync.get( - `https://api.spotify.com/v1/me/albums?limit=1&offset=${randomIndex - 1}`, - ); - const randomAlbum = randomResult.items[0]; + if (albumsCount) { + const randomAlbum = await getRandomAlbum(albumsCount); Spicetify.Platform.History.push(`/album/${randomAlbum.album.id}`);