GPT4 Model Switcher

切换 OpenAI GPT-4 使用的模型(gpt-4 和 gpt-4-mobile)。

目前為 2023-05-27 提交的版本,檢視 最新版本

您需要先安裝使用者腳本管理器擴展,如 TampermonkeyGreasemonkeyViolentmonkey 之後才能安裝該腳本。

You will need to install an extension such as Tampermonkey to install this script.

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyViolentmonkey 後才能安裝該腳本。

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyUserscripts 後才能安裝該腳本。

你需要先安裝一款使用者腳本管理器擴展,比如 Tampermonkey,才能安裝此腳本

您需要先安裝使用者腳本管理器擴充功能後才能安裝該腳本。

(我已經安裝了使用者腳本管理器,讓我安裝!)

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

(我已經安裝了使用者樣式管理器,讓我安裝!)

// ==UserScript==
// @name        GPT4 Model Switcher
// @namespace   https://github.com/LShang001
// @description 切换 OpenAI GPT-4 使用的模型(gpt-4 和 gpt-4-mobile)。
// @author       LShang
// @license MIT
// @match       https://chat.openai.com/*
// @match       https://chat.zhile.io/*
// @version     2
// @grant       none
// ==/UserScript==

(function () {
  'use strict';

  // 读取本地存储的脚本状态
  let isScriptEnabled = localStorage.getItem('isScriptEnabled') === 'true';
  // 根据当前脚本状态确定正在使用的模型
  let modelInUse = isScriptEnabled ? 'gpt-4-mobile' : 'gpt-4';

  // 创建并插入新的 CSS 样式
  const style = document.createElement('style');
  style.innerHTML = `
    /* 设置开关的样式 */
  `;
  document.head.appendChild(style);

  // 创建切换按钮
  const toggleButton = document.createElement('label');
  toggleButton.className = 'toggle-button';

  // 创建并添加显示模型名称的文本
  const toggleText = document.createElement('span');
  toggleText.textContent = 'Model: ' + modelInUse;
  toggleButton.appendChild(toggleText);

  // 创建并添加切换输入元素
  const toggleInput = document.createElement('input');
  toggleInput.type = 'checkbox';
  toggleInput.checked = isScriptEnabled;
  toggleInput.addEventListener('change', toggleScript);
  toggleButton.appendChild(toggleInput);

  // 创建并添加滑块元素
  const slider = document.createElement('span');
  slider.className = 'slider';
  toggleButton.appendChild(slider);

  // 将切换按钮添加到页面中
  document.body.appendChild(toggleButton);

  // 定义切换脚本状态的函数
  function toggleScript() {
    // 更新脚本状态
    isScriptEnabled = !isScriptEnabled;
    // 存储新的脚本状态
    localStorage.setItem('isScriptEnabled', isScriptEnabled);
    // 更新当前使用的模型
    modelInUse = isScriptEnabled ? 'gpt-4-mobile' : 'gpt-4';
    // 更新显示的模型名称
    toggleText.textContent = 'Model: ' + modelInUse;
  }

  // 保存原始的 fetch 函数
  const originalFetch = window.fetch;

  // 定义修改过的 fetch 函数
  function modifiedFetch(url, init) {
    // 如果脚本处于禁用状态,直接使用原始的 fetch 函数
    if (!isScriptEnabled) {
      return originalFetch(url, init);
    }
    try {
      // 检查请求的方法、体和头部
      if (init && init.method === 'POST' && init.body && init.headers['Content-Type'] === 'application/json') {
        // 解析请求体中的 JSON 数据
        let data = JSON.parse(init.body);
        // 检查数据中是否包含 model 属性
        if (data.hasOwnProperty('model')) {
          // 切换模型
          data.model = data.model === 'gpt-4' ? 'gpt-4-mobile' : 'gpt-4';
          // 更新请求体中的数据
          init.body = JSON.stringify(data);
        }
      }
      // 使用原始的 fetch 函数发送修改过的请求
      return originalFetch(url, init);
    } catch (e) {
      // 如果在处理请求时出现错误,记录错误信息并使用原始的 fetch 函数发送请求
      console.error('在处理请求时出现错误:', e);
      return originalFetch(url, init);
    }
  }

  // 在页面加载完成后替换 fetch 函数
  window.addEventListener('load', () => {
    window.fetch = modifiedFetch;
  });
})();