Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[stats] Button #164

Open
herbatniczek0 opened this issue Nov 21, 2024 · 5 comments
Open

[stats] Button #164

herbatniczek0 opened this issue Nov 21, 2024 · 5 comments

Comments

@herbatniczek0
Copy link

herbatniczek0 commented Nov 21, 2024

In general, all these buttons do not work and the music play button does not work either
image
image
[Edit] and version 1.1.0 works about 99% only the top buttons are a bit crooked and the play button does not work

@TeitokuZeon
Copy link

#116 (comment)

@CanYouJustWorkPlease
Copy link

CanYouJustWorkPlease commented Nov 25, 2024

I added the Play button to context menu, and that makes it possible to play the song.

To get it, navigate to:

  • for Windows:
    %appdata%\Spicetify\Extensions

  • for Linux:
    ~/.config/spicetify/Extensions

Create a new file called context-menu-play.js

Use this code:

(function playContextMenu() {
    function checkForSpicetify() {
        if (
            typeof Spicetify === 'undefined' ||
            !Spicetify.Player ||
            !Spicetify.ContextMenu ||
            !Spicetify.URI ||
            !Spicetify.showNotification ||
            !Spicetify.SVGIcons
        ) {
            console.log("Spicetify APIs not ready, retrying...");
            setTimeout(checkForSpicetify, 1000);
            return;
        }

        console.log("Spicetify APIs are ready.");
        initializeExtension();
    }

    checkForSpicetify();

    function initializeExtension() {
        // Function to determine when to show the "Play" option
        const shouldAddPlayOption = (uris) => {
            if (!uris || uris.length !== 1) return false;
            const uriObj = Spicetify.URI.fromString(uris[0]);
            const validTypes = [
                Spicetify.URI.Type.TRACK,
                Spicetify.URI.Type.ALBUM,
                Spicetify.URI.Type.PLAYLIST,
                Spicetify.URI.Type.ARTIST,
                Spicetify.URI.Type.SHOW,
                Spicetify.URI.Type.EPISODE,
            ];
            return validTypes.includes(uriObj.type);
        };

        // Function to handle playing the selected item
        const playItem = async (uris) => {
            if (!uris || uris.length !== 1) {
                Spicetify.showNotification("Please select a single item to play.");
                return;
            }

            const uri = uris[0];
            console.log("Selected URI:", uri);

            try {
                await Spicetify.Player.playUri(uri);
                // Spicetify.showNotification("Playing...");
                console.log("Playback started for URI:", uri);
            } catch (err) {
                console.error("Error playing URI:", err);
                Spicetify.showNotification("Failed to play the selected item.");
            }
        };

        // Create a new menu item using Spicetify.ContextMenu.Item
        const menuItem = new Spicetify.ContextMenu.Item(
            "Play",             // Name of the menu item
            playItem,           // Callback function when clicked
            shouldAddPlayOption, // Function to determine when to show the item
            Spicetify.SVGIcons["play"], // Icon for the menu item
            false               // Not disabled
        );

        // Register the menu item to add it to the context menu
        menuItem.register();

        // Attempt to adjust the menu item's position (if supported)
        if (typeof menuItem.priority !== 'undefined') {
            menuItem.priority = 1; // Lower number means higher priority
        }

        console.log("Context Menu Play extension loaded successfully.");
    }
})();

In Terminal type in:

spicetify config extensions context-menu-play.js

spicetify apply

It should look like this now:

image

To check if the Extension was indeed added, type in Terminal:
spicetify config

, and look for context-menu-play.js in the file.

@Heisii
Copy link

Heisii commented Nov 26, 2024

i might need a step by step guide to doing what you said in words so i gotta applicate it im pretty stupid in this domain

@CanYouJustWorkPlease
Copy link

For Windows

  1. Locate the Extensions Folder:

    • Press Win + R to open the Run dialog.
    • Type %appdata%\Spicetify\Extensions and press Enter.
  2. Create the context-menu-play.js File:

    • Inside the Extensions folder:
      • Right-click -> New -> Text Document.
      • Rename the file to context-menu-play.js (ensure the extension is .js, not .txt).
  3. Add the JavaScript Code:

    • Open the newly created context-menu-play.js file with a text editor like Notepad or Notepad++.
    • Paste this JavaScript code into the file:
      (function playContextMenu() {
          function checkForSpicetify() {
              if (
                  typeof Spicetify === 'undefined' ||
                  !Spicetify.Player ||
                  !Spicetify.ContextMenu ||
                  !Spicetify.URI ||
                  !Spicetify.showNotification ||
                  !Spicetify.SVGIcons
              ) {
                  console.log("Spicetify APIs not ready, retrying...");
                  setTimeout(checkForSpicetify, 1000);
                  return;
              }
      
              console.log("Spicetify APIs are ready.");
              initializeExtension();
          }
      
          checkForSpicetify();
      
          function initializeExtension() {
              const shouldAddPlayOption = (uris) => {
                  if (!uris || uris.length !== 1) return false;
                  const uriObj = Spicetify.URI.fromString(uris[0]);
                  const validTypes = [
                      Spicetify.URI.Type.TRACK,
                      Spicetify.URI.Type.ALBUM,
                      Spicetify.URI.Type.PLAYLIST,
                      Spicetify.URI.Type.ARTIST,
                      Spicetify.URI.Type.SHOW,
                      Spicetify.URI.Type.EPISODE,
                  ];
                  return validTypes.includes(uriObj.type);
              };
      
              const playItem = async (uris) => {
                  if (!uris || uris.length !== 1) {
                      Spicetify.showNotification("Please select a single item to play.");
                      return;
                  }
      
                  const uri = uris[0];
                  console.log("Selected URI:", uri);
      
                  try {
                      await Spicetify.Player.playUri(uri);
                      console.log("Playback started for URI:", uri);
                  } catch (err) {
                      console.error("Error playing URI:", err);
                      Spicetify.showNotification("Failed to play the selected item.");
                  }
              };
      
              const menuItem = new Spicetify.ContextMenu.Item(
                  "Play",
                  playItem,
                  shouldAddPlayOption,
                  Spicetify.SVGIcons["play"],
                  false
              );
      
              menuItem.register();
              if (typeof menuItem.priority !== 'undefined') {
                  menuItem.priority = 1;
              }
      
              console.log("Context Menu Play extension loaded successfully.");
          }
      })();
    • Save and close the file.
  4. Configure Spicetify:

    • Open a Command Prompt (press Win + R, type cmd, and press Enter).
    • Run the following commands:
      spicetify config extensions context-menu-play.js
      spicetify apply
  5. Verify the Extension:

    • Type in the Command Prompt:
      spicetify config
    • Look for context-menu-play.js under the extensions section.
  6. Test the Extension:

    • Open Spotify.
    • Right-click a track, album, or playlist.
    • You should see a "Play" option in the context menu. Clicking it will start playback.

For Linux

  1. Locate the Extensions Folder:

    • Open a terminal. Common shortcuts for opening the terminal are:
      • GNOME: Ctrl + Alt + T
      • KDE: Ctrl + Alt + T or Alt + F2 -> type konsole
      • Xfce: Ctrl + Alt + T or Alt + F2 -> type xfce4-terminal
    • Navigate to the Spicetify extensions folder:
      cd ~/.config/spicetify/Extensions
  2. Create the context-menu-play.js File:

    • Create a new file with nano or your preferred text editor:
      nano context-menu-play.js
    • Paste this JavaScript code into the file:
      (function playContextMenu() {
          function checkForSpicetify() {
              if (
                  typeof Spicetify === 'undefined' ||
                  !Spicetify.Player ||
                  !Spicetify.ContextMenu ||
                  !Spicetify.URI ||
                  !Spicetify.showNotification ||
                  !Spicetify.SVGIcons
              ) {
                  console.log("Spicetify APIs not ready, retrying...");
                  setTimeout(checkForSpicetify, 1000);
                  return;
              }
      
              console.log("Spicetify APIs are ready.");
              initializeExtension();
          }
      
          checkForSpicetify();
      
          function initializeExtension() {
              const shouldAddPlayOption = (uris) => {
                  if (!uris || uris.length !== 1) return false;
                  const uriObj = Spicetify.URI.fromString(uris[0]);
                  const validTypes = [
                      Spicetify.URI.Type.TRACK,
                      Spicetify.URI.Type.ALBUM,
                      Spicetify.URI.Type.PLAYLIST,
                      Spicetify.URI.Type.ARTIST,
                      Spicetify.URI.Type.SHOW,
                      Spicetify.URI.Type.EPISODE,
                  ];
                  return validTypes.includes(uriObj.type);
              };
      
              const playItem = async (uris) => {
                  if (!uris || uris.length !== 1) {
                      Spicetify.showNotification("Please select a single item to play.");
                      return;
                  }
      
                  const uri = uris[0];
                  console.log("Selected URI:", uri);
      
                  try {
                      await Spicetify.Player.playUri(uri);
                      console.log("Playback started for URI:", uri);
                  } catch (err) {
                      console.error("Error playing URI:", err);
                      Spicetify.showNotification("Failed to play the selected item.");
                  }
              };
      
              const menuItem = new Spicetify.ContextMenu.Item(
                  "Play",
                  playItem,
                  shouldAddPlayOption,
                  Spicetify.SVGIcons["play"],
                  false
              );
      
              menuItem.register();
              if (typeof menuItem.priority !== 'undefined') {
                  menuItem.priority = 1;
              }
      
              console.log("Context Menu Play extension loaded successfully.");
          }
      })();
    • Save the file by pressing Ctrl + O, then press Enter. Exit the editor with Ctrl + X.
  3. Configure Spicetify:

    • Run the following commands in the terminal:
      spicetify config extensions context-menu-play.js
      spicetify apply
  4. Verify the Extension:

    • Check the Spicetify configuration:
      spicetify config
    • Ensure context-menu-play.js is listed under the extensions section.
  5. Test the Extension:

    • Open Spotify.
    • Right-click on a track, album, or playlist.
    • You should see a "Play" option in the context menu. Clicking it should start playback.

Troubleshooting

  • If the "Play" option doesn’t appear:
    1. Ensure Spicetify is correctly installed and configured.
    2. Verify the file path and name (context-menu-play.js).
    3. Run:
      spicetify backup apply
    4. Restart Spotify and reapply changes:
      spicetify apply

With this setup, you can easily play items directly from Spotify’s context menu.

@CanYouJustWorkPlease
Copy link

MrBiscuit921's solution is better than mine, as he's done the proper fix, so check it out:
#137 (comment)

If you need more detailed steps on how he's done it, let me know.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants