获取ChatGPT SessionToken

一键获取并复制OpenAI聊天的SessionToken,简化用户操作。

  1. // ==UserScript==
  2. // @name 获取ChatGPT SessionToken
  3. // @namespace http://tampermonkey.net/
  4. // @version 1.0
  5. // @description 一键获取并复制OpenAI聊天的SessionToken,简化用户操作。
  6. // @author Flyrr & Yongmo & lyy0709 & GPT-4
  7. // @match https://chatgpt.com/*
  8. // @grant GM_xmlhttpRequest
  9. // @grant GM_setClipboard
  10. // @grant GM_addStyle
  11. // @license MIT
  12. // ==/UserScript==
  13.  
  14. (function() {
  15. 'use strict';
  16.  
  17. // 创建一个悬浮面板
  18. const panel = document.createElement('div');
  19. panel.style.cssText = 'position: fixed; top: 20px; right: 20px; z-index: 1000; padding: 15px; border: 1px solid #ccc; border-radius: 12px; background: white; box-shadow: 0 4px 8px rgba(0,0,0,0.3); font-family: "Microsoft YaHei", sans-serif; display: none; flex-direction: column; align-items: start;';
  20.  
  21. // 创建展开面板按钮
  22. const toggleButton = document.createElement('button');
  23. toggleButton.textContent = '☰';
  24. toggleButton.style.cssText = 'position: fixed; top: 20px; right: 20px; z-index: 1100; padding: 4px 8px; background: #007bff; color: white; cursor: pointer; font-size: 24px; border-radius: 6px; display: block;';
  25.  
  26. // 创建操作按钮
  27. const btnFetchAndCopy = document.createElement('button');
  28. btnFetchAndCopy.textContent = '获取并复制SessionToken';
  29. btnFetchAndCopy.style.cssText = 'margin-bottom: 10px; padding: 8px 15px; border: none; border-radius: 6px; color: white; cursor: pointer; font-size: 16px; width: 100%; text-align: center; background-color: #007bff;';
  30.  
  31. // 创建文本显示区域
  32. const tokenDisplay = document.createElement('textarea');
  33. tokenDisplay.style.cssText = 'width: 100%; height: 60px; margin-bottom: 10px; padding: 10px; border-radius: 6px; border: 1px solid #ccc;';
  34.  
  35. // 隐藏面板按钮
  36. const btnHidePanel = document.createElement('button');
  37. btnHidePanel.textContent = '隐藏面板';
  38. btnHidePanel.style.cssText = 'margin-bottom: 10px; padding: 8px 15px; border: none; border-radius: 6px; color: white; cursor: pointer; font-size: 16px; width: 100%; text-align: center; background-color: #ffc107;';
  39. btnHidePanel.onclick = function() {
  40. panel.style.display = 'none';
  41. toggleButton.style.display = 'block';
  42. };
  43.  
  44. // 将元素添加到面板
  45. panel.appendChild(tokenDisplay);
  46. panel.appendChild(btnFetchAndCopy);
  47. panel.appendChild(btnHidePanel);
  48. document.body.appendChild(panel);
  49. document.body.appendChild(toggleButton);
  50.  
  51. // Toggle panel display
  52. toggleButton.onclick = function() {
  53. panel.style.display = (panel.style.display === 'none') ? 'flex' : 'none';
  54. toggleButton.style.display = (panel.style.display === 'flex') ? 'none' : 'block';
  55. };
  56.  
  57. // 按钮点击事件:获取并复制Token
  58. btnFetchAndCopy.onclick = function() {
  59. GM_xmlhttpRequest({
  60. method: "GET",
  61. url: "https://chatgpt.com/chat",
  62. onload: function(response) {
  63. const cookieHeader = response.responseHeaders;
  64. const match = cookieHeader.match(/__Secure-next-auth.session-token=([^;]+);/);
  65. if (match && match[1]) {
  66. const sessionToken = match[1];
  67. tokenDisplay.value = sessionToken;
  68. GM_setClipboard(sessionToken, 'text');
  69. alert('Session Token 已复制到剪贴板:\n' + sessionToken);
  70. } else {
  71. alert('Session Token 未找到。可能是因为浏览器安全策略限制了头部信息的获取。');
  72. }
  73. },
  74. onerror: function() {
  75. alert('Failed to fetch session information.');
  76. }
  77. });
  78. };
  79. })();