ChatGPT Enhance

宽度对话框 & 一键清空聊天记录 & 向GPT声明指定语言回复

  1. // ==UserScript==
  2. // @name ChatGPT Enhance
  3. // @name:en ChatGPT Enhance
  4. // @name:zh-CN ChatGPT 增强
  5. // @name:zh-TW ChatGPT 增強
  6. // @name:ja ChatGPT 拡張
  7. // @name:ko ChatGPT 향상
  8. // @name:de ChatGPT verbessern
  9. // @name:fr ChatGPT améliorer
  10. // @name:es ChatGPT mejorar
  11. // @name:pt ChatGPT melhorar
  12. // @name:ru ChatGPT улучшить
  13. // @name:it ChatGPT migliorare
  14. // @name:tr ChatGPT geliştirmek
  15. // @name:ar ChatGPT تحسين
  16. // @name:th ChatGPT ปรับปรุง
  17. // @name:vi ChatGPT cải thiện
  18. // @name:id ChatGPT meningkatkan
  19. // @namespace Violentmonkey Scripts
  20. // @match *://chat.openai.com/*
  21. // @match *://chatgpt.com/*
  22. // @version XiaoYing_2024.10.12.1
  23. // @grant GM_info
  24. // @grant GM_getValue
  25. // @grant GM_setValue
  26. // @grant GM_addStyle
  27. // @grant GM_deleteValue
  28. // @grant GM_xmlhttpRequest
  29. // @grant GM_setClipboard
  30. // @grant GM_registerMenuCommand
  31. // @grant GM_unregisterMenuCommand
  32. // @grant GM_getResourceText
  33. // @grant GM_getResourceURL
  34. // @grant GM_openInTab
  35. // @grant unsafeWindow
  36. // @run-at document-start
  37. // @author github.com @XiaoYingYo
  38. // @require https://greasyfork.org/scripts/464929-module-jquery-xiaoying/code/module_jquery_XiaoYing.js
  39. // @require https://greasyfork.org/scripts/464780-global-module/code/global_module.js
  40. // @require https://greasyfork.org/scripts/465643-ajaxhookerlatest/code/ajaxHookerLatest.js
  41. // @require https://greasyfork.org/scripts/440334-jquery-like-spa-operation-library/code/jQuery-like%20SPA%20operation%20library.js
  42. // @description 宽度对话框 & 一键清空聊天记录 & 向GPT声明指定语言回复
  43. // @description:en Wide dialog & Clear chat history & Declare specified language reply to GPT
  44. // @description:zh-CN 宽度对话框 & 一键清空聊天记录 & 向GPT声明指定语言回复
  45. // @description:zh-TW 寬度對話框 & 一鍵清空聊天記錄 & 向GPT聲明指定語言回復
  46. // @description:ja 幅広いダイアログ & チャット履歴をクリア & 指定された言語でGPTに宣言する
  47. // @description:ko 넓은 대화 상자 & 채팅 기록 지우기 & 지정된 언어로 GPT에 선언
  48. // @description:de Breites Dialogfeld & Chatverlauf löschen & GPT in angegebener Sprache deklarieren
  49. // @description:fr Boîte de dialogue large & Effacer l'historique du chat & Déclarer la réponse dans la langue spécifiée à GPT
  50. // @description:es Cuadro de diálogo ancho & Borrar el historial del chat & Declarar respuesta en el idioma especificado a GPT
  51. // @description:pt Caixa de diálogo ampla & Limpar o histórico do bate-papo & Declarar resposta no idioma especificado ao GPT
  52. // @description:ru Широкий диалоговое окно & Очистить историю чата & Объявить ответ на указанном языке в GPT
  53. // @description:it Ampia finestra di dialogo & Cancella la cronologia della chat & Dichiarare la risposta nella lingua specificata a GPT
  54. // @description:tr Geniş diyalog & Sohbet geçmişini temizle & GPT'ye belirtilen dilde yanıt bildir
  55. // @description:ar مربع حوار واسع & مسح سجل المحادثة & إعلان الرد باللغة المحددة إلى GPT
  56. // @description:th กล่องโต้ตอบกว้าง & ล้างประวัติการแชท & ประกาศการตอบกลับในภาษาที่ระบุไว้กับ GPT
  57. // @description:vi Hộp thoại rộng & Xóa lịch sử trò chuyện & Khai báo trả lời bằng ngôn ngữ được chỉ định cho GPT
  58. // @description:id Kotak dialog lebar & Hapus riwayat obrolan & Nyatakan balasan dalam bahasa yang ditentukan ke GPT
  59. // ==/UserScript==
  60.  
  61. // eslint-disable-next-line no-undef
  62. ajaxHooker.protect();
  63.  
  64. var globalVariable = new Map();
  65. var browserLanguage = navigator.language;
  66. var ignoreHookStr = '&ignoreHookStr';
  67. var clearButtonSvg = '<svg t="1722633865116" class="icon" viewBox="0 0 1024 1024" version="1.1" xmlns="http://www.w3.org/2000/svg" p-id="1793" width="24" height="24"><path d="M901.3 504.8l-76.3-150c-13.4-26.3-40-42.6-69.5-42.6H639c-1.1 0-2-0.9-2-2V120.6c0-31.1-25.3-56.3-56.3-56.3h-90c-31.1 0-56.3 25.3-56.3 56.3v189.6c0 1.1-0.9 2-2 2H315.8c-29.5 0-56.1 16.3-69.5 42.6l-76.3 150c-9.2 18.1-8.4 39.3 2.2 56.6 10.3 16.8 27.9 27 47.4 27.6-4.8 101-38.3 205.9-90.2 279.5-12.5 17.8-14.1 40.8-4.1 60.1 10 19.3 29.7 31.3 51.5 31.3h601.5c35 0 66-23.6 75.2-57.4 15.5-56.5 28.4-107.9 29.4-164.9C884 685 874 636 852.9 589c19-1.1 36.1-11.2 46.2-27.6 10.6-17.3 11.4-38.5 2.2-56.6z m-681.4 25.4l76.3-150c3.8-7.4 11.3-12 19.6-12h116.4c32 0 58-26 58-58V120.6c0-0.1 0.2-0.3 0.3-0.3h90c0.1 0 0.3 0.2 0.3 0.3v189.6c0 32 26 58 58 58h116.4c8.3 0 15.8 4.6 19.6 12l76.3 150c0.2 0.3 0.5 1-0.1 2s-1.3 1-1.7 1H221.7c-0.4 0-1.1 0-1.7-1-0.6-1-0.3-1.7-0.1-2zM827 736.6c-0.9 50.5-12.9 98.3-27.4 151.1-2.6 9.5-11.3 16.2-21.2 16.2H651.8c11.3-22.3 18.5-44 23.1-61.2 7.1-26.7 10.7-53.5 10.6-78-0.1-17.1-15.5-30.1-32.4-27.4-13.6 2.2-23.6 14-23.6 27.8 0.1 42.7-14.1 98.2-42.7 138.8H406.2c15.2-21.7 26.1-43.8 33.6-61.9 10-24.3 17.4-49.7 21.2-72.5 2.8-17-10.4-32.5-27.6-32.5-13.6 0-25.3 9.8-27.6 23.3-2.8 16.6-8.3 37.7-17.7 60.4-10.1 24.6-27.8 58.1-55.6 83.3H176.9c-0.5 0-1.2 0-1.8-1.1-0.6-1.1-0.2-1.6 0.1-2 29.7-42.1 54.8-94.5 72.5-151.4 16.2-52.1 25.7-106.9 28-160.3h514.6C816 635.6 828 684 827 736.6z" fill="#ffffff" p-id="1794"></path></svg>';
  68.  
  69. (async function () {
  70. function initSession() {
  71. return new Promise((resolve) => {
  72. $.get('/api/auth/session', { headers: { Accept: 'application/json', 'Content-Type': 'application/json' } }).done((res) => {
  73. globalVariable.set('session', res);
  74. globalVariable.set('accessToken', res.accessToken);
  75. resolve();
  76. });
  77. });
  78. }
  79.  
  80. function clearAllConversations() {
  81. return new Promise(async (resolve) => {
  82. let data = await getAllItems();
  83. let Tasks = [];
  84. for (let i = 0; i < data.length; i++) {
  85. let item = data[i];
  86. let id = item.id;
  87. let title = item.title;
  88. if (title.charAt(0) == '#') {
  89. continue;
  90. }
  91. Tasks.push(deleteItem(id));
  92. }
  93. await Promise.all(Tasks);
  94. resolve();
  95. // $.ajax({
  96. // type: 'PATCH',
  97. // url: '/backend-api/conversations',
  98. // headers: { Accept: 'application/json', 'Content-Type': 'application/json', Authorization: `Bearer ${globalVariable.get('accessToken')}` },
  99. // data: JSON.stringify({ is_visible: false }),
  100. // success: (res) => {
  101. // resolve(res);
  102. // }
  103. // });
  104. });
  105. }
  106.  
  107. function initClearButton() {
  108. return new Promise(async (resolve) => {
  109. let sureClearButtonSvg = '<svg t="1722633889519" class="icon" viewBox="0 0 1024 1024" version="1.1" xmlns="http://www.w3.org/2000/svg" p-id="3987" width="24" height="24"><path d="M675.328 117.717333A425.429333 425.429333 0 0 0 512 85.333333C276.352 85.333333 85.333333 276.352 85.333333 512s191.018667 426.666667 426.666667 426.666667 426.666667-191.018667 426.666667-426.666667c0-56.746667-11.093333-112-32.384-163.328a21.333333 21.333333 0 0 0-39.402667 16.341333A382.762667 382.762667 0 0 1 896 512c0 212.074667-171.925333 384-384 384S128 724.074667 128 512 299.925333 128 512 128c51.114667 0 100.8 9.984 146.986667 29.12a21.333333 21.333333 0 0 0 16.341333-39.402667z m-213.333333 468.608l-105.664-105.642666a21.248 21.248 0 0 0-30.122667 0.042666c-8.32 8.32-8.213333 21.973333-0.064 30.101334l120.810667 120.832a21.248 21.248 0 0 0 30.122666-0.085334l211.157334-211.157333a21.290667 21.290667 0 0 0 0-30.186667 21.397333 21.397333 0 0 0-30.250667 0.106667l-196.010667 195.989333z" fill="#ffffff" p-id="3988"></path></svg>';
  110. let oneBtn = await getNav();
  111. oneBtn = oneBtn.find('button').eq(0);
  112. let newBtn = global_module.cloneAndHide(oneBtn[0]);
  113. newBtn = $(newBtn).eq(0).attr('status', 0);
  114. oneBtn.show();
  115. newBtn.find('svg').remove();
  116. newBtn.append(clearButtonSvg);
  117. newBtn.off('click').on('click', async () => {
  118. let status = newBtn.attr('status');
  119. newBtn.attr('disabled', 'disabled');
  120. if (status == 0) {
  121. newBtn.attr('status', 1);
  122. newBtn.find('svg').remove();
  123. newBtn.append(sureClearButtonSvg);
  124. } else {
  125. newBtn.attr('status', 0);
  126. newBtn.find('svg').remove();
  127. newBtn.append(clearButtonSvg);
  128. await clearAllConversations();
  129. unsafeWindow.location.href = '/';
  130. }
  131. newBtn.removeAttr('disabled');
  132. });
  133. globalVariable.set('oneBtn', oneBtn);
  134. resolve();
  135. });
  136. }
  137.  
  138. function initIncognitoModeButton() {
  139. return new Promise(async (resolve) => {
  140. let oneBtn = globalVariable.get('oneBtn');
  141. let yesSvg = '<svg t="1723101878912" class="icon" viewBox="0 0 1024 1024" version="1.1" xmlns="http://www.w3.org/2000/svg" p-id="1489" width="20" height="20"><path d="M57.138023 960l-4.916377 2.253977c6.984386 3.50514 11.778354 10.731991 11.778354 19.079356v-21.333333h-6.861977z m6.861977-3.146152l90.17909-41.350768c60.799706-38.04572 137.66091-38.940248 199.217434-2.68594l114.954005 33.609563 8.144919 5.429554c0.986336 0.657949 4.247834 2.026814 9.755072 3.492194 6.686602 1.778464 15.168147 3.255614 25.343411 4.327871 10.816736-1.261756 19.773793-2.871908 26.74994-4.728055 5.555494-1.479503 8.671044-2.770685 9.155973-3.093187l8.144919-5.430731 114.935173-33.603678c61.55417-36.256662 138.414198-35.364487 199.21508 2.677701l90.204984 41.356653V512c0-247.423117-200.576883-448-448-448S64 264.576883 64 512v444.853848z m896 3.146152v21.333333c0-8.347366 4.793968-15.574216 11.778354-19.079356l-4.917554-2.253977h-6.8608zM512 0c282.769949 0 512 229.230051 512 512v469.333333c0 23.56377-19.102897 42.666667-42.666667 42.666667h-28.444836l-113.909996-52.224c-42.99623-28.661407-99.00903-28.66023-142.002906 0.005885l-113.972377 33.322372C569.069756 1014.395586 545.402409 1020.692598 512 1024c-32.605572-2.774216-56.27292-9.071228-71.005572-18.892211l-113.993564-33.329435c-42.993876-28.662584-99.007853-28.662584-142.002905 0L71.110326 1024H42.666667C19.102897 1024 0 1004.897103 0 981.333333V512C0 229.230051 229.230051 0 512 0z m-48.70709 373.333922h97.41418c16.914832-49.634575 63.935264-85.333333 119.292469-85.333333h42.000478c69.587274 0 125.99908 56.411807 125.99908 125.99908v4.000662c0 69.587274-56.411807 125.99908-125.99908 125.99908h-42.000478c-55.357205 0-102.377637-35.698759-119.292469-85.333333h-97.41418c-16.914832 49.634575-63.935264 85.333333-119.292469 85.333333h-42.000478c-69.587274 0-125.99908-56.411807-125.99908-125.99908v-4.000662c0-69.587274 56.411807-125.99908 125.99908-125.99908h42.000478c55.357205 0 102.377637 35.698759 119.292469 85.333333z m279.707366 42.666667c11.598271 0 21.000239-9.551448 21.000239-21.333334s-9.401968-21.333333-21.000239-21.333333-21.000239 9.550271-21.000239 21.333333c0 11.781885 9.401968 21.333333 21.000239 21.333334z m-378.000773 0c11.598271 0 21.000239-9.551448 21.00024-21.333334s-9.401968-21.333333-21.00024-21.333333c-11.597094 0-20.999062 9.550271-20.999062 21.333333 0 11.781885 9.401968 21.333333 20.999062 21.333334zM464.001471 640h95.999412c26.50983 0 47.999706 21.489876 47.999706 47.999706v23.999264c0 39.765333-32.234814 72.001324-71.998971 72.001324h-48.000882c-39.764156 0-71.99897-32.235991-71.99897-72.000147v-23.999264c0-26.511007 21.489876-48.000883 47.998528-48.000883z" fill="#ffffff" p-id="1490"></path></svg>';
  142. let noSvg = '<svg t="1723101932727" class="icon" viewBox="0 0 1024 1024" version="1.1" xmlns="http://www.w3.org/2000/svg" p-id="2649" width="24" height="24"><path d="M512.812373 665.6c-85.333333 0-153.6-68.266667-153.6-153.6s68.266667-153.6 153.6-153.6 153.6 68.266667 153.6 153.6-68.266667 153.6-153.6 153.6z m0-221.866667c-38.4 0-68.266667 32-68.266666 68.266667s32 68.266667 68.266666 68.266667 68.266667-32 68.266667-68.266667-29.866667-68.266667-68.266667-68.266667z" p-id="2650" fill="#ffffff"></path><path d="M43.47904 554.666667c-4.266667 0-10.666667 0-14.933333-2.133334-21.333333-8.533333-32-34.133333-23.466667-55.466666 70.4-185.6 302.933333-341.333333 507.733333-341.333334 202.666667 0 435.2 153.6 507.733334 334.933334 8.533333 21.333333-2.133333 46.933333-23.466667 55.466666-21.333333 8.533333-46.933333-2.133333-55.466667-23.466666-59.733333-149.333333-260.266667-281.6-428.8-281.6-168.533333 0-373.333333 136.533333-430.933333 285.866666-4.266667 17.066667-21.333333 27.733333-38.4 27.733334z" p-id="2651" fill="#ffffff"></path><path d="M512.812373 868.266667c-204.8 0-439.466667-155.733333-509.866666-339.2-8.533333-21.333333 2.133333-46.933333 23.466666-55.466667 21.333333-8.533333 46.933333 2.133333 55.466667 23.466667 57.6 149.333333 262.4 285.866667 430.933333 285.866666 168.533333 0 369.066667-132.266667 428.8-281.6 8.533333-21.333333 34.133333-32 55.466667-23.466666 21.333333 8.533333 32 34.133333 23.466667 55.466666-72.533333 181.333333-305.066667 334.933333-507.733334 334.933334z" p-id="2652" fill="#ffffff"></path></svg>';
  143. oneBtn = oneBtn.eq(0);
  144. let newBtn = global_module.cloneAndHide(oneBtn[0]);
  145. newBtn = $(newBtn).eq(0).attr('status', 0);
  146. oneBtn.show();
  147. newBtn.find('svg').remove();
  148. let incognitoMode = GM_getValue('incognitoMode', false);
  149. let judgeToIncognitoMode = function (i) {
  150. if (i) {
  151. if (global_module.GetUrlParm(null, 'temporary-chat') != 'true') {
  152. window.location.href = '/?temporary-chat=true';
  153. }
  154. } else {
  155. if (global_module.GetUrlParm(null, 'temporary-chat') == 'true') {
  156. window.location.href = '/';
  157. }
  158. }
  159. };
  160. if (!incognitoMode) {
  161. newBtn.append(yesSvg);
  162. } else {
  163. newBtn.append(noSvg);
  164. judgeToIncognitoMode(incognitoMode);
  165. }
  166. newBtn.off('click').on('click', async () => {
  167. if (incognitoMode) {
  168. newBtn.attr('status', 1);
  169. newBtn.find('svg').remove();
  170. newBtn.append(yesSvg);
  171. } else {
  172. newBtn.attr('status', 0);
  173. newBtn.find('svg').remove();
  174. newBtn.append(noSvg);
  175. }
  176. GM_setValue('incognitoMode', !incognitoMode);
  177. incognitoMode = GM_getValue('incognitoMode', false);
  178. judgeToIncognitoMode(incognitoMode);
  179. });
  180. resolve();
  181. });
  182. }
  183. await initSession();
  184. await initClearButton();
  185. await initIncognitoModeButton();
  186. })();
  187.  
  188. function purify() {
  189. return new Promise(async (resolve) => {
  190. let Tasks = [];
  191. let nav = await getNav();
  192. Tasks.push(
  193. (() => {
  194. return new Promise(async (resolve) => {
  195. let upgradeDom = await global_module.waitForElement('span[class*="border-token-border-light"]', null, null, 100, -1, nav);
  196. upgradeDom = upgradeDom.eq(upgradeDom.length - 1);
  197. upgradeDom.parents('a').eq(0).hide();
  198. resolve();
  199. });
  200. })()
  201. );
  202. Tasks.push(
  203. (() => {
  204. return new Promise(async (resolve) => {
  205. let presentation = await global_module.waitForElement('div[role="presentation"]', null, null, 100, -1);
  206. presentation = presentation.eq(presentation.length - 1);
  207. let presentationTip = await global_module.waitForElement('div:contains("ChatGPT "):not([class])', null, null, 100, -1, presentation);
  208. presentationTip
  209. .eq(presentationTip.length - 1)
  210. .parent()
  211. .hide();
  212. resolve();
  213. });
  214. })()
  215. );
  216. Tasks.push(
  217. (() => {
  218. return new Promise(async (resolve) => {
  219. $(await global_module.waitForElement('button[data-state="closed"][id^="radix"]:contains("?")', null, null, 100, -1)).hide();
  220. resolve();
  221. });
  222. })()
  223. );
  224. await Promise.all(Tasks);
  225. resolve();
  226. });
  227. }
  228.  
  229. function getAllItems() {
  230. return new Promise(async (resolve) => {
  231. let limit = 30;
  232. let currentTotal = 0;
  233. let retItems = [];
  234. let getItems = function (offset) {
  235. return new Promise(async (resolve) => {
  236. $.ajax({
  237. type: 'GET',
  238. url: '/backend-api/conversations?offset=' + offset + '&limit=' + limit + '&order=updated',
  239. headers: { Accept: 'application/json', 'Content-Type': 'application/json', Authorization: `Bearer ${globalVariable.get('accessToken')}` },
  240. success: (data) => {
  241. resolve(data);
  242. },
  243. error: async () => {
  244. resolve(await getAllItems(offset));
  245. }
  246. });
  247. });
  248. };
  249. let data = await getItems(0);
  250. let total = data.total;
  251. let items = data.items;
  252. currentTotal = currentTotal + items.length;
  253. retItems = retItems.concat(items);
  254. while (currentTotal < total) {
  255. data = await getItems(currentTotal);
  256. total = data.total;
  257. items = data.items;
  258. currentTotal = currentTotal + items.length;
  259. retItems = retItems.concat(items);
  260. }
  261. resolve(retItems);
  262. });
  263. }
  264.  
  265. function deleteItem(id) {
  266. let item = globalVariable.get('itemDom')[id];
  267. if (item && !globalVariable.get('deleteItem_' + id + '_loading_setInterval')) {
  268. let textDom = item.find('[dir="auto"]').eq(0);
  269. let oldHtml = textDom.html();
  270. textDom.text('.');
  271. globalVariable.set(
  272. 'deleteItem_' + id + '_loading_setInterval',
  273. setInterval(() => {
  274. if (globalVariable.get('deleteItem_' + id + '_loading_setInterval_done')) {
  275. globalVariable.delete('deleteItem_' + id + '_loading_setInterval_done');
  276. textDom.html(oldHtml);
  277. clearInterval(globalVariable.get('deleteItem_' + id + '_loading_setInterval'));
  278. return;
  279. }
  280. textDom.text(textDom.text() + '.');
  281. if (textDom.text().length > 6) {
  282. textDom.text('.');
  283. }
  284. }, 500)
  285. );
  286. }
  287. return new Promise(async (resolve) => {
  288. $.ajax({
  289. type: 'PATCH',
  290. url: '/backend-api/conversation/' + id,
  291. headers: { Accept: 'application/json', 'Content-Type': 'application/json', Authorization: `Bearer ${globalVariable.get('accessToken')}` },
  292. data: JSON.stringify({ is_visible: false }),
  293. success: () => {
  294. if (item) {
  295. item.hide();
  296. }
  297. globalVariable.set('deleteItem_' + id + '_loading_setInterval_done', true);
  298. resolve(true);
  299. },
  300. error: async () => {
  301. resolve(await deleteItem(id));
  302. }
  303. });
  304. });
  305. }
  306.  
  307. function getNav() {
  308. return new Promise(async (resolve) => {
  309. let nav = await global_module.waitForElement('nav[aria-label]', null, null, 100, -1);
  310. resolve(nav);
  311. });
  312. }
  313.  
  314. function initItemDeleteBtn() {
  315. return new Promise(async (resolve) => {
  316. let nav = await getNav();
  317. let itemDiv = await global_module.waitForElement('div[class*="text-token-text-primary text-sm"]', null, null, 100, -1, nav);
  318. let liList = await global_module.waitForElement('li', null, null, 100, -1, itemDiv);
  319. globalVariable.set('itemDom', {});
  320. let SeeSel = 'button:contains("See ")';
  321. let seeBtn = $(nav).eq(0).find(SeeSel);
  322. if (seeBtn.length != 0) {
  323. let seeBtnText = seeBtn.text().replace('See ', '');
  324. if (seeBtnText != 'less') {
  325. global_module.clickElement(seeBtn[0]);
  326. }
  327. setTimeout(() => {
  328. seeBtn = $(nav).eq(0).find(SeeSel);
  329. seeBtn.hide();
  330. }, 200);
  331. }
  332. for (let i = 0; i < liList.length; i++) {
  333. let spanBtn = $(liList[i]).find('span[class][data-state="closed"]');
  334. let that = spanBtn.eq(0);
  335. let li = that.parents('li');
  336. let a = li.find('a');
  337. let href = a.attr('href');
  338. let id = href.replace('/c/', '');
  339. globalVariable.get('itemDom')[id] = li;
  340. if (spanBtn.length != 1) {
  341. continue;
  342. }
  343. let newBtn = global_module.cloneAndHide(that[0], 2);
  344. newBtn = $(newBtn);
  345. newBtn.find('svg').remove();
  346. newBtn.append(clearButtonSvg);
  347. that.show();
  348. newBtn.css('cursor', 'pointer');
  349. newBtn.off('click').on('click', async () => {
  350. await deleteItem(id);
  351. });
  352. }
  353. resolve();
  354. });
  355. }
  356.  
  357. function widescreenDialogue() {
  358. return new Promise(async (resolve) => {
  359. if ($('body').find('style[id="widescreenDialogueCss]').length != 0) {
  360. resolve();
  361. return;
  362. }
  363. let sel = 'div[id="prompt-textarea"]';
  364. let Btn = await global_module.waitForElement(sel, null, null, 1000, -1);
  365. Btn = Btn.eq(0);
  366. let BtnParent = Btn.parents('form').eq(0).parent().eq(0);
  367. let cssClassName = BtnParent.attr('class').split(' ')[0];
  368. let styleHtml = '.' + cssClassName + '{width:100%;max-width:100%;}';
  369. $('body').append('<style id="widescreenDialogueCss">' + styleHtml + '</style>');
  370. resolve();
  371. });
  372. }
  373.  
  374. var HookFun = new Map();
  375. HookFun.set('/backend-api/conversation', function (req, res, Text, period) {
  376. if (period === 'preload') {
  377. let additional = 'Please reply me with ';
  378. let additionals = additional + browserLanguage;
  379. let body = JSON.parse(req.data);
  380. let messages = body.messages;
  381. if (messages instanceof Array) {
  382. for (let i = 0; i < messages.length; i++) {
  383. let parts = messages[i].content.parts;
  384. if (parts instanceof Array) {
  385. for (let j = 0; j < parts.length; j++) {
  386. if (typeof parts[j] == 'object') {
  387. continue;
  388. }
  389. if (parts[j].indexOf(additional) != -1) {
  390. continue;
  391. }
  392. parts[j] = parts[j] + '\n' + additionals;
  393. }
  394. }
  395. }
  396. }
  397. req.data = JSON.stringify(body);
  398. return;
  399. }
  400. return new Promise(async (resolve) => {
  401. if (period !== 'done') {
  402. resolve(null);
  403. return;
  404. }
  405. setTimeout(async () => {
  406. await widescreenDialogue();
  407. }, 100);
  408. resolve(null);
  409. });
  410. });
  411. HookFun.set('/backend-api/conversations', function (req, res, Text, period) {
  412. return new Promise(async (resolve) => {
  413. if (period !== 'done') {
  414. resolve(null);
  415. return;
  416. }
  417. setTimeout(async () => {
  418. await initItemDeleteBtn();
  419. await widescreenDialogue();
  420. await purify();
  421. let hookPath = '/backend-api/memories';
  422. if (HookFun.get(hookPath) != null) {
  423. return;
  424. }
  425. HookFun.set(hookPath, function (req, res, Text, period) {
  426. return new Promise(async (resolve) => {
  427. if (period !== 'done') {
  428. resolve(null);
  429. return;
  430. }
  431. setTimeout(async () => {
  432. await initItemDeleteBtn();
  433. }, 1000);
  434. resolve(null);
  435. });
  436. });
  437. }, 1000);
  438. resolve(null);
  439. });
  440. });
  441.  
  442. function handleResponse(request) {
  443. if (!request) {
  444. return;
  445. }
  446. if (request.url.indexOf(ignoreHookStr) != -1) {
  447. return;
  448. }
  449. let tempUrl = request.url;
  450. if (tempUrl.indexOf('http') == -1 && tempUrl[0] == '/') {
  451. tempUrl = location.origin + tempUrl;
  452. }
  453. let pathname = new URL(tempUrl).pathname;
  454. let fun = HookFun.get(pathname);
  455. if (!fun) {
  456. return;
  457. }
  458. fun(request, null, null, 'preload');
  459. request.response = (res) => {
  460. let Type = 0;
  461. let responseText = res.responseText;
  462. if (typeof responseText !== 'string') {
  463. Type = 1;
  464. responseText = res.text;
  465. }
  466. if (typeof responseText !== 'string') {
  467. Type = 2;
  468. responseText = JSON.stringify(res.json);
  469. }
  470. const oldText = responseText;
  471. res.responseText = new Promise(async (resolve) => {
  472. let ret = await fun(request, res, responseText, 'done');
  473. if (!ret) {
  474. ret = oldText;
  475. }
  476. if (Type === 2) {
  477. if (typeof ret === 'string') {
  478. ret = JSON.parse(ret);
  479. }
  480. }
  481. resolve(ret);
  482. });
  483. };
  484. }
  485.  
  486. // eslint-disable-next-line no-undef
  487. ajaxHooker.hook(handleResponse);
  488.  
  489. $.onurlchange(function () {
  490. setTimeout(async () => {
  491. await widescreenDialogue();
  492. await purify();
  493. }, 1000);
  494. });