Stigs Torn Scripts

Adds options to request an assist on the attack page

当前为 2024-06-16 提交的版本,查看 最新版本

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Greasemonkey 油猴子Violentmonkey 暴力猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Violentmonkey 暴力猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Userscripts ,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 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();
})();