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.4
  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. }
  47. }
  48. };
  49.  
  50. const switchModel = () => {
  51. const model = `${GM_getValue('defaultModel')}`
  52. setTimeout(() => clickListItemByTextContent(model), 0);
  53. };
  54.  
  55. const checkButton = (addedNode) => {
  56. const BUTTON_INFOS = getOtherInfo();
  57. if (addedNode.nodeType === Node.ELEMENT_NODE) {
  58. const buttons = addedNode.querySelectorAll('button');
  59. for (let button of buttons) {
  60. if (BUTTON_INFOS.includes(button.textContent)) {
  61. button.click();
  62. switchModel();
  63. return true;
  64. }
  65. }
  66. }
  67. return false;
  68. }
  69.  
  70. const handleClick = () => {
  71. isSwitch = true;
  72. }
  73.  
  74. // 监听newChat事件
  75. const addEventTargetA = () => {
  76. const buttons = document.querySelectorAll('a')
  77. for (const button of buttons) {
  78. if (button.textContent === 'New chat') {
  79. button.removeEventListener('click', handleClick)
  80. button.addEventListener('click', handleClick)
  81. break;
  82. }
  83. }
  84. }
  85.  
  86.  
  87.  
  88. const callback = (mutationRecords) => {
  89. for (const mutationRecord of mutationRecords) {
  90. if (mutationRecord.addedNodes.length) {
  91. for (const addedNode of mutationRecord.addedNodes) {
  92. if (checkButton(addedNode)) return;
  93. }
  94. }
  95. }
  96. addEventTargetA()
  97. };
  98. registerMenuCommand()
  99. addEventTargetA();
  100. const observer = new MutationObserver(callback);
  101. observer.observe(document.getElementById('__next'), {
  102. childList: true,
  103. subtree: true,
  104. });
  105.  
  106. // 修改pushStatus和replaceStatus
  107. const pushState = window.history.pushState;
  108. const replaceState = window.history.replaceState;
  109. window.history.pushState = function () {
  110. if (isSwitch) {
  111. setTimeout(() => checkButton(document.getElementById('__next')), 300)
  112. }
  113. pushState.apply(this, arguments);
  114. isSwitch = false
  115. }
  116. window.history.replaceState = function () {
  117. if (isSwitch) {
  118. setTimeout(() => checkButton(document.getElementById('__next')), 300)
  119. }
  120. replaceState.apply(this, arguments);
  121. isSwitch = false
  122. }
  123.  
  124. })();