GPT4 Model Switcher

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

Versione datata 27/05/2023. Vedi la nuova versione l'ultima versione.

Dovrai installare un'estensione come Tampermonkey, Greasemonkey o Violentmonkey per installare questo script.

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

Dovrai installare un'estensione come Tampermonkey o Violentmonkey per installare questo script.

Dovrai installare un'estensione come Tampermonkey o Userscripts per installare questo script.

Dovrai installare un'estensione come ad esempio Tampermonkey per installare questo script.

Dovrai installare un gestore di script utente per installare questo script.

(Ho già un gestore di script utente, lasciamelo installare!)

Dovrai installare un'estensione come ad esempio Stylus per installare questo stile.

Dovrai installare un'estensione come ad esempio Stylus per installare questo stile.

Dovrai installare un'estensione come ad esempio Stylus per installare questo stile.

Dovrai installare un'estensione per la gestione degli stili utente per installare questo stile.

Dovrai installare un'estensione per la gestione degli stili utente per installare questo stile.

Dovrai installare un'estensione per la gestione degli stili utente per installare questo stile.

(Ho già un gestore di stile utente, lasciamelo installare!)

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