配置宝塔key

Add a button to configure API key for IP:port URLs

Verze ze dne 28. 03. 2024. Zobrazit nejnovější verzi.

K instalaci tototo skriptu si budete muset nainstalovat rozšíření jako Tampermonkey, Greasemonkey nebo Violentmonkey.

You will need to install an extension such as Tampermonkey to install this script.

K instalaci tohoto skriptu si budete muset nainstalovat rozšíření jako Tampermonkey nebo Violentmonkey.

K instalaci tohoto skriptu si budete muset nainstalovat rozšíření jako Tampermonkey nebo Userscripts.

You will need to install an extension such as Tampermonkey to install this script.

K instalaci tohoto skriptu si budete muset nainstalovat manažer uživatelských skriptů.

(Už mám manažer uživatelských skriptů, nechte mě ho nainstalovat!)

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

(Už mám manažer uživatelských stylů, nechte mě ho nainstalovat!)

// ==UserScript==
// @name         配置宝塔key
// @namespace    http://tampermonkey.net/
// @version      0.1
// @description  Add a button to configure API key for IP:port URLs
// @author       You
// @match        *://*/*
// @grant        none
// @license MIT
// ==/UserScript==

(function() {
    'use strict';

    // 检查当前页面是否为 IP:端口 格式
    const url = new URL(window.location.href);
    const isIpPort = /^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}:\d+$/.test(url.host);

    if (isIpPort) {
        // 创建按钮
        const button = document.createElement('button');
        button.textContent = '配置 API Key';
        button.style.position = 'fixed';
        button.style.zIndex = '9999';
        button.style.top = '10px';
        button.style.right = '10px';

        // 添加点击事件处理程序
        button.addEventListener('click', async () => {
            const apiUrl = `http://${url.host}/config?action=set_token`;

            try {
                const response = await fetch(apiUrl, {
                    headers: {
                        "accept": "*/*",
                        "accept-language": "zh-CN,zh;q=0.9,en;q=0.8,en-GB;q=0.7,en-US;q=0.6",
                        "cache-control": "no-cache",
                        "content-type": "application/x-www-form-urlencoded; charset=UTF-8",
                        "pragma": "no-cache",
                        "proxy-connection": "keep-alive",
                        "x-http-token": $("#request_token_head").attr('token'),
                        "x-requested-with": "XMLHttpRequest"
                    },
                    referrer: `http://${url.host}/config`,
                    referrerPolicy: "strict-origin-when-cross-origin",
                    body: `t_type=3&limit_addr=${url.host}`,
                    method: "POST",
                    mode: "cors",
                    credentials: "include"
                });

                const result = await response.json();
                const decodedResult = JSON.parse(JSON.stringify(result).replace(/\\/g, '\\\\').replace(/\u([0-9a-fA-F]{4})/g, '\\u$1'));
                alert(`API 响应结果:\n${JSON.stringify(decodedResult, null, 2)}`);
            } catch (error) {
                alert(`发生错误: ${error}`);
            }
        });

        // 将按钮添加到页面
        document.body.appendChild(button);
    }
})();