GPT4 Model Switcher

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

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

  1. // ==UserScript==
  2. // @name GPT4 Model Switcher
  3. // @namespace https://github.com/LShang001
  4. // @description 切换 OpenAI GPT-4 使用的模型(gpt-4 和 gpt-4-mobile)。
  5. // @author LShang
  6. // @license MIT
  7. // @match https://chat.openai.com/*
  8. // @match https://chat.zhile.io/*
  9. // @version 3.0
  10. // @grant none
  11. // ==/UserScript==
  12.  
  13. (function () {
  14. 'use strict';
  15.  
  16. // 读取本地存储的脚本状态
  17. let isScriptEnabled = localStorage.getItem('isScriptEnabled') === 'true';
  18. // 根据当前脚本状态确定正在使用的模型
  19. let modelInUse = isScriptEnabled ? 'gpt-4-mobile' : 'gpt-4';
  20.  
  21. // 创建并插入新的 CSS 样式
  22. const style = document.createElement('style');
  23. style.innerHTML = `
  24. /* 设置开关的样式 */
  25. `;
  26. document.head.appendChild(style);
  27.  
  28. // 创建切换按钮
  29. const toggleButton = document.createElement('label');
  30. toggleButton.className = 'toggle-button';
  31.  
  32. // 创建并添加显示模型名称的文本
  33. const toggleText = document.createElement('span');
  34. toggleText.textContent = 'Model: ' + modelInUse;
  35. toggleButton.appendChild(toggleText);
  36.  
  37. // 创建并添加切换输入元素
  38. const toggleInput = document.createElement('input');
  39. toggleInput.type = 'checkbox';
  40. toggleInput.checked = isScriptEnabled;
  41. toggleInput.addEventListener('change', toggleScript);
  42. toggleButton.appendChild(toggleInput);
  43.  
  44. // 创建并添加滑块元素
  45. const slider = document.createElement('span');
  46. slider.className = 'slider';
  47. toggleButton.appendChild(slider);
  48.  
  49. // 将切换按钮添加到页面中
  50. document.body.appendChild(toggleButton);
  51.  
  52. // 定义切换脚本状态的函数
  53. function toggleScript() {
  54. // 更新脚本状态
  55. isScriptEnabled = !isScriptEnabled;
  56. // 存储新的脚本状态
  57. localStorage.setItem('isScriptEnabled', isScriptEnabled);
  58. // 更新当前使用的模型
  59. modelInUse = isScriptEnabled ? 'gpt-4-mobile' : 'gpt-4';
  60. // 更新显示的模型名称
  61. toggleText.textContent = 'Model: ' + modelInUse;
  62. }
  63.  
  64. // 保存原始的 fetch 函数
  65. const originalFetch = window.fetch;
  66.  
  67. // 定义修改过的 fetch 函数
  68. function modifiedFetch(url, init) {
  69. // 如果脚本处于禁用状态,直接使用原始的 fetch 函数
  70. if (!isScriptEnabled) {
  71. return originalFetch(url, init);
  72. }
  73. try {
  74. // 检查请求的方法、体和头部
  75. if (init && init.method === 'POST' && init.body && init.headers['Content-Type'] === 'application/json') {
  76. // 解析请求体中的 JSON 数据
  77. let data = JSON.parse(init.body);
  78. // 检查数据中是否包含 model 属性
  79. if (data.hasOwnProperty('model')) {
  80. // 切换模型
  81. data.model = data.model === 'gpt-4' ? 'gpt-4-mobile' : 'gpt-4';
  82. // 更新请求体中的数据
  83. init.body = JSON.stringify(data);
  84. }
  85. }
  86. // 使用原始的 fetch 函数发送修改过的请求
  87. return originalFetch(url, init);
  88. } catch (e) {
  89. // 如果在处理请求时出现错误,记录错误信息并使用原始的 fetch 函数发送请求
  90. console.error('在处理请求时出现错误:', e);
  91. return originalFetch(url, init);
  92. }
  93. }
  94.  
  95. // 在页面加载完成后替换 fetch 函数
  96. window.addEventListener('load', () => {
  97. window.fetch = modifiedFetch;
  98. });
  99. })();