soundcloud shuffle likes

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

目前为 2019-12-01 提交的版本。查看 最新版本

// ==UserScript==
// @name         soundcloud shuffle likes
// @version      1.1
// @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 insert_button_loop() {
        var url = window.location.href;
        var btn_shuffle = 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" && !btn_shuffle) {
            // if it doesnt, create a new button
            btn_shuffle = document.createElement('Button');
            btn_shuffle.className = 'bhackel-shuffle-likes sc-button sc-button-large';
            btn_shuffle.innerHTML = 'Shuffle Play';
            btn_shuffle.onclick = function(){ setup_load(this); };

            // check if element has loaded
            var collection_top = document.getElementsByClassName('collectionSection__top')[0];
            if (collection_top) {
                // insert the button above the track list
                collection_top.insertBefore(btn_shuffle, collection_top.children[2]);
                btn_shuffle.interval = 0;
            } else {
                setTimeout(insert_button_loop, 1000);
            }
        }
        // perform another check in 3 seconds
        setTimeout(insert_button_loop, 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 setup_load(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 liked_tracks = document.getElementsByClassName('lazyLoadingList__list')[0];
            if (liked_tracks.childElementCount > 2) {
                // Set the current queue to the collection of tracks
                var first_track = liked_tracks.children[0];
                var second_track = liked_tracks.children[1];
                // hardcoded yeyeye
                var first_play_button = first_track.children[0].children[0].children[1].children[0];
                var second_play_button = second_track.children[0].children[0].children[1].children[0];
                // play 2, play 1, pause 1
                second_play_button.click();
                setTimeout(function(){ first_play_button.click(); }, 50);
                setTimeout(function(){ first_play_button.click(); }, 100);

                // open the queue if it is closed, to refresh queue
                var queue = document.getElementsByClassName('queue')[0];
                if (!queue.classList.contains('m-visible')) {
                    var queueTrigger = document.getElementsByClassName('playbackSoundBadge__queueCircle')[0];
                    queueTrigger.click();
                    // place focus back to play button
                    document.getElementsByClassName('playControl')[0].focus();
                }

                // setup the scrolling loop - needs adequate time before running so the queue resets
                setTimeout(function(){
                    btn.interval = setInterval(function() { scroll_queue(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 scroll_queue(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 autoplay_div = document.getElementsByClassName('queue__fallback')[0];
            if (autoplay_div) {
                clearInterval(btn.interval);
                btn.interval = 0;
                shuffle_and_end(btn);
            }
        } else {
            // open the queue if it is not open
            var queueTrigger = document.getElementsByClassName('playbackSoundBadge__queueCircle')[0];
            queueTrigger.click();
            // place focus back to play button
            document.getElementsByClassName('playControl')[0].focus();
        }
    }

    /* Shuffles the queue, then plays it
    */
    function shuffle_and_end(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
        var queue = document.getElementsByClassName('queue')[0];
        if (queue.classList.contains('m-visible')) {
            var queueTrigger = document.getElementsByClassName('playbackSoundBadge__queueCircle')[0];
            queueTrigger.click();
            // place focus back to play button
            document.getElementsByClassName('playControl')[0].focus();
        }
    }

    insert_button_loop();

})();

QingJ © 2025

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