shigatora / Trip checker

// ==UserScript==
// @name         Trip checker
// @version      0.1
// @description  Make sure to have island view active and click on the menu item "Check trips".
// @author       Vít Dolínek
// @include      http://*.grepolis.com/game/*
// @include      https://*.grepolis.com/game/*
// @icon64       https://st2.depositphotos.com/5943796/11382/v/950/depositphotos_113825054-stock-illustration-initial-letter-lg-green-swoosh.jpg
// @require      https://cdn.jsdelivr.net/npm/lodash@4.17.15/lodash.min.js
// @copyright    2024, LG (https://openuserjs.org/users/shigatora)
// @licence      MIT
// ==/UserScript==

$(document).on("game:load", function () {
  "use strict";

  var windows = require("game/windows/ids");

    if (document.querySelector("li.main_menu_item.last")) {
      const iconSpan = document.createElement("span");
      iconSpan.setAttribute("class", "icon");
      iconSpan.innerHTML = "⚔️";
      iconSpan.setAttribute(
        "style",
        "font-size: 14px; position: absolute; left: 4px !important;"
      );

      const uiHighlightDiv = document.createElement("div");
      uiHighlightDiv.setAttribute("class", "ui_highlight");
      uiHighlightDiv.setAttribute("data-type", "main_menu");

      const buttonSpan = document.createElement("span");
      buttonSpan.setAttribute("class", "button");
      buttonSpan.setAttribute("id", "spam_attack_button");
      const nameSpan = document.createElement("span");
      nameSpan.setAttribute("class", "name");
      nameSpan.innerHTML = "Check trips";

      const buttonWrapperSpan = document.createElement("span");
      buttonWrapperSpan.setAttribute("class", "button_wrapper");
      const nameWrapperSpan = document.createElement("span");
      nameWrapperSpan.setAttribute("class", "name_wrapper");

      const contentWrapperSpan = document.createElement("span");
      contentWrapperSpan.setAttribute("class", "content_wrapper");

      const liItem = document.createElement("li");
      liItem.setAttribute("id", "attack_alarm_menu");
      liItem.setAttribute("class", "main_menu_item");
      liItem.appendChild(contentWrapperSpan);
      contentWrapperSpan
        .appendChild(buttonWrapperSpan)
        .appendChild(nameWrapperSpan);
      buttonWrapperSpan.appendChild(buttonSpan);
      buttonSpan.appendChild(iconSpan);
      buttonSpan.appendChild(uiHighlightDiv);
      nameWrapperSpan.appendChild(nameSpan);
      const lastMenuItem = document.querySelector("li.main_menu_item.last");
      liItem.addEventListener("click", () => {
      if (Game.layout_mode !== "island_view") retur
      gpAjax.ajaxGet(
  "island_info",
  "index",
  { island_id: MM.getModels().Town[ITowns.getCurrentTown().getId()].getIslandId() },
  !0,
  {
    success: (i, { json }) => {
      const currentTownOuterUnits = Object.values(MM.getModels().Units).filter(
        (model) =>
          model.attributes.home_town_id === ITowns.getCurrentTown().getId() &&
          model.attributes.same_island &&
          model.attributes.current_town_player_id !== Game.player_id
      );
      const townsMissing = {};
      json.town_list.forEach((t) => (townsMissing[t.id] = true));
      currentTownOuterUnits.forEach((units) => {
        const destination = $(`#town_flag_${units.attributes.current_town_id}`);

        destination.append(
          '<span style="border: 4px solid green;color:green;">●</span>'
        );

        delete townsMissing[units.attributes.current_town_id];
      });

      Object.keys(townsMissing).forEach((townMissing) => {
        const destination = $(`#town_flag_${townMissing}`);

        destination.append(
          '<span style="border: 4px solid red;color:red;">●</span>'
        );
      });
    },
  }
);
      });

      if (document.getElementsByClassName("main_menu_item").length === 8) {
        document
          .querySelector("div.nui_main_menu > div.middle > div.content > ul")
          .setAttribute("style", "height: 293px");
      } else if (
        document.getElementsByClassName("main_menu_item").length === 9
      ) {
        document
          .querySelector("div.nui_main_menu > div.middle > div.content > ul")
          .setAttribute("style", "height: 330px");
      } else {
        let height;

        document
          .querySelector("div.nui_main_menu > div.middle > div.content > ul")
          .getAttribute("style")
          .split(";")
          .forEach((style) => {
            if (style.search("height") !== -1) {
              height = style.replace("height: ", "");
            }
          });

        height = height.replace("px", "");

        document
          .querySelector("div.nui_main_menu > div.middle > div.content > ul")
          .setAttribute("style", `height: ${parseInt(height) + 35}px`);
      }

      lastMenuItem.parentNode.insertBefore(liItem, lastMenuItem);
    }
})