Greasy Fork

Greasy Fork is available in English.

ChatGPT AccessToken 自动更新

每隔7天获取accessToken并发送POST请求后自动跳转,增加可拖动的可视化窗口

当前为 2024-08-12 提交的版本,查看 最新版本

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

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

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

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

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

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

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

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

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

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

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

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

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

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

// ==UserScript==
// @name         ChatGPT AccessToken 自动更新
// @namespace    http://tampermonkey.net/
// @version      2.0
// @description  每隔7天获取accessToken并发送POST请求后自动跳转,增加可拖动的可视化窗口
// @author       AMT
// @match        *://new.oaifree.com/*
// @grant        GM_xmlhttpRequest
// @grant        GM_addStyle
// @connect      chatgpt.com
// @connect      new.oaifree.com
// @license      MIT
// ==/UserScript==

(function() {
    'use strict';

    // 定义获取accessToken的URL
    const tokenUrl = "https://chatgpt.com/api/auth/session";
    // 定义POST请求的目标URL
    const postUrl = "https://new.oaifree.com/auth/login_token";
    // 定义跳转的目标URL
    const redirectUrl = "https://new.oaifree.com"; // 这里替换为你想要跳转的URL
    // 定义localStorage的key
    const lastRunKey = 'lastRunTimestamp';
    // 获取当前时间的时间戳(毫秒)
    const currentTime = new Date().getTime();
    // 定义7天的毫秒数
    const sevenDaysInMillis = 7 * 24 * 60 * 60 * 1000;

    // 从localStorage获取上次运行的时间
    const lastRun = localStorage.getItem(lastRunKey);

    // 计算距离上次运行的时间
    let timeSinceLastRun = 0;
    if (lastRun) {
        timeSinceLastRun = currentTime - lastRun;
    }
    const daysSinceLastRun = Math.floor(timeSinceLastRun / (24 * 60 * 60 * 1000));
    const hoursSinceLastRun = Math.floor((timeSinceLastRun % (24 * 60 * 60 * 1000)) / (60 * 60 * 1000));

    // 创建可视化窗口
    const panel = document.createElement('div');
    panel.id = 'script-panel';
    panel.innerHTML = `
        <div id="panel-content">
            <p>距离上次运行脚本已过:${daysSinceLastRun}天${hoursSinceLastRun}小时</p>
            <button id="run-script-button">立即获取AccessToken并POST</button>
        </div>
    `;
    document.body.appendChild(panel);

    // 添加样式
    GM_addStyle(`
        #script-panel {
            position: fixed;
            top: 10%;
            right: 0;
            width: 300px;
            background-color: rgba(0, 0, 0, 0.7);
            color: white;
            padding: 15px;
            border-radius: 10px 0 0 10px;
            box-shadow: 0px 0px 10px rgba(0, 0, 0, 0.5);
            z-index: 10000;
            transform: translateX(98%);
            transition: transform 0.5s ease-in-out;
            cursor: move;
        }
        #panel-content {
            display: block;
            text-align: center;
        }
        #script-panel:hover {
            transform: translateX(0);
        }
        #run-script-button {
            background-color: #4CAF50;
            color: white;
            border: none;
            padding: 10px 15px;
            text-align: center;
            text-decoration: none;
            display: inline-block;
            font-size: 14px;
            margin: 10px 0;
            cursor: pointer;
            border-radius: 5px;
        }
    `);

    // 添加拖动功能
    let isDragging = false;
    let startY = 0;
    let startTop = 0;

    panel.addEventListener('mousedown', function(e) {
        isDragging = true;
        startY = e.clientY;
        startTop = panel.offsetTop;
        document.addEventListener('mousemove', onMouseMove);
        document.addEventListener('mouseup', onMouseUp);
    });

    function onMouseMove(e) {
        if (isDragging) {
            const deltaY = e.clientY - startY;
            panel.style.top = `${startTop + deltaY}px`;
        }
    }

    function onMouseUp() {
        isDragging = false;
        document.removeEventListener('mousemove', onMouseMove);
        document.removeEventListener('mouseup', onMouseUp);
    }

    // 添加按钮点击事件
    document.getElementById('run-script-button').addEventListener('click', function() {
        fetchAndPostToken();
    });

    // 检查是否已经过去7天
    if (!lastRun || timeSinceLastRun > sevenDaysInMillis) {
        fetchAndPostToken();
    } else {
        console.log("Script not run: 7-day interval not reached.");
    }

    // 获取token并发送POST请求的函数
    function fetchAndPostToken() {
        GM_xmlhttpRequest({
            method: "GET",
            url: tokenUrl,
            onload: function(response) {
                if (response.status === 200) {
                    // 解析返回的JSON
                    const responseData = JSON.parse(response.responseText);
                    // 提取accessToken
                    const accessToken = responseData.accessToken;

                    // 如果获取到accessToken,则发送POST请求
                    sendPostRequest(accessToken);

                    // 更新最后运行的时间
                    localStorage.setItem(lastRunKey, currentTime.toString());
                } else {
                    console.error("Failed to fetch accessToken. Status:", response.status);
                }
            }
        });
    }

    // 发送POST请求的函数
    function sendPostRequest(accessToken) {
        const data = {
            action: "token",
            access_token: accessToken
        };

        GM_xmlhttpRequest({
            method: "POST",
            url: postUrl,
            headers: {
                "Content-Type": "application/x-www-form-urlencoded"
            },
            data: Object.keys(data).map(key => `${encodeURIComponent(key)}=${encodeURIComponent(data[key])}`).join('&'),
            onload: function(response) {
                console.log("Status Code:", response.status);
                console.log("Response:", response.responseText);

                // 成功发送POST请求后自动跳转
                if (response.status === 200) {
                    window.location.href = redirectUrl;
                }
            }
        });
    }
})();