Spotifresh

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

您需要先安裝使用者腳本管理器擴展,如 TampermonkeyGreasemonkeyViolentmonkey 之後才能安裝該腳本。

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyViolentmonkey 後才能安裝該腳本。

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyViolentmonkey 後才能安裝該腳本。

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyUserscripts 後才能安裝該腳本。

你需要先安裝一款使用者腳本管理器擴展,比如 Tampermonkey,才能安裝此腳本

您需要先安裝使用者腳本管理器擴充功能後才能安裝該腳本。

(我已經安裝了使用者腳本管理器,讓我安裝!)

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

(我已經安裝了使用者樣式管理器,讓我安裝!)

// ==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);
})();