GPT4 Model Switcher

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

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

// ==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;
  });
})();

QingJ © 2025

镜像随时可能失效,请加Q群300939539或关注我们的公众号极客氢云获取最新地址