作用:给ChatGPT的页面添加一个按钮,点击后自动获取最新 token 并更新到 chat2api 的 token 列表
使用前务必将 CHAT2API_URL 替换为你的 Chat2API 接口地址
使用前务必将 CHAT2API_URL 替换为你的 Chat2API 接口地址
使用前务必将 CHAT2API_URL 替换为你的 Chat2API 接口地址
安装完成后打开 https://chatgpt.com/api/auth/session 就可以看到一个大大的更新按钮
// ==UserScript==
// @name 更新 ChatGPT AccessToken
// @description 在 ChatGPT 的 session 页面点击自动更新 AccessToken 到 Chat2API 中
// @author NeverMind
// @namespace http://tampermonkey.net/
// @version 1.0.0
// @match https://chatgpt.com/api/auth/session
// @icon https://www.google.com/s2/favicons?sz=64&domain=chatgpt.com
// @grant GM_xmlhttpRequest
// @connect *
// ==/UserScript==
// 请将 CHAT2API_URL 替换为你的 Chat2API 接口地址
// 例如:
// https://example.com
// https://example.com/api_prefix
const CHAT2API_URL = "!!!REPLACE_ME!!!";
const getUrl = pathname => {
const path = pathname.startsWith("/") ? pathname.slice(1) : pathname;
const url = new URL(CHAT2API_URL);
if (url.pathname.endsWith("/")) {
url.pathname += path;
} else {
url.pathname += `/${path}`;
}
return url.href;
};
const createButton = callback => {
const button = document.createElement("button");
button.textContent = "更新 AccessToken";
button.style.fontSize = "clamp(0.875rem, 0.5139rem + 1.5408vw, 1.5rem)";
button.style.padding = "0.5em";
button.style.margin = "2em";
button.addEventListener("click", callback);
document.body.prepend(button);
return button;
};
const getAccessToken = () => {
const pre = document.querySelector("pre");
if (!pre) return "";
try {
const data = JSON.parse(pre.textContent);
return data.accessToken;
} catch {
return "";
}
};
const updateAccessToken = async token => {
await fetch(getUrl("/tokens/clear"), {
method: "POST",
});
await fetch(getUrl("/tokens/upload"), {
headers: {
"content-type": "application/x-www-form-urlencoded",
},
body: `text=${token}`,
method: "POST",
});
};
(function () {
"use strict";
createButton(async () => {
const token = getAccessToken();
if (token) {
await updateAccessToken(token);
alert("更新 AccessToken 成功");
} else {
alert("获取 AccessToken 失败");
}
});
})();