3jameo3 / HaremHeroes Salary

// ==UserScript==
// @name         HaremHeroes Salary
// @namespace    3jameo3
// @version      0.1
// @description  This will no longer be updated.
// @author       3jameo3
// @match        http*://nutaku.haremheroes.com/*
// @updateURL    https://openuserjs.org/meta/3jameo3/HaremHeroes_Salary.meta.js
// @require      https://cdn.jsdelivr.net/js-cookie/2.2.0/js.cookie.js
// @grant        GM_addStyle
// @license      MIT
// ==/UserScript==

GM_addStyle('/* The switch - the box around the slider */ .switch { position: relative; display: inline-block; width: 60px; height: 34px; } /* Hide default HTML checkbox */ .switch input {display:none;} /* The slider */ .slider { position: absolute; cursor: pointer; top: 0; left: 0; right: 0; bottom: 0; background-color: #ccc; -webkit-transition: .4s; transition: .4s; } .slider:before { position: absolute; content: ""; height: 26px; width: 26px; left: 4px; bottom: 4px; background-color: white; -webkit-transition: .4s; transition: .4s; } input:checked + .slider { background-color: #2196F3; } input:focus + .slider { box-shadow: 0 0 1px #2196F3; } input:checked + .slider:before { -webkit-transform: translateX(26px); -ms-transform: translateX(26px); transform: translateX(26px); } /* Rounded sliders */ .slider.round { border-radius: 34px; } .slider.round:before { border-radius: 50%; }');

function sleep(milliseconds) {
  var start = new Date().getTime();
  for (var i = 0; i < 1e7; i++) {
    if ((new Date().getTime() - start) > milliseconds) {
      break;
    }
  }
}

var getSalary = function () {
  try {
    if ($("#breadcrumbs span").last().text() === "Harem") {
      console.log("Detected Harem Screen. Fetching Salary");
      $("#harem_whole #harem_left .salary:not('.loads') button").each(function (index) {
        $(this).click();
      sleep(1000);
      });
      document.cookie = "nextSalaryTime=;";
    }
    else {
      // Not at Harem screen then goto the Harem screen.
      console.log("Navigating to Harem window.");
      sessionStorage.autoLoop = "false";
      window.location = window.location.origin + $("nav div[rel='content'] a:has(.harem)").attr("href");
      sleep(1000);
      return;
    }
  }
  catch (ex) {
    console.log("Could not collect salary... " + ex);
  }
};

var updateData = function () {
  //console.log("updating UI");
  sessionStorage.autoSalary = document.getElementById("autoSalaryCheckbox").checked;
};

var autoLoop = function () {
  updateData();
  var busy = false;
  var page = window.location.href;

  if (sessionStorage.autoSalary === "true" && busy === false) {
    if (Cookies.get("nextSalaryTime") === undefined) {
      console.log("Time to fetch salary.");
      getSalary();
      busy = true;
    }
    else if (Cookies.get("nextSalaryTime") === "") {
      console.log("Salary fetched. Getting next fetch time");
      if ($("nav div[rel='content'] a:has(.home)").attr("href") !== window.location.pathname) {
        console.log("Moving to home.");
        sessionStorage.autoLoop = "false";
        // Goto Home page.
        window.location = window.location.origin + $("nav div[rel='content'] a:has(.home)").attr("href");
        busy = true;
        return;
      }
      var CAB = $("#collect_all_bar .in").text();
      var closestTime;
      //console.log(CAB);
      if (CAB.includes('h') && CAB.includes('m')) {
        CAB = $("#collect_all_bar .in").text().replace('h', '').replace('m', '').replace(' ', '');
        //console.log('"'+CAB+'"');
        if (CAB.length == 1) {
          closestTime = Number((CAB[0]) * 3600);
        }
        if (CAB.length == 2) {
          closestTime = Number((CAB[0]) * 3600) + (Number(CAB[1]) * 60);
        }
        if (CAB.length == 3) {
          closestTime = Number((CAB[0]) * 3600) + (Number(CAB[1] + CAB[2]) * 60);
        }
        if (CAB.length == 4) {
          closestTime = Number((CAB[0] + CAB[1]) * 3600) + (Number(CAB[2] + CAB[3]) * 60);
        }
        //console.log('"'+closestTime+'"');
      }
      if (CAB.includes('m')) {
        CAB.replace('m', '');
        //console.log('"'+CAB+'"');
        closestTime = Number(CAB.replace('m', '')) * 60;
        //console.log('"'+closestTime+'"');
      }
      if (CAB.includes('s')) {
        CAB.replace('s', '');
        //console.log('"'+CAB+'"');
        closestTime = Number(CAB.replace('s', ''));
        //console.log('"'+closestTime+'"');
      }
      document.cookie = "nextSalaryTime=present;max-age=" + (closestTime < 0 ? 0 : closestTime);
      console.log("New fetch time stored in nextSalaryTime cookie.(+" + closestTime + " sec.)");
      busy = false;
    }
    // Just in case something goes wrong area
    var collect = ($('#collect_all').text().replace(/\s/g, ""));
    if (collect.length >= 18 && (Cookies.get("nextSalaryTime") === undefined ||Cookies.get("nextSalaryTime") === 0 || Cookies.get("nextSalaryTime") === "")) {
      document.cookie = "nextSalaryTime=;";
      console.log("New fetch time stored in nextSalaryTime cookie.(+" + closestTime + " sec.)");
      console.log("Time to fetch salary.");
      getSalary();
      busy = true;
    }
    else {
      busy = false;
    }
  }
  if (busy === true && sessionStorage.userLink === "none") {
    sessionStorage.userLink = page;
  }
  else if (sessionStorage.userLink !== "none" && busy === false) {
    console.log("Restoring page " + sessionStorage.userLink);
    window.location = sessionStorage.userLink;
    sessionStorage.userLink = "none";
  }

  if (isNaN(sessionStorage.autoLoopTimeMili)) {
    console.log("AutoLoopTimeMili is not a number.");
    setDefaults();
  }
  else {
    if (sessionStorage.autoLoop === "true") setTimeout(autoLoop, Number(sessionStorage.autoLoopTimeMili));
    else console.log("autoLoop Disabled");
  }
};

var setDefaults = function () {
  console.log("Setting Defaults.");
  var busy = "false";
  var nextSalaryTime = "0";
  var closestTime = "0";
  sessionStorage.autoSalary = "false";
  sessionStorage.autoLoop = "true";
  sessionStorage.userLink = "none";
  sessionStorage.autoLoopTimeMili = "500";
  sessionStorage.freshStart = "no";
};

var start = function () {
  //console.log("script started");
  // Add UI buttons.
  var UIcontainer = $("#contains_all nav div[rel='content']");
  UIcontainer.html('<div style="position: absolute;right: -16.475%; padding: 10px;width: inherit;text-align: center;display:flex;flex-direction:column;">' +
    '<span>AutoSal.</span><div><label class=\"switch\"><input id=\"autoSalaryCheckbox\" type=\"checkbox\"><span class=\"slider round\"></span></label></div>' +
    '</div>' + UIcontainer.html());
  document.getElementById("autoSalaryCheckbox").checked = sessionStorage.autoSalary === "true";
  sessionStorage.autoLoop = "true";
  if (typeof sessionStorage.freshStart == "undefined" || isNaN(Number(sessionStorage.autoLoopTimeMili))) {
    setDefaults();
  }
  autoLoop();
};
$("document").ready(start);