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     4.0
// @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) {
    if (!isScriptEnabled) {
      return originalFetch(url, init);
    }
    try {
      if (init && init.method === 'POST' && init.body && init.headers['Content-Type'] === 'application/json') {
        let data = JSON.parse(init.body);
        if (data.hasOwnProperty('model')) {
          if (data.model === 'gpt-4' || data.model === 'gpt-4-mobile') {
            data.model = modelInUse;
            init.body = JSON.stringify(data);
          } else {
            // 如果模型不是 'gpt-4' 或 'gpt-4-mobile',则不进行切换,并更新显示的模型名称
            toggleText.textContent = 'Model: ' + data.model;
          }
        }
      }
      return originalFetch(url, init);
    } catch (e) {
      console.error('在处理请求时出现错误:', e);
      return originalFetch(url, init);
    }
  }

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