ChatGPT默认为GPT4(仅PLUS可用)

ChatGPT switch The default is GPT4

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

  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.7
  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 = ['GPT-3.5', 'GPT-4']
  20. const DEFAULT_BUTTON = 'GPT-4'
  21. let menus = []
  22. let isSwitch = false;
  23.  
  24. // 注册脚本菜单
  25. const registerMenuCommand = () => {
  26. const onHandle = (value) => {
  27. GM_setValue('defaultModel', value)
  28. registerMenuCommand()
  29. }
  30. if (!GM_getValue('defaultModel')) GM_setValue('defaultModel', DEFAULT_BUTTON)
  31. const defaultValue = GM_getValue('defaultModel')
  32. menus.forEach(menu => GM_unregisterMenuCommand(menu))
  33. menus = BUTTONS_GROUPS.map((buttonText) => GM_registerMenuCommand(`切换默认为:${buttonText}${defaultValue === buttonText ? '(当前)' : ''}`, () => onHandle(buttonText)))
  34. }
  35.  
  36. const checkButton = (addedNode) => {
  37. const model = `${GM_getValue('defaultModel')}`
  38. if (addedNode.nodeType === Node.ELEMENT_NODE) {
  39. const buttons = addedNode.querySelectorAll('button');
  40. for (let button of buttons) {
  41. if (button.textContent === model) {
  42. button.querySelector('span')?.click();
  43. button.querySelector('span')?.click();
  44. return true;
  45. }
  46. }
  47. }
  48. return false;
  49. }
  50.  
  51. const handleClick = () => {
  52. isSwitch = true;
  53. }
  54.  
  55. // 监听newChat事件
  56. const addEventTargetA = () => {
  57. const buttons = document.querySelectorAll('a')
  58. for (const button of buttons) {
  59. if (button.textContent === 'New chat') {
  60. button.removeEventListener('click', handleClick)
  61. button.addEventListener('click', handleClick)
  62. break;
  63. }
  64. }
  65. }
  66.  
  67.  
  68.  
  69. const callback = (mutationRecords) => {
  70. for (const mutationRecord of mutationRecords) {
  71. if (mutationRecord.addedNodes.length) {
  72. for (const addedNode of mutationRecord.addedNodes) {
  73. if (checkButton(addedNode)) return;
  74. }
  75. }
  76. }
  77. addEventTargetA()
  78. };
  79. registerMenuCommand()
  80. addEventTargetA();
  81. const observer = new MutationObserver(callback);
  82. observer.observe(document.getElementById('__next'), {
  83. childList: true,
  84. subtree: true,
  85. });
  86.  
  87. // 修改pushStatus和replaceStatus
  88. const pushState = window.history.pushState;
  89. const replaceState = window.history.replaceState;
  90. window.history.pushState = function () {
  91. if (isSwitch) {
  92. setTimeout(() => checkButton(document.getElementById('__next')), 300)
  93. }
  94. pushState.apply(this, arguments);
  95. isSwitch = false
  96. }
  97. window.history.replaceState = function () {
  98. if (isSwitch) {
  99. setTimeout(() => checkButton(document.getElementById('__next')), 300)
  100. }
  101. replaceState.apply(this, arguments);
  102. isSwitch = false
  103. }
  104.  
  105. })();