Spaces:
Sleeping
Sleeping
File size: 15,781 Bytes
b39ce2c e9c4dc3 0618646 e9c4dc3 b39ce2c b791a35 98d8027 e7d04a4 2a53137 cfb1733 2a53137 b791a35 b39ce2c e9c4dc3 0d358c4 0618646 0d358c4 b39ce2c e9c4dc3 2a53137 cfb1733 b39ce2c 0d358c4 b39ce2c b791a35 98d8027 2a53137 e7d04a4 b39ce2c cfb1733 98d8027 b39ce2c 0d358c4 cfb1733 b39ce2c 0d358c4 2a53137 b791a35 cfb1733 b791a35 2a53137 b39ce2c e7d04a4 b39ce2c e9c4dc3 b39ce2c 0618646 e9c4dc3 b39ce2c 0618646 0d358c4 cfb1733 c2e5d5f cfb1733 c2e5d5f cfb1733 0618646 2a53137 0618646 2a53137 0618646 b39ce2c afdec9a b39ce2c afdec9a b39ce2c 98d8027 b39ce2c 9aeba67 b39ce2c 9aeba67 b39ce2c 98d8027 b39ce2c dfcbb49 0d358c4 fa02110 0d358c4 fa02110 0d358c4 dfcbb49 0d358c4 fa02110 0d358c4 fa02110 dfcbb49 2a53137 31955a6 2a53137 31955a6 2a53137 31955a6 2a53137 31955a6 2a53137 31955a6 2a53137 b791a35 2a53137 b791a35 2a53137 b791a35 2a53137 b791a35 2a53137 b791a35 2a53137 b791a35 2a53137 b791a35 cfb1733 b791a35 cfb1733 b791a35 cfb1733 b791a35 cfb1733 e7d04a4 cfb1733 e7d04a4 0d358c4 0618646 b39ce2c 0618646 e9c4dc3 0d358c4 0618646 0d358c4 cfb1733 dfcbb49 b39ce2c |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 |
// custom javascript here
const MAX_HISTORY_LENGTH = 32;
var key_down_history = [];
var currentIndex = -1;
var user_input_ta;
var gradioContainer = null;
var user_input_ta = null;
var user_input_tb = null;
var userInfoDiv = null;
var appTitleDiv = null;
var chatbot = null;
var chatbotWrap = null;
var apSwitch = null;
var empty_botton = null;
var messageBotDivs = null;
var renderLatex = null;
var loginUserForm = null;
var logginUser = null;
var userLogged = false;
var usernameGotten = false;
var shouldRenderLatex = false;
var historyLoaded = false;
var ga = document.getElementsByTagName("gradio-app");
var targetNode = ga[0];
var isInIframe = (window.self !== window.top);
// gradio 页面加载好了么??? 我能动你的元素了么??
function gradioLoaded(mutations) {
for (var i = 0; i < mutations.length; i++) {
if (mutations[i].addedNodes.length) {
loginUserForm = document.querySelector(".gradio-container > .main > .wrap > .panel > .form")
gradioContainer = document.querySelector(".gradio-container");
user_input_tb = document.getElementById('user_input_tb');
userInfoDiv = document.getElementById("user_info");
appTitleDiv = document.getElementById("app_title");
chatbot = document.querySelector('#chuanhu_chatbot');
chatbotWrap = document.querySelector('#chuanhu_chatbot > .wrap');
apSwitch = document.querySelector('.apSwitch input[type="checkbox"]');
renderLatex = document.querySelector("#render_latex_checkbox > label > input");
empty_botton = document.getElementById("empty_btn")
if (loginUserForm) {
localStorage.setItem("userLogged", true);
userLogged = true;
}
if (gradioContainer && apSwitch) { // gradioCainter 加载出来了没?
adjustDarkMode();
}
if (user_input_tb) { // user_input_tb 加载出来了没?
selectHistory();
}
if (userInfoDiv && appTitleDiv) { // userInfoDiv 和 appTitleDiv 加载出来了没?
if (!usernameGotten) {
getUserInfo();
}
setTimeout(showOrHideUserInfo(), 2000);
}
if (chatbot) { // chatbot 加载出来了没?
setChatbotHeight();
}
if (chatbotWrap) {
if (!historyLoaded) {
loadHistoryHtml();
}
}
if (renderLatex) { // renderLatex 加载出来了没?
shouldRenderLatex = renderLatex.checked;
updateMathJax();
}
if (empty_botton) {
emptyHistory();
}
}
}
}
function selectHistory() {
user_input_ta = user_input_tb.querySelector("textarea");
if (user_input_ta) {
observer.disconnect(); // 停止监听
// 在 textarea 上监听 keydown 事件
user_input_ta.addEventListener("keydown", function (event) {
var value = user_input_ta.value.trim();
// 判断按下的是否为方向键
if (event.code === 'ArrowUp' || event.code === 'ArrowDown') {
// 如果按下的是方向键,且输入框中有内容,且历史记录中没有该内容,则不执行操作
if (value && key_down_history.indexOf(value) === -1)
return;
// 对于需要响应的动作,阻止默认行为。
event.preventDefault();
var length = key_down_history.length;
if (length === 0) {
currentIndex = -1; // 如果历史记录为空,直接将当前选中的记录重置
return;
}
if (currentIndex === -1) {
currentIndex = length;
}
if (event.code === 'ArrowUp' && currentIndex > 0) {
currentIndex--;
user_input_ta.value = key_down_history[currentIndex];
} else if (event.code === 'ArrowDown' && currentIndex < length - 1) {
currentIndex++;
user_input_ta.value = key_down_history[currentIndex];
}
user_input_ta.selectionStart = user_input_ta.value.length;
user_input_ta.selectionEnd = user_input_ta.value.length;
const input_event = new InputEvent("input", { bubbles: true, cancelable: true });
user_input_ta.dispatchEvent(input_event);
} else if (event.code === "Enter") {
if (value) {
currentIndex = -1;
if (key_down_history.indexOf(value) === -1) {
key_down_history.push(value);
if (key_down_history.length > MAX_HISTORY_LENGTH) {
key_down_history.shift();
}
}
}
}
});
}
}
var username = null;
function getUserInfo() {
if (usernameGotten) {
return;
}
userLogged = localStorage.getItem('userLogged');
if (userLogged) {
username = userInfoDiv.innerText;
if (username) {
if (username.includes("getting user info…")) {
setTimeout(getUserInfo, 500);
return;
} else if (username === " ") {
localStorage.removeItem("username");
localStorage.removeItem("userLogged")
userLogged = false;
usernameGotten = true;
return;
} else {
username = username.match(/User:\s*(.*)/)[1] || username;
localStorage.setItem("username", username);
usernameGotten = true;
clearHistoryHtml();
}
}
}
}
function toggleUserInfoVisibility(shouldHide) {
if (userInfoDiv) {
if (shouldHide) {
userInfoDiv.classList.add("hideK");
} else {
userInfoDiv.classList.remove("hideK");
}
}
}
function showOrHideUserInfo() {
var sendBtn = document.getElementById("submit_btn");
// Bind mouse/touch events to show/hide user info
appTitleDiv.addEventListener("mouseenter", function () {
toggleUserInfoVisibility(false);
});
userInfoDiv.addEventListener("mouseenter", function () {
toggleUserInfoVisibility(false);
});
sendBtn.addEventListener("mouseenter", function () {
toggleUserInfoVisibility(false);
});
appTitleDiv.addEventListener("mouseleave", function () {
toggleUserInfoVisibility(true);
});
userInfoDiv.addEventListener("mouseleave", function () {
toggleUserInfoVisibility(true);
});
sendBtn.addEventListener("mouseleave", function () {
toggleUserInfoVisibility(true);
});
appTitleDiv.ontouchstart = function () {
toggleUserInfoVisibility(false);
};
userInfoDiv.ontouchstart = function () {
toggleUserInfoVisibility(false);
};
sendBtn.ontouchstart = function () {
toggleUserInfoVisibility(false);
};
appTitleDiv.ontouchend = function () {
setTimeout(function () {
toggleUserInfoVisibility(true);
}, 3000);
};
userInfoDiv.ontouchend = function () {
setTimeout(function () {
toggleUserInfoVisibility(true);
}, 3000);
};
sendBtn.ontouchend = function () {
setTimeout(function () {
toggleUserInfoVisibility(true);
}, 3000); // Delay 1 second to hide user info
};
// Hide user info after 2 second
setTimeout(function () {
toggleUserInfoVisibility(true);
}, 2000);
}
function toggleDarkMode(isEnabled) {
if (isEnabled) {
gradioContainer.classList.add("dark");
document.body.style.setProperty("background-color", "var(--neutral-950)", "important");
} else {
gradioContainer.classList.remove("dark");
document.body.style.backgroundColor = "";
}
}
function adjustDarkMode() {
const darkModeQuery = window.matchMedia("(prefers-color-scheme: dark)");
// 根据当前颜色模式设置初始状态
apSwitch.checked = darkModeQuery.matches;
toggleDarkMode(darkModeQuery.matches);
// 监听颜色模式变化
darkModeQuery.addEventListener("change", (e) => {
apSwitch.checked = e.matches;
toggleDarkMode(e.matches);
});
// apSwitch = document.querySelector('.apSwitch input[type="checkbox"]');
apSwitch.addEventListener("change", (e) => {
toggleDarkMode(e.target.checked);
});
}
function setChatbotHeight() {
const screenWidth = window.innerWidth;
const statusDisplay = document.querySelector('#status_display');
const statusDisplayHeight = statusDisplay ? statusDisplay.offsetHeight : 0;
const wrap = chatbot.querySelector('.wrap');
const vh = window.innerHeight * 0.01;
document.documentElement.style.setProperty('--vh', `${vh}px`);
if (isInIframe) {
chatbot.style.height = `700px`;
wrap.style.maxHeight = `calc(700px - var(--line-sm) * 1rem - 2 * var(--block-label-margin))`
} else {
if (screenWidth <= 320) {
chatbot.style.height = `calc(var(--vh, 1vh) * 100 - ${statusDisplayHeight + 150}px)`;
wrap.style.maxHeight = `calc(var(--vh, 1vh) * 100 - ${statusDisplayHeight + 150}px - var(--line-sm) * 1rem - 2 * var(--block-label-margin))`;
} else if (screenWidth <= 499) {
chatbot.style.height = `calc(var(--vh, 1vh) * 100 - ${statusDisplayHeight + 100}px)`;
wrap.style.maxHeight = `calc(var(--vh, 1vh) * 100 - ${statusDisplayHeight + 100}px - var(--line-sm) * 1rem - 2 * var(--block-label-margin))`;
} else {
chatbot.style.height = `calc(var(--vh, 1vh) * 100 - ${statusDisplayHeight + 160}px)`;
wrap.style.maxHeight = `calc(var(--vh, 1vh) * 100 - ${statusDisplayHeight + 160}px - var(--line-sm) * 1rem - 2 * var(--block-label-margin))`;
}
}
}
var rendertime = 0; // for debugging
var mathjaxUpdated = false;
function renderMathJax() {
messageBotDivs = document.querySelectorAll('.message.bot');
for (var i = 0; i < messageBotDivs.length; i++) {
var mathJaxSpan = messageBotDivs[i].querySelector('.MathJax_Preview');
if (!mathJaxSpan && shouldRenderLatex && !mathjaxUpdated) {
MathJax.Hub.Queue(["Typeset", MathJax.Hub, messageBotDivs[i]]);
rendertime +=1; // for debugging
// console.log("renderingMathJax", i)
}
}
mathjaxUpdated = true;
console.log("MathJax Rendered")
}
function removeMathjax() {
// var jax = MathJax.Hub.getAllJax();
// for (var i = 0; i < jax.length; i++) {
// // MathJax.typesetClear(jax[i]);
// jax[i].Text(newmath)
// jax[i].Reprocess()
// }
// 我真的不会了啊啊啊,mathjax并没有提供转换为原先文本的办法。
mathjaxUpdated = true;
// console.log("MathJax removed!");
}
function updateMathJax() {
renderLatex.addEventListener("change", function() {
shouldRenderLatex = renderLatex.checked;
// console.log(shouldRenderLatex)
if (!mathjaxUpdated) {
if (shouldRenderLatex) {
renderMathJax();
} else {
console.log("MathJax Disabled")
removeMathjax();
}
} else {
if (!shouldRenderLatex) {
mathjaxUpdated = false; // reset
}
}
});
if (shouldRenderLatex && !mathjaxUpdated) {
renderMathJax();
}
mathjaxUpdated = false;
}
let timeoutId;
let isThrottled = false;
// 监听所有元素中message的变化,用来查找需要渲染的mathjax
var mObserver = new MutationObserver(function (mutationsList, observer) {
for (var mutation of mutationsList) {
if (mutation.type === 'childList') {
for (var node of mutation.addedNodes) {
if (node.nodeType === 1 && node.classList.contains('message') && node.classList.contains('bot')) {
if (shouldRenderLatex) {
renderMathJax();
mathjaxUpdated = false;
}
saveHistoryHtml();
}
}
for (var node of mutation.removedNodes) {
if (node.nodeType === 1 && node.classList.contains('message') && node.classList.contains('bot')) {
if (shouldRenderLatex) {
renderMathJax();
mathjaxUpdated = false;
}
saveHistoryHtml();
}
}
} else if (mutation.type === 'attributes') {
if (mutation.target.nodeType === 1 && mutation.target.classList.contains('message') && mutation.target.classList.contains('bot')) {
if (isThrottled) break; // 为了防止重复不断疯狂渲染,加上等待_(:з」∠)_
isThrottled = true;
clearTimeout(timeoutId);
timeoutId = setTimeout(() => {
isThrottled = false;
if (shouldRenderLatex) {
// console.log("changed");
renderMathJax();
mathjaxUpdated = false;
}
saveHistoryHtml();
}, 500);
}
}
}
});
mObserver.observe(targetNode, { attributes: true, childList: true, subtree: true });
var loadhistorytime = 0; // for debugging
function saveHistoryHtml() {
var historyHtml = document.querySelector('#chuanhu_chatbot > .wrap');
localStorage.setItem('chatHistory', historyHtml.innerHTML);
console.log("History Saved")
historyLoaded = false;
}
function loadHistoryHtml() {
var historyHtml = localStorage.getItem('chatHistory');
if (!historyHtml) {
historyLoaded = true;
return; // no history, do nothing
}
userLogged = localStorage.getItem('userLogged');
if (userLogged){
historyLoaded = true;
return; // logged in, do nothing
}
if (!historyLoaded) {
var fakeHistory = document.createElement('div');
fakeHistory.classList.add('history-message');
fakeHistory.innerHTML = historyHtml;
chatbotWrap.insertBefore(fakeHistory, chatbotWrap.firstChild);
historyLoaded = true;
console.log("History Loaded");
loadhistorytime += 1; // for debugging
} else {
historyLoaded = false;
}
}
function clearHistoryHtml() {
localStorage.removeItem("chatHistory");
historyMessages = chatbotWrap.querySelector('.history-message');
if (historyMessages) {
chatbotWrap.removeChild(historyMessages);
console.log("History Cleared");
}
}
function emptyHistory() {
empty_botton.addEventListener("click", function () {
clearHistoryHtml();
});
}
// 监视页面内部 DOM 变动
var observer = new MutationObserver(function (mutations) {
gradioLoaded(mutations);
});
observer.observe(targetNode, { childList: true, subtree: true });
// 监视页面变化
window.addEventListener("DOMContentLoaded", function () {
isInIframe = (window.self !== window.top);
historyLoaded = false;
});
window.addEventListener('resize', setChatbotHeight);
window.addEventListener('scroll', setChatbotHeight);
window.matchMedia("(prefers-color-scheme: dark)").addEventListener("change", adjustDarkMode); |