Spotifresh

Reload and autoplay Spotify when it stops (because I'm blocking ads)

Aby zainstalować ten skrypt, wymagana jest instalacje jednego z następujących rozszerzeń: Tampermonkey, Greasemonkey lub Violentmonkey.

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

Aby zainstalować ten skrypt, wymagana jest instalacje jednego z następujących rozszerzeń: Tampermonkey, Violentmonkey.

Aby zainstalować ten skrypt, wymagana będzie instalacja rozszerzenia Tampermonkey lub Userscripts.

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

Aby zainstalować ten skrypt, musisz zainstalować rozszerzenie menedżera skryptów użytkownika.

(Mam już menedżera skryptów użytkownika, pozwól mi to zainstalować!)

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

Będziesz musiał zainstalować rozszerzenie menedżera stylów użytkownika, aby zainstalować ten styl.

Będziesz musiał zainstalować rozszerzenie menedżera stylów użytkownika, aby zainstalować ten styl.

Musisz zainstalować rozszerzenie menedżera stylów użytkownika, aby zainstalować ten styl.

(Mam już menedżera stylów użytkownika, pozwól mi to zainstalować!)

// ==UserScript==
// @name         Spotifresh
// @namespace    http://tampermonkey.net/
// @version      0.1
// @description  Reload and autoplay Spotify when it stops (because I'm blocking ads)
// @author       Your neighbor
// @include        https://open.spotify.com/playlist/*
// @include        https://open.spotify.com/album/*
// @grant        none
// ==/UserScript==

(function() {
    'use strict';
    
const ONE_SECOND = 1000;
    
const playOnLoad = () => {
  const intervalLoop = () => {
    const playButton = document.getElementsByClassName("spoticon-play-16")[0];
    const playbackTimeEl = document.getElementsByClassName(
      "playback-bar__progress-time"
    )[0];
    const playbackTime = playbackTimeEl ? playbackTimeEl.textContent : null;
    const pausedAndNotAdvancing = playbackTime === "0:00" && playButton;
    setTimeout(() => {
      pausedAndNotAdvancing ? playButton.click() : intervalLoop();
    }, ONE_SECOND);
  };
  intervalLoop();
};
    
setTimeout(() => {
    playOnLoad();
}, 5 * ONE_SECOND);


const callAfterNSecondsOfTrue = (callback, seconds, condition) => {
  const intervalLoop = secondsLeft => {
    if (secondsLeft < seconds) {
      console.log("tick ", secondsLeft);
    }
    if (secondsLeft <= 0) {
      callback();
    }
    setTimeout(() => {
      condition() ? intervalLoop(secondsLeft - 1) : intervalLoop(seconds);
    }, ONE_SECOND);
  };
  intervalLoop(seconds);
};

function playingAndNotAdvancing() {
  const pauseButton = document.getElementsByClassName("spoticon-pause-16")[0];
  const playbackTimeEl = document.getElementsByClassName(
    "playback-bar__progress-time"
  )[0];
  const playbackTime = playbackTimeEl ? playbackTimeEl.textContent : null;
  return playbackTime === "0:00" && pauseButton;
}

const reload = () => {
  console.log("reloading...");
  location.reload();
};

callAfterNSecondsOfTrue(reload, 10, playingAndNotAdvancing);
})();