rainbowcorn / 仙人指公测版

// ==UserScript==
// @name         仙人指公测版
// @namespace    https://openuserjs.org/users/rainbowcorn
// @version      0.0.2
// @description  仙人南山公测版辅助工具
// @author       rainbowcorn
// @match        https://xrns.ngame.io/
// @grant        none
// @licence      MIT
// @copyright    2019, rainbowcorn (https://openuserjs.org/users/rainbowcorn)
// @require      http://code.jquery.com/jquery-3.4.1.min.js
// ==/UserScript==
(function() {
    'use strict';

    const host = 'https://xrns.ngame.io/api/',
          log = window.addLogFunc,
          jq = jQuery.noConflict(),
          toolBox = document.createElement('div'),
          btnMergeSoulPieces = document.createElement('button');

    function bulkSell (id, name, count, cb = function () {}) {
        let data = {
            sell_json: JSON.stringify([
                {id: id, count: count, name: name}
            ]),
            sell_type: 'zhd' //zht = 杂货店
        };
        jq.post(host + 'sellGoods', data, res => {
            if (res.code === 200) {
                log('<p class="loginfo">向杂货店出售【<span style="text-shadow:1px 1px px dimgray;color:dimgray;">' + name + '</span>】x' + count + '</p>');
            } else {
                log(res.msg);
            }
            cb(res.data);
        });
    }

    function createToolBox () {
        let infoDiv = document.querySelector('.divinfo'),
            logoutBtn = infoDiv.querySelector('a[onclick="loginOutFunc()"]'),
            hr = document.createElement('hr'),
            title = document.createElement('h6'),
            toolBoxContainer = document.createElement('div');

        title.innerHTML = '快捷工具箱';
        title.classList.add('hxx');
        toolBox.id = 'tool-box';
        toolBoxContainer.classList.add('tool-box');
        toolBoxContainer.appendChild(title);
        toolBoxContainer.appendChild(toolBox);
        btnMergeSoulPieces.id = 'merge-soul-pieces';
        btnMergeSoulPieces.innerHTML = '一键合成神龙的信仰';
        btnMergeSoulPieces.style.backgroundColor = '#fff';
        btnMergeSoulPieces.style.borderRadius = '3px';
        btnMergeSoulPieces.style.cursor = 'pointer';
        btnMergeSoulPieces.style.fontSize = '13px';
        btnMergeSoulPieces.style.outline = 'none';
        btnMergeSoulPieces.style.width = '100%';
        btnMergeSoulPieces.addEventListener('click', function () {
            mergeSoulPieces(btnMergeSoulPieces);
        });

        toolBox.appendChild(btnMergeSoulPieces);

        infoDiv.insertBefore(toolBoxContainer, logoutBtn);
        infoDiv.insertBefore(hr, logoutBtn);
    }

    function initialise () {
        createToolBox();

        const good_list = document.getElementById('goods-list');
        const config = {attributes: true, childList: true, subtree: true };
        const callback = function(mutations, observer) {
            for (let mutation of mutations) {
                if (mutation.type === 'childList') {
                    if (mutation.addedNodes.length) {
                        mutation.addedNodes.forEach(function (added_node) {
                            // Must be the ".goods-block" element and must be in stock
                            if (typeof added_node.classList !== 'undefined' && added_node.classList.contains('goods-block') && added_node.querySelector('[id^="count-"]') !== null) {
                                let a = document.createElement('a'),
                                    count = parseInt(added_node.querySelector('[id^="count-"]').innerText),
                                    id = added_node.id.replace('div-', ''),
                                    name = added_node.querySelector('a[title]').title.split('/')[0];
                                a.innerHTML = '出售(' + count + ')';
                                a.style.cursor = 'pointer';
                                a.style.marginLeft = '10px';
                                added_node.querySelector('.prompt-box .goods-sub > span').append(a);

                                if (name.indexOf('残魂碎片') > -1) {
                                    updateMergeSoulPiecesButton(id, name, count);
                                }

                                a.addEventListener('click', function () {
                                    bulkSell(id, name, count, function (user) {
                                        added_node.remove();
                                        updateCopper(user.game_copper);
                                    });
                                });
                            }
                        });
                    }
                }
            }
        };
        const observer = new MutationObserver(callback);
        observer.observe(good_list, config);
    }

    function mergeSoulPieces (btn) {
        let data = JSON.parse(btn.getAttribute('data')) || {},
            pieces = [];
        for (let name in data) {
            pieces.push({
                count: 1,
                id: data[name].id,
                name: name
            });
        }

        jq.post(host + 'makeGoods', {
            sell_json: JSON.stringify(pieces),
            sell_type: 'make' //make = 合成
        }, res => {
            if (res.code === 200) {
                log('<p class="loginfo">成功合成【<span style="text-shadow:1px 1px 15px darkgoldenrod;color:darkgoldenrod;background-color:blanchedalmond;">神龙的信仰</span>】x1</p>');
                getUserGoods();
            } else {
                log(res.msg);
            }
        });
    }

    function updateCopper (copper) {
        let copperHolder = document.getElementById('header-zy');
        if (copperHolder !== null) {
            copperHolder.innerText = Math.round(copper);
        }
    }

    function updateMergeSoulPiecesButton (id, name, count) {
        if (typeof count === 'undefined') {
            console.log(name);
        }
        let data = JSON.parse(btnMergeSoulPieces.getAttribute('data')) || {};

        if (data.hasOwnProperty(name)) {
            data[name].count = count;
            data[name].id = id;
        } else {
            data[name] = {
                count: count,
                id: id
            };
        }

        btnMergeSoulPieces.setAttribute('data', JSON.stringify(data));
    }

    initialise();
})();