soundcloud shuffle likes

Adds a button to the "Likes" screen that loads all the tracks and shuffles them

目前為 2019-12-09 提交的版本,檢視 最新版本

// ==UserScript==
// @name         soundcloud shuffle likes
// @version      1.2
// @description  Adds a button to the "Likes" screen that loads all the tracks and shuffles them
// @author       bhackel
// @match        https://soundcloud.com/*
// @grant        none
// @run-at       document-end
// @noframes
// @namespace https://gf.qytechs.cn/en/users/324178-bhackel
// ==/UserScript==

(function() {
    'use strict';

    /* Injects Button into the page once it has loaded,
       then tries to re-add it if it disappears
    */
    function insertButtonLoop() {
        var url = window.location.href;
        var btnShuffle = document.getElementsByClassName('bhackel-shuffle-likes')[0];
        // ensure user is on 'likes' page, and check if the button exists
        if (url === "https://soundcloud.com/you/likes" && !btnShuffle) {
            // if it doesnt, create a new button
            btnShuffle = document.createElement('Button');
            btnShuffle.className = 'bhackel-shuffle-likes sc-button sc-button-large';
            btnShuffle.innerHTML = 'Shuffle Play';
            btnShuffle.onclick = function(){ setupLoad(this); };

            // check if element has loaded
            var collectionTop = document.getElementsByClassName('collectionSection__top')[0];
            if (collectionTop) {
                // insert the button above the track list
                collectionTop.insertBefore(btnShuffle, collectionTop.children[2]);
                btnShuffle.interval = 0;
            } else {
                setTimeout(insertButtonLoop, 1000);
            }
        }
        // perform another check in 3 seconds
        setTimeout(insertButtonLoop, 3000);
    }

    /* Changes the text of the button, resets the queue to have the user's
       likes, then starts the scrolling loop. Or it stops the loop from running.
    */
    function setupLoad(btn) {
        // check whether the loop is running or not
        if (btn.interval === 0) {
            btn.innerHTML = 'Click to Stop Loading';
            // list of tracks visible on screen
            var likedTracks = document.getElementsByClassName('lazyLoadingList__list')[0];
            if (likedTracks.childElementCount > 2) {
                // Set the current queue to the collection of tracks
                var firstTrack = likedTracks.children[0];
                var secondTrack = likedTracks.children[1];
                // hardcoded yeyeye
                var firstPlayButton = firstTrack.children[0].children[0].children[1].children[0];
                var secondPlayButton = secondTrack.children[0].children[0].children[1].children[0];
                // play 2, play 1, pause 1
                secondPlayButton.click();
                setTimeout(function(){ firstPlayButton.click(); }, 50);
                setTimeout(function(){ firstPlayButton.click(); }, 100);

                // open the queue if it is closed, to refresh queue
                checkToggleQueue('open');

                // setup the scrolling loop - needs adequate time before running so the queue resets
                setTimeout(function(){
                    btn.interval = setInterval(function() { scrollQueue(btn); }, 500);
                }, 3000);
            } else {
                // the user has two or less tracks in their collection; cannot shuffle play
                btn.innerHTML = 'Error: Too Few Tracks';
            }
        } else {
            clearInterval(btn.interval);
            btn.interval = 0;
            btn.innerHTML = 'Shuffle Play';
        }
    }

    /* Scrolls the queue down, ensuring that the queue is open by opening it
    */
    function scrollQueue(btn) {
        var queue = document.getElementsByClassName('queue')[0];
        // check to see if the queue is open
        if (queue.classList.contains('m-visible')) {
            // scroll the queue to the bottom, which loads new tracks below
            var scrollableQueue = document.getElementsByClassName('queue__scrollableInner')[0];
            var queueContainer = document.getElementsByClassName('queue__itemsHeight')[0];
            var scrollToHeight = parseInt(queueContainer.style.height);
            scrollableQueue.scroll(0,scrollToHeight);

            // check if it has loaded all tracks, then shuffle and play
            var autoplayDiv = document.getElementsByClassName('queue__fallback')[0];
            if (autoplayDiv) {
                clearInterval(btn.interval);
                btn.interval = 0;
                shuffleAndPlay(btn);
            }
        } else {
            // open the queue if it is not open
            checkToggleQueue('open');
        }
    }

    /* Shuffles the queue, then plays it
    */
    function shuffleAndPlay(btn) {
        btn.innerHTML = 'Shuffle Play';
        var playButton = document.getElementsByClassName('playControl')[0];
        var shuffleButton = document.getElementsByClassName('shuffleControl')[0];
        // only shuffle/play if it is disabled already
        if (!shuffleButton.classList.contains('m-shuffling')) {
            shuffleButton.click();
        }
        if (!playButton.classList.contains('playing')) {
            playButton.click();
        }

        // close the queue if it is open
        checkToggleQueue('close');
    }

    /* Opens or closes the song queue
    */
    function checkToggleQueue(changeToState) {
        var queue = document.getElementsByClassName('queue')[0];
        var queueOpen = queue.classList.contains('m-visible');
        // toggle queue if the queue is open and it should be closed, or if it's closed and should be open
        if ((queueOpen && changeToState === 'close') || (!queueOpen && changeToState === 'open')) {
            var queueTrigger = document.getElementsByClassName('playbackSoundBadge__queueCircle')[0];
            queueTrigger.click();
        }
    }

    insertButtonLoop();

})();

QingJ © 2025

镜像随时可能失效,请加Q群300939539或关注我们的公众号极客氢云获取最新地址