MyKirito Helper

讓你可以更星爆

目前為 2020-06-18 提交的版本,檢視 最新版本

// ==UserScript==
// @name         MyKirito Helper
// @namespace    https://greasyfork.org/users/600262
// @version      0.2.4
// @description  讓你可以更星爆
// @author       ganmaRRRRR
// @match        https://mykirito.com/*
// @require      https://cdn.jsdelivr.net/gh/CoeJoder/[email protected]/waitForKeyElements.js
// @require      https://unpkg.com/@popperjs/core@2
// @require      https://unpkg.com/tippy.js@6
// @grant        none
// @run-at       document-start
// ==/UserScript==
const pkWinTable = [[25, 30, 32, 35, 50], [28, 45, 59, 54, 68], [62, 85, 97, 103, 133], [81, 120, 130, 140, 150]];
const pkLoseTable = [25, 35, 50, 60];
const actTable = ["15~19", "15", "13~19", "18", "18", "15", "15", "430",	"820", "1610", "3200"];
const expTable = [0, 30, 60, 100, 150, 200, 250, 300, 370, 450, 500, 650, 800, 950, 1200, 1450, 1700, 1950, 2200, 2500, 2800, 3100, 3400, 3700, 4000, 4400, 4800, 5200, 5600, 6000, 6500, 7000, 7500, 8000, 8500, 9100, 9700, 10300, 11000, 11800, 12600, 13500, 14400, 15300, 16200, 17100, 18000, 19000, 20000, 21000, 23000, 25000, 27000, 29000, 31000, 33000, 35000, 37000, 39000, 41000, 44000, 47000, 50000, 53000, 56000, 59000, 62000, 65000, 68000, 71000];
const delay = 500; // 一些東西的延遲 (以防抓不到網頁元素)

(async function() {
    'use strict';

    // 抓Ajax Event
    function ajaxEventTrigger(event) {
        let ajaxEvent = new CustomEvent(event, { detail: this });
        window.dispatchEvent(ajaxEvent);
    }
    let oldXHR = window.XMLHttpRequest;
    function newXHR() {
        let realXHR = new oldXHR();
        // this指向window
        realXHR.addEventListener('readystatechange', function() { ajaxEventTrigger.call(this, 'ajaxReadyStateChange'); }, false);
        return realXHR;
    }
    window.XMLHttpRequest = newXHR;
    window.addEventListener('ajaxReadyStateChange', function (e) {
        // 處理成功的Request
        if (e.detail.readyState === oldXHR.DONE && e.detail.status === 200) {
            ajaxEventHandler(e.detail.responseURL, JSON.parse(e.detail.response));
        }
    });
    waitForKeyElements("div#root > nav", init);
})();

function init() {
    // Navbar置頂 (from https://greasyfork.org/zh-TW/scripts/404006-kirito-tools)
    let root = document.querySelector("div#root");
    let navbar = document.querySelector("div#root > nav");
    let navbarHeight = navbar.offsetHeight;
    root.style.paddingTop = `calc(${navbarHeight}px + 18px)`; // height + margin bottom
    navbar.style.position = "fixed";
    navbar.style.top = "0";

    // 加上選單按鈕
    let button = document.createElement("a");
    button.className = "sc-fzqAui eoGDzK";
    button.innerHTML = "MyKirito Helper";
    button.id = "mykirito_helper";
    navbar.insertBefore(button, navbar.lastChild);
    // button.addEventListener("click");

    // 要求通知權限
    if (Notification.permission === 'default' || Notification.permission === 'undefined') {
        Notification.requestPermission();
    }
}

function ajaxEventHandler(url, response) {
    let page = window.location.pathname.split("/")[1];
    switch (page) {
        case "":// 我的桐人
            myKirito(url, response);
            break;
        case "profile": // 別的桐人
            otherKirito(url, response);
            break;
    }
}

function myKirito(url, response) {
    let act = new URL(url).pathname.split("/");
    if (act[2] === "my-kirito") {
        switch (act[3]) {
            case undefined: // 自己的資料
                setTimeout(updateExpReq, delay);
                setTimeout(updateTeam, delay);
                setTimeout(addTooltip, delay);
                break;
            case "teammate": // 隊伍資料
                setTimeout(updateTeam, delay);
                break;
            case "doaction": // 行動
                response = response.myKirito;
                setTimeout(updateExpReq, delay);
                break;
        }
    }

    function updateExpReq() {
        let expReq = document.getElementById("exp_require");
        if (expReq == null) {
            waitForKeyElements("div#root table > tbody", addExpReq);
            expReq = document.getElementById("exp_require");
        }
        let lv = response.lv;
        let exp = response.exp;
        expReq.innerHTML = expTable[lv] - exp;

        function addExpReq(table) {
            let tr = table.lastChild.cloneNode(true);
            tr.childNodes[0].innerHTML = "距離升級";
            tr.childNodes[1].id = "exp_require";
            tr.removeChild(tr.lastChild);
            tr.removeChild(tr.lastChild);
            table.appendChild(tr);
        }
    }

    function updateTeam() {
        let teamRef = document.getElementById("team_ref");
        if (teamRef == null) {
            waitForKeyElements("div#root div > h3 ~ div ~ div ~ div", addTeamRef);
            teamRef = document.getElementById("team_ref");
        }
        let teammateUID = response.teammateUID;
        let teammateName = response.teammate;
        if (teammateName === undefined) {
            teammateName = document.querySelector("div ~ div > input").value;
        }
        if (teammateUID) {
            teamRef.href = `/profile/${teammateUID}`;
            teamRef.innerHTML = teammateName;
        }
        else {
            teamRef.href = "";
            teamRef.innerHTML = "";
        }

        function addTeamRef(team) {
            let a = document.createElement("a");
            a.id = "team_ref";
            team.appendChild(a);
        }
    }

    function addTooltip() {
        let actButton = document.querySelectorAll("button");
        try{
            for (let i = actButton.length - 11; i < actButton.length; i++) {
                tippy(actButton[i], {
                    delay: [250, 100],
                    content: `${actTable[i - (actButton.length - 11)]} 經驗值`,
                });
            }
        }
        catch(e) {}
    }
}

var vsData = new Array(2);
function otherKirito(url, response) {
    switch (url.split("/")[4]) {
        case "profile": // 別人的資料
            vsData[1] = response.profile;
            break;
        case "my-kirito":
            vsData[0] = response;
            break;
    }
    if (vsData[0] !== undefined && vsData[1] !== undefined) {
        showRattr();
        addTooltip();
        vsData = new Array(2);
    }

    function showRattr() {
        let btnDetail = document.querySelectorAll("button")[0];
        let btnCompare = document.querySelectorAll("button")[1];
        let rattrs = vsData[1].rattrs;
        let floor = vsData[1].floor;
        let achieveP = vsData[1].achievementPoints;
        btnSwitch();
        btnDetail.addEventListener('click', () => {setTimeout(btnSwitch, delay)});
        btnCompare.addEventListener('click', () => {setTimeout(btnSwitch, delay)});

        function btnSwitch() {
            // 詳細資料
            if (btnDetail.disabled) {
                let table = document.querySelector("div#root tbody");
                let count = 3;
                for (let k in rattrs) {
                    count++;
                    if (rattrs[k] === 0) {
                        continue;
                    }
                    let r = document.createElement("span");
                    r.className = "sc-fzoLsD fYZyZu";
                    r.innerHTML = ` (+${rattrs[k]})`;
                    table.childNodes[count].childNodes[1].appendChild(r);
                }

                // 一些有的沒的
                let tr = table.lastChild.cloneNode(true);
                tr.childNodes[0].innerHTML = "目前層數";
                tr.childNodes[1].innerHTML = floor;
                tr.childNodes[2].innerHTML = "成就點數";
                tr.childNodes[3].innerHTML = achieveP;
                table.appendChild(tr);
            }
            // 能力比對
            else {
                let table = document.querySelector("div#root table ~ table > tbody");
                let count = 5;
                for (let k in rattrs) {
                    count++;
                    if (rattrs[k] === 0) {
                        continue;
                    }
                    let r = document.createElement("span");
                    r.className = "sc-fzoLsD fYZyZu";
                    r.innerHTML = ` (+${rattrs[k]})`;
                    table.childNodes[count].childNodes[1].appendChild(r);
                }
            }
        }
    }

    function addTooltip() {
        let opponentLv = vsData[1].lv;
        let selfLv = vsData[0].lv
        let mul;
        switch (vsData[1].color) {
            case 'black':
                mul = 1;
                break;
            case 'orange':
                mul = 1.4;
                break;
            case 'red':
                mul = 1.7;
                break;
        }
        let pkButton = document.querySelectorAll("button");
        try{
            for (let i = 2; i < 6; i++) {
                let lvDiff = opponentLv - selfLv;
                let text;
                if (lvDiff > 3) {text = `>${pkWinTable[i - 2][4]} / >${pkLoseTable[i - 2]}`;}
                else if (lvDiff < -1) {text = `<${pkWinTable[i - 2][0]} / <${pkLoseTable[i - 2]}`;}
                else {text = `${pkWinTable[i - 2][lvDiff + 1]} / ${pkLoseTable[i - 2]}`;}
                tippy(pkButton[i], {
                    delay: [250, 100],
                    content: `${text} 經驗值`,
                });
            }
        }
        catch(e) {}
    }
}