3jameo3 / HaremHeroes Activities

// ==UserScript==
// @name         HaremHeroes Activities
// @namespace    3jameo3
// @version      0.4
// @description  This will no longer be updated.
// @author       3jameo3
// @match        http*://nutaku.haremheroes.com/*
// @updateURL    https://openuserjs.org/meta/3jameo3/HaremHeroes_Activities.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 updateData = function () {
  console.log("Updating Data");
  sessionStorage.autoMissions = document.getElementById("autoMissions").checked;
};

var getMission = function () {
  //console.log("Auto Missions Running");
  try {
    if ($("#breadcrumbs span").last().text() === "Activities") {
      console.log("Detected mission screen. Accepting a mission");
      var buttonMatch = $("#missions button:not([style='display: none;'])");
      var Accept = $("#missions button[rel^='mission_start']:not([style='display: none;'])");
      /*
      var Finish = $("#missions button[rel^='finish']:not([style='display: none;'])");
      var Present = $("#base_block #end_gift button:not([style='display: none;'])");
      */

      if (buttonMatch.length === 0) console.log("Could not find mission button.");
      else if (Accept) {
        console.log("Starting mission.");
        Accept.click();
        sleep(1000);
        document.cookie = "nextActivityTime=;";
      }
      /*
      else if (Finish) {
          console.log("Lets not click that.");
          //Finish.click();
      }
      else if (($("#base_block #end_gift")) && Present) {
          console.log("Claiming mission completion reward(s).");
          Present.click();
      }
      */
      else {
        console.log("Could not identify given mission button.");
        sessionStorage.missionRequirement = "unknownMissionButton";
      }
    }
    else {
      // Not at Mission screen then goto the Mission screen.
      if (Cookies.get("nextActivityTime") === "0" || Cookies.get("nextActivityTime") === undefined || Cookies.get("nextActivityTime") === "" || (($("#home_missions_bar1 .text").length) === 0 && ($("#home_missions_bar2 .text").length) <= 2)) {
        console.log("Navigating to mission window.");
        sessionStorage.autoLoop = "false";
        window.location = window.location.origin + $("nav div[rel='content'] a:has(.activities)").attr("href");
        return;
      }
    }
  }
  catch (ex) {
    console.log("Could not do any missions... " + ex);
  }
};

var collectMission = function () {
  //console.log("Auto Missions Running");
  try {
    if ($("#breadcrumbs span").last().text() === "Activities") {
      console.log("Detected mission screen. Collecting mission(s)");
      var buttonMatch = $("#missions button:not([style='display: none;'])");
      var Claim = $("#missions button[rel^='claim']:not([style='display: none;'])");
      var Groovy = $("#popups #mission_rewards button[rel^='groovy']:not([style='display: none;'])");

      if (buttonMatch.length === 0) console.log("Could not find mission button.");
      else if (Claim && (Cookies.get("nextActivityTime") <= 1)) {
        console.log("Claiming mission");
        Claim.click();
        sleep(1000);
      }
      else if (click) {}
      else if (($("#popups #mission_rewards")) && Groovy) {
        console.log("Claiming mission reward(s).");
        Groovy.click();
        sleep(500);
        document.cookie = "nextActivityTime=;";
      }
      else {
        console.log("Could not identify given mission button.");
        sessionStorage.missionRequirement = "unknownMissionButton";
      }
    }
    else {
      // Not at Mission screen then goto the Mission screen.
      if (Cookies.get("nextActivityTime") === "0" || Cookies.get("nextActivityTime") === undefined || Cookies.get("nextActivityTime") === "" || (($("#home_missions_bar1 .text").length) === 0 && ($("#home_missions_bar2 .text").length) <= 2)) {
        console.log("Navigating to Mission window.");
        sessionStorage.autoLoop = "false";
        window.location = window.location.origin + $("nav div[rel='content'] a:has(.activities)").attr("href");
        return;
      }
    }
  }
  catch (ex) {
    console.log("Could not collect mission(s)... " + ex);
  }
};

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

  if (sessionStorage.autoMissions === "true" && busy === false) {
    if (Cookies.get("nextActivityTime") === undefined && ($("#home_missions_bar1 .text").length) === 0 && ($("#home_missions_bar2 .text").length) <= 2 && ($("div[class='collect_notif']:not([style=''])"))) {
      console.log("Time to get a mission.");
      getMission();
      busy = true;
    }
    else if (Cookies.get("nextActivityTime") === "" && ($("#home_missions_bar1 .text").length) >= 0 && ($("#home_missions_bar2 .text").length) <= 2) {
      console.log("Accepting a mission. Getting countdown to next mission");
      if ($("nav div[rel='content'] a:has(.home)").attr("href") !== window.location.pathname) {
        console.log("Moving to home.");
        sessionStorage.autoLoop = "false";
        window.location = window.location.origin + $("nav div[rel='content'] a:has(.home)").attr("href");
        busy = true;
        return;
      }
      var MB1 = $("#home_missions_bar1 .text").text().replace('Complete in ', '');
      var closestTime1;
      //console.log(MB1)
      if (MB1.includes('h') && MB1.includes('m')) {
        MB1 = $("#home_missions_bar1 .text").text().replace('Complete in ', '').replace('h', '').replace('m', '').replace(' ', '');
        //console.log('"'+MB1+'"');
        if (MB1.length == 1) {
          closestTime1 = Number((MB1[0]) * 3600);
        }
        if (MB1.length == 2) {
          closestTime1 = Number((MB1[0]) * 3600) + (Number(MB1[1]) * 60);
        }
        if (MB1.length == 3) {
          closestTime1 = Number((MB1[0]) * 3600) + (Number(MB1[1] + MB1[2]) * 60);
        }
        if (MB1.length == 4) {
          closestTime1 = Number((MB1[0] + MB1[1]) * 3600) + (Number(MB1[2] + MB1[3]) * 60);
        }
        //console.log('"'+closestTime1+'"');
      }
      if (MB1.includes('m')) {
        MB1.replace('m', '');
        //console.log('"'+MB1+'"');
        closestTime1 = Number(MB1.replace('m', '')) * 60;
        //console.log('"'+closestTime1+'"');
      }
      if (MB1.includes('s')) {
        MB1.replace('s', '');
        //console.log('"'+MB1+'"');
        closestTime1 = Number(MB1.replace('s', ''));
        //console.log('"'+closestTime1+'"');
      }
      document.cookie = "nextActivityTime=present;max-age=" + (closestTime1 < 0 ? 0 : closestTime1);
      console.log("Time until next mission stored in nextActivityTime cookie.(+" + closestTime1 + " sec.)");
      busy = false;
    }
    if (Cookies.get("nextActivityTime") === "" && ($("#home_missions_bar1 .text").length) >= 0 && ($("#home_missions_bar2 .text").length) >= 0 && $("div[class='collect_notif']:not([style='display: none;'])")) {
      console.log("Time to collect the completed mission(s).");
      collectMission();
      busy = true;
    }
    if ((Cookies.get("nextActivityTime") === "" || Cookies.get("nextActivityTime") === undefined || Cookies.get("nextActivityTime") === 0) && ($("#home_missions_bar1 .text").length) <= 2 && ($("#home_missions_bar2 .text").length) >= 1) {
      console.log("All Missions Completed. Getting time until missions can be accepted again.");
      if ($("nav div[rel='content'] a:has(.home)").attr("href") !== window.location.pathname) {
        console.log("Moving to home.");
        sessionStorage.autoLoop = "false";
        window.location = window.location.origin + $("nav div[rel='content'] a:has(.home)").attr("href");
        busy = true;
        return;
      }
      var MB2 = $("#home_missions_bar2 .text").text().replace('Complete in ', '');
      //console.log(MB2);
      if (MB2.includes('h') && MB2.includes('m')) {
        MB2 = $("#home_missions_bar2 .text").text().replace('Complete in ', '').replace('h', '').replace('m', '').replace(' ', '');
        //console.log('"'+MB2+'"');
        if (MB2.length == 1) {
          closestTime1 = Number((MB2[0]) * 3600);
        }
        if (MB2.length == 2) {
          closestTime1 = Number((MB2[0]) * 3600) + (Number(MB2[1]) * 60);
        }
        if (MB2.length == 3) {
          closestTime1 = Number((MB2[0]) * 3600) + (Number(MB2[1] + MB2[2]) * 60);
        }
        if (MB2.length == 4) {
          closestTime1 = Number((MB2[0] + MB2[1]) * 3600) + (Number(MB2[2] + MB2[3]) * 60);
        }
        //console.log('"'+closestTime1+'"');
      }
      if (MB2.includes('m')) {
        MB2.replace('m', '');
        //console.log('"'+MB2+'"');
        closestTime1 = Number(MB2.replace('m', '')) * 60;
        //console.log('"'+closestTime1+'"');
      }
      if (MB2.includes('s')) {
        MB2.replace('s', '');
        //console.log('"'+MB2+'"');
        closestTime1 = Number(MB2.replace('s', ''));
        //console.log('"'+closestTime1+'"');
      }
      document.cookie = "nextActivityTime=present;max-age=" + (closestTime1 < 0 ? 0 : closestTime1);
      console.log("Time until new missions stored in nextActivityTime cookie.(+" + closestTime1 + " sec.)");
      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 nextActivityTime = "0";
  var closestTime1 = 0;
  sessionStorage.autoMissions = "false";
  sessionStorage.autoLoop = "true";
  sessionStorage.userLink = "none";
  sessionStorage.autoLoopTimeMili = "500";
  sessionStorage.freshStart = "no";
};

var start = function () {
  console.log("Starting Script.");
  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>AutoMissions</span><div><label class=\"switch\"><input id=\"autoMissions\" type=\"checkbox\"><span class=\"slider round\"></span></label></div>' +
    '</div>' + UIcontainer.html());
  document.getElementById("autoMissions").checked = sessionStorage.autoMissions === "true";
  sessionStorage.autoLoop = "true";
  if (typeof sessionStorage.freshStart == "undefined" || isNaN(Number(sessionStorage.autoLoopTimeMili))) {
    setDefaults();
  }
  autoLoop();
};
$("document").ready(start);