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 5.0
  10. // @grant none
  11. // ==/UserScript==
  12.  
  13.  
  14. (function () {
  15. 'use strict';
  16.  
  17. let isScriptEnabled = localStorage.getItem('isScriptEnabled') === 'true';
  18. let modelInUse = isScriptEnabled ? 'gpt-4-mobile' : 'gpt-4';
  19.  
  20. const style = document.createElement('style');
  21. style.innerHTML = `
  22. .toggle-button {
  23. position: fixed;
  24. bottom: 10px;
  25. right: 10px;
  26. z-index: 9999;
  27. display: flex;
  28. align-items: center;
  29. gap: 10px;
  30. padding: 5px;
  31. background-color: #242424;
  32. border-radius: 5px;
  33. box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
  34. }
  35.  
  36. .toggle-button span {
  37. color: white;
  38. font-size: 16px;
  39. }
  40.  
  41. .toggle-button input {
  42. display: none;
  43. }
  44.  
  45. .slider {
  46. cursor: pointer;
  47. background-color: #ccc;
  48. transition: 0.4s;
  49. border-radius: 34px;
  50. width: 60px;
  51. height: 34px;
  52. position: relative;
  53. }
  54.  
  55. .slider:before {
  56. position: absolute;
  57. content: "";
  58. height: 26px;
  59. width: 26px;
  60. left: 4px;
  61. bottom: 4px;
  62. background-color: white;
  63. transition: 0.4s;
  64. border-radius: 50%;
  65. }
  66.  
  67. input:checked + .slider {
  68. background-color: #4CAF50;
  69. }
  70.  
  71. input:checked + .slider:before {
  72. transform: translateX(26px);
  73. }
  74. `;
  75. document.head.appendChild(style);
  76.  
  77. const toggleButton = document.createElement('label');
  78. toggleButton.className = 'toggle-button';
  79.  
  80. const toggleText = document.createElement('span');
  81. toggleText.textContent = 'Model: ' + modelInUse;
  82. toggleButton.appendChild(toggleText);
  83.  
  84. const toggleInput = document.createElement('input');
  85. toggleInput.type = 'checkbox';
  86. toggleInput.checked = isScriptEnabled;
  87. toggleInput.addEventListener('change', toggleScript);
  88. toggleButton.appendChild(toggleInput);
  89.  
  90. const slider = document.createElement('span');
  91. slider.className = 'slider';
  92. toggleButton.appendChild(slider);
  93.  
  94. document.body.appendChild(toggleButton);
  95.  
  96. function toggleScript() {
  97. isScriptEnabled = !isScriptEnabled;
  98. localStorage.setItem('isScriptEnabled', isScriptEnabled);
  99. modelInUse = isScriptEnabled ? 'gpt-4-mobile' : 'gpt-4';
  100. toggleText.textContent = 'Model: ' + modelInUse;
  101. }
  102.  
  103. const originalFetch = window.fetch;
  104.  
  105. function modifiedFetch(url, init) {
  106. if (!isScriptEnabled) {
  107. return originalFetch(url, init);
  108. }
  109. try {
  110. if (init && init.method === 'POST' && init.body && init.headers['Content-Type'] === 'application/json') {
  111. let data = JSON.parse(init.body);
  112. if (data.hasOwnProperty('model') && (data.model === 'gpt-4' || data.model === 'gpt-4-mobile')) {
  113. data.model = isScriptEnabled ? 'gpt-4-mobile' : 'gpt-4';
  114. init.body = JSON.stringify(data);
  115. }
  116. }
  117. return originalFetch(url, init);
  118. } catch (e) {
  119. console.error('在处理请求时出现错误:', e);
  120. return originalFetch(url, init);
  121. }
  122. }
  123.  
  124. window.addEventListener('load', () => {
  125. window.fetch = modifiedFetch;
  126. });
  127. })();