HWM_DisableArtsDiscarding

Удаление кнопок выброса артов + более внятное окно подтверждения

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

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

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

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

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

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

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

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

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

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

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

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

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

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

// ==UserScript==
// @name         HWM_DisableArtsDiscarding
// @namespace    Небылица
// @version      1.1
// @description  Удаление кнопок выброса артов + более внятное окно подтверждения
// @author       Небылица
// @include      /^https{0,1}:\/\/((www|qrator)\.heroeswm\.ru|178\.248\.235\.15)\/inventory\.php/
// @grant        GM_setValue
// @grant        GM_getValue
// ==/UserScript==

(function() {
    "use strict";

    // Вспомогательные функции
    function freezeConfirmationButton(time){ // Ставит заморозку на time мс на кнопку подтверждения после её появления (3 секунды по умолчанию)
        if (!time){time = 3000};

        var discardingAs = document.querySelectorAll("a[href='/inventory.php'][onclick^='javascript: inv_sweet_']"),
            confirmationButton;
        discardingAs.forEach(function(a){
            a.parentNode.addEventListener("click", function(){
                confirmationButton = document.querySelector("button[class='confirm']");
                confirmationButton.disabled = true;
                window.setTimeout(function(){confirmationButton.disabled = false;}, time);
            });
        });
    }
    function setDiscardingButtonsVisibility(visibility){ // Прячет (false), либо показывает (true) кнопки выброса на текущей вкладке
        var discardingAs = document.querySelectorAll("a[href='/inventory.php'][onclick^='javascript: inv_sweet_']");
        discardingAs.forEach(function(a){
            a.parentNode.hidden = !visibility;
        });
    }
    function setupVisibilityResetting(){ // Привязка перевыставления видимости кнопок выброса и заморозки кнопки подтверждения к смене выборки артов (по актуальным на момент смены настройкам))
        var artsDiv = document.getElementById("test"),
            observer = new MutationObserver(function(mutations){
                mutations.forEach(function(mutation){
                    setDiscardingButtonsVisibility(GM_getValue("enableDiscarding" + plIdSubKey));
                    freezeConfirmationButton();
                });
            }),
            config = {characterData: false, attributes: false, childList: true, subtree: false};
        observer.observe(artsDiv, config);
    }
    function setEnableDiscardingCheckboxTitle(enableDiscardingCheckbox, checked){ // Выставляет подпись для чекбокса настройки в зависимости от переданного состояния
        var title = (checked) ? "Отключить выброс артов" : "Включить выброс артов";
        enableDiscardingCheckbox.title = title;
    }
    function insertAfter(newNode, referenceNode){ // Вставка newNode после referenceNode
        referenceNode.parentNode.insertBefore(newNode, referenceNode.nextSibling);
    }
    String.prototype.replaceLast = function(searchValue, newValue){ // Заменяет последнее вхождение searchValue в строке на newValue
        var lastIndex = this.lastIndexOf(searchValue);
        if (lastIndex !== -1){
            return this.substring(0, lastIndex) + newValue + this.substring(lastIndex + searchValue.length, this.length);
        }
    };
    //


    // получаем id текущего персонажа и кусок ключа по нему
    var plId = document.querySelector("li > a[href^='pl_hunter_stat.php']").getAttribute("href").split("id=")[1],
        plIdSubKey = "|#" + plId;

    // дефолтно убираем кнопки выброса
    if (GM_getValue("enableDiscarding" + plIdSubKey) === undefined){GM_setValue("enableDiscarding" + plIdSubKey, false);}

    // показываем/отключаем кнопки в зависимости от настройки
    setDiscardingButtonsVisibility(GM_getValue("enableDiscarding" + plIdSubKey));

    // привязываем перевыставление видимости кнопок выброса и заморозку кнопки подтверждения к смене выборки артов (по актуальным на момент смены настройкам)
    setupVisibilityResetting();

    // отрисовываем чекбокс настройки
    var enableDiscardingCheckboxSpan = document.createElement("span"),
        enableDiscardingCheckbox = document.createElement("input"),
        questionMarkElement = document.querySelector("a[title^='(доступно']");

    enableDiscardingCheckboxSpan.setAttribute("id", "enableDiscardingCheckboxSpan");
    enableDiscardingCheckboxSpan.innerHTML = " ";

    enableDiscardingCheckbox.setAttribute("type", "checkbox");
    enableDiscardingCheckbox.setAttribute("id", "enableDiscardingCheckbox");
    enableDiscardingCheckbox.style.margin = "0px";

    // выставляем галочку и подпись
    if (GM_getValue("enableDiscarding" + plIdSubKey)){
        enableDiscardingCheckbox.checked = true;
    }
    setEnableDiscardingCheckboxTitle(enableDiscardingCheckbox, enableDiscardingCheckbox.checked);

    // вставляем в страницу
    enableDiscardingCheckboxSpan.appendChild(enableDiscardingCheckbox);
    insertAfter(enableDiscardingCheckboxSpan, questionMarkElement);

    // записываем и применяем настройку по изменению чекбокса (переспрашиваем, если включили выброс)
    enableDiscardingCheckbox.onchange = function(){
        if (enableDiscardingCheckbox.checked){
            if (confirm("Включить выброс артов?")){
                GM_setValue("enableDiscarding" + plIdSubKey, true);
                setEnableDiscardingCheckboxTitle(enableDiscardingCheckbox, true);
                setDiscardingButtonsVisibility(true);
            } else{
                enableDiscardingCheckbox.checked = false;
            }
        } else{
            GM_setValue("enableDiscarding" + plIdSubKey, false);
            setEnableDiscardingCheckboxTitle(enableDiscardingCheckbox, false);
            setDiscardingButtonsVisibility(false);
        }
    };


    // перерисовываем окошко подтверждения
    var changeConfirmationAlertsScript = document.createElement("script");
    changeConfirmationAlertsScript.type = "text/javascript";

    changeConfirmationAlertsScript.innerHTML =
        "var drop_str = '';" +
        "function inv_sweet_prompt(del_name, link){" +
        "    return swal(" +
        "        {" +
        "        title: del_name.replaceLast('[', '[<text style=\"color: red;\"\>').replaceLast(']', '</text>]')," +
        "        text: 'Выбросить этот предмет? Введите ник для подтверждения'," +
        "        type: 'input'," +
        "        confirmButtonColor: '#DD6B55'," +
        "        cancelButtonText: 'Отмена'," +
        "        showCancelButton: true," +
        "        closeOnConfirm: false," +
        "        html: true," +
        "        inputPlaceholder: 'Введите ваш ник'" +
        "        }," +
        "        function(inputValue){" +
        "            if (inputValue === false) return false;" +
        "            if (inputValue === '') {swal.showInputError('Чтобы выбросить предмет, вы должны ввести ник!');return false}" +
        "            if (inputValue.toLowerCase() != player_nick) {swal.showInputError('Ник не совпадает');return false}" +
        "            swal.close(); window.location.href = 'inventory.php?sell=' + link + '&sign=' + sign;" +
        "        }" +
        "    );" +
        "}" +

        "function inv_sweet_confirm(del_name, link){" +
        "     return swal(" +
        "        {" +
        "        title: del_name.replaceLast('[', '[<text style=\"color: red;\"\>').replaceLast(']', '</text>]') + '</b>'," +
        "        text: 'Выбросить этот предмет?'," +
        "        type: 'warning'," +
        "        confirmButtonColor: '#DD6B55'," +
        "        cancelButtonText: 'Отмена'," +
        "        confirmButtonText: 'Да, выбросить!'," +
        "        showCancelButton: true," +
        "        closeOnConfirm: false," +
        "        html: true" +
        "        }," +
        "        function(){" +
        "            swal.close(); window.location.href = 'inventory.php?sell=' + link + '&sign=' + sign;" +
        "        }" +
        "    );" +
        "}";

    document.getElementsByTagName("body")[0].appendChild(changeConfirmationAlertsScript);

    // ставим фриз на кнопку подтверждения
    freezeConfirmationButton();
})();