GPT4 Model Switcher

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

当前为 2023-05-27 提交的版本,查看 最新版本

您需要先安装一个扩展,例如 篡改猴Greasemonkey暴力猴,之后才能安装此脚本。

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

您需要先安装一个扩展,例如 篡改猴暴力猴,之后才能安装此脚本。

您需要先安装一个扩展,例如 篡改猴Userscripts ,之后才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 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     1
// @grant       none
// ==/UserScript==

(function () {
  'use strict';

  let isScriptEnabled = localStorage.getItem('isScriptEnabled') === 'true';
  let modelInUse = isScriptEnabled ? 'gpt-4-mobile' : 'gpt-4';

  const style = document.createElement('style');
  style.innerHTML = `
    .toggle-button {
      position: fixed;
      bottom: 10px;
      right: 10px;
      z-index: 9999;
      display: flex;
      align-items: center;
      gap: 10px;
      padding: 5px;
      background-color: #242424;
      border-radius: 5px;
      box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
    }

    .toggle-button span {
      color: white;
      font-size: 16px;
    }

    .toggle-button input {
      display: none;
    }

    .slider {
      cursor: pointer;
      background-color: #ccc;
      transition: 0.4s;
      border-radius: 34px;
      width: 60px;
      height: 34px;
      position: relative;
    }

    .slider:before {
      position: absolute;
      content: "";
      height: 26px;
      width: 26px;
      left: 4px;
      bottom: 4px;
      background-color: white;
      transition: 0.4s;
      border-radius: 50%;
    }

    input:checked + .slider {
      background-color: #4CAF50;
    }

    input:checked + .slider:before {
      transform: translateX(26px);
    }
  `;
  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;
  }

  const originalFetch = window.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')) {
          data.model = data.model === 'gpt-4' ? 'gpt-4-mobile' : 'gpt-4';
          init.body = JSON.stringify(data);
        }
      }
      return originalFetch(url, init);
    } catch (e) {
      console.error('在处理请求时出现错误:', e);
      return originalFetch(url, init);
    }
  }

  window.addEventListener('load', () => {
    window.fetch = modifiedFetch;
  });
})();