shlsdv / Working ExHentai Login Screen (Cookies)

// ==UserScript==
// @name        Working ExHentai Login Screen (Cookies)
// @namespace   Violentmonkey Scripts
// @match       https://exhentai.org/*
// @grant       none
// @version     1.3
// @author      shlsdv
// @license     MIT
// @description Displays a (working) login form.
// @icon        https://e-hentai.org/favicon.ico
// ==/UserScript==


function getCookie(name) {
  var cookies = document.cookie.split(';');
  for (var i = 0; i < cookies.length; i++) {
    var cookie = cookies[i].trim();
    if (cookie.startsWith(name + '=')) {
      return cookie.substring(name.length + 1);
    }
  }
  return null;
}

function setCookie(name, value, domain) {
  console.log(name + '=' + value + '; path=/' + (domain ? '; domain=' + domain : ''));
  document.cookie = name + '=' + value + '; path=/' + (domain ? '; domain=' + domain+';' : '');
}

function deleteAllCookies() {
  var cookieList  = document.cookie.split (/;\s*/);
  for (var J = cookieList.length - 1;   J >= 0;  --J) {
      var cookieName = cookieList[J].replace (/\s*(\w+)=.+$/, "$1");
      eraseCookie (cookieName);
  }
}

function eraseCookie (cookieName) {
    var domain      = document.domain;
    var domain2     = document.domain.replace (/^www\./, "");
    var domain3     = document.domain.replace (/^(\w+\.)+?(\w+\.\w+)$/, "$2");;
    var pathNodes   = location.pathname.split ("/").map ( function (pathWord) {
        return '/' + pathWord;
    } );
    var cookPaths   = [""].concat (pathNodes.map ( function (pathNode) {
        if (this.pathStr) {
            this.pathStr += pathNode;
        } else {
            this.pathStr = "; path=";
            return (this.pathStr + pathNode);
        }
        return (this.pathStr);
    } ) );
    ( eraseCookie = function (cookieName) {
        cookPaths.forEach ( function (pathStr) {
            var diagStr     = cookieName + "=" + pathStr + "; expires=Thu, 01-Jan-1970 00:00:01 GMT;";
            document.cookie = diagStr;
            document.cookie = cookieName + "=" + pathStr + "; domain=" + domain  + "; expires=Thu, 01-Jan-1970 00:00:01 GMT;";
            document.cookie = cookieName + "=" + pathStr + "; domain=" + domain2 + "; expires=Thu, 01-Jan-1970 00:00:01 GMT;";
            document.cookie = cookieName + "=" + pathStr + "; domain=" + domain3 + "; expires=Thu, 01-Jan-1970 00:00:01 GMT;";
        } );
    } ) (cookieName);
}

function addCustomCookies(id, igneous, pass, sk=""){
  setCookie('sl', 'dm_1');
  setCookie('ipb_member_id', id);
  setCookie('igneous', igneous);
  setCookie('ipb_pass_hash', pass);
  if (sk) {
    setCookie('sk', sk, '.exhentai.org');
  }
}

function submitLoginForm() {
  deleteAllCookies();
  var username = document.getElementById('username').value;
  var password = document.getElementById('password').value;
  var [id, igneous, sk=""] = username.split(';');
  addCustomCookies(id.trim(), igneous.trim(), password.trim(), sk.trim());
  location.reload();
}

function disableCategory(num) {
  var element = document.getElementById(`cat_${num}`);
  if (element && !element.hasAttribute('data-disabled')) {
    toggle_category(num);
  }
}

window.onload = function() {
  if (!getCookie('igneous') || getCookie('igneous')==='mystery') {
    (function() {
      'use strict';

      var loginForm = document.createElement('form');
      loginForm.setAttribute('id', 'login-form');
      loginForm.style.position = 'fixed';
      loginForm.style.top = '50%';
      loginForm.style.left = '50%';
      loginForm.style.transform = 'translate(-50%, -50%)';
      loginForm.style.zIndex = '9999';
      loginForm.style.backgroundColor = '#fff';
      loginForm.style.padding = '20px';
      loginForm.style.border = '1px solid #ccc';
      loginForm.style.boxShadow = '0 0 10px rgba(0, 0, 0, 0.2)';

      loginForm.innerHTML = `
          <img src="https://i.warosu.org/data/vt/img/0480/04/1682324270431906.gif" width=50 style='position: absolute; left: 40px; top: 30px;'>
          <img src="https://i.warosu.org/data/vt/img/0480/04/1682324270431906.gif" width=50 style='position: absolute; right: 40px; top: 30px; transform: scaleX(-1);'>

          <div style="text-align: center;">
              <h2>Login</h2>
              <input type="text" id="username" name="username" placeholder="ipb_member_id;igneous;sk" style='height: 30px;' required><br><br>
              <input type="password" id="password" name="password" placeholder="ipb_pass_hash" style='height: 30px;' required><br><br>
              <input type="submit" value="Submit" style='width: 80px; height: 40px;'>
          </div>
          <p>
            In the first field, enter your \'ipb_member_id\' and \'igneous\' cookies, separated by ';'.
            In the second field, enter your \'ipb_pass_hash\'.
            Obtain these three cookies in your browser's developer tools after successfully entering ExHentai.
            You can also enter your \'sk\' cookie after another semicolon to fix potential watchlist-related issues.
          </p>
      `;

      loginForm.addEventListener('submit', function(event) {
          event.preventDefault();
          submitLoginForm();
      });

      document.addEventListener('keydown', function(event) {
          if (event.keyCode === 13) {
              event.preventDefault();
              submitLoginForm();
          }
      });

      document.body.appendChild(loginForm);
    })();
  }
};