SoundCloud Keybinder

Allows for customization of SoundCloud keybinds, and adds a keybind to open the Add to Playlist menu.

ही स्क्रिप्ट इंस्टॉल करण्यासाठी तुम्हाला Tampermonkey, Greasemonkey किंवा Violentmonkey यासारखे एक्स्टेंशन इंस्टॉल करावे लागेल.

ही स्क्रिप्ट इंस्टॉल करण्यासाठी तुम्हाला Tampermonkey किंवा Violentmonkey यासारखे एक्स्टेंशन इंस्टॉल करावे लागेल..

ही स्क्रिप्ट इंस्टॉल करण्यासाठी तुम्हाला Tampermonkey किंवा Violentmonkey यासारखे एक्स्टेंशन इंस्टॉल करावे लागेल..

You will need to install an extension such as Tampermonkey or Userscripts to install this script.

ही स्क्रिप्ट इंस्टॉल करण्यासाठी तुम्हाला Tampermonkey यासारखे एक्स्टेंशन इंस्टॉल करावे लागेल..

ही स्क्रिप्ट इंस्टॉल करण्यासाठी तुम्हाला एक युझर स्क्रिप्ट व्यवस्थापक एक्स्टेंशन इंस्टॉल करावे लागेल.

(माझ्याकडे आधीच युझर स्क्रिप्ट व्यवस्थापक आहे, मला इंस्टॉल करू द्या!)

ही स्टाईल इंस्टॉल करण्यासाठी तुम्हाला Stylus सारखे एक्स्टेंशन इंस्टॉल करावे लागेल.

ही स्टाईल इंस्टॉल करण्यासाठी तुम्हाला Stylus सारखे एक्स्टेंशन इंस्टॉल करावे लागेल.

ही स्टाईल इंस्टॉल करण्यासाठी तुम्हाला Stylus सारखे एक्स्टेंशन इंस्टॉल करावे लागेल.

ही स्टाईल इंस्टॉल करण्यासाठी तुम्हाला एक युझर स्टाईल व्यवस्थापक इंस्टॉल करावे लागेल.

ही स्टाईल इंस्टॉल करण्यासाठी तुम्हाला एक युझर स्टाईल व्यवस्थापक इंस्टॉल करावे लागेल.

ही स्टाईल इंस्टॉल करण्यासाठी तुम्हाला एक युझर स्टाईल व्यवस्थापक इंस्टॉल करावे लागेल.

(माझ्याकडे आधीच युझर स्टाईल व्यवस्थापक आहे, मला इंस्टॉल करू द्या!)

// ==UserScript==
// @name         SoundCloud Keybinder
// @version      1.0
// @description  Allows for customization of SoundCloud keybinds, and adds a keybind to open the Add to Playlist menu.
// @author       bhackel
// @match        https://soundcloud.com/*
// @grant        none
// @run-at       document-end
// @noframes
// @namespace    https://greasyfork.org/en/users/324178-bhackel
// ==/UserScript==
// jshint esversion: 6

(function() {
    'use strict';

    /****** Settings ******/
    let blockedKeys = []; // List of keys that will be blocked. Ex: ['r', 'l'];
    let addToPlaylistKey = 'a'; // Key for opening the Add to Playlist menu for the active song
    /****** Settings ******/

    let keybinder = function (event) {
        // Do nothing if the user is typing in a text box
        if (document.activeElement.tagName === 'INPUT') {
            return;
        }
        // Block keypresses specified in list
        else if (blockedKeys.includes(event.key)) {
            // Prevent other listeners from doing anything
            event.stopPropagation();
        }
        // Add keybind for Add to Playlist hotkey
        else if (event.key === addToPlaylistKey) {
            openLoadQueue();
        }
    };

    // Insert the listener above the default onkeydown listener
    window.addEventListener("keydown", keybinder, true);


    /* Functions for the Add to Playlist hotkey */

    // Opens and waits for the song queue to load
    function openLoadQueue() {
        // Check if it is already open
        let queue = document.querySelector('.m-queueVisible');
        if (queue === null) {
            let queueButton = document.querySelector('.playbackSoundBadge__queueCircle');
            queueButton.click();
            setTimeout(openLoadQueue, 500);
            return;
        }
        // If queue gets opened, wait for it to load before running next part
        setTimeout(openAddToPlaylist, 1250);
    }

    // With an open and loaded queue, clicks the Add to Playlist menu
    function openAddToPlaylist() {
        // Close queue
        let queueButton = document.querySelector('.playbackSoundBadge__queueCircle');
        queueButton.click();
        // Open the menu with additional actions
        let activeSong = document.querySelector('.queueItemView.sc-border-box.sc-font-light.m-active');
        let moreButton = activeSong.querySelector('.sc-button-more');
        moreButton.click();
        // Click the Add to Playlist button
        let menuActions = document.querySelector('.moreActions');
        let addToPlaylist = document.querySelector('.sc-button-addtoset');
        addToPlaylist.click();
        // Put focus back on play/pause button
        document.querySelector('.playControl').focus();
    }
})();