Greasy Fork

GBF周回本胜利跳过

已重构

当前为 2025-07-13 提交的版本,查看 最新版本

// ==UserScript==
// @name         GBF周回本胜利跳过
// @version      1.1
// @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 checkWin = (scenario) => {
    const win = scenario.find(s => s.cmd === "win");
    console.log(win);
    if (win) {
        if (win.is_last_raid === true) {
            history.go(-1);
        } else {
            reload();
        }
    }
}

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';
    console.log(
            `auto refresh: ${ar_enabled ? 'enabled' : 'disabled'}`,
            `is attack: ${is_attack ? 'true' : 'false'}`
            )
    if (ar_enabled && is_attack) {
        reload();
    } else {
        checkWin(response.scenario);
    }

};

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)
)