common.js 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648
  1. // -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
  2. // 第 1 部分: 工具函数
  3. // -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
  4. function gradioApp() {
  5. // https://github.com/GaiZhenbiao/ChuanhuChatGPT/tree/main/web_assets/javascript
  6. const elems = document.getElementsByTagName('gradio-app');
  7. const elem = elems.length == 0 ? document : elems[0];
  8. if (elem !== document) {
  9. elem.getElementById = function (id) {
  10. return document.getElementById(id);
  11. };
  12. }
  13. return elem.shadowRoot ? elem.shadowRoot : elem;
  14. }
  15. function setCookie(name, value, days) {
  16. var expires = "";
  17. if (days) {
  18. var date = new Date();
  19. date.setTime(date.getTime() + (days * 24 * 60 * 60 * 1000));
  20. expires = "; expires=" + date.toUTCString();
  21. }
  22. document.cookie = name + "=" + value + expires + "; path=/";
  23. }
  24. function getCookie(name) {
  25. var decodedCookie = decodeURIComponent(document.cookie);
  26. var cookies = decodedCookie.split(';');
  27. for (var i = 0; i < cookies.length; i++) {
  28. var cookie = cookies[i].trim();
  29. if (cookie.indexOf(name + "=") === 0) {
  30. return cookie.substring(name.length + 1, cookie.length);
  31. }
  32. }
  33. return null;
  34. }
  35. let toastCount = 0;
  36. function toast_push(msg, duration) {
  37. duration = isNaN(duration) ? 3000 : duration;
  38. const existingToasts = document.querySelectorAll('.toast');
  39. existingToasts.forEach(toast => {
  40. toast.style.top = `${parseInt(toast.style.top, 10) - 70}px`;
  41. });
  42. const m = document.createElement('div');
  43. m.innerHTML = msg;
  44. m.classList.add('toast');
  45. m.style.cssText = `font-size: var(--text-md) !important; color: rgb(255, 255, 255); background-color: rgba(0, 0, 0, 0.6); padding: 10px 15px; border-radius: 4px; position: fixed; top: ${50 + toastCount * 70}%; left: 50%; transform: translateX(-50%); width: auto; text-align: center; transition: top 0.3s;`;
  46. document.body.appendChild(m);
  47. setTimeout(function () {
  48. m.style.opacity = '0';
  49. setTimeout(function () {
  50. document.body.removeChild(m);
  51. toastCount--;
  52. }, 500);
  53. }, duration);
  54. toastCount++;
  55. }
  56. function toast_up(msg) {
  57. var m = document.getElementById('toast_up');
  58. if (m) {
  59. document.body.removeChild(m); // remove the loader from the body
  60. }
  61. m = document.createElement('div');
  62. m.id = 'toast_up';
  63. m.innerHTML = msg;
  64. m.style.cssText = "font-size: var(--text-md) !important; color: rgb(255, 255, 255); background-color: rgba(0, 0, 100, 0.6); padding: 10px 15px; margin: 0 0 0 -60px; border-radius: 4px; position: fixed; top: 50%; left: 50%; width: auto; text-align: center;";
  65. document.body.appendChild(m);
  66. }
  67. function toast_down() {
  68. var m = document.getElementById('toast_up');
  69. if (m) {
  70. document.body.removeChild(m); // remove the loader from the body
  71. }
  72. }
  73. function begin_loading_status() {
  74. // Create the loader div and add styling
  75. var loader = document.createElement('div');
  76. loader.id = 'Js_File_Loading';
  77. var C1 = document.createElement('div');
  78. var C2 = document.createElement('div');
  79. // var C3 = document.createElement('span');
  80. // C3.textContent = '上传中...'
  81. // C3.style.position = "fixed";
  82. // C3.style.top = "50%";
  83. // C3.style.left = "50%";
  84. // C3.style.width = "80px";
  85. // C3.style.height = "80px";
  86. // C3.style.margin = "-40px 0 0 -40px";
  87. C1.style.position = "fixed";
  88. C1.style.top = "50%";
  89. C1.style.left = "50%";
  90. C1.style.width = "80px";
  91. C1.style.height = "80px";
  92. C1.style.borderLeft = "12px solid #00f3f300";
  93. C1.style.borderRight = "12px solid #00f3f300";
  94. C1.style.borderTop = "12px solid #82aaff";
  95. C1.style.borderBottom = "12px solid #82aaff"; // Added for effect
  96. C1.style.borderRadius = "50%";
  97. C1.style.margin = "-40px 0 0 -40px";
  98. C1.style.animation = "spinAndPulse 2s linear infinite";
  99. C2.style.position = "fixed";
  100. C2.style.top = "50%";
  101. C2.style.left = "50%";
  102. C2.style.width = "40px";
  103. C2.style.height = "40px";
  104. C2.style.borderLeft = "12px solid #00f3f300";
  105. C2.style.borderRight = "12px solid #00f3f300";
  106. C2.style.borderTop = "12px solid #33c9db";
  107. C2.style.borderBottom = "12px solid #33c9db"; // Added for effect
  108. C2.style.borderRadius = "50%";
  109. C2.style.margin = "-20px 0 0 -20px";
  110. C2.style.animation = "spinAndPulse2 2s linear infinite";
  111. loader.appendChild(C1);
  112. loader.appendChild(C2);
  113. // loader.appendChild(C3);
  114. document.body.appendChild(loader); // Add the loader to the body
  115. // Set the CSS animation keyframes for spin and pulse to be synchronized
  116. var styleSheet = document.createElement('style');
  117. styleSheet.id = 'Js_File_Loading_Style';
  118. styleSheet.textContent = `
  119. @keyframes spinAndPulse {
  120. 0% { transform: rotate(0deg) scale(1); }
  121. 25% { transform: rotate(90deg) scale(1.1); }
  122. 50% { transform: rotate(180deg) scale(1); }
  123. 75% { transform: rotate(270deg) scale(0.9); }
  124. 100% { transform: rotate(360deg) scale(1); }
  125. }
  126. @keyframes spinAndPulse2 {
  127. 0% { transform: rotate(-90deg);}
  128. 25% { transform: rotate(-180deg);}
  129. 50% { transform: rotate(-270deg);}
  130. 75% { transform: rotate(-360deg);}
  131. 100% { transform: rotate(-450deg);}
  132. }
  133. `;
  134. document.head.appendChild(styleSheet);
  135. }
  136. function cancel_loading_status() {
  137. // remove the loader from the body
  138. var loadingElement = document.getElementById('Js_File_Loading');
  139. if (loadingElement) {
  140. document.body.removeChild(loadingElement);
  141. }
  142. var loadingStyle = document.getElementById('Js_File_Loading_Style');
  143. if (loadingStyle) {
  144. document.head.removeChild(loadingStyle);
  145. }
  146. // create new listen event
  147. let clearButton = document.querySelectorAll('div[id*="elem_upload"] button[aria-label="Clear"]');
  148. for (let button of clearButton) {
  149. button.addEventListener('click', function () {
  150. setTimeout(function () {
  151. register_upload_event();
  152. }, 50);
  153. });
  154. }
  155. }
  156. // -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
  157. // 第 2 部分: 复制按钮
  158. // -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
  159. function addCopyButton(botElement) {
  160. // https://github.com/GaiZhenbiao/ChuanhuChatGPT/tree/main/web_assets/javascript
  161. // Copy bot button
  162. const copiedIcon = '<span><svg stroke="currentColor" fill="none" stroke-width="2" viewBox="0 0 24 24" stroke-linecap="round" stroke-linejoin="round" height=".8em" width=".8em" xmlns="http://www.w3.org/2000/svg"><polyline points="20 6 9 17 4 12"></polyline></svg></span>';
  163. const copyIcon = '<span><svg stroke="currentColor" fill="none" stroke-width="2" viewBox="0 0 24 24" stroke-linecap="round" stroke-linejoin="round" height=".8em" width=".8em" xmlns="http://www.w3.org/2000/svg"><rect x="9" y="9" width="13" height="13" rx="2" ry="2"></rect><path d="M5 15H4a2 2 0 0 1-2-2V4a2 2 0 0 1 2-2h9a2 2 0 0 1 2 2v1"></path></svg></span>';
  164. const messageBtnColumnElement = botElement.querySelector('.message-btn-row');
  165. if (messageBtnColumnElement) {
  166. // if .message-btn-column exists
  167. return;
  168. }
  169. var copyButton = document.createElement('button');
  170. copyButton.classList.add('copy-bot-btn');
  171. copyButton.setAttribute('aria-label', 'Copy');
  172. copyButton.innerHTML = copyIcon;
  173. copyButton.addEventListener('click', async () => {
  174. const textToCopy = botElement.innerText;
  175. try {
  176. if ("clipboard" in navigator) {
  177. await navigator.clipboard.writeText(textToCopy);
  178. copyButton.innerHTML = copiedIcon;
  179. setTimeout(() => {
  180. copyButton.innerHTML = copyIcon;
  181. }, 1500);
  182. } else {
  183. const textArea = document.createElement("textarea");
  184. textArea.value = textToCopy;
  185. document.body.appendChild(textArea);
  186. textArea.select();
  187. try {
  188. document.execCommand('copy');
  189. copyButton.innerHTML = copiedIcon;
  190. setTimeout(() => {
  191. copyButton.innerHTML = copyIcon;
  192. }, 1500);
  193. } catch (error) {
  194. console.error("Copy failed: ", error);
  195. }
  196. document.body.removeChild(textArea);
  197. }
  198. } catch (error) {
  199. console.error("Copy failed: ", error);
  200. }
  201. });
  202. var messageBtnColumn = document.createElement('div');
  203. messageBtnColumn.classList.add('message-btn-row');
  204. messageBtnColumn.appendChild(copyButton);
  205. botElement.appendChild(messageBtnColumn);
  206. }
  207. function chatbotContentChanged(attempt = 1, force = false) {
  208. // https://github.com/GaiZhenbiao/ChuanhuChatGPT/tree/main/web_assets/javascript
  209. for (var i = 0; i < attempt; i++) {
  210. setTimeout(() => {
  211. gradioApp().querySelectorAll('#gpt-chatbot .message-wrap .message.bot').forEach(addCopyButton);
  212. }, i === 0 ? 0 : 200);
  213. }
  214. }
  215. // -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
  216. // 第 3 部分: chatbot动态高度调整
  217. // -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
  218. function chatbotAutoHeight() {
  219. // 自动调整高度:立即
  220. function update_height() {
  221. var { height_target, chatbot_height, chatbot } = get_elements(true);
  222. if (height_target != chatbot_height) {
  223. var pixelString = height_target.toString() + 'px';
  224. chatbot.style.maxHeight = pixelString; chatbot.style.height = pixelString;
  225. }
  226. }
  227. // 自动调整高度:缓慢
  228. function update_height_slow() {
  229. var { height_target, chatbot_height, chatbot } = get_elements();
  230. if (height_target != chatbot_height) {
  231. // sign = (height_target - chatbot_height)/Math.abs(height_target - chatbot_height);
  232. // speed = Math.max(Math.abs(height_target - chatbot_height), 1);
  233. new_panel_height = (height_target - chatbot_height) * 0.5 + chatbot_height;
  234. if (Math.abs(new_panel_height - height_target) < 10) {
  235. new_panel_height = height_target;
  236. }
  237. var pixelString = new_panel_height.toString() + 'px';
  238. chatbot.style.maxHeight = pixelString; chatbot.style.height = pixelString;
  239. }
  240. }
  241. monitoring_input_box()
  242. update_height();
  243. window.addEventListener('resize', function() { update_height(); });
  244. window.addEventListener('scroll', function() { update_height_slow(); });
  245. setInterval(function () { update_height_slow() }, 50); // 每50毫秒执行一次
  246. }
  247. swapped = false;
  248. function swap_input_area() {
  249. // Get the elements to be swapped
  250. var element1 = document.querySelector("#input-panel");
  251. var element2 = document.querySelector("#basic-panel");
  252. // Get the parent of the elements
  253. var parent = element1.parentNode;
  254. // Get the next sibling of element2
  255. var nextSibling = element2.nextSibling;
  256. // Swap the elements
  257. parent.insertBefore(element2, element1);
  258. parent.insertBefore(element1, nextSibling);
  259. if (swapped) {swapped = false;}
  260. else {swapped = true;}
  261. }
  262. function get_elements(consider_state_panel = false) {
  263. var chatbot = document.querySelector('#gpt-chatbot > div.wrap.svelte-18telvq');
  264. if (!chatbot) {
  265. chatbot = document.querySelector('#gpt-chatbot');
  266. }
  267. const panel1 = document.querySelector('#input-panel').getBoundingClientRect();
  268. const panel2 = document.querySelector('#basic-panel').getBoundingClientRect()
  269. const panel3 = document.querySelector('#plugin-panel').getBoundingClientRect();
  270. // const panel4 = document.querySelector('#interact-panel').getBoundingClientRect();
  271. const panel_active = document.querySelector('#state-panel').getBoundingClientRect();
  272. if (consider_state_panel || panel_active.height < 25) {
  273. document.state_panel_height = panel_active.height;
  274. }
  275. // 25 是chatbot的label高度, 16 是右侧的gap
  276. var height_target = panel1.height + panel2.height + panel3.height + 0 + 0 - 25 + 16 * 2;
  277. // 禁止动态的state-panel高度影响
  278. height_target = height_target + (document.state_panel_height - panel_active.height)
  279. var height_target = parseInt(height_target);
  280. var chatbot_height = chatbot.style.height;
  281. // 交换输入区位置,使得输入区始终可用
  282. if (!swapped){
  283. if (panel1.top!=0 && (panel1.bottom + panel1.top)/2 < 0){ swap_input_area(); }
  284. }
  285. else if (swapped){
  286. if (panel2.top!=0 && panel2.top > 0){ swap_input_area(); }
  287. }
  288. // 调整高度
  289. const err_tor = 5;
  290. if (Math.abs(panel1.left - chatbot.getBoundingClientRect().left) < err_tor){
  291. // 是否处于窄屏模式
  292. height_target = window.innerHeight * 0.6;
  293. }else{
  294. // 调整高度
  295. const chatbot_height_exceed = 15;
  296. const chatbot_height_exceed_m = 10;
  297. b_panel = Math.max(panel1.bottom, panel2.bottom, panel3.bottom)
  298. if (b_panel >= window.innerHeight - chatbot_height_exceed) {
  299. height_target = window.innerHeight - chatbot.getBoundingClientRect().top - chatbot_height_exceed_m;
  300. }
  301. else if (b_panel < window.innerHeight * 0.75) {
  302. height_target = window.innerHeight * 0.8;
  303. }
  304. }
  305. var chatbot_height = parseInt(chatbot_height);
  306. return { height_target, chatbot_height, chatbot };
  307. }
  308. // -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
  309. // 第 4 部分: 粘贴、拖拽文件上传
  310. // -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
  311. var elem_upload = null;
  312. var elem_upload_float = null;
  313. var elem_input_main = null;
  314. var elem_input_float = null;
  315. var elem_chatbot = null;
  316. var elem_upload_component_float = null;
  317. var elem_upload_component = null;
  318. var exist_file_msg = '⚠️请先删除上传区(左上方)中的历史文件,再尝试上传。'
  319. function locate_upload_elems(){
  320. elem_upload = document.getElementById('elem_upload')
  321. elem_upload_float = document.getElementById('elem_upload_float')
  322. elem_input_main = document.getElementById('user_input_main')
  323. elem_input_float = document.getElementById('user_input_float')
  324. elem_chatbot = document.getElementById('gpt-chatbot')
  325. elem_upload_component_float = elem_upload_float.querySelector("input[type=file]");
  326. elem_upload_component = elem_upload.querySelector("input[type=file]");
  327. }
  328. async function upload_files(files) {
  329. let totalSizeMb = 0
  330. elem_upload_component_float = elem_upload_float.querySelector("input[type=file]");
  331. if (files && files.length > 0) {
  332. // 执行具体的上传逻辑
  333. if (elem_upload_component_float) {
  334. for (let i = 0; i < files.length; i++) {
  335. // 将从文件数组中获取的文件大小(单位为字节)转换为MB,
  336. totalSizeMb += files[i].size / 1024 / 1024;
  337. }
  338. // 检查文件总大小是否超过20MB
  339. if (totalSizeMb > 20) {
  340. toast_push('⚠️文件夹大于 20MB 🚀上传文件中', 3000);
  341. }
  342. let event = new Event("change");
  343. Object.defineProperty(event, "target", { value: elem_upload_component_float, enumerable: true });
  344. Object.defineProperty(event, "currentTarget", { value: elem_upload_component_float, enumerable: true });
  345. Object.defineProperty(elem_upload_component_float, "files", { value: files, enumerable: true });
  346. elem_upload_component_float.dispatchEvent(event);
  347. } else {
  348. console.log(exist_file_msg);
  349. toast_push(exist_file_msg, 3000);
  350. }
  351. }
  352. }
  353. function register_func_paste(input) {
  354. let paste_files = [];
  355. if (input) {
  356. input.addEventListener("paste", async function (e) {
  357. const clipboardData = e.clipboardData || window.clipboardData;
  358. const items = clipboardData.items;
  359. if (items) {
  360. for (i = 0; i < items.length; i++) {
  361. if (items[i].kind === "file") { // 确保是文件类型
  362. const file = items[i].getAsFile();
  363. // 将每一个粘贴的文件添加到files数组中
  364. paste_files.push(file);
  365. e.preventDefault(); // 避免粘贴文件名到输入框
  366. }
  367. }
  368. if (paste_files.length > 0) {
  369. // 按照文件列表执行批量上传逻辑
  370. await upload_files(paste_files);
  371. paste_files = []
  372. }
  373. }
  374. });
  375. }
  376. }
  377. function register_func_drag(elem) {
  378. if (elem) {
  379. const dragEvents = ["dragover"];
  380. const leaveEvents = ["dragleave", "dragend", "drop"];
  381. const onDrag = function (e) {
  382. e.preventDefault();
  383. e.stopPropagation();
  384. if (elem_upload_float.querySelector("input[type=file]")) {
  385. toast_up('⚠️释放以上传文件')
  386. } else {
  387. toast_up(exist_file_msg)
  388. }
  389. };
  390. const onLeave = function (e) {
  391. toast_down();
  392. e.preventDefault();
  393. e.stopPropagation();
  394. };
  395. dragEvents.forEach(event => {
  396. elem.addEventListener(event, onDrag);
  397. });
  398. leaveEvents.forEach(event => {
  399. elem.addEventListener(event, onLeave);
  400. });
  401. elem.addEventListener("drop", async function (e) {
  402. const files = e.dataTransfer.files;
  403. await upload_files(files);
  404. });
  405. }
  406. }
  407. function elem_upload_component_pop_message(elem) {
  408. if (elem) {
  409. const dragEvents = ["dragover"];
  410. const leaveEvents = ["dragleave", "dragend", "drop"];
  411. dragEvents.forEach(event => {
  412. elem.addEventListener(event, function (e) {
  413. e.preventDefault();
  414. e.stopPropagation();
  415. if (elem_upload_float.querySelector("input[type=file]")) {
  416. toast_up('⚠️释放以上传文件')
  417. } else {
  418. toast_up(exist_file_msg)
  419. }
  420. });
  421. });
  422. leaveEvents.forEach(event => {
  423. elem.addEventListener(event, function (e) {
  424. toast_down();
  425. e.preventDefault();
  426. e.stopPropagation();
  427. });
  428. });
  429. elem.addEventListener("drop", async function (e) {
  430. toast_push('正在上传中,请稍等。', 2000);
  431. begin_loading_status();
  432. });
  433. }
  434. }
  435. function register_upload_event() {
  436. locate_upload_elems();
  437. if (elem_upload_float) {
  438. _upload = document.querySelector("#elem_upload_float div.center.boundedheight.flex")
  439. elem_upload_component_pop_message(_upload);
  440. }
  441. if (elem_upload_component_float) {
  442. elem_upload_component_float.addEventListener('change', function (event) {
  443. toast_push('正在上传中,请稍等。', 2000);
  444. begin_loading_status();
  445. });
  446. }
  447. if (elem_upload_component) {
  448. elem_upload_component.addEventListener('change', function (event) {
  449. toast_push('正在上传中,请稍等。', 2000);
  450. begin_loading_status();
  451. });
  452. }else{
  453. toast_push("oppps", 3000);
  454. }
  455. }
  456. function monitoring_input_box() {
  457. register_upload_event();
  458. if (elem_input_main) {
  459. if (elem_input_main.querySelector("textarea")) {
  460. register_func_paste(elem_input_main.querySelector("textarea"))
  461. }
  462. }
  463. if (elem_input_float) {
  464. if (elem_input_float.querySelector("textarea")) {
  465. register_func_paste(elem_input_float.querySelector("textarea"))
  466. }
  467. }
  468. if (elem_chatbot) {
  469. register_func_drag(elem_chatbot)
  470. }
  471. }
  472. // 监视页面变化
  473. window.addEventListener("DOMContentLoaded", function () {
  474. // const ga = document.getElementsByTagName("gradio-app");
  475. gradioApp().addEventListener("render", monitoring_input_box);
  476. });
  477. // -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
  478. // 第 5 部分: 音频按钮样式变化
  479. // -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
  480. function audio_fn_init() {
  481. let audio_component = document.getElementById('elem_audio');
  482. if (audio_component) {
  483. let buttonElement = audio_component.querySelector('button');
  484. let specificElement = audio_component.querySelector('.hide.sr-only');
  485. specificElement.remove();
  486. buttonElement.childNodes[1].nodeValue = '启动麦克风';
  487. buttonElement.addEventListener('click', function (event) {
  488. event.stopPropagation();
  489. toast_push('您启动了麦克风!下一步请点击“实时语音对话”启动语音对话。');
  490. });
  491. // 查找语音插件按钮
  492. let buttons = document.querySelectorAll('button');
  493. let audio_button = null;
  494. for (let button of buttons) {
  495. if (button.textContent.includes('语音')) {
  496. audio_button = button;
  497. break;
  498. }
  499. }
  500. if (audio_button) {
  501. audio_button.addEventListener('click', function () {
  502. toast_push('您点击了“实时语音对话”启动语音对话。');
  503. });
  504. let parent_element = audio_component.parentElement; // 将buttonElement移动到audio_button的内部
  505. audio_button.appendChild(audio_component);
  506. buttonElement.style.cssText = 'border-color: #00ffe0;border-width: 2px; height: 25px;'
  507. parent_element.remove();
  508. audio_component.style.cssText = 'width: 250px;right: 0px;display: inline-flex;flex-flow: row-reverse wrap;place-content: stretch space-between;align-items: center;background-color: #ffffff00;';
  509. }
  510. }
  511. }
  512. function minor_ui_adjustment() {
  513. let cbsc_area = document.getElementById('cbsc');
  514. cbsc_area.style.paddingTop = '15px';
  515. var bar_btn_width = [];
  516. // 自动隐藏超出范围的toolbar按钮
  517. function auto_hide_toolbar() {
  518. var qq = document.getElementById('tooltip');
  519. var tab_nav = qq.getElementsByClassName('tab-nav');
  520. if (tab_nav.length == 0){ return; }
  521. var btn_list = tab_nav[0].getElementsByTagName('button')
  522. if (btn_list.length == 0){ return; }
  523. // 获取页面宽度
  524. var page_width = document.documentElement.clientWidth;
  525. // 总是保留的按钮数量
  526. const always_preserve = 2;
  527. // 获取最后一个按钮的右侧位置
  528. var cur_right = btn_list[always_preserve-1].getBoundingClientRect().right;
  529. if (bar_btn_width.length == 0){
  530. // 首次运行,记录每个按钮的宽度
  531. for (var i = 0; i < btn_list.length; i++) {
  532. bar_btn_width.push(btn_list[i].getBoundingClientRect().width);
  533. }
  534. }
  535. // 处理每一个按钮
  536. for (var i = always_preserve; i < btn_list.length; i++) {
  537. var element = btn_list[i];
  538. var element_right = element.getBoundingClientRect().right;
  539. if (element_right!=0){ cur_right = element_right; }
  540. if (element.style.display === 'none') {
  541. if ((cur_right + bar_btn_width[i]) < (page_width * 0.37)) {
  542. // 恢复显示当前按钮
  543. element.style.display = 'block';
  544. // console.log('show');
  545. return;
  546. }else{
  547. return;
  548. }
  549. } else {
  550. if (cur_right > (page_width * 0.38)) {
  551. // 隐藏当前按钮以及右侧所有按钮
  552. for (var j = i; j < btn_list.length; j++) {
  553. if (btn_list[j].style.display !== 'none') {
  554. btn_list[j].style.display = 'none';
  555. }
  556. }
  557. // console.log('show');
  558. return;
  559. }
  560. }
  561. }
  562. }
  563. setInterval(function () {
  564. auto_hide_toolbar()
  565. }, 200); // 每50毫秒执行一次
  566. }
  567. // -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
  568. // 第 6 部分: JS初始化函数
  569. // -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
  570. function GptAcademicJavaScriptInit(LAYOUT = "LEFT-RIGHT") {
  571. audio_fn_init();
  572. minor_ui_adjustment();
  573. chatbotIndicator = gradioApp().querySelector('#gpt-chatbot > div.wrap');
  574. var chatbotObserver = new MutationObserver(() => {
  575. chatbotContentChanged(1);
  576. });
  577. chatbotObserver.observe(chatbotIndicator, { attributes: true, childList: true, subtree: true });
  578. if (LAYOUT === "LEFT-RIGHT") { chatbotAutoHeight(); }
  579. }