GGn Inventory Prices

Show prices in GGn inventory

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         GGn Inventory Prices
// @namespace    https://gazellegames.net/
// @version      1.0
// @description  Show prices in GGn inventory
// @author       monkeys
// @license      MIT
// @match        https://gazellegames.net/user.php?*action=inventory*
// @icon         https://gazellegames.net/favicon.ico
// @homepage     https://greasyfork.org/en/scripts/554362-ggn-inventory-prices
// @grant        GM.getValue
// @grant        GM.setValue
// @grant        GM.deleteValue
// ==/UserScript==

(async function() {
  'use strict';
  var coins_img = document.createElement('img');
  coins_img.src = '/static/styles/game_room/images/icons/coins.png'
  const user_id = new URLSearchParams(location.search).get("userid");
  var api_key = await GM.getValue("api_key");

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

  const url = `https://gazellegames.net/api.php?request=items&type=inventory&include_info=true&userid=${user_id}`;
  const options = {
    method: "GET",
    mode: "same-origin",
    credentials: "omit",
    redirect: "error",
    referrerPolicy: "no-referrer",
    headers: {
      "X-API-Key": api_key
    }
  };
  var inventory = await (await fetch(url, options)).json();
  if (inventory.status !== "success") {
    if (inventory.status === 401) {
      GM.deleteValue("api_key");
    }
    return;
  }
  for (const item_obj of inventory.response) {
    var item = item_obj.item;
    var amount_elm = document.getElementById (`amount_${item.id}`);
    if (!amount_elm) { continue; }
    var costs_elm = document.createElement('p');
    costs_elm.innerText = `Cost: ${Number(item.gold).toLocaleString()}, Total Value: ${(item.gold * item_obj.amount).toLocaleString()}`;
    // TODO: Can't get this coins img to actually show up
    // costs_elm.appendChild(coins_img);

    var shop_elm = document.createElement('p');
    shop_elm.innerText = `${item.infStock ? 'In' : 'Not in'} shop`;
    amount_elm.parentNode.insertBefore(shop_elm, amount_elm.nextSibling);
    amount_elm.parentNode.insertBefore(costs_elm, amount_elm.nextSibling);
  }
})();