Greasy Fork is available in English.

sell_items_from_inventory

allows you to sell items from inventory

// ==UserScript==
// @name         sell_items_from_inventory
// @namespace    http://tampermonkey.net/
// @version      2.1
// @description  allows you to sell items from inventory
// @author       Лосось
// @match        https://my.lordswm.com/inventory.php*
// @match        https://my.lordswm.com/auction_new_lot.php*
// @match        https://www.heroeswm.ru/inventory.php*
// @match        https://www.heroeswm.ru/auction_new_lot.php*
// @include      https://my.lordswm.com/inventory.php*
// @include      https://my.lordswm.com/auction_new_lot.php*
// @include      https://www.heroeswm.ru/inventory.php*
// @include      https://www.heroeswm.ru/auction_new_lot.php*
// @icon         https://www.google.com/s2/favicons?sz=64&domain=lordswm.com
// @grant        none
// @license      MIT
// ==/UserScript==

(function() {
    'use strict';

    const createEl = (el, style, innerText, placeholder, type) => {
        let element = document.createElement(el);
        if (style) element.style = style;
        if (innerText) element.innerText = innerText;
        if (placeholder) element.placeholder = placeholder;
        if (type) element.type = type;
        return element;
    }

    let links = ['https://my.lordswm.com', 'https://www.heroeswm.ru'];
    let link = location.href.slice(0,22) === 'https://my.lordswm.com' ? links[0] : links[1];

    let linkToMarket = '';
    let query = '&sort=204&type=0&snew=';

    const fetchXml = (link, callback) => {
        if (!link) return;
        const xhr = new XMLHttpRequest();
        xhr.open('get', link);
        xhr.setRequestHeader('Content-type', 'text/html; charset=windows-1251');
        if (xhr.overrideMimeType) {
            xhr.overrideMimeType('text/html; charset=windows-1251');
        }

        xhr.addEventListener('load', () => {
            let parser = new DOMParser();
            let doc = parser.parseFromString(xhr.responseText, "text/html");
            callback(doc);
        })
        xhr.send();
    }

    const getPrices = (doc) => {
        let priceBlock = doc.getElementsByClassName('wbwhite')[0];
        let price = Number(priceBlock.getElementsByTagName('table')[3].getElementsByTagName('td')[1].innerText.replace(',', ''));
        let strength = Number(priceBlock.getElementsByTagName('table')[1].getElementsByTagName('td')[1].innerText.match(/Прочность: .+/gi)[0].match(/\d+/gi)[0]);
        let priceOneBattle = Math.round(price / strength);

        let artName = JSON.parse(localStorage.getItem('artNameLS'));
        let isIntact = artName.match(/\d+\/\d+/)[0].split('/');
        isIntact = Number(isIntact[0]) === Number(isIntact[1]);

        let myStrength = artName.match(/\d+\/\d+/)[0].split('/');
        myStrength = Number(myStrength[0]);
        let myPrice = Math.round(myStrength * priceOneBattle);

        let priceInput = document.getElementById('anl_price');

        if (isIntact && price > myPrice) {
            priceInput.value = Math.round(myPrice * 0.95)
        } else if (isIntact) {
            priceInput.value = Math.round(price * 0.95)
        } else if (!isIntact) {
            priceInput.value = Math.round(myPrice * 0.95)
        }

        let center = document.getElementsByTagName('center')[1];
        let infoBlock = createEl('div', 'width: 900px');
        infoBlock.innerHTML = priceBlock.innerHTML;
        center.appendChild(infoBlock);
    }

    const getLinkToMarket = (doc) => {
        let block = doc.getElementsByClassName('art_info_left_block')[0];
        block = [...block.getElementsByTagName('a')];
        let link = block.filter(el => el.href.includes('auction'))[0];
        if (link) linkToMarket = link.href;

        let artName = JSON.parse(localStorage.getItem('artNameLS'));
        let isIntact = artName.match(/\d+\/\d+/)[0].split('/');
        isIntact = Number(isIntact[0]) === Number(isIntact[1]);
        isIntact ? fetchXml(linkToMarket + query + 1, getPrices) : fetchXml(linkToMarket + query + 0, getPrices);
    }

    if (location.href === `${link}/inventory.php`) {

        const refreshTabs = () => {
            let arts = [...document.getElementById('inventory_block').children];
            arts = arts.filter(el => !el.classList.contains('inventory_item_div_empty'));
            arts.map(el => {
                el.addEventListener('click', () => {
                    let menu = document.getElementById('inv_menu');
                    let buttons = document.getElementById('inv_item_buttons');

                    if (buttons.lastChild.id === 'sellBtn') buttons.lastChild.remove();
                    let sellBtn = createEl('button', 'border-radius: 100%; border: 2px solid #dbb681; background-color: rgba(105, 62, 15, 0.651)', '');
                    let sellBtnImg = createEl('img');
                    sellBtnImg.src = 'https://img.icons8.com/?size=80&id=cChy69pWtl7N&format=png';
                    sellBtnImg.classList.add('inv_item_select_img', 'show_hint');
                    sellBtn.title = 'sell on market';
                    sellBtn.appendChild(sellBtnImg);
                    sellBtn.classList.add('inv_item_select', 'inv_item_select_img');
                    sellBtn.id = 'sellBtn';

                    let artInfoLink = document.getElementById('inv_item_select_info_a').href;
                    localStorage.setItem('artInfoLink', JSON.stringify(artInfoLink));

                    let artName = document.getElementById('inv_menu_art_name').firstChild.innerText;
                    artName = artName.replace('[', '');
                    artName = artName.replace(']', '');

                    sellBtn.addEventListener('click', () => {
                        localStorage.setItem('artNameLS', JSON.stringify(artName));
                        window.open(`${link}/auction_new_lot.php`, "_blank");
                    })
                    buttons.appendChild(sellBtn);
                })
            });
        }

        refreshTabs();

        let artsTabs = [...document.getElementsByClassName('filter_tabs_block')[0].children];
        artsTabs.map(el => el.addEventListener('click', refreshTabs))

    }

    if (location.href === `${link}/auction_new_lot.php`) {
        let select = document.getElementById('sel');
        let options = [...select.options];

        let artName = JSON.parse(localStorage.getItem('artNameLS'));
        select[options.findIndex(el => el.innerText.includes(artName))].selected = true;

        let amount = document.getElementById('anl_count');
        amount.value = '1';

        let time = document.getElementById('duration');
        time.lastChild.selected = true;

        fetchXml(JSON.parse(localStorage.getItem('artInfoLink')), getLinkToMarket);
    }

})();