ChatGPT默认为GPT4(仅PLUS可用)

ChatGPT switch The default is GPT4

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

  1. // ==UserScript==
  2. // @name ChatGPT is GPT4 by default(PLUS only available)
  3. // @name:zh-CN ChatGPT默认为GPT4(仅PLUS可用)
  4. // @description ChatGPT switch The default is GPT4
  5. // @description:zh-cn ChatGPT switch The default is GPT4
  6. // @version 0.1.5
  7. // @icon https://chat.openai.com/favicon.ico
  8. // @match https://chat.openai.com/*
  9. // @grant GM_registerMenuCommand
  10. // @grant GM_unregisterMenuCommand
  11. // @grant GM_getValue
  12. // @grant GM_setValue
  13. // @namespace https://greasyfork.org/zh-CN/scripts/464284-chatgpt-is-gpt4-by-default-plus-only-available
  14. // @license MIT
  15. // ==/UserScript==
  16.  
  17. (function () {
  18. 'use strict';
  19. const BUTTONS_GROUPS = ['Default (GPT-3.5)', 'Pluginsalpha', 'GPT-4']
  20. const DEFAULT_BUTTON = 'GPT-4'
  21. let menus = []
  22. let isSwitch = false;
  23.  
  24. const getOtherInfo = () => {
  25. const defaultValue = GM_getValue('defaultModel')
  26. return BUTTONS_GROUPS.filter((buttonText) => buttonText !== defaultValue).map((buttonText) => `Model${buttonText}`);
  27. }
  28.  
  29. // 注册脚本菜单
  30. const registerMenuCommand = () => {
  31. const onHandle = (value) => {
  32. GM_setValue('defaultModel', value)
  33. registerMenuCommand()
  34. }
  35. if (!GM_getValue('defaultModel')) GM_setValue('defaultModel', DEFAULT_BUTTON)
  36. const defaultValue = GM_getValue('defaultModel')
  37. menus.forEach(menu => GM_unregisterMenuCommand(menu))
  38. menus = BUTTONS_GROUPS.map((buttonText) => GM_registerMenuCommand(`切换默认为:${buttonText}${defaultValue === buttonText ? '(当前)' : ''}`, () => onHandle(buttonText)))
  39. }
  40.  
  41. const clickListItemByTextContent = (textContent) => {
  42. const listItems = document.getElementsByTagName('li');
  43. for (let listItem of listItems) {
  44. if (listItem.textContent === textContent) {
  45. listItem.click();
  46. return;
  47. }
  48. }
  49. };
  50.  
  51. const switchModel = () => {
  52. const model = `${GM_getValue('defaultModel')}`
  53. setTimeout(() => clickListItemByTextContent(model), 0);
  54. };
  55.  
  56. const checkButton = (addedNode) => {
  57. const BUTTON_INFOS = getOtherInfo();
  58. if (addedNode.nodeType === Node.ELEMENT_NODE) {
  59. const buttons = addedNode.querySelectorAll('button');
  60. for (let button of buttons) {
  61. if (BUTTON_INFOS.includes(button.textContent)) {
  62. button.click();
  63. switchModel();
  64. return true;
  65. }
  66. }
  67. }
  68. return false;
  69. }
  70.  
  71. const handleClick = () => {
  72. isSwitch = true;
  73. }
  74.  
  75. // 监听newChat事件
  76. const addEventTargetA = () => {
  77. const buttons = document.querySelectorAll('a')
  78. for (const button of buttons) {
  79. if (button.textContent === 'New chat') {
  80. button.removeEventListener('click', handleClick)
  81. button.addEventListener('click', handleClick)
  82. break;
  83. }
  84. }
  85. }
  86.  
  87.  
  88.  
  89. const callback = (mutationRecords) => {
  90. for (const mutationRecord of mutationRecords) {
  91. if (mutationRecord.addedNodes.length) {
  92. for (const addedNode of mutationRecord.addedNodes) {
  93. if (checkButton(addedNode)) return;
  94. }
  95. }
  96. }
  97. addEventTargetA()
  98. };
  99. registerMenuCommand()
  100. addEventTargetA();
  101. const observer = new MutationObserver(callback);
  102. observer.observe(document.getElementById('__next'), {
  103. childList: true,
  104. subtree: true,
  105. });
  106.  
  107. // 修改pushStatus和replaceStatus
  108. const pushState = window.history.pushState;
  109. const replaceState = window.history.replaceState;
  110. window.history.pushState = function () {
  111. if (isSwitch) {
  112. setTimeout(() => checkButton(document.getElementById('__next')), 300)
  113. }
  114. pushState.apply(this, arguments);
  115. isSwitch = false
  116. }
  117. window.history.replaceState = function () {
  118. if (isSwitch) {
  119. setTimeout(() => checkButton(document.getElementById('__next')), 300)
  120. }
  121. replaceState.apply(this, arguments);
  122. isSwitch = false
  123. }
  124.  
  125. })();