[Closed Test] AMQ Accelerate Loading

Load media faster from alternative sources.

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

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

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

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

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

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

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

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

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

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

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

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

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

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

// ==UserScript==
// @name            [Closed Test] AMQ Accelerate Loading
// @namespace       https://github.com/SlashNephy
// @version         0.2.1
// @author          SlashNephy
// @description     Load media faster from alternative sources.
// @description:ja  メディアを代替ソースから高速にロードします。
// @homepage        https://scrapbox.io/slashnephy/AMQ_Accelerate_Loading
// @homepageURL     https://scrapbox.io/slashnephy/AMQ_Accelerate_Loading
// @icon            https://animemusicquiz.com/favicon-32x32.png
// @supportURL      https://github.com/SlashNephy/userscripts/issues
// @match           https://animemusicquiz.com/*
// @require         https://cdn.jsdelivr.net/gh/TheJoseph98/AMQ-Scripts@b97377730c4e8553d2dcdda7fba00f6e83d5a18a/common/amqScriptInfo.js
// @grant           none
// @license         MIT license
// ==/UserScript==

(function () {
    'use strict';

    const awaitFor = async (predicate, timeout) => new Promise((resolve, reject) => {
        let timer;
        const interval = window.setInterval(() => {
            if (predicate()) {
                clearInterval(interval);
                clearTimeout(timer);
                resolve();
            }
        }, 500);
        if (timeout !== undefined) {
            timer = window.setTimeout(() => {
                clearInterval(interval);
                clearTimeout(timer);
                reject(new Error('timeout'));
            }, timeout);
        }
    });

    const onReady = (callback) => {
        if (document.getElementById('startPage')) {
            return;
        }
        awaitFor(() => document.getElementById('loadingScreen')?.classList.contains('hidden') === true)
            .then(callback)
            .catch(console.error);
    };

    const proxyHost = 'https://amq-proxy.starry.blue';
    async function checkSession() {
        try {
            const response = await fetch(`${proxyHost}/healthcheck`, {
                redirect: 'error',
                mode: 'cors',
                credentials: 'include',
            });
            return response.ok;
        }
        catch (e) {
            return false;
        }
    }
    function replaceUrl(url) {
        return `${proxyHost}/api/media?u=${encodeURIComponent(url)}`;
    }
    onReady(async () => {
        const isAuthenticated = await checkSession();
        if (!isAuthenticated) {
            setTimeout(() => {
                void swal({
                    title: 'AMQ Accelerate Loading',
                    text: 'この UserScript を使用するにはユーザ認証が必要です。認証後、AMQ に再度ログインしてください。',
                    confirmButtonText: '認証',
                    showCancelButton: true,
                    cancelButtonText: 'キャンセル',
                    allowOutsideClick: false,
                    focusConfirm: true,
                    showCloseButton: false,
                    allowEscapeKey: false,
                }).then(() => {
                    window.location.replace(proxyHost);
                });
            }, 5000);
            return;
        }
        else {
            popoutMessages.displayStandardMessage('AMQ Accelerate Loading', '認証成功! UserScript は正常に動作しています。');
        }
        const { getNextVideoId } = MoeVideoPlayer.prototype;
        MoeVideoPlayer.prototype.getNextVideoId = function () {
            const url = getNextVideoId.apply(this);
            if (!url) {
                return undefined;
            }
            return replaceUrl(url);
        };
        const { showVideoPreview } = ExpandQuestionBox.prototype;
        ExpandQuestionBox.prototype.showVideoPreview = function (url, ...args) {
            if (url) {
                showVideoPreview.apply(this, [replaceUrl(url), ...args]);
            }
            else {
                showVideoPreview.apply(this, [url, ...args]);
            }
        };
        AMQ_addScriptData({
            name: '[Closed Test] Accelerate Loading',
            author: 'SlashNephy <[email protected]>',
            description: 'Load media faster from alternative sources.',
        });
    });

})();