Infinite Scroll Spotify Episodes

Automatically clicks the "Load More Episodes" button when scrolling down the page on Spotify

2024-07-05 يوللانغان نەشرى. ئەڭ يېڭى نەشرىنى كۆرۈش.

// ==UserScript==
// @name         Infinite Scroll Spotify Episodes
// @namespace    https://greatest.deepsurf.us/en/users/1200587-trilla-g
// @version      1.0
// @description  Automatically clicks the "Load More Episodes" button when scrolling down the page on Spotify
// @author       Trilla_G
// @match        *://*.open.spotify.com/*
// @grant        none
// @license      MIT
// ==/UserScript==

(function() {
    'use strict';

    let isPaused = false;

    // Define the selector for the button with the exact class name
    const buttonSelector = '.vqQmhCMZq7eUtTV7YYOQ.epDbBe.LegacyChipInner__ChipInnerComponent-sc-1qguixk-0';

    // Function to check for the button and click it
    function checkForButtonAndClick() {
        if (isPaused) return;
        const button = document.querySelector(buttonSelector);
        if (button) {
            button.click();
            console.log('Button clicked!');
        }
    }

    // Function to handle scroll events
    function onScroll() {
        if (!isPaused) {
            checkForButtonAndClick();
        }
    }

    // Add scroll event listener
    window.addEventListener('scroll', onScroll);

    // Use a MutationObserver to detect when the button is added to the DOM
    const observer = new MutationObserver((mutations) => {
        mutations.forEach((mutation) => {
            if (!isPaused && mutation.addedNodes.length > 0) {
                checkForButtonAndClick();
            }
        });
    });

    // Start observing the body for added nodes
    observer.observe(document.body, { childList: true, subtree: true });

    // Initial check in case the button is already present when the script runs
    checkForButtonAndClick();

    // Add an event listener for the specific keystroke to pause the function
    document.addEventListener('keydown', (event) => {
        if (event.code === 'Delete') {
            isPaused = true;
            console.log('Paused for 15 seconds');
            setTimeout(() => {
                isPaused = false;
                console.log('Resumed');
            }, 15000); // 15000 milliseconds = 15 seconds
        }
    });
})();