cuzi / Spotify Automatic facebook login

// ==UserScript==
// @name         Spotify Automatic facebook login
// @description  Automatically log in to Spotify web with your facebook account, no need to click the multiple login buttons anymore
// @namespace    cuzi
// @version      10
// @grant        GM.setValue
// @grant        GM.getValue
// @license      GPL-3.0-or-later; http://www.gnu.org/licenses/gpl-3.0.txt
// @include      https://open.spotify.com/*
// @include      https://accounts.spotify.com/*
// ==/UserScript==
"use strict";

var stopFlag = false;
var iv;

function stop() {
  // Stop script execution
  stopFlag = true;
  clearInterval(iv)
}

async function login() {
  if (stopFlag) return;

  if (document.querySelector('button[title~="Upgrade"]')) {
    document.querySelector('button[title~="Upgrade"]').remove()
  }

  if (document.querySelector('#main .dialog h1') && document.querySelector('#main .dialog h1').innerHTML.indexOf('An error occurred') !== -1) {
    // An error occurred. Something went wrong. Try reloading the page.  --> Logout and then reload
    fetch('https://open.spotify.com/logout')
      .then(function (response) {
        return response.json();
      })
      .then(function (myJson) {
        if (myJson && myJson.success) {
          window.location.reload(true);
        }
        else {
          document.location.href = 'https://open.spotify.com/logout'
        }
      });
    return stop()
  }

  let button = document.querySelector("button#has-account");
  if (button) {
    button.click();
  }

  button = document.querySelector(".btn-facebook");
  if (button) {
    button.click();
  }

  button = document.querySelector(".navBar-signupPrompt button.btn-black");
  if (button) {
    button.click();
  }

  let buttons = document.querySelectorAll('.Root__main-view header button,.Root__top-bar header button')
  buttons.forEach(function(b) {
    if (b.textContent.toLowerCase().indexOf('log in') !== -1) {
      b.click()
    }
  })

  if (document.querySelector("#login-screen")) {
    await GM.setValue("userscript_auto_redirect", document.location.href);
  }

  if (document.location.href.indexOf("login?continue=") != -1) {
    await GM.setValue("userscript_auto_redirect", document.location.href);
  }

  if (document.location.href == "https://accounts.spotify.com/en/status") {
    let url = await GM.getValue("userscript_auto_redirect", false);
    if (url) {
      await GM.setValue("userscript_auto_redirect", false);
      document.location.href = url;
      return stop()
    }
  }

}

login();
window.setTimeout(login, 500);
window.setTimeout(login, 1000);
iv = window.setInterval(login, 3000);