Greasy Fork is available in English.

Webgame - svetovy trh - prodej

Cena zbozi na trhu

// ==UserScript==
// @name         Webgame - svetovy trh - prodej
// @version      0.1.6.1
// @description  Cena zbozi na trhu
// @author       yS
// @match        *://*.webgame.cz/wg/index.php?p=svetovy_trh&s=trhposlat*
// @match        *://webgame.cz/wg/index.php?p=svetovy_trh&s=trhposlat*
// @match        *://*.webgame.cz/wg/index.php?p=svetovy_trh&s=techposlat*
// @match        *://webgame.cz/wg/index.php?p=svetovy_trh&s=techposlat*
// @icon         https://www.google.com/s2/favicons?sz=64&domain=webgame.cz
// @namespace https://greasyfork.org/users/1005892
// ==/UserScript==

const is_trh = document.URL.indexOf("trhposlat") != -1;

const market_prices = new Map();

let table_selling;
const div = document.getElementById("icontent");
const tables = div.getElementsByTagName("table");
table_selling = tables[1];

let skipped_rows = [];
let gold_offset = 0;

let th = document.createElement("th");
th.innerHTML = "Aktualní cena";

let rows = table_selling.rows;
if (rows[0].children[2].innerText == "Začátek prodeje") {
    gold_offset = 1;
}
rows[0].insertBefore(th, rows[0].children[4+gold_offset]);

for (let i = 1; i < rows.length-2; i++) {
    const cell = document.createElement("td");
    cell.innerHTML = '-';
    cell.classList.add("msaleprice");
    rows[i].insertBefore(cell, rows[i].children[6+gold_offset]);
    skipped_rows.push(rows[i]);
}

for (let i = rows.length-2; i < rows.length; i++) {
    rows[i].children[0].colSpan = 11;
}

let urls = ["index.php?p=svetovy_trh&s=techkoupit", "index.php?p=svetovy_trh&s=trhkoupit"];
httpGetAsync(urls[0], updateSkippedRows);
httpGetAsync(urls[1], updateSkippedRows);



//////////////////////////////////////////
//              FUNCTIONS               //
//////////////////////////////////////////

function getMarketTable(doc) {
    const div = doc.getElementById("icontent");
    const tables = div.getElementsByTagName("table");

    return tables[gold_offset];
}

function getValues(name) {
    name = formatCommodityName(name);
    if (name == null) {
        return null;
    }

    return market_prices.get(name);
}

function getMarketPrices(table_market) {
    for (let i=1; i < table_market.rows.length-1; i++) {
        const row = table_market.rows[i];
        const name = row.children[0].innerText;

        const value = parseInt(row.children[4].innerText.replaceAll(" ", ""));
        const amount = row.children[2].innerText.replaceAll(" ", "");


        //market_prices.set(name, value);
        market_prices.set(name, [value, amount]);
    }
}

// Formats the name of the commodity
// returns null if in the wrong market window (doesn't have price for it)
function formatCommodityName(name) {
    name = name.split("Technologie ");
    if (name.length == 2) {
        name = name[1];
    } else {
        name = name[0];
    }

    name = name.split("(");
    name = name[0];

    name = name.replace(":", "");

    return name;
}

function comparePrices(price1, price2) {
    price1 = price1;
    price2 = price2.slice(0, -1);
    return price1 < price2;
}

function updateSkippedRows(response) {
    let responseText = response.responseText;
    let dom = new DOMParser().parseFromString(responseText, "text/html");
    let table_market = getMarketTable(dom);
    getMarketPrices(table_market);

    for (let i = 0; i < skipped_rows.length; i++) {
        let values = getValues(skipped_rows[i].children[4+gold_offset].innerText);
        if (values == null) {
            continue;
        }
        let price = values[0];
        let amount = parseInt(values[1]);
        const market_cheaper = comparePrices(price, skipped_rows[i].children[7+gold_offset].innerText.replaceAll(" ", ""));

        const cell = skipped_rows[i].children[6+gold_offset];
        cell.innerText = "";

        let div = document.createElement("div");
        div.innerText = price + "$";
        cell.append(div);

        div = document.createElement("div");
        div.innerText = "(" + new Intl.NumberFormat("de-DE").format(amount) + ")";
        cell.append(div);

        if (price != "-" && market_cheaper) {
            cell.style = "color: red;";
        }
    }
}


function httpGetAsync(theUrl, callback) {
    let xmlHttp = new XMLHttpRequest();
    xmlHttp.onreadystatechange = function() {
        if (xmlHttp.readyState == 4 && xmlHttp.status == 200) {
            callback(xmlHttp);
        }
    }
    xmlHttp.open("GET", theUrl, true); // true for asynchronous
    xmlHttp.send(null);
}