Greasy Fork

来自缓存

Greasy Fork is available in English.

GBF Victory Skip

Redirects to reward page when win scenario is detected

您需要先安装一个扩展,例如 篡改猴Greasemonkey暴力猴,之后才能安装此脚本。

您需要先安装一个扩展,例如 篡改猴暴力猴,之后才能安装此脚本。

您需要先安装一个扩展,例如 篡改猴暴力猴,之后才能安装此脚本。

您需要先安装一个扩展,例如 篡改猴Userscripts ,之后才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey,才能安装此脚本。

您需要先安装用户脚本管理器扩展后才能安装此脚本。

(我已经安装了用户脚本管理器,让我安装!)

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

(我已经安装了用户样式管理器,让我安装!)

// ==UserScript==
// @name         GBF Victory Skip
// @version      0.0.1
// @description  Redirects to reward page when win scenario is detected
// @match        http://game.granbluefantasy.jp/
// @grant        unsafeWindow
// @run-at       document-start
// @namespace http://greasyfork.icu/users/18331
// ==/UserScript==
const DEBUG = false;

const tryParseJSON = text => {
    let json;
    try {
        json = JSON.parse(text);
    } catch (e) {
        if (e instanceof SyntaxError) {
            return text;
        }
        throw e;
    }
    return json;
};

const log = (...args) => {
    if (!DEBUG) return;
    console.debug("GVS", ...args);
};

const customLoad = (xhr, ...args) => {
    // ex:
    // http://game.granbluefantasy.jp/rest/multiraid/ability_result.json
    const url = new URL(xhr.responseURL);
    const req = tryParseJSON(args[0]);
    const res = tryParseJSON(xhr.response);

    if (url.pathname.indexOf('.json') < 0) return;
    if (url.pathname.indexOf('/rest') < 0) return;
    log("» FOUND MATCHING REQUEST");

    if (!res.scenario) return;
    log("»» FOUND SCENARIO", res.scenario);

    const win = res.scenario.find(s => s.cmd === "win");
    if (win) {
        log("»»» DETECTED WIN CONDITION", win);
        const raidId = win.raid_id;                            // Contains the next raid id if it's not the last round
        const isLastRaid = !!win.is_last_raid;

        log("»»»» CURRENT_URL ", location.href.toString());
        const redirectURL = (isLastRaid) ?
              location.href.toString().replace("raid", "result") :
              location.href.toString().replace(/\d{3,10}/, raidId);
        log("»»»»» REDIRECT_URL", redirectURL);

        location.href = redirectURL;
    }
};

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