Greasy Fork is available in English.

Check and Deploy

Too long to describe

// ==UserScript==
// @name         Check and Deploy
// @namespace    http://tampermonkey.net/
// @version      3.2
// @description  Too long to describe
// @author       you know someone
// @match        https://www.erepublik.com/en/military/battlefield/*
// @grant        GM_xmlhttpRequest
// ==/UserScript==

(function () {
  'use strict';

  // Variable declarations
  const yourDiv = 1; // add your division ID
  let URLcheck = false;
  const locID = erepublik.citizen.countryLocationId;

  function checkAndDeploy() {
    // Get the current URL and write it to the console
    const currentURL = window.location.href;
    console.log("Current URL:", currentURL);

    // Extract the value after "https://www.erepublik.com/en/military/battlefield/"
    const extractedValue = currentURL.replace("https://www.erepublik.com/en/military/battlefield/", "");

    // Split the extractedValue by the slash ("/") and get the second part (22664080)
    const extractedValueParts = extractedValue.split('/');
    const secondPartValue = extractedValueParts[1];
    console.log("Second part value:", extractedValue);

    // Check if the secondPartValue exists and is more than 22000000
    if (secondPartValue !== undefined && parseInt(secondPartValue) > 22000000) {
      URLcheck = true;

      // Construct the new URL with the extracted value
      const newURL = `https://www.erepublik.com/en/military/battle-stats/${extractedValue}`;
      console.log("New URL:", newURL);

      // Fetch the JSON data from the new URL
      fetch(newURL)
        .then((response) => response.json())
        .then((data) => {
          // Extract the latest object (highest numerical value) from stats.current
          const statsKeys = Object.keys(data.stats.current);
          const latestKey = Math.max(...statsKeys);
          const latestStatsObject = data.stats.current[latestKey];
          console.log("Latest stats.current object:", latestStatsObject);

          // Check if the nested object yourDiv.locID.secondPartValue exists
          if (latestStatsObject[yourDiv]?.[locID]?.[secondPartValue]) {
            console.log(`Object with key ${yourDiv}.${locID}.${secondPartValue} exists.`);
          } else {
            console.log(`Object with key ${yourDiv}.${locID}.${secondPartValue} does not exist.`);
            deployFunction();
          }
        })
        .catch((error) => {
          console.error("Error fetching data:", error);
        });
    } else {
      URLcheck = false;
    }
  }

  function deployFunction() {
    // Find the "Deploy" button
    const deployButton = document.querySelector(".deployBtn[title='Deploy']");
    if (deployButton) {
      deployButton.click();
      console.log("Clicked 'Deploy' button.");

      // Wait for the popup to open
      setTimeout(function () {
        // Find the "Start Deploy" button inside the popup
        const startDeployButton = document.querySelector(".deployBtn[title='Start Deploy']");
        if (startDeployButton) {
          // Wait for 5 seconds before changing the value of the input type="range"
          setTimeout(function () {
            // Find the input type="range" element with class "slider"
            const foodInput = document.querySelector(".slider");
            if (foodInput) {
              // Get the values of min and max attributes
              const minFood = parseInt(foodInput.getAttribute("min"));
              const maxFood = parseInt(foodInput.getAttribute("max"));
              console.log("minFood:", minFood);
              console.log("maxFood:", maxFood);

              // Find the element with class "bar foodBar" and get its width percentage
              const foodBar = document.querySelector(".bar.foodBar");
              if (foodBar) {
                const widthPercentage = parseFloat(foodBar.style.width.replace("%", ""));
                console.log("Width percentage:", widthPercentage);

                // Calculate the value based on the width percentage
                let value = minFood + (widthPercentage / 100) * (maxFood - minFood);

                // Constrain the value between 11 and 14
                value = Math.max(11, Math.min(14, value));
                console.log("value:", value);

                // Find the input type="range" element with class "slider" and set its value
                foodInput.value = value;

                // Trigger the "input" event to simulate user interaction
                const inputEvent = new Event("input", {
                  bubbles: true,
                  cancelable: true
                });
                foodInput.dispatchEvent(inputEvent);
                console.log("Changed food value to:", value);

                // Wait for 3 seconds before clicking the button again
                setTimeout(function () {
                  startDeployButton.click();
                  console.log("Clicked 'Start Deploy' button again after changing input value.");
                }, 3000);
              } else {
                console.error("Failed to find element with class 'bar foodBar'.");
              }
            } else {
              console.error("Failed to find element with class 'slider'.");
            }
          }, 5000);
        } else {
          console.error("Failed to find element with class 'deployBtn[title='Start Deploy']' inside the popup.");
        }
      }, 5000); // Wait for 5 seconds for the popup to open
    } else {
      console.error("Failed to find element with class 'deployBtn[title='Deploy']'.");
    }
  }

  // Run the main function
  checkAndDeploy();
})();