Spaces:
Sleeping
Sleeping
Keldos
commited on
Commit
•
b791a35
1
Parent(s):
00fb9a8
feat: 将聊天框中的历史保存在浏览器中,刷新后读取
Browse files- assets/custom.css +37 -0
- assets/custom.js +65 -18
assets/custom.css
CHANGED
@@ -252,6 +252,43 @@ ol:not(.options), ul:not(.options) {
|
|
252 |
border-radius: 10px !important;
|
253 |
}
|
254 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
255 |
/* 表格 */
|
256 |
table {
|
257 |
margin: 1em 0;
|
|
|
252 |
border-radius: 10px !important;
|
253 |
}
|
254 |
|
255 |
+
/* history message */
|
256 |
+
.wrap>.history-message {
|
257 |
+
padding: 10px !important;
|
258 |
+
}
|
259 |
+
.history-message {
|
260 |
+
/* padding: 0 !important; */
|
261 |
+
opacity: 80%;
|
262 |
+
display: flex;
|
263 |
+
flex-direction: column;
|
264 |
+
}
|
265 |
+
.history-message>.history-message {
|
266 |
+
padding: 0 !important;
|
267 |
+
}
|
268 |
+
.history-message>.message-wrap {
|
269 |
+
padding: 0 !important;
|
270 |
+
margin-bottom: 16px;
|
271 |
+
}
|
272 |
+
.history-message>.message {
|
273 |
+
margin-bottom: 16px;
|
274 |
+
}
|
275 |
+
.wrap>.history-message::after {
|
276 |
+
content: "";
|
277 |
+
display: block;
|
278 |
+
height: 2px;
|
279 |
+
background-color: var(--body-text-color-subdued);
|
280 |
+
margin-bottom: 10px;
|
281 |
+
margin-top: -10px;
|
282 |
+
clear: both;
|
283 |
+
}
|
284 |
+
.wrap>.history-message>:last-child::after {
|
285 |
+
content: "仅供查看";
|
286 |
+
display: block;
|
287 |
+
text-align: center;
|
288 |
+
color: var(--body-text-color-subdued);
|
289 |
+
font-size: 0.8em;
|
290 |
+
}
|
291 |
+
|
292 |
/* 表格 */
|
293 |
table {
|
294 |
margin: 1em 0;
|
assets/custom.js
CHANGED
@@ -13,10 +13,12 @@ var user_input_tb = null;
|
|
13 |
var userInfoDiv = null;
|
14 |
var appTitleDiv = null;
|
15 |
var chatbot = null;
|
|
|
16 |
var apSwitch = null;
|
17 |
var messageBotDivs = null;
|
18 |
var renderLatex = null;
|
19 |
var shouldRenderLatex = false;
|
|
|
20 |
|
21 |
var ga = document.getElementsByTagName("gradio-app");
|
22 |
var targetNode = ga[0];
|
@@ -31,6 +33,7 @@ function gradioLoaded(mutations) {
|
|
31 |
userInfoDiv = document.getElementById("user_info");
|
32 |
appTitleDiv = document.getElementById("app_title");
|
33 |
chatbot = document.querySelector('#chuanhu_chatbot');
|
|
|
34 |
apSwitch = document.querySelector('.apSwitch input[type="checkbox"]');
|
35 |
renderLatex = document.querySelector("#render_latex_checkbox > label > input");
|
36 |
|
@@ -46,6 +49,11 @@ function gradioLoaded(mutations) {
|
|
46 |
if (chatbot) { // chatbot 加载出来了没?
|
47 |
setChatbotHeight();
|
48 |
}
|
|
|
|
|
|
|
|
|
|
|
49 |
if (renderLatex) { // renderLatex 加载出来了没?
|
50 |
shouldRenderLatex = renderLatex.checked;
|
51 |
updateMathJax();
|
@@ -273,41 +281,79 @@ let timeoutId;
|
|
273 |
let isThrottled = false;
|
274 |
// 监听所有元素中message的变化,用来查找需要渲染的mathjax
|
275 |
var mObserver = new MutationObserver(function (mutationsList, observer) {
|
276 |
-
|
277 |
-
|
278 |
-
|
279 |
-
|
280 |
-
if (
|
281 |
-
// console.log("added");
|
282 |
renderMathJax();
|
283 |
mathjaxUpdated = false;
|
284 |
}
|
|
|
285 |
}
|
286 |
-
|
287 |
-
|
288 |
-
|
|
|
289 |
renderMathJax();
|
290 |
mathjaxUpdated = false;
|
291 |
}
|
|
|
292 |
}
|
293 |
-
}
|
294 |
-
|
295 |
-
|
296 |
-
|
297 |
-
|
298 |
-
|
299 |
-
|
|
|
|
|
300 |
// console.log("changed");
|
301 |
renderMathJax();
|
302 |
mathjaxUpdated = false;
|
303 |
-
}
|
304 |
-
|
|
|
305 |
}
|
306 |
}
|
307 |
}
|
308 |
});
|
309 |
mObserver.observe(targetNode, { attributes: true, childList: true, subtree: true });
|
310 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
311 |
// 监视页面内部 DOM 变动
|
312 |
var observer = new MutationObserver(function (mutations) {
|
313 |
gradioLoaded(mutations);
|
@@ -317,6 +363,7 @@ observer.observe(targetNode, { childList: true, subtree: true });
|
|
317 |
// 监视页面变化
|
318 |
window.addEventListener("DOMContentLoaded", function () {
|
319 |
isInIframe = (window.self !== window.top);
|
|
|
320 |
});
|
321 |
window.addEventListener('resize', setChatbotHeight);
|
322 |
window.addEventListener('scroll', setChatbotHeight);
|
|
|
13 |
var userInfoDiv = null;
|
14 |
var appTitleDiv = null;
|
15 |
var chatbot = null;
|
16 |
+
var chatbotWrap = null;
|
17 |
var apSwitch = null;
|
18 |
var messageBotDivs = null;
|
19 |
var renderLatex = null;
|
20 |
var shouldRenderLatex = false;
|
21 |
+
var historyLoaded = false;
|
22 |
|
23 |
var ga = document.getElementsByTagName("gradio-app");
|
24 |
var targetNode = ga[0];
|
|
|
33 |
userInfoDiv = document.getElementById("user_info");
|
34 |
appTitleDiv = document.getElementById("app_title");
|
35 |
chatbot = document.querySelector('#chuanhu_chatbot');
|
36 |
+
chatbotWrap = document.querySelector('#chuanhu_chatbot > .wrap');
|
37 |
apSwitch = document.querySelector('.apSwitch input[type="checkbox"]');
|
38 |
renderLatex = document.querySelector("#render_latex_checkbox > label > input");
|
39 |
|
|
|
49 |
if (chatbot) { // chatbot 加载出来了没?
|
50 |
setChatbotHeight();
|
51 |
}
|
52 |
+
if (chatbotWrap) {
|
53 |
+
if (!historyLoaded){
|
54 |
+
loadHistoryHtml();
|
55 |
+
}
|
56 |
+
}
|
57 |
if (renderLatex) { // renderLatex 加载出来了没?
|
58 |
shouldRenderLatex = renderLatex.checked;
|
59 |
updateMathJax();
|
|
|
281 |
let isThrottled = false;
|
282 |
// 监听所有元素中message的变化,用来查找需要渲染的mathjax
|
283 |
var mObserver = new MutationObserver(function (mutationsList, observer) {
|
284 |
+
for (var mutation of mutationsList) {
|
285 |
+
if (mutation.type === 'childList') {
|
286 |
+
for (var node of mutation.addedNodes) {
|
287 |
+
if (node.nodeType === 1 && node.classList.contains('message') && node.classList.contains('bot')) {
|
288 |
+
if (shouldRenderLatex) {
|
|
|
289 |
renderMathJax();
|
290 |
mathjaxUpdated = false;
|
291 |
}
|
292 |
+
saveHistoryHtml();
|
293 |
}
|
294 |
+
}
|
295 |
+
for (var node of mutation.removedNodes) {
|
296 |
+
if (node.nodeType === 1 && node.classList.contains('message') && node.classList.contains('bot')) {
|
297 |
+
if (shouldRenderLatex) {
|
298 |
renderMathJax();
|
299 |
mathjaxUpdated = false;
|
300 |
}
|
301 |
+
saveHistoryHtml();
|
302 |
}
|
303 |
+
}
|
304 |
+
} else if (mutation.type === 'attributes') {
|
305 |
+
if (mutation.target.nodeType === 1 && mutation.target.classList.contains('message') && mutation.target.classList.contains('bot')) {
|
306 |
+
if (isThrottled) break; // 为了防止重复不断疯狂渲染,加上等待_(:з」∠)_
|
307 |
+
isThrottled = true;
|
308 |
+
clearTimeout(timeoutId);
|
309 |
+
timeoutId = setTimeout(() => {
|
310 |
+
isThrottled = false;
|
311 |
+
if (shouldRenderLatex) {
|
312 |
// console.log("changed");
|
313 |
renderMathJax();
|
314 |
mathjaxUpdated = false;
|
315 |
+
}
|
316 |
+
saveHistoryHtml();
|
317 |
+
}, 500);
|
318 |
}
|
319 |
}
|
320 |
}
|
321 |
});
|
322 |
mObserver.observe(targetNode, { attributes: true, childList: true, subtree: true });
|
323 |
|
324 |
+
var loadhistorytime = 0; // for debugging
|
325 |
+
function saveHistoryHtml() {
|
326 |
+
var historyHtml = document.querySelector('#chuanhu_chatbot > .wrap');
|
327 |
+
// innerHTML;
|
328 |
+
localStorage.setItem('chatHistory', historyHtml.innerHTML);
|
329 |
+
console.log("history saved")
|
330 |
+
historyLoaded = false;
|
331 |
+
}
|
332 |
+
|
333 |
+
var fakeHistory;
|
334 |
+
function loadHistoryHtml() {
|
335 |
+
var historyHtml = localStorage.getItem('chatHistory');
|
336 |
+
if (!historyLoaded) {
|
337 |
+
fakeHistory = document.querySelector('.history-message');
|
338 |
+
if (!fakeHistory) {
|
339 |
+
fakeHistory = document.createElement('div');
|
340 |
+
fakeHistory.classList.add('history-message');
|
341 |
+
fakeHistory.innerHTML = historyHtml;
|
342 |
+
chatbotWrap.insertBefore(fakeHistory, chatbotWrap.firstChild);
|
343 |
+
// chatbotWrap.appendChild(fakeHistory);
|
344 |
+
} else {
|
345 |
+
chatbotWrap.insertBefore(fakeHistory, chatbotWrap.firstChild);
|
346 |
+
// chatbotWrap.appendChild(fakeHistory);
|
347 |
+
}
|
348 |
+
historyLoaded = true;
|
349 |
+
// localStorage.removeItem("chatHistory");
|
350 |
+
console.log("History Loaded");
|
351 |
+
loadhistorytime += 1; // for debugging
|
352 |
+
} else {
|
353 |
+
historyLoaded = false;
|
354 |
+
}
|
355 |
+
}
|
356 |
+
|
357 |
// 监视页面内部 DOM 变动
|
358 |
var observer = new MutationObserver(function (mutations) {
|
359 |
gradioLoaded(mutations);
|
|
|
363 |
// 监视页面变化
|
364 |
window.addEventListener("DOMContentLoaded", function () {
|
365 |
isInIframe = (window.self !== window.top);
|
366 |
+
historyLoaded = false
|
367 |
});
|
368 |
window.addEventListener('resize', setChatbotHeight);
|
369 |
window.addEventListener('scroll', setChatbotHeight);
|