GPT4 Model Switcher

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

  1. // ==UserScript==
  2. // @name GPT4 Model Switcher
  3. // @description 切换 OpenAI GPT-4 使用的模型(gpt-4 和 gpt-4-mobile)仅plus用户可用。
  4. // @author LShang
  5. // @license MIT
  6. // @match https://chat.openai.com/*
  7. // @match https://chat.zhile.io/*
  8. // @version 6.0
  9. // @grant none
  10. // @namespace https://github.com/LShang001
  11. // ==/UserScript==
  12.  
  13. (function () {
  14. 'use strict';
  15.  
  16. // 获取localStorage中的脚本是否启用的状态,默认为 gpt-4
  17. let isScriptEnabled = localStorage.getItem('isScriptEnabled') === 'true';
  18. let modelInUse = isScriptEnabled ? 'gpt-4-mobile' : 'gpt-4';
  19.  
  20. // 创建并插入样式,用于显示切换按钮
  21. const style = document.createElement('style');
  22. style.innerHTML = `
  23. .toggle-button {
  24. position: fixed;
  25. bottom: 10px;
  26. right: 10px;
  27. z-index: 9999;
  28. display: flex;
  29. align-items: center;
  30. gap: 10px;
  31. padding: 5px;
  32. background-color: #242424;
  33. border-radius: 5px;
  34. box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
  35. }
  36.  
  37. .toggle-button span {
  38. color: white;
  39. font-size: 16px;
  40. }
  41.  
  42. .toggle-button input {
  43. display: none;
  44. }
  45.  
  46. .slider {
  47. cursor: pointer;
  48. background-color: #ccc;
  49. transition: 0.4s;
  50. border-radius: 34px;
  51. width: 60px;
  52. height: 34px;
  53. position: relative;
  54. }
  55.  
  56. .slider:before {
  57. position: absolute;
  58. content: "";
  59. height: 26px;
  60. width: 26px;
  61. left: 4px;
  62. bottom: 4px;
  63. background-color: white;
  64. transition: 0.4s;
  65. border-radius: 50%;
  66. }
  67.  
  68. input:checked + .slider {
  69. background-color: #4CAF50;
  70. }
  71.  
  72. input:checked + .slider:before {
  73. transform: translateX(26px);
  74. }
  75. `;
  76. document.head.appendChild(style);
  77.  
  78. // 创建切换按钮和相关元素
  79. const toggleButton = document.createElement('label');
  80. toggleButton.className = 'toggle-button';
  81.  
  82. const toggleText = document.createElement('span');
  83. toggleText.textContent = 'Model: ' + modelInUse;
  84. toggleButton.appendChild(toggleText);
  85.  
  86. const toggleInput = document.createElement('input');
  87. toggleInput.type = 'checkbox';
  88. toggleInput.checked = isScriptEnabled;
  89. // 当切换状态时,改变localStorage中的脚本启用状态,并更新模型名称
  90. toggleInput.addEventListener('change', toggleScript);
  91. toggleButton.appendChild(toggleInput);
  92.  
  93. const slider = document.createElement('span');
  94. slider.className = 'slider';
  95. toggleButton.appendChild(slider);
  96.  
  97. document.body.appendChild(toggleButton);
  98.  
  99. // 切换脚本的启用状态,并更新模型名称
  100. function toggleScript() {
  101. isScriptEnabled = !isScriptEnabled;
  102. localStorage.setItem('isScriptEnabled', isScriptEnabled);
  103. modelInUse = isScriptEnabled ? 'gpt-4-mobile' : 'gpt-4';
  104. toggleText.textContent = 'Model: ' + modelInUse;
  105. }
  106.  
  107. // 保存原始的fetch函数
  108. const originalFetch = window.fetch;
  109.  
  110. // 修改fetch函数,对于POST请求的模型参数进行修改
  111. function modifiedFetch(url, init) {
  112. if (!isScriptEnabled) {
  113. return originalFetch(url, init);
  114. }
  115. try {
  116. if (init && init.method === 'POST' && init.body && init.headers['Content-Type'] === 'application/json') {
  117. let data = JSON.parse(init.body);
  118. if (data.hasOwnProperty('model') && (data.model === 'gpt-4' || data.model === 'gpt-4-mobile')) {
  119. data.model = isScriptEnabled ? 'gpt-4-mobile' : 'gpt-4';
  120. init.body = JSON.stringify(data);
  121. }
  122. }
  123. return originalFetch(url, init);
  124. } catch (e) {
  125. console.error('在处理请求时出现错误:', e);
  126. return originalFetch(url, init);
  127. }
  128. }
  129.  
  130. // 在页面加载后替换原始的fetch函数
  131. window.addEventListener('load', () => {
  132. window.fetch = modifiedFetch;
  133. });
  134. })();