HHU Revive Request Script

Harasses someone from HHU to revive you.

Dovrai installare un'estensione come Tampermonkey, Greasemonkey o Violentmonkey per installare questo script.

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

Dovrai installare un'estensione come Tampermonkey o Violentmonkey per installare questo script.

Dovrai installare un'estensione come Tampermonkey o Userscripts per installare questo script.

Dovrai installare un'estensione come ad esempio Tampermonkey per installare questo script.

Dovrai installare un gestore di script utente per installare questo script.

(Ho già un gestore di script utente, lasciamelo installare!)

Dovrai installare un'estensione come ad esempio Stylus per installare questo stile.

Dovrai installare un'estensione come ad esempio Stylus per installare questo stile.

Dovrai installare un'estensione come ad esempio Stylus per installare questo stile.

Dovrai installare un'estensione per la gestione degli stili utente per installare questo stile.

Dovrai installare un'estensione per la gestione degli stili utente per installare questo stile.

Dovrai installare un'estensione per la gestione degli stili utente per installare questo stile.

(Ho già un gestore di stile utente, lasciamelo installare!)

// ==UserScript==
// @name         HHU Revive Request Script
// @namespace    http://tampermonkey.net/
// @version      2.0.1
// @description  Harasses someone from HHU to revive you.
// @author       not Mavri [2402357]
// @match        https://www.torn.com/*
// @grant        GM.xmlHttpRequest
// @grant        GM_addStyle
// @homepage     https://mavriware.me
// @require      https://cdn.jsdelivr.net/npm/axios@~0.21.1/dist/axios.min.js
// @require      https://cdn.jsdelivr.net/npm/axios-userscript-adapter@~0.1.2/dist/axiosGmxhrAdapter.min.js
// @connect      mavriware.me
// ==/UserScript==


/* eslint-disable no-shadow */
/* eslint-disable no-inline-comments */
/* eslint-disable no-undef */

'use strict';

setTimeout(renderButton, 500);

axios.defaults.adapter = axiosGmxhrAdapter;


GM_addStyle(`
    .imp {
    box-shadow:inset 0px 1px 0px 0px #cf866c;
    background:linear-gradient(to bottom, #d0451b 5%, #bc3315 100%);
    background-color:#d0451b;
    border-radius:1em;
    border:1px solid #942911;
    display:inline-block;
    cursor:pointer;
    color:#ffffff;
    font-family:Arial;
    font-size:.75em;
    padding:.3em .5em;
    text-decoration:none;
    text-shadow:0px 1px 0px #854629;
    margin-bottom: .7em;
}
.imp:hover {
    background:linear-gradient(to bottom, #bc3315 5%, #d0451b 100%);
    background-color:#bc3315;
}
.imp:active {
    position:relative;
    top:1px;
}
`);

function renderButton() {
	if (!document.getElementById('imp-button')) {
		let sidebar;
		let elem;
		try {
			sidebar = getSidebar();
			if (sidebar.statusIcons.icons.hospital) { // true if ape's in hospital
				if (sidebar.windowSize != 'mobile') {
					elem = document.querySelector('#barLife');
				}
				if (sidebar.windowSize == 'mobile') {
					elem = document.querySelector('.header-buttons-wrapper');
				}
				if (elem != null) {
					const html = '<a href="#" class="imp" id="imp-button">REVIVE ME</a>';
					elem.children[0].insertAdjacentHTML('beforebegin', html);
					const impButton = document.getElementById('imp-button');
					impButton.addEventListener('click', function() {
						sendInfo();
					});
				}

			}
		}
		catch (e) {
			/* ! die lol */
		}
	}
	setTimeout(renderButton, 500);
}

function sendInfo() {

	const sidebar = getSidebar();
	const uid = sidebar.user.userID;
	const name = sidebar.user.name;
	const faction = sidebar.statusIcons.icons.faction.subtitle.split(' of ')[1];
	const source = 'IMPS';
	const url = 'http://api.mavriware.me/revive';
	const hospitalIcon = sidebar.statusIcons.icons.hospital;

	if (hospitalIcon == null) {
		alert('You are not in the hospital!');
	}
	else {
		const obj = {
			'userID': uid,
			'userName': name,
			'factionName': faction,
			'source': source,
		};

		console.log(obj);

		axios({
			method: 'post',
			url: url,
			headers: {
				'Content-Type': 'application/x-www-form-urlencoded',
			},
			data: obj,
		});
		alert('Request sent');
	}
}

function getSidebar() {
	const key = Object.keys(sessionStorage).find(key => /sidebarData\d+/.test(key));
	const sidebarData = JSON.parse(sessionStorage.getItem(key));
	return sidebarData;
}