Spotify - Random Buttons

Add missing randomize features to Spotify (only random artist for now, more to come later).

  1. // ==UserScript==
  2. // @name Spotify - Random Buttons
  3. // @namespace spotify-random-buttons
  4. // @version 0.2
  5. // @description Add missing randomize features to Spotify (only random artist for now, more to come later).
  6. // @author Mark McEver
  7. // @match https://open.spotify.com/*
  8. // @icon https://www.google.com/s2/favicons?sz=64&domain=spotify.com
  9. // @grant GM_registerMenuCommand
  10. // ==/UserScript==
  11.  
  12. GM_registerMenuCommand('Random Followed Artist', () => {
  13. new class{
  14. constructor(){
  15. const variables = encodeURIComponent(JSON.stringify({"filters":["Artists"],"order":null,"textFilter":"","features":["LIKED_SONGS","YOUR_EPISODES"],"limit":50,"offset":0,"flatten":false,"expandedFolders":[],"folderUri":null,"includeFoldersWhenFlattening":true,"withCuration":false}))
  16. const extensions = encodeURIComponent(JSON.stringify({"persistedQuery":{"version":1,"sha256Hash":"0cc9ca58bd1dad0ce11712768bf4357ca8a9c6dab1dc0b43331fe526c47ff885"}}))
  17.  
  18. fetch('https://api-partner.spotify.com/pathfinder/v1/query?operationName=libraryV3&variables=' + variables + '&extensions=' + extensions, {
  19. headers: {
  20. 'Authorization': 'Bearer ' + JSON.parse(document.querySelector('#session').textContent).accessToken
  21. }
  22. })
  23. .then(response => response.json())
  24. .then(data => {
  25. const artists = data.data.me.libraryV3.items
  26. const artist = artists[this.rand(0, artists.length-1)]
  27. const id = artist.item.data.uri.split(':')[2]
  28. location.href = '/artist/' + id
  29. })
  30. }
  31.  
  32. rand(min, max){
  33. return Math.floor(Math.random() * (max - min + 1) + min)
  34. }
  35. }
  36. })