GazelleGames pet leveling info

Adds pet leveling info to your own profile page

目前為 2022-02-05 提交的版本,檢視 最新版本

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

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

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

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

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

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

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

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

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

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

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

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

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

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

// ==UserScript==
// @name        GazelleGames pet leveling info
// @namespace   v3rrrr82xk1c96vvo1c6
// @match       https://gazellegames.net/user.php?id=*
// @grant       GM.getValue
// @grant       GM.setValue
// @grant       GM.deleteValue
// @version     1.2.1
// @description Adds pet leveling info to your own profile page
// @run-at      document-start
// @inject-into content
// @license     MIT
// ==/UserScript==

(async function () {
	"use strict";


	const theirUserID = new URLSearchParams(location.search).get("id");
	const ownUserID = await GM.getValue("you").then((yourID) => {
		// Own ID is cached
		if (yourID) {
			return yourID;
		}

		// Not cached, get it from page once it's loaded
		return new Promise((resolve) => {
			window.addEventListener("DOMContentLoaded", () => {
				yourID = new URLSearchParams(document.body.querySelector("#nav_userinfo a.username").search).get("id");
				GM.setValue("you", yourID);
				resolve(yourID);
			});
		});
	});


	// Only runs on your own user page
	if (theirUserID !== ownUserID) {
		return;
	}


	let apiKey = await GM.getValue("apiKey");

	if (!apiKey) {
		if (!(apiKey = prompt("Please enter an API key with the 'Items' permission to use this script."))) {
			return;
		}
		GM.setValue("apiKey", apiKey);
	}


	const endpoint = `https://${location.host}/api.php?request=items&include_info=true&type=users_equipped`;
	const options = {
		method: "GET",
		mode: "same-origin",
		credentials: "omit",
		redirect: "error",
		referrerPolicy: "no-referrer",
		headers: {
			"X-API-Key": apiKey
		}
	};


	const equipment = await (await fetch(endpoint, options)).json();

	if (equipment.status !== "success") {
		if (equipment.status === 401) {
			GM.deleteValue("apiKey");
		}
		return;
	}


	const pets = [];

	for (const equip of equipment.response) {
		const type = equip.item.equipType;

		if (type && String(type) === "18" && equip.experience && equip.experience > 0) {
			pets.push({
				name: equip.item.name,
				xp: parseInt(equip.experience, 10),
				lv: parseInt(equip.level, 10),
				id: String(equip.itemid)
			});
		}
	}

	if (!pets.length) return;


	const box = document.createElement("div");
	const innerBox = document.createElement("div");
	const list = document.createElement("ul");

	box.className = "box_personal_history";
	innerBox.className = "box";
	list.className = "stats nobullet";

	innerBox.innerHTML = '<div class="head colhead_dark">Pet Leveling</div>';
	innerBox.append(list);
	box.append(innerBox);


	function totalXP(lv) {
		return Math.ceil((lv * lv * 625) / 9);
	}


	for (const pet of pets) {
		const li = document.createElement("li");
		const li2 = document.createElement("li");
		const li3 = document.createElement("li");
		const shopLink = document.createElement("a");
		const strong = document.createElement("strong");

		li2.style.paddingLeft = li3.style.paddingLeft = "10px";


		shopLink.href = `/shop.php?ItemID=${pet.id}`;
		shopLink.title = "Shop for this pet";

		const nextLevel = pet.lv + 1;
		const missingXP = totalXP(nextLevel) - pet.xp;
		const days = Math.floor(missingXP / 24);
		const hours = missingXP % 24;

		let timeString = "";
		if (days) {
			const s = (days === 1) ? "" : "s";
			timeString += `${days} day${s}`;
		}
		if (hours) {
			const s = (hours === 1) ? "" : "s";
			timeString += ` ${hours} hour${s}`;
		} else if (!timeString) {
			timeString = "0 hours"; // ???
		}

		strong.append(pet.name);
		shopLink.append(strong);
		li.append(shopLink);
		li2.append(timeString);
		li3.append(`Level ${pet.lv} → ${nextLevel}`);

		list.append(li, li2, li3);
	}


	function insert() {
		document.getElementsByName("user_info")[0]?.insertAdjacentElement("afterend", box);
	}

	insert();
	if (!box.isConnected) {
		window.addEventListener("DOMContentLoaded", insert);
	}
})();