Infinite Scroll Spotify Episodes

Automatically clicks the "Load More Episodes" button when visible on Spotify

// ==UserScript==
// @name         Infinite Scroll Spotify Episodes
// @namespace    https://greasyfork.org/en/users/1200587-trilla-g
// @version      3.7
// @description  Automatically clicks the "Load More Episodes" button when visible on Spotify
// @author       Trilla_G
// @match        *://*.open.spotify.com/*
// @grant        none
// @license      MIT
// ==/UserScript==

(function() {
    'use strict';

    // Define the selector for the button with the new provided selector
    const buttonSelector = '.vqQmhCMZq7eUtTV7YYOQ.eJmJgo.LegacyChipInner__ChipInnerComponent-sc-1qguixk-0 > .encore-text-body-small-bold.encore-text';

    // Function to check if an element is in the viewport
    function isInViewport(element) {
        const rect = element.getBoundingClientRect();
        return (
            rect.top >= 0 &&
            rect.left >= 0 &&
            rect.bottom <= (window.innerHeight || document.documentElement.clientHeight) &&
            rect.right <= (window.innerWidth || document.documentElement.clientWidth)
        );
    }

    // Function to check for the button and click it if it's in the viewport
    function checkForButtonAndClick() {
        const button = document.querySelector(buttonSelector);
        if (button && isInViewport(button)) {
            button.click();
        }
    }

    // Run checkForButtonAndClick every second
    setInterval(checkForButtonAndClick, 1000);

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