HeLa Revives

Adds a button to request a revive from HeLa

目前為 2024-03-31 提交的版本,檢視 最新版本

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

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

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

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

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

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

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

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

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

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

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

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

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

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

// ==UserScript==
// @name         HeLa Revives
// @namespace    http://tampermonkey.net/
// @version      1.2.4
// @description  Adds a button to request a revive from HeLa
// @author       Lazerpent [2112641] & Stig [2648238]
// @match        https://www.torn.com/*
// @grant        GM_addStyle
// @grant        GM.xmlHttpRequest
// @grant        GM_registerMenuCommand
// @connect      api.no1irishstig.co.uk
// ==/UserScript==
'use strict';

const owner = "HeLa";
const source = "HeLa Healers Script";
const buttonLabel = "SH Revive Me!";
const colour = "#33B3F8";
const API_URL = 'https://api.no1irishstig.co.uk/request';

setInterval(checkButton, 500);

GM_addStyle(`
  .hela-revive {
    margin: auto;
    display: table;
    cursor: pointer;
    border-radius: .5em;
    padding: .25em .5em;
    text-decoration: none;

    color: black;
    background: lightgreen;
    border: 1px solid black;
    box-shadow: inset 0px 1px 0px 0px white;
  }

  .hela-revive:hover {
    background: green;
  }

  .hela-desktop:hover {
    background: #3cb371;
  }

  .hela-revive[disabled='true'] {
      opacity: 0.5;
  }

  .hela-desktop {
    margin-bottom: 8px;
  }

  .hela-mobile {
    display: inline;
  }

  .hela-dark-mode {
    color: white;
    background: darkgreen;
    border: 1px solid white;
    box-shadow: inset 0px 1px 0px 0px black;
  }

  .hela-desktop:before {
    content: 'HeLa Quick ';
  }

  @media(min-width: 380px) {
    .hela-mobile:before {
      content: 'HeLa ';
    }
  }
`);

let btn;

function getButton(mobile) {
  btn = document.createElement('a');

  btn.setAttribute('role', 'button');
  btn.setAttribute('aria-pressed', 'false');
  btn.setAttribute('aria-live', 'polite');

  btn.classList.add('hela-revive');
  btn.classList.add('hela-' + (mobile ? 'mobile' : 'desktop'));

  if (mobile || document.body.classList.contains('dark-mode')) {
    btn.classList.add('hela-dark-mode');
  }

  btn.id = 'hela-btn';
  btn.innerHTML = 'Revive';
  btn.href = '#hela-revive';
  btn.addEventListener('click', submitRequest);
  return btn;
}

function checkButton() {
  if(inCloudflareChallenge()) return;
  const {hospital, mobile} = getSessionData();
  const exists = document.getElementById('hela-btn');

  if (!hospital) {
    if (btn) {
      btn.remove();
    }
    return;
  }

  if (exists) {
    return;
  }

  const location = document.querySelector(mobile ? '.header-buttons-wrapper' : '.life___PlnzK');
  if (location != null) {
    location.children[0].insertAdjacentElement('beforebegin', getButton(mobile));
  }
}

function handleResponse(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.`);
    return;
  }

  let contract = false;
  try {
    contract = !!JSON.parse(response.responseText).contract;
  } catch (e) {
  }

  if (contract) {
    alert(`Contract request has been sent to ${owner}. Thank you!`)
  } else {
    alert(`Request has been sent to ${owner}. Please pay your reviver a Xanax or $1m. Thank you!`);
  }
}

function submitRequest(e) {
  e?.preventDefault();

  const sessionData = getSessionData();
  if (!sessionData.hospital) {
    alert('You are not in the hospital.');
    return;
  }

  btn.setAttribute('disabled', true);
  btn.setAttribute('aria-pressed', true);

  GM.xmlHttpRequest({
    method: 'POST',
    url: API_URL,
    headers: {
      'Content-Type': 'application/json',
    },
    data: JSON.stringify({
      'tornid': parseInt(sessionData.userID),
      'username': '' + sessionData.userName,
      "vendor": owner,
      'source': `${source} ${GM_info.script.version}`,
      'type': 'revive'
    }),
    onload: handleResponse,
  });
}

function getSessionData() {
  const sidebar = Object.keys(sessionStorage).find((k) => /sidebarData\d+/.test(k));
  const data = JSON.parse(sessionStorage.getItem(sidebar));
  return {
    userID: data.user.userID,
    userName: data.user.name,
    mobile: data.windowSize === 'mobile',
    hospital: data.statusIcons?.icons?.hospital,
  };
}

function inCloudflareChallenge() {
  return document.getElementsByClassName('iAmUnderAttack').length;
}

GM_registerMenuCommand(`Request revive from ${owner}`, () => submitRequest());