-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathspotifyGenres.js
1 lines (1 loc) · 13.5 KB
/
spotifyGenres.js
1
"use strict";(()=>{var e="44654ea047786d90338c17331a5f5d95";function t(e){return function(e){return e.charAt(0).toUpperCase()+e.slice(1)}(e.toLowerCase().replace(/[^a-zA-Z0-9]+(.)/g,((e,t)=>t.toUpperCase())))}var n="[better-spotify-genres]";!function r(){if(!(Spicetify.CosmosAsync&&Spicetify.Platform&&Spicetify.URI&&Spicetify.Player.data))return void setTimeout(r,300);window.genrePopup=()=>S(),window.artistPageGenreOnClick=e=>function(e){Spicetify.Platform.History.push(`/search/${e}/playlists`)}(e);let i=[],a=[];const o='<svg xmlns="http://www.w3.org/2000/svg" version="1.0" width="17px" height="17px" viewBox="0 0 892.000000 877.000000" preserveAspectRatio="xMidYMid meet"><g transform="translate(0.000000,877.000000) scale(0.100000,-0.100000)" fill="currentcolor" stroke="none"><path d="M4365 8296 c-86 -27 -154 -88 -193 -174 -16 -34 -17 -213 -22 -2550 l-5 -2512 -75 53 c-279 197 -634 329 -1015 378 -129 17 -481 17 -610 0 -901 -116 -1574 -677 -1681 -1402 -92 -628 306 -1259 991 -1566 225 -102 440 -162 690 -194 129 -17 481 -17 610 0 674 86 1226 421 1513 916 86 149 149 344 171 530 7 61 11 686 11 1946 l0 1857 1413 -530 c916 -344 1430 -532 1463 -535 115 -12 238 58 290 164 l29 58 0 775 c0 720 -1 781 -19 855 -59 257 -147 430 -310 617 -115 131 -252 235 -409 308 -81 38 -2546 965 -2662 1001 -69 22 -123 23 -180 5z m2578 -1545 c169 -74 303 -227 374 -427 l28 -79 3 -501 3 -502 -1298 487 -1298 487 -3 681 -2 681 1072 -403 c590 -222 1094 -412 1121 -424z m-3963 -3856 c593 -78 1044 -400 1150 -820 118 -470 -282 -947 -930 -1109 -805 -201 -1677 171 -1830 779 -118 471 289 955 937 1111 219 53 461 67 673 39z"/></g></svg>',s='<svg xmlns="http://www.w3.org/2000/svg" version="1.0" width="17px" height="17px" viewBox="0 0 892.000000 877.000000" preserveAspectRatio="xMidYMid meet"><g transform="translate(0.000000,877.000000) scale(0.100000,-0.100000)" fill="currentcolor" stroke="none"><path d="M4365 8296 c-86 -27 -154 -88 -193 -174 -16 -34 -17 -213 -22 -2550 l-5 -2512 -75 53 c-279 197 -634 329 -1015 378 -129 17 -481 17 -610 0 -901 -116 -1574 -677 -1681 -1402 -92 -628 306 -1259 991 -1566 225 -102 440 -162 690 -194 129 -17 481 -17 610 0 674 86 1226 421 1513 916 86 149 149 344 171 530 7 61 11 686 11 1946 l0 1857 1413 -530 c916 -344 1430 -532 1463 -535 115 -12 238 58 290 164 l29 58 0 775 c0 720 -1 781 -19 855 -59 257 -147 430 -310 617 -115 131 -252 235 -409 308 -81 38 -2546 965 -2662 1001 -69 22 -123 23 -180 5z"/></g></svg>',c=Spicetify.React;function l(e,t){Spicetify.LocalStorage.set(e,t)}function p(){try{return JSON.parse((e="showGenre:settings",Spicetify.LocalStorage.get(e)??"{}"))}catch{return l("showGenre:settings","{}"),{}}var e}const u={state:!0,cached:{pop:"spotify:playlist:6gS3HhOiI17QNojjPuPzqc"}};let d=p();function f(e,t){if(e){let n=p();return n[e]=t,void l("showGenre:settings",JSON.stringify(n))}l("showGenre:settings",JSON.stringify(d))}async function m(e,t=null,n=document.body){return new Promise((r=>{if(document.querySelector(e))return r(document.querySelector(e));const i=new MutationObserver((()=>{document.querySelector(e)?(r(document.querySelector(e)),i.disconnect()):"number"==typeof t&&setTimeout((()=>{i.disconnect(),r(null)}),t)}));i.observe(n,{childList:!0,subtree:!0})}))}async function g(e){const n=d.cached[t(e)];if(null!=n)return{uri:n,genre:e};const r=new RegExp(`^the sound of ${e.replace(/[.*+?^${}()|[\]\\]/g,"\\$&")}$`,"i"),i=await Spicetify.CosmosAsync.get(`https://api.spotify.com/v1/search?q=${encodeURIComponent("The Sound of "+e)}&type=playlist`);for(const n of i.playlists.items)return"thesoundsofspotify"===n.owner.id&&r.test(n.name)?(d.cached[t(e)]=n.uri,f("cached",d.cached),{uri:n.uri,genre:e}):{uri:n.uri+"|||",genre:e};return{uri:null,genre:e}}async function y(e,t=null){const n=e.map((e=>async function(e){return(await Spicetify.CosmosAsync.get(`https://api.spotify.com/v1/artists/${e}`)).genres}(e.split(":")[2])));let r=(await Promise.allSettled(n)).map((e=>{if("fulfilled"===e.status)return e.value})).filter(Boolean).flat();if(0===r.length){let n;if("artist"===t)n=e[0].split(":")[2];else{if("recursive"===t)return[];n=Spicetify.Player.data.item.metadata.artist_uri.split(":")[2]}const i=await Spicetify.CosmosAsync.get(`wg://artist/v1/${n}/desktop?format=json`).catch((()=>null));if(!i)return[];if(!i.related_artists?.artists)return[];const a=i.related_artists.artists.map((e=>e.uri));let o=5;for(;25!==o;)r=await y(a.slice(o-5,o),"recursive"),0!=r.length?o=25:o+=5}return r=Array.from(new Set(r)),t||(i=r),r.slice(0,5)}async function h(){let e=[...a];if(0===e.length){console.warn(n,"No Last.FM tags found for the current track. Fetching genres from artists...");let t=function(){const e=Spicetify.Player.data.item.metadata,t=[e.artist_uri];for(let n=1;n<10;n++){const r=e[`artist_uri:${n}`];if(!r)break;t.push(r)}return t}();e=await y(t)}if(console.info(n,"All genres of the current track: ",e),!e)return console.warn(n,"No genres found for the current track. Removing..."),i=[],void E();const t=(await Promise.all(e.map((e=>g(e))))).filter((e=>null!==e.uri)).map((e=>[[`<a href="${e.uri.includes("|||")?'#" onclick="genrePopup()" ':e.uri+'"'} style="color: var(--spice-subtext); font-size: 12px">${e.genre.replace(/(^\w{1})|([\s-]+\w{1})/g,(e=>e.toUpperCase()))}</a>`],["<span>, </span>"]])).flat(1/0);"<span>, </span>"==t[t.length-1]&&t.pop();const r=t.join("");null!==P&&(P.innerHTML=r),k=await m("div.main-trackInfo-container",1e3),null!==P&&null!==k&&(k.style.gridTemplate='"title title" "badges subtitle" "genres genres" / auto 1fr auto',k.appendChild(P))}Object.keys(u).forEach((e=>{const t=e;void 0===d[t]&&(d={...d,[t]:u[t]})})),f();const w=c.createElement("style",null,'.popup-row::after {\n content: "";\n display: table;\n clear: both;\n }\n .popup-row .col {\n display: flex;\n padding: 10px 0;\n align-items: center;\n }\n .popup-row .col.description {\n float: left;\n padding-right: 15px;\n }\n .popup-row .col.action {\n float: right;\n text-align: right;\n }\n .popup-row .div-title {\n color: var(--spice-text);\n } \n .popup-row .divider {\n height: 2px;\n border-width: 0;\n background-color: var(--spice-button-disabled);\n }\n .popup-row .space {\n margin-bottom: 20px;\n visibility: hidden;\n }\n .popup-row .info {\n /* font-size: 13px; */\n }\n .popup-row .red {\n font-size: 13px;\n color: #59CE8F;\n }\n .popup-row .demo {\n font-size: 13px;\n color: #59CE8F;\n }\n .popup-row .little-space {\n margin-bottom: 10px;\n }\n .popup-row .inputbox {\n padding: 10px;\n border-radius: 15px;\n border: 0;\n box-shadow: 4px 4px 10px rgba(0, 0, 0, 0.06);\n }\n button.checkbox {\n align-items: center;\n color: var(--spice-text);\n cursor: pointer;\n display: flex;\n margin-inline-start: 12px;\n }\n button.checkbox.disabled {\n color: rgba(var(--spice-rgb-text), 0.3);\n }\n select {\n color: var(--spice-text);\n background: rgba(var(--spice-rgb-shadow), 0.7);\n border: 0;\n height: 32px;\n }\n ::-webkit-scrollbar {\n width: 8px;\n }\n .login-button {\n background-color: var(--spice-button);\n border-radius: 8px;\n border-style: none;\n color: var(--spice-text);\n cursor: pointer;\n font-size: 14px;\n height: 40px;\n margin: 10px;\n padding: 5px 10px;\n text-align: center;\n }\n .green {\n background-color: #76ba99;\n color: #25316D;\n }\n .red {\n background-color: #A9555E;\n }\n .small-button.red {\n background-color: #A9555E !important;\n }\n input.small-input {\n padding: 5px !important;\n border-radius: 6px !important;\n right: 0px !important;\n margin: 5px;\n }\n .small-button {\n margin-right: 20px;\n }\n .popup-row .inputbox[type="color"] {\n background-color: var(--spice-custom-main-secondary) !important;\n padding: 0px;\n border-radius: 5px !important;\n border: none;\n margin-right: 10px;\n }\n .popup-row .inputbox[type="color"]::-webkit-color-swatch {\n border-radius: 5px !important;\n border: none;\n }\n .popup-row.search-div .col {\n position: relative;\n }\n .popup-row .nord-search-container {\n width: 100%;\n }\n .popup-row .nord-search-icon {\n position: absolute;\n margin: 10px;\n }\n .popup-row .nord-search {\n padding: 10px 36px !important;\n width: 100%;\n }\n .popup-row .display-none {\n display: none !important;\n }\n .GenericModal[aria-label*="Genres of"] .main-trackCreditsModal-header .tetrax-settings-discord-link {\n color: var(--spice-custom-success);\n }\n .GenericModal[aria-label*="Genres of"] .main-trackCreditsModal-header .tetrax-settings-discord-link:hover {\n color: var(--spice-custom-link-hover);\n }');function b({name:e="",color:t="",onclickFun:n=(()=>{})}){return c.createElement("button",{className:`login-button${t}`,onClick:()=>n()},e)}async function v(){const{artist_name:t,title:n}=Spicetify.Player.data.item.metadata;a=[];const r=await async function(t,n){const r=`https://ws.audioscrobbler.com/2.0/?method=track.getInfo&api_key=${e}&artist=${encodeURIComponent(t)}&track=${encodeURIComponent(n)}&format=json`;try{const e=await fetch(r),t=await e.json();return"error"in t?null:t}catch{return null}}(t,n);if(!r)return;const i=r.track.toptags.tag;if(i)for(const e of i)/\d/.test(e.name)||a.push(e.name)}const x=c.createElement("div",null,w,c.createElement("p",{className:"popup-row"},'Tip: You can right click on genres in the "Player Bar" to open this popup.'),c.createElement("div",{className:"popup-row"},c.createElement("hr",{className:"space"},null)),c.createElement((function(){const[e,t]=c.useState(i);return Spicetify.Player.addEventListener("songchange",(()=>{setTimeout((()=>t(i)),500)})),e.map((e=>c.createElement(b,{name:e.replace(/(^\w{1})|([\s-]+\w{1})/g,(e=>e.toUpperCase())),onclickFun:async()=>{const t=await g(e);null===t.uri||t.uri.includes("|||")?Spicetify.Platform.History.push(`/search/${e}/playlists`):Spicetify.Platform.History.push(`/playlist/${t.uri.split(":")[2]}`),Spicetify.PopupModal.hide()}})))}),null,null),c.createElement((function(){if(0===a.length)return c.createElement("div",null,null);const[e,t]=c.useState(a);return Spicetify.Player.addEventListener("songchange",(()=>{setTimeout((()=>t(a)),500)})),c.createElement("div",null,c.createElement("div",{className:"popup-row"},c.createElement("hr",{className:"space"},null)),c.createElement("div",{className:"popup-row"},c.createElement("h1",{className:"div-title"},"Last FM Tags")),e.map((e=>c.createElement(b,{name:e.replace(/(^\w{1})|([\s-]+\w{1})/g,(e=>e.toUpperCase())),onclickFun:async()=>{Spicetify.Platform.History.push(`/search/${e}/playlists`),Spicetify.PopupModal.hide()}}))))}),null,null));function S(){Spicetify.PopupModal.display({title:'Genres of "'+Spicetify.Player.data.item.metadata.title.replace(/\(.+?\)/g,"").replace(/\[.+?\]/g,"").replace(/\s\-\s.+?$/,"").replace(/,.+?$/,"").trim()+'"',content:x,isLarge:!0})}let k=null,P=null;async function E(){k=await m("div.main-trackInfo-container",1e3);try{if(null===k||null===P)return;k.style.removeProperty("grid-template"),k.removeChild(P)}catch{}}async function C(){if(!d.state||Spicetify.Player.data.item.metadata.is_local||"track"!==Spicetify.URI.fromString(Spicetify.Player.data.item.uri).type)return console.warn(n,"State is disabled or the current track is local. Removing..."),void E();await v(),await h()}async function $(e){let t=e.split("/");"artist"==t[1]&&3==t.length&&async function(e){if(!e)return;const t=e.map((async e=>{const t=await g(e);if(null!==t.uri)return[[`<a class="main-entityHeader-genreLink" href="${t.uri.includes("|||")?`#" data-value="${e}" onclick="artistPageGenreOnClick(this.getAttribute('data-value'))" `:t.uri+'"'} style="color: var(--spice-subtext); font-size: 1rem">${e.replace(/(^\w{1})|([\s-]+\w{1})/g,(e=>e.toUpperCase()))}</a>`],["<span>, </span>"]]}));let n=(await Promise.all(t)).flat(1/0);"<span>, </span>"==n[n.length-1]&&n.pop(),n.unshift("<span>Artist Genres : </span>");const r=n.join("");let i=document.createElement("div");i.className="main-entityHeader-detailsText genre-container",i.innerHTML=r;try{document.querySelector(".genre-container")?.remove()}catch{}let a=await m("div.main-entityHeader-headerText",1e3),o=await m("span.main-entityHeader-detailsText",1e3);a?.insertBefore(i,o)}(await y(["spotify:artist:"+t[2]],"artist"))}!function e(){Spicetify.Player.data?async function(){k=await m("div.main-trackInfo-container",1e3),k&&(k.style.gridTemplate='"title title" "badges subtitle" "genres genres" / auto 1fr auto');P=document.createElement("div"),P.className="ellipsis-one-line main-type-finale",P.style.gridArea="genres",P.addEventListener("contextmenu",S),await C(),Spicetify.Player.addEventListener("songchange",C),await $(Spicetify.Platform.History.location.pathname),Spicetify.Platform.History.listen((e=>{$(e.pathname)})),Spicetify.Playbar?.Widget&&new Spicetify.Playbar.Widget(d.state?"Hide Spotify Genres":"Show Spotify Genres",d.state?s:o,(e=>{d.state?(e.icon=o,e.label="Show Spotify Genres",d.state=!1):(e.icon=s,e.label="Hide Spotify Genres",d.state=!0),f("state",d.state),C()}),!1)}():setTimeout(e,1e3)}()}()})();