cuzi / openuserjs.org Autologin

// ==UserScript==
// @name        openuserjs.org Autologin
// @namespace   openuserjs.org
// @license     MIT
// @updateURL   https://openuserjs.org/meta/cuzi/openuserjs.org_Autologin.meta.js
// @version     8
// @include     /^https?:\/\/openuserjs\.org(\/.*)?$/
// @exclude     /^https?:\/\/openuserjs\.org\/auth\/.*$
// @grant       GM_setValue
// @grant       GM_getValue
// @grant       GM.setValue
// @grant       GM.getValue
// @require     https://greasemonkey.github.io/gm4-polyfill/gm4-polyfill.js
// ==/UserScript==
(function() {
"use strict";


GM.getValue("username", false).then(function(username) {

  if(username) {
    var li = document.createElement("li");
    li.innerHTML = '<a href="#forget" title="Forget autologin"><span class="visible-xs-inline">Forget autologin</span><i class="fa fa-chain-broken"></i></a>';
    document.querySelector(".navbar-nav").insertBefore(li, document.querySelector(".navbar-nav li:last-child"));
    li.addEventListener("click", async function() {
      await GM.setValue("username", false);
      alert("Your autologin username has been deleted.");
    });
  }

  if(document.title.indexOf("503 ") === 0) {
    // We're busy right now. Try again later.
  } else if(document.location.href.match(/^https?:\/\/openuserjs\.org\/login\/?$/)) {
    if(username) {
      document.getElementsByName("username")[0].value = username;
      document.getElementById("action").click();
    } else {
      var button = document.getElementById("action");
      button.addEventListener("click", async function(ev) {
        var inputs = document.getElementsByName("username");
        for(var j = 0; j < inputs.length; j++) {
          if(inputs[j].value) {
            await GM.setValue("username", inputs[j].value);
            return;
          }
        }
      });
    }
  } else if(username && document.getElementsByClassName("fa fa-sign-in") && document.getElementsByClassName("fa fa-sign-in")[0]) {
    document.getElementsByClassName("fa fa-sign-in")[0].click();
  }
});

})();