n/aiu/dearfaint · 4 个月前
注册 登录
n/ai · 4 个月前 · 280 次浏览
u/dearfaint白银

来个多id 批量加入的油猴脚本

油猴脚本,登录完你的gpt (gmail)后,右下角弹出的输入框,把你的空间 id 都扔进去(一行一个)
批量加入,加入完后刷新页面,看左边个人信息,就可以看到成功加入多少空间

// ==UserScript==
// @name         ChatGPT 批量加入空间
// @namespace    https://chatgpt.com/
// @version      1.1.0
// @description  输入多个空间 Id,按顺序发送加入请求
// @match        https://chatgpt.com/*
// @grant        none
// ==/UserScript==

(function () {
  'use strict';

  const box = document.createElement('div');
  box.style.cssText = `
    position: fixed;
    right: 16px;
    bottom: 16px;
    z-index: 999999;
    width: 340px;
    padding: 12px;
    background: #fff;
    color: #111;
    border: 1px solid #ddd;
    border-radius: 8px;
    box-shadow: 0 6px 24px rgba(0,0,0,.16);
    font: 14px/1.4 system-ui, -apple-system, BlinkMacSystemFont, sans-serif;
  `;

  box.innerHTML = `
    <div style="font-weight:600;margin-bottom:8px;">批量加入 ChatGPT 空间</div>
    <textarea id="cg-space-ids" placeholder="每行一个空间 Id"
      style="box-sizing:border-box;width:100%;height:120px;padding:8px;border:1px solid #ccc;border-radius:6px;margin-bottom:8px;resize:vertical;"></textarea>
    <button id="cg-join-btn"
      style="width:100%;padding:8px;border:0;border-radius:6px;background:#10a37f;color:white;cursor:pointer;">
      开始加入
    </button>
    <div id="cg-status" style="margin-top:8px;font-size:12px;color:#555;white-space:pre-wrap;max-height:180px;overflow:auto;"></div>
  `;

  document.body.appendChild(box);

  const input = box.querySelector('#cg-space-ids');
  const button = box.querySelector('#cg-join-btn');
  const status = box.querySelector('#cg-status');

  function setStatus(text) {
    status.textContent = text;
  }

  function appendStatus(text) {
    status.textContent += `${text}\n`;
    status.scrollTop = status.scrollHeight;
  }

  async function readJson(response) {
    const text = await response.text();
    return text ? JSON.parse(text) : {};
  }

  async function getAccessToken() {
    const response = await fetch('https://chatgpt.com/api/auth/session', {
      method: 'GET',
      credentials: 'include',
    });

    const sessionData = await readJson(response);

    if (!sessionData.accessToken) {
      throw new Error('未能获取到 accessToken,请确认已登录 ChatGPT');
    }

    return sessionData.accessToken;
  }

  async function joinSpace(spaceId, accessToken) {
    const response = await fetch(
      `https://chatgpt.com/backend-api/accounts/${encodeURIComponent(spaceId)}/invites/request`,
      {
        method: 'POST',
        headers: {
          accept: '*/*',
          authorization: `Bearer ${accessToken}`,
          'cache-control': 'no-cache',
          'oai-language': 'en-US',
          pragma: 'no-cache',
        },
        credentials: 'include',
        mode: 'cors',
        body: null,
      }
    );

    const result = await readJson(response);

    if (!response.ok) {
      throw new Error(result?.detail || result?.message || `HTTP ${response.status}`);
    }

    return result;
  }

  async function joinSpaces() {
    const spaceIds = input.value
      .split('\n')
      .map(id => id.trim())
      .filter(Boolean);

    if (spaceIds.length === 0) {
      setStatus('请先输入空间 Id');
      return;
    }

    button.disabled = true;
    button.textContent = '加入中...';
    setStatus('正在获取 accessToken...\n');

    let successCount = 0;
    let failCount = 0;

    try {
      const accessToken = await getAccessToken();

      for (let index = 0; index < spaceIds.length; index++) {
        const spaceId = spaceIds[index];

        appendStatus(`[${index + 1}/${spaceIds.length}] 正在加入:${spaceId}`);

        try {
          const result = await joinSpace(spaceId, accessToken);
          successCount++;
          console.log('加入成功:', spaceId, result);
          appendStatus(`成功:${spaceId}`);
        } catch (error) {
          failCount++;
          console.error('加入失败:', spaceId, error);
          appendStatus(`失败:${spaceId},${error.message}`);
        }
      }

      appendStatus(`\n全部处理完成:成功 ${successCount} 个,失败 ${failCount} 个`);
    } catch (error) {
      console.error('批量加入失败:', error);
      appendStatus(`失败:${error.message}`);
    } finally {
      button.disabled = false;
      button.textContent = '开始加入';
    }
  }

  button.addEventListener('click', joinSpaces);
})();
6
4
登录后参与讨论。

4 条回复

排序
  1. mahao2200 白银 4 个月前

    好东西,可以用

    2
  2. metucky 白银 4 个月前

    好东西

    1
  3. ccw 黄金 4 个月前

    有意思

    1
  4. lingfan 黄金 4 个月前

    牛

    0