WoD Equipment Tips

Display minimum equipment requirements!

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

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

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

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

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

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

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

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

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

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

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

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

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

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

// ==UserScript==
// @name         WoD Equipment Tips
// @namespace    https://www.wannaexpresso.com/
// @version      0.3
// @description  Display minimum equipment requirements!
// @author       DotIN13
// @include      http://*.world-of-dungeons.org/wod/spiel/hero/items.php*view=gear*
// @include      https://*.wannaexpresso.com/wod/spiel/hero/items.php*view=gear*
// @grant        none
// ==/UserScript==

// todo:
// add error notification

(function() {
    'use strict';

    var reqList = [];
    var hasTooltip = [];
    var xhr = [];
    var progress = 1;
    var equipSel;

    // Sort Equipment Requirements //////////////////////////////////////////////

    function sortReq () {
        var sortList = [{item:[],req:"力量至少为0"},{item:[],req:"体质至少为0"},{item:[],req:"智力至少为0"},{item:[],req:"灵巧至少为0"},{item:[],req:"魅力至少为0"},{item:[],req:"敏捷至少为0"},{item:[],req:"感知至少为0"},{item:[],req:"意志至少为0"}];
        for (var i = 0; i < equipSel.length; i++){
            if (reqList[i] != "") {
                for (var j = 0; j < reqList[i].length; j++){
                    var isMatch = false;
                    for (var k = 0; k < sortList.length; k++){
                        if (reqList[i][j].replace(/\d*/g,"") == sortList[k].req.replace(/\d*/g,"")){
                            //console.log(reqList[i][j] + "match" + sortList[k].req);
                            var requestVal = parseInt(reqList[i][j].replace(/(<([^>]+)>)/ig,"").match(/[^0-9]*(\d*)/)[1]);
                            var sortVal = parseInt(sortList[k].req.replace(/(<([^>]+)>)/ig,"").match(/[^0-9]*(\d*)/)[1]);
                            if (requestVal > sortVal) {
                                sortList[k].req = reqList[i][j];
                                sortList[k].item = [equipSel[i].options[equipSel[i].selectedIndex].innerText];
                            } else if (requestVal == sortVal) {
                                sortList[k].item.push(equipSel[i].options[equipSel[i].selectedIndex].innerText);
                            }
                            isMatch = true;
                        }
                    }
                    if (!isMatch) sortList.push({item:[equipSel[i].options[equipSel[i].selectedIndex].innerText], req:reqList[i][j]});
                }
            }
        }
        //console.log("done");
        var outputHTML = "<tr class = 'header'><th>装备</th><th>要求</th></tr>";
        for(var m = 0; m < sortList.length; m++) {
            if(m % 2){
                outputHTML += "<tr class = 'row1'><td>" + sortList[m].item.join("<br>") + "</td><td>" + sortList[m].req + "</td></tr>\n";
            }else{
                outputHTML += "<tr class = 'row0'><td>" + sortList[m].item.join("<br>") + "</td><td>" + sortList[m].req + "</td></tr>\n";
            }
        }
        outputTable.innerHTML = outputHTML;
    }

    // XMLrequest /////////////////////////////////////////////////////////////

    window.XMLrequest = function(URL, i, length){
        xhr[i] = new XMLHttpRequest();
        xhr[i].onreadystatechange = function() {
            if (this.readyState == 4 && this.status == 200) {
                outputDiv.innerHTML = "正在加载第" + progress +"件物品";
                var reqTitle = $("#details>table>tbody>tr>td:contains('装备要求')",this.responseXML)[0];
                if(reqTitle != undefined){
                    var req = reqTitle.parentElement.lastElementChild.innerHTML.trim();
                    //console.log(req);
                    if (!hasTooltip[i]){
                        wodToolTip(equipSel[i].parentElement,req);
                        hasTooltip[i] = true;
                    }else{
                        wodToolTipContent[equipSel[i].parentElement.id] = req;
                    }
                    reqList[i] = req.replace("<i>无任何需求</i>","").split("<br>");
                }else{
                    reqList[i] = [""];
                }
                progress++;
                if (progress == length + 1) {
                    outputDiv.innerHTML = "加载完成";
                    progress = 1;
                    //console.log(reqList);
                    sortReq();
                }
            }
        };
        xhr[i].open("GET", URL);
        xhr[i].responseType = "document";
        xhr[i].send();
    }

    // Add WoD Tip to Equipments //////////////////////////////////////////////

    window.equipmentTip = function(){
        equipSel = document.querySelectorAll("form table select");
        if (equipSel[0] != undefined) {
            var id, req, i;
            for (i = 0; i < equipSel.length; i++) {
                id = equipSel[i].options[equipSel[i].selectedIndex].value == "0" ? -equipSel[i].firstElementChild.value : equipSel[i].options[equipSel[i].selectedIndex].value;
                var URL = "/wod/spiel/hero/item.php?item_instance_id=" + id;
                equipSel[i].setAttribute("onchange", "document.getElementById('outputDiv').innerHTML = '正在加载第1件物品'; var id = this.value == '0' ? -this.firstElementChild.value : this.value; XMLrequest('/wod/spiel/hero/item.php?item_instance_id=' + id, " + i + ", 1);");
                XMLrequest(URL, i, equipSel.length);
            }
        }else{alert("请先清包!");}
    }

    // Frontend ////////////////////////////////////////////////////////////

    //let calcButton = document.createElement("input");
    //calcButton.id = "reqCalcButton";
    //calcButton.type = "button";
    //calcButton.setAttribute("class","button clickable");
    //calcButton.value = "计算最低装备要求";

    let tipButton = document.createElement("input");
    tipButton.id = "reqTipButton";
    tipButton.type = "button";
    tipButton.setAttribute("class","button clickable");
    tipButton.value = "拉取装备要求";
    tipButton.setAttribute("onclick","equipmentTip();");

    let outputDiv = document.createElement("div");
    outputDiv.id = "outputDiv";

    let outputTable = document.createElement("table");
    outputTable.id = "outputTable";
    outputTable.setAttribute("class","content_table");

    var equipTable = document.querySelector("form table");
    //equipTable.parentNode.insertBefore(calcButton, equipTable.nextElementSibling);
    equipTable.parentNode.insertBefore(outputTable, equipTable.nextElementSibling);
    equipTable.parentNode.insertBefore(outputDiv, equipTable.nextElementSibling);
    equipTable.parentNode.insertBefore(tipButton, equipTable.nextElementSibling);
    var pageBody = document.querySelector("#gadgettable-center-gadgets");
})();