自动获取 ChatGPT accessToken 并提供复制功能

Auto Fetch AccessToken on ChatGPT

目前為 2024-08-29 提交的版本,檢視 最新版本

// ==UserScript==
// @name         自动获取 ChatGPT accessToken 并提供复制功能
// @namespace    http://tampermonkey.net/
// @version      1.0
// @description  Auto Fetch AccessToken on ChatGPT
// @author       ChatGPT指导员
// @match        https://chatgpt.com/*
// @grant        none
// @license      V:ChatGPT4V
// @icon         https://image.webchat-ai.com/OpenAI%20Developer%20Forum.ico
// ==/UserScript==

(function() {
    'use strict';

    // 检查当前网址是否是 chatgpt.com
    if (window.location.hostname === 'chatgpt.com') {
        fetch('https://chatgpt.com/api/auth/session')
            .then(res => res.json())
            .then(({ accessToken }) => {
                // 如果 accessToken 为 undefined,则不执行后续操作
                if (typeof accessToken === 'undefined' || accessToken === null) {
                    console.warn('AccessToken 未定义或未找到,未执行复制操作。');
                    return;
                }

                // 创建弹窗容器
                const modal = document.createElement('div');
                modal.style.position = 'fixed';
                modal.style.top = '50%';
                modal.style.left = '50%';
                modal.style.transform = 'translate(-50%, -50%)';
                modal.style.backgroundColor = '#f9f9f9';
                modal.style.padding = '20px';
                modal.style.borderRadius = '8px';
                modal.style.boxShadow = '0 4px 8px rgba(0, 0, 0, 0.1)';
                modal.style.textAlign = 'center';
                modal.style.zIndex = 1000;
                modal.style.fontFamily = 'Arial, sans-serif';
                modal.style.color = '#333';
                modal.style.display = 'inline-block'; // 使宽度自适应内容
                modal.style.maxWidth = '90%'; // 限制最大宽度为屏幕的90%
                modal.style.boxSizing = 'border-box'; // 盒模型调整

                // 创建提示文本
                const promptText = document.createElement('p');
                promptText.textContent = 'Token 已获取,请点击复制按钮';
                promptText.style.marginBottom = '20px';
                promptText.style.fontSize = '16px'; // 调整字体大小
                modal.appendChild(promptText);

                // 创建复制按钮
                const copyBtn = document.createElement('button');
                copyBtn.textContent = '复制';
                copyBtn.style.backgroundColor = '#4CAF50';
                copyBtn.style.color = 'white';
                copyBtn.style.border = 'none';
                copyBtn.style.borderRadius = '5px';
                copyBtn.style.padding = '10px 20px';
                copyBtn.style.marginRight = '10px';
                copyBtn.style.cursor = 'pointer';
                copyBtn.style.fontSize = '16px';
                copyBtn.onclick = () => {
                    navigator.clipboard.writeText(accessToken).then(() => {
                        document.body.removeChild(modal);  // 复制成功后关闭弹窗

                        // 创建自动消失的提示框
                        const toast = document.createElement('div');
                        toast.textContent = 'Token 已复制到剪贴板';
                        toast.style.position = 'fixed';
                        toast.style.top = '50%';
                        toast.style.left = '50%';
                        toast.style.transform = 'translate(-50%, -50%) translateY(-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);

                        // 3秒后自动移除提示框
                        setTimeout(() => {
                            document.body.removeChild(toast);
                        }, 1500);
                    }).catch(err => {
                        console.error('复制失败', err);
                    });
                };
                copyBtn.onmouseover = () => {
                    copyBtn.style.backgroundColor = '#45a049';
                };
                copyBtn.onmouseout = () => {
                    copyBtn.style.backgroundColor = '#4CAF50';
                };
                modal.appendChild(copyBtn);

                // 创建关闭按钮
                const closeBtn = document.createElement('button');
                closeBtn.textContent = '关闭';
                closeBtn.style.backgroundColor = '#f44336';
                closeBtn.style.color = 'white';
                closeBtn.style.border = 'none';
                closeBtn.style.borderRadius = '5px';
                closeBtn.style.padding = '10px 20px';
                closeBtn.style.cursor = 'pointer';
                closeBtn.style.fontSize = '16px';
                closeBtn.onclick = () => {
                    document.body.removeChild(modal);
                };
                closeBtn.onmouseover = () => {
                    closeBtn.style.backgroundColor = '#e53935';
                };
                closeBtn.onmouseout = () => {
                    closeBtn.style.backgroundColor = '#f44336';
                };
                modal.appendChild(closeBtn);

                // 添加弹窗到页面
                document.body.appendChild(modal);
            })
            .catch(console.error);
    }
})();

QingJ © 2025

镜像随时可能失效,请加Q群300939539或关注我们的公众号极客氢云获取最新地址