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