Greasy Fork

GBF周回本胜利跳过

已重构

// ==UserScript==
// @name         GBF周回本胜利跳过
// @version      1.2
// @author       Alk
// @license GPL3.0
// @description  已重构
// @match        *.granbluefantasy.jp/
// @grant        unsafeWindow
// @grant        GM_registerMenuCommand
// @grant        GM_setValue
// @grant        GM_getValue
// @run-at       document-start
// @namespace https://greasyfork.org/users/1455240
// ==/UserScript==
const tryParseJSON = (response) => {
    let json;
    try {
        json = JSON.parse(response);
    } catch (e) {
        if (e instanceof SyntaxError) {
            return response;
        }
        throw e;
    }
    return json;
};

const getURL = (base) => {
    const url = new URL(base);
    return {
        pathname: url.pathname,
        levels: url.pathname.split('/'),
        url_type: url.pathname.split('/')[1],
        action_type: url.pathname.split('/')[3],
    }
}

const detectURL = (action_type) => {
    if (action_type === 'normal_attack_result.json') {
        return 'attack'
    } else if (action_type === 'ability_result.json') {
        return 'ability'
    } else if (action_type === 'summon_result.json') {
        return 'summon'
    } else {
        return 'other'
    }
};

const reload = () => {
    location.reload(true);
}

const goback = () => {
    history.go(-1);
}

const checkWin = (scenario) => {
    const win = scenario.find(s => s.cmd === "win");
    if (win) {
        if (win.is_last_raid === true) {
            return 1
        } else {
            return 2;
        }
    } else return 0;
}

const checkEnabledAttackRefresh = () => {
    return GM_getValue('AttackRefresh', true);
}

const switchEnabledAttackRefresh = () => {
    GM_setValue('AttackRefresh', !ar_enabled);
    reload();
}

const customLoad = (xhr, ...args) => {
    const {
        pathname,
        levels,
        url_type,
        action_type,
    } = getURL(xhr.responseURL);
    if (url_type !== 'rest') return;

    if (!action_type.includes('.json')) return;

    const response = tryParseJSON(xhr.response);
    if (!response.scenario) return;

    const is_attack = detectURL(action_type) === 'attack';
    const status = checkWin(response.scenario);
    
    // Status === 0 => 未胜利
        // Status === 1 => 胜利且结算
        // Status === 2 => 胜利进入下一场景
    if (ar_enabled && is_attack) {
        if (status === 0) {
            reload();
        } else if (status === 1) {
            goback();
        } else if (status === 2) {
            reload();
        }
    } else {
        if (status === 1) {
            goback();
        } else if (status === 2) {
            reload();
        }
    }

};

const origSend = unsafeWindow.XMLHttpRequest.prototype.send;

unsafeWindow.XMLHttpRequest.prototype.send = function (...args) {
    this.addEventListener('load', () => {
        if (this.status === 200) {
            customLoad(this, args);
        }
    });
    origSend.apply(this, args);
};

let ar_enabled = checkEnabledAttackRefresh();

(
    GM_registerMenuCommand(
        `攻击后自动刷新:${ar_enabled
            ? '已开启' : '已关闭'}`
        , switchEnabledAttackRefresh)
)