Stigs Torn Scripts

Adds options to request an assist on the attack page

目前為 2024-06-16 提交的版本,檢視 最新版本

您需要先安裝使用者腳本管理器擴展,如 TampermonkeyGreasemonkeyViolentmonkey 之後才能安裝該腳本。

You will need to install an extension such as Tampermonkey to install this script.

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyViolentmonkey 後才能安裝該腳本。

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyUserscripts 後才能安裝該腳本。

你需要先安裝一款使用者腳本管理器擴展,比如 Tampermonkey,才能安裝此腳本

您需要先安裝使用者腳本管理器擴充功能後才能安裝該腳本。

(我已經安裝了使用者腳本管理器,讓我安裝!)

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

(我已經安裝了使用者樣式管理器,讓我安裝!)

// ==UserScript==
// @name         Stigs Torn Scripts
// @namespace    http://tampermonkey.net/
// @version      1.2
// @description  Adds options to request an assist on the attack page
// @author       Stig [2648238]
// @match        https://www.torn.com/loader.php?sid=attack*
// @grant        GM_addStyle
// @grant        GM.xmlHttpRequest
// @grant        GM_registerMenuCommand
// @connect      api.no1irishstig.co.uk
// ==/UserScript==

function addLocID() {
  const locID = prompt("Please provide a Location, this should be provided by your Faction");

  if (locID) {
      localStorage.setItem('assistLocID', locID);
      location.reload();
  }
}

GM_registerMenuCommand('Select Assist Location', () => addLocID());
const source = 'Stigs Script';
let interval;
let bar = null;

(function() {
  'use strict';

  GM_addStyle(`
  .assist-request-top {
    justify-content: center !important;
    gap: 5px;
    flex-wrap: wrap;
    padding: 0 5px;
  }

  .assist-request-button {
    width: unset !important;
    margin: 5px 0;
    cursor: pointer;
  }

  .arb-default:hover {
    background: #AAAAAA !important;
  }

  :root .dark-mode .arb-default:hover {
    background: #666666 !important;
  }

  .arb-green {
    background: linear-gradient(180deg,#f8f2eb,#d2e8bc) !important;
    cursor: not-allowed !important;
  }

  :root .dark-mode .arb-green {
    background: linear-gradient(180deg,#7e9160,#363636) !important;
  }

  .arb-red {
    background: linear-gradient(180deg, #f8ebeb, #e8b3b3) !important;
    cursor: not-allowed !important;
  }

  :root .dark-mode .arb-red {
    background: linear-gradient(180deg, #8e4343, #363636) !important;
  }
`);

  var buttonList = [];

  function waitForElement(selector, callback) {
      let el = document.querySelector(selector);
      if (el) {
          callback(el);
      } else {
          setTimeout(function() {
              waitForElement(selector, callback);
          }, 500);
      }
  }

  async function getCustomButtons() {
      return new Promise((resolve, reject) => {
          GM.xmlHttpRequest({
              method: 'GET',
              url: `https:/api.no1irishstig.co.uk/abtns?locid=${localStorage['assistLocID']}`,
              headers: {
                  'Content-Type': 'application/json',
              },
              onload: function(response) {
                  if (response?.status && response.status !== 200) {
                      var responseText = response.responseText.replace(/^"|"$/g, '');
                      alert(`Error Code: ${response.status}\nMessage: ${responseText}` || `An unknown error has occurred - Please report this to ${owner} leadership.`);
                      reject(new Error(responseText));
                  } else {
                      let json = JSON.parse(response.responseText);
                      for (const [key, value] of Object.entries(json)) {
                          buttonList.push({ name: key, value: value });
                      }
                      resolve();
                  }
              }
          });
      });
  }

  function render() {
      let bar = document.getElementById('assist-request');
      if (bar != null) {
          return;
      }

      const header = document.getElementById('react-root');
      const element = document.createElement('div');
      element.className = 'msg-info-wrap';
      header.parentNode.insertBefore(element, header);

      if (localStorage['assistLocID'] === undefined) {
          element.innerHTML = `
          <div id='assist-request' class='info-msg-cont border-round m-top10 assist-request-top'>
              <div class='info-msg border-round assist-request-top'>
                  <a class='assist-request-button arb-default msg border-round' tabindex='0' href='#' id='setup_location'>
                      Set Assist Location
                  </a>
              </div>
          </div>
          <hr class='page-head-delimiter m-top10 m-bottom10 '>
      `;
          document.getElementById("setup_location").addEventListener("click", addLocID);
          return;
      }

      if (buttonList.length === 0) {
          return;
      }

      let buttons = '';
      for (const b of buttonList) {
          buttons += `
          <div class='assist-request-button arb-default msg border-round' tabindex='0' data-assist-type='${b.value}'>
              ${b.name}
          </div>
      `;
      }

      element.innerHTML = `
      <div id='assist-request' class='info-msg-cont border-round m-top10 assist-request-top'>
          <div class='info-msg border-round assist-request-top'>
              ${buttons}
          </div>
      </div>
      <hr class='page-head-delimiter m-top10 m-bottom10 '>
  `;

      bar = document.getElementById('assist-request');
      const buttonSet = document.getElementsByClassName('assist-request-button');
      for (let i = 0; i < buttonSet.length; i++) {
          buttonSet[i].addEventListener('click', request.bind(buttonSet[i]));
      }
  }

  async function main() {
      await waitForElement('.coreWrap___LtSEy', async function(coreWrap) {
          await getCustomButtons();
          render();
      });
  }
  main();
})();