ChatGPT Token & PoW 监控(优化版)

获取 ChatGPT Token,并显示 PoW 算力值,修复 UI 问题

当前为 2025-02-28 提交的版本,查看 最新版本

// ==UserScript==
// @name         ChatGPT Token & PoW 监控(优化版)
// @namespace    http://tampermonkey.net/
// @version      2.7
// @description  获取 ChatGPT Token,并显示 PoW 算力值,修复 UI 问题
// @author       ChatGPT指导员
// @match        https://chatgpt.com/*
// @grant        none
// @license      V:ChatGPT4V
// ==/UserScript==

(function() {
    'use strict';

    let powDifficulty = "N/A";  // 初始算力值,不存储上次值

    // 监听 PoW 算力 API
    const originalFetch = window.fetch;
    window.fetch = async function(resource, options) {
        const response = await originalFetch(resource, options);
        const url = typeof resource === "string" ? resource : resource?.url;

        if (url && url.includes("/sentinel/chat-requirements") && options?.method === "POST") {
            try {
                const clonedResponse = response.clone();
                clonedResponse.json().then(data => {
                    powDifficulty = data?.proofofwork?.difficulty || "N/A";
                    updatePowButton(); // 更新按钮显示的算力值
                });
            } catch (error) {
                console.error("获取 PoW 失败:", error);
            }
        }
        return response;
    };

    // 检查当前网址是否是 chatgpt.com
    if (window.location.hostname === 'chatgpt.com') {
        fetch('https://chatgpt.com/api/auth/session')
            .then(res => res.json())
            .then((data) => {
                const { accessToken } = data;

                if (typeof accessToken === 'undefined' || accessToken === null) {
                    console.warn('AccessToken 未定义或未找到,未执行复制操作。');
                    return;
                }

                // 创建弹窗容器
                const modal = document.createElement('div');
                modal.id = "custom-modal";
                modal.style.position = 'fixed';
                modal.style.top = '50%';
                modal.style.left = '50%';
                modal.style.transform = 'translate(-50%, -50%)';
                modal.style.backgroundColor = '#FFFFFF';
                modal.style.padding = '15px';
                modal.style.borderRadius = '10px';
                modal.style.boxShadow = '0 4px 8px rgba(0, 0, 0, 0.1)';
                modal.style.textAlign = 'center';
                modal.style.zIndex = 1001;
                modal.style.fontFamily = 'Arial, sans-serif';
                modal.style.color = '#333';
                modal.style.display = 'inline-block';
                modal.style.maxWidth = '90%';
                modal.style.boxSizing = 'border-box';
                modal.style.border = '1px solid #ccc';

                const promptText = document.createElement('p');
                promptText.textContent = 'Token 已获取,点击按钮复制';
                promptText.style.marginBottom = '12px';
                promptText.style.fontSize = '16px';
                modal.appendChild(promptText);

                const buttonContainer = document.createElement('div');
                buttonContainer.style.display = 'flex';
                buttonContainer.style.justifyContent = 'center';
                buttonContainer.style.gap = '10px';
                modal.appendChild(buttonContainer);

                const copyTokenBtn = document.createElement('button');
                copyTokenBtn.textContent = '单Token';
                copyTokenBtn.style.backgroundColor = 'rgb(254, 94, 8)';
                copyTokenBtn.style.color = 'white';
                copyTokenBtn.style.border = 'none';
                copyTokenBtn.style.borderRadius = '5px';
                copyTokenBtn.style.padding = '10px 20px';
                copyTokenBtn.style.cursor = 'pointer';
                copyTokenBtn.style.fontSize = '16px';
                copyTokenBtn.onclick = () => {
                    navigator.clipboard.writeText(accessToken).then(() => {
                        showToast("Token 已复制到剪贴板");
                        closeModal();
                    }).catch(err => {
                        console.error('复制失败', err);
                    });
                };
                buttonContainer.appendChild(copyTokenBtn);

                const powButton = document.createElement('button');
                powButton.id = "pow-button";
                powButton.textContent = `算力:${powDifficulty}`;
                powButton.style.backgroundColor = '#f44336';
                powButton.style.color = 'white';
                powButton.style.border = 'none';
                powButton.style.borderRadius = '5px';
                powButton.style.padding = '10px 20px';
                powButton.style.cursor = 'pointer';
                powButton.style.fontSize = '16px';
                powButton.onclick = closeModal;
                buttonContainer.appendChild(powButton);

                document.body.appendChild(modal);

                // 监听点击事件,检测点击是否在弹窗外部
                setTimeout(() => {
                    document.addEventListener('click', function handleOutsideClick(event) {
                        if (!modal.contains(event.target)) {
                            closeModal();
                            document.removeEventListener('click', handleOutsideClick);
                        }
                    });
                }, 0);
            })
            .catch(console.error);
    }

    function updatePowButton() {
        const powButton = document.getElementById("pow-button");
        if (powButton) {
            powButton.textContent = `算力值:${powDifficulty}`;
        }
    }

    function closeModal() {
        const modal = document.getElementById("custom-modal");
        if (modal) {
            modal.remove();
        }
    }

    function showToast(message) {
        const toast = document.createElement('div');
        toast.textContent = message;
        toast.style.position = 'fixed';
        toast.style.top = '50%';
        toast.style.left = '50%';
        toast.style.transform = 'translate(-50%, -50%)';
        toast.style.backgroundColor = 'rgba(0, 0, 0, 0.8)';
        toast.style.color = 'white';
        toast.style.padding = '10px 20px';
        toast.style.borderRadius = '5px';
        toast.style.zIndex = 1000;
        toast.style.fontSize = '16px';
        toast.style.boxShadow = '0 4px 8px rgba(0, 0, 0, 0.2)';

        document.body.appendChild(toast);

        setTimeout(() => {
            toast.remove();
        }, 1500);
    }
})();