Greasy Fork

Greasy Fork is available in English.

蜜柑计划 Arai2 RPC 下载 - Mikan Project Aria2 RPC Download

蜜柑计划 Arai2 RPC 下载 - Mikan Project Aria2 RPC Download.

当前为 2023-12-10 提交的版本,查看 最新版本

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Greasemonkey 油猴子Violentmonkey 暴力猴,才能安装此脚本。

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

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

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

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

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

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

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

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

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

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

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

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

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

// ==UserScript==
// @name         蜜柑计划 Arai2 RPC 下载 - Mikan Project Aria2 RPC Download
// @namespace    http://tampermonkey.net/
// @version      0.1
// @description  蜜柑计划 Arai2 RPC 下载 - Mikan Project Aria2 RPC Download.
// @author       Licht
// @license      MIT
// @homepage      https://github.com/ewigl/mpard
// @match        http*://mikanani.me/*
// @icon         https://mikanani.me/images/favicon.ico?v=2
// @require      https://unpkg.com/[email protected]/dist/jquery.min.js
// @require      https://unpkg.com/[email protected]/dist/sweetalert2.all.min.js
// @connect      localhost
// @connect      *
// @grant        unsafeWindow
// @grant        GM_xmlhttpRequest
// @grant        GM_getValue
// @grant        GM_setValue
// @grant        GM_addStyle
// ==/UserScript==

;(function () {
    'use strict'

    let styleCSS = `
    #magnet-link-box {
        display: none;
        border-left: 1px solid;
        padding-left: 8px;
    }

    #rpc-setting-box {
        display: none;
        border-left: 1px solid;
        padding-left: 8px;
    }

    .custom-button {
        color: white;
        background-color: slategrey;
        padding: 4px;
        margin: 8px 0px;
        border: none;
        border-radius: 5px;
    }

    .rpc-settings-label {
        display: flex;
        align-items: center;
    }

    .rpc-settings-label div {
        width: 20%;
    }

    .rpc-settings-input {
        display: inline-block;
        flex: 1;
        height: 32px;
        padding: 5px;
        border: 1px solid;
        border-radius: 5px;
    }
    `
    GM_addStyle(styleCSS)

    // 默认设置
    let defaultConfig = {
        rpcSettings: [
            {
                name: 'rpc_address',
                value: 'http://localhost:6800/jsonrpc',
            },
            {
                name: 'rpc_secret',
                value: '',
            },
            {
                name: 'rpc_dir',
                value: '',
            },
        ],
    }

    // 默认 message
    let message = Swal.mixin({
        position: 'center-end',
        toast: true,
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        width: '32rem',
        timer: 5000,
        timerProgressBar: true,
    })

    // 工具
    let util = {
        getValue(name) {
            return GM_getValue(name)
        },
        setValue(name, value) {
            GM_setValue(name, value)
        },
        initDefaultConfig() {
            defaultConfig.rpcSettings.forEach((value) => {
                util.getValue(value.name) === undefined && util.setValue(value.name, value.value)
            })
        },
        resetDefaultConfig() {
            defaultConfig.rpcSettings.forEach((value) => {
                util.setValue(value.name, value.value)
            })
        },

        sendToRPC: async (magnetLink) => {
            let rpc = {
                address: util.getValue('rpc_address'),
                secret: util.getValue('rpc_secret'),
                dir: util.getValue('rpc_dir').trim() === '' ? undefined : util.getValue('rpc_dir'),
            }

            let rpcData = {
                id: new Date().getTime(),
                jsonrpc: '2.0',
                method: 'aria2.addUri',
                params: [
                    `token:${rpc.secret}`,
                    [magnetLink],
                    {
                        dir: rpc.dir,
                    },
                ],
            }

            GM_xmlhttpRequest({
                method: 'POST',
                url: rpc.address,
                data: JSON.stringify(rpcData),
                onload: (response) => {
                    let resJson = JSON.parse(response.responseText)

                    if (resJson.result) {
                        message.fire({
                            icon: 'success',
                            title: 'RPC请求发送成功, 请前往控制台查看',
                        })
                    } else {
                        message.fire({
                            icon: 'error',
                            title: 'RPC请求发送失败, 请检查RPC设置是否正确',
                            text: `${resJson.error.code} / ${resJson.error.message}`,
                        })
                    }
                },
                onerror: (error) => {
                    message.fire({
                        icon: 'error',
                        title: 'RPC请求发送失败, 请检查RPC设置是否正确',
                        text: JSON.stringify(error),
                    })
                },
                onabort: () => {
                    message.fire({
                        icon: 'error',
                        title: '内部错误',
                    })
                },
            })
        },
    }

    let operation = {
        onCopyMagnet: (event) => {
            let target = event.target
            let magnetLink = $(target).attr('data-clipboard-text')
            // 主 DOM
            let mpardDom = `
            <div>
                <div>
                    <b>
                        是否使用 Aria2 RPC 下载该磁力链接 ?
                    </b>
                </div>
                <button id="show-magnet-button" class="custom-button">
                    显示/隐藏磁链
                </button>
                <div id="magnet-link-box">
                    ${magnetLink}
                </div>
                <div>
                    <button id="rpc-settings-button" class="custom-button">
                        显示/隐藏 RPC 设置
                    </button>
                </div>
            </div>

            <div id="rpc-setting-box">
                <div>
                    修改时自动保存
                </div>
                <br>
                <div>
                    <label class="rpc-settings-label">
                        <div>RPC地址:</div>
                        <input
                            id="rpc-address"
                            type="text"
                            class="rpc-settings-input"
                            title="默认地址为 http://localhost:6800/jsonrpc"
                            value="${util.getValue('rpc_address')}"
                        >
                    </label>
                </div>
                <div>
                    <label class="rpc-settings-label">
                        <div>RPC密钥:</div>
                        <input
                            id="rpc-secret"
                            type="text"
                            class="rpc-settings-input"
                            title="无密钥时留空"
                            value="${util.getValue('rpc_secret')}"
                        >
                    </label>
                </div>
                <div>
                    <label class="rpc-settings-label">
                        <div>下载目录:</div>
                        <input
                            id="rpc-dir"
                            type="text"
                            class="rpc-settings-input"
                            title="留空则为 aria2 默认路径"
                            value="${util.getValue('rpc_dir')}"
                        >
                    </label>
                </div>
                <button id="rpc-reset-button" class="custom-button">
                    重置为默认设置
                </button>
            </div>
            `

            if (magnetLink) {
                message
                    .fire({
                        icon: 'success',
                        showCancelButton: true,
                        title: '已复制磁力链接到剪切板',
                        html: mpardDom,
                        timer: undefined,
                    })
                    .then((result) => {
                        if (result.isConfirmed) {
                            util.sendToRPC(magnetLink)
                        }
                    })
            }
        },
        onClickShowMagnetLinkButton: async () => {
            $('#magnet-link-box').toggle()
        },
        onClickRPCSettingsButton: async () => {
            $('#rpc-setting-box').toggle()
        },
        onResetRPCSettings: async () => {
            util.resetDefaultConfig()
            $('#rpc-address').val(util.getValue('rpc_address'))
            $('#rpc-secret').val(util.getValue('rpc_secret'))
            $('#rpc-dir').val(util.getValue('rpc_dir'))
        },
    }

    // Main
    let main = {
        init() {
            util.initDefaultConfig()
            this.addListeners()
        },
        addListeners() {
            // 入口
            $(document).on('click', '[data-clipboard-text]', operation.onCopyMagnet)
            // 显示磁链
            $(document).on('click', '#show-magnet-button', operation.onClickShowMagnetLinkButton)
            // RPC设置
            $(document).on('click', '#rpc-settings-button', operation.onClickRPCSettingsButton)
            // 重置RPC设置
            $(document).on('click', '#rpc-reset-button', operation.onResetRPCSettings)

            // RPC表单
            $(document).on('input', '#rpc-address', async (e) => {
                util.setValue('rpc_address', e.target.value)
            })
            $(document).on('input', '#rpc-secret', async (e) => {
                util.setValue('rpc_secret', e.target.value)
            })
            $(document).on('input', '#rpc-dir', async (e) => {
                util.setValue('rpc_dir', e.target.value)
            })
        },
    }

    main.init()
})()