Spaces:
Sleeping
Sleeping
Keldos
commited on
Commit
·
d65cba4
1
Parent(s):
8131457
refactor: use init js when load web page
Browse files
web_assets/javascript/ChuanhuChat.js
CHANGED
@@ -5,7 +5,6 @@ const MAX_HISTORY_LENGTH = 32;
|
|
5 |
|
6 |
var key_down_history = [];
|
7 |
var currentIndex = -1;
|
8 |
-
var user_input_ta;
|
9 |
|
10 |
var gradioContainer = null;
|
11 |
var user_input_ta = null;
|
@@ -28,72 +27,82 @@ var statusDisplay = null;
|
|
28 |
|
29 |
var isInIframe = (window.self !== window.top);
|
30 |
var currentTime = new Date().getTime();
|
31 |
-
|
32 |
|
33 |
// gradio 页面加载好了么??? 我能动你的元素了么??
|
34 |
function gradioLoaded(mutations) {
|
35 |
for (var i = 0; i < mutations.length; i++) {
|
36 |
if (mutations[i].addedNodes.length) {
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
userInfoDiv = document.getElementById("user-info");
|
41 |
-
appTitleDiv = document.getElementById("app-title");
|
42 |
-
chatbot = document.querySelector('#chuanhu-chatbot');
|
43 |
-
chatbotWrap = document.querySelector('#chuanhu-chatbot > .wrapper > .wrap');
|
44 |
-
apSwitch = document.querySelector('.apSwitch input[type="checkbox"]');
|
45 |
-
updateToast = document.querySelector("#toast-update");
|
46 |
-
sendBtn = document.getElementById("submit-btn");
|
47 |
-
cancelBtn = document.getElementById("cancel-btn");
|
48 |
-
sliders = document.querySelectorAll('input[type="range"]');
|
49 |
-
updateChuanhuBtn = document.getElementById("update-chuanhu-btn");
|
50 |
-
statusDisplay = document.querySelector('#status-display');
|
51 |
-
|
52 |
-
if (loginUserForm) {
|
53 |
-
localStorage.setItem("userLogged", true);
|
54 |
-
userLogged = true;
|
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 |
-
if (updateToast) {
|
86 |
-
const lastCheckTime = localStorage.getItem('lastCheckTime') || 0;
|
87 |
-
const longTimeNoCheck = currentTime - lastCheckTime > 3 * 24 * 60 * 60 * 1000;
|
88 |
-
if (longTimeNoCheck && !updateInfoGotten && !isLatestVersion || isLatestVersion && !updateInfoGotten) {
|
89 |
-
updateLatestVersion();
|
90 |
-
}
|
91 |
-
}
|
92 |
-
if (cancelBtn) {
|
93 |
-
submitObserver.observe(cancelBtn, { attributes: true, characterData: true});
|
94 |
-
}
|
95 |
}
|
96 |
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
97 |
}
|
98 |
|
99 |
function showConfirmationDialog(a, file, c) {
|
@@ -109,7 +118,6 @@ function showConfirmationDialog(a, file, c) {
|
|
109 |
function selectHistory() {
|
110 |
user_input_ta = user_input_tb.querySelector("textarea");
|
111 |
if (user_input_ta) {
|
112 |
-
observer.disconnect(); // 停止监听
|
113 |
disableSendBtn();
|
114 |
// 在 textarea 上监听 keydown 事件
|
115 |
user_input_ta.addEventListener("keydown", function (event) {
|
@@ -213,6 +221,40 @@ function setChatbotScroll() {
|
|
213 |
}
|
214 |
|
215 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
216 |
var submitObserver = new MutationObserver(function (mutationsList) {
|
217 |
document.querySelectorAll('#chuanhu-chatbot .message-wrap .message.bot').forEach(addChuanhuButton);
|
218 |
saveHistoryHtml();
|
|
|
5 |
|
6 |
var key_down_history = [];
|
7 |
var currentIndex = -1;
|
|
|
8 |
|
9 |
var gradioContainer = null;
|
10 |
var user_input_ta = null;
|
|
|
27 |
|
28 |
var isInIframe = (window.self !== window.top);
|
29 |
var currentTime = new Date().getTime();
|
30 |
+
var initialized = false;
|
31 |
|
32 |
// gradio 页面加载好了么??? 我能动你的元素了么??
|
33 |
function gradioLoaded(mutations) {
|
34 |
for (var i = 0; i < mutations.length; i++) {
|
35 |
if (mutations[i].addedNodes.length) {
|
36 |
+
if (initialized) {
|
37 |
+
observer.disconnect(); // 停止监听
|
38 |
+
return;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
39 |
}
|
40 |
+
initialize();
|
41 |
+
}
|
42 |
+
}
|
43 |
+
}
|
44 |
|
45 |
+
function initialize() {
|
46 |
+
var needInit = {gradioContainer, apSwitch, user_input_tb, userInfoDiv, appTitleDiv, chatbot, chatbotWrap, statusDisplay, sliders, updateChuanhuBtn};
|
47 |
+
initialized = true;
|
48 |
+
|
49 |
+
loginUserForm = gradioApp().querySelector(".gradio-container > .main > .wrap > .panel > .form")
|
50 |
+
gradioContainer = gradioApp().querySelector(".gradio-container");
|
51 |
+
user_input_tb = gradioApp().getElementById('user-input-tb');
|
52 |
+
userInfoDiv = gradioApp().getElementById("user-info");
|
53 |
+
appTitleDiv = gradioApp().getElementById("app-title");
|
54 |
+
chatbot = gradioApp().querySelector('#chuanhu-chatbot');
|
55 |
+
chatbotWrap = gradioApp().querySelector('#chuanhu-chatbot > .wrapper > .wrap');
|
56 |
+
apSwitch = gradioApp().querySelector('.apSwitch input[type="checkbox"]');
|
57 |
+
updateToast = gradioApp().querySelector("#toast-update");
|
58 |
+
sendBtn = gradioApp().getElementById("submit-btn");
|
59 |
+
cancelBtn = gradioApp().getElementById("cancel-btn");
|
60 |
+
sliders = gradioApp().querySelectorAll('input[type="range"]');
|
61 |
+
updateChuanhuBtn = gradioApp().getElementById("update-chuanhu-btn");
|
62 |
+
statusDisplay = gradioApp().querySelector('#status-display');
|
63 |
+
|
64 |
+
if (loginUserForm) {
|
65 |
+
localStorage.setItem("userLogged", true);
|
66 |
+
userLogged = true;
|
67 |
+
}
|
68 |
+
|
69 |
+
for (let elem in needInit) {
|
70 |
+
if (needInit[elem] == null) {
|
71 |
+
initialized = false;
|
72 |
+
return;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
73 |
}
|
74 |
}
|
75 |
+
|
76 |
+
if (initialized) {
|
77 |
+
adjustDarkMode();
|
78 |
+
selectHistory();
|
79 |
+
setTimeout(showOrHideUserInfo(), 2000);
|
80 |
+
setChatbotHeight();
|
81 |
+
setChatbotScroll();
|
82 |
+
setSlider();
|
83 |
+
if (!historyLoaded) loadHistoryHtml();
|
84 |
+
if (!usernameGotten) getUserInfo();
|
85 |
+
mObserver.observe(chatbotWrap, { attributes: true, childList: true, subtree: true, characterData: true });
|
86 |
+
submitObserver.observe(cancelBtn, { attributes: true, characterData: true });
|
87 |
+
|
88 |
+
const lastCheckTime = localStorage.getItem('lastCheckTime') || 0;
|
89 |
+
const longTimeNoCheck = currentTime - lastCheckTime > 3 * 24 * 60 * 60 * 1000;
|
90 |
+
if (longTimeNoCheck && !updateInfoGotten && !isLatestVersion || isLatestVersion && !updateInfoGotten) {
|
91 |
+
updateLatestVersion();
|
92 |
+
}
|
93 |
+
}
|
94 |
+
}
|
95 |
+
|
96 |
+
function gradioApp() {
|
97 |
+
const elems = document.getElementsByTagName('gradio-app');
|
98 |
+
const elem = elems.length == 0 ? document : elems[0];
|
99 |
+
|
100 |
+
if (elem !== document) {
|
101 |
+
elem.getElementById = function(id) {
|
102 |
+
return document.getElementById(id);
|
103 |
+
};
|
104 |
+
}
|
105 |
+
return elem.shadowRoot ? elem.shadowRoot : elem;
|
106 |
}
|
107 |
|
108 |
function showConfirmationDialog(a, file, c) {
|
|
|
118 |
function selectHistory() {
|
119 |
user_input_ta = user_input_tb.querySelector("textarea");
|
120 |
if (user_input_ta) {
|
|
|
121 |
disableSendBtn();
|
122 |
// 在 textarea 上监听 keydown 事件
|
123 |
user_input_ta.addEventListener("keydown", function (event) {
|
|
|
221 |
}
|
222 |
|
223 |
|
224 |
+
let timeoutId;
|
225 |
+
let isThrottled = false;
|
226 |
+
// 监听chatWrap元素的变化,为 bot 消息添加复制按钮。
|
227 |
+
var mObserver = new MutationObserver(function (mutationsList) {
|
228 |
+
for (const mmutation of mutationsList) {
|
229 |
+
if (mmutation.type === 'childList') {
|
230 |
+
for (var node of mmutation.addedNodes) {
|
231 |
+
if (node.nodeType === 1 && node.classList.contains('message')) {
|
232 |
+
saveHistoryHtml();
|
233 |
+
disableSendBtn();
|
234 |
+
document.querySelectorAll('#chuanhu-chatbot .message-wrap .message.bot').forEach(addChuanhuButton);
|
235 |
+
}
|
236 |
+
}
|
237 |
+
for (var node of mmutation.removedNodes) {
|
238 |
+
if (node.nodeType === 1 && node.classList.contains('message')) {
|
239 |
+
saveHistoryHtml();
|
240 |
+
disableSendBtn();
|
241 |
+
document.querySelectorAll('#chuanhu-chatbot .message-wrap .message.bot').forEach(addChuanhuButton);
|
242 |
+
}
|
243 |
+
}
|
244 |
+
} else if (mmutation.type === 'attributes') {
|
245 |
+
if (isThrottled) break; // 为了防止重复不断疯狂渲染,加上等待_(:з」∠)_
|
246 |
+
isThrottled = true;
|
247 |
+
clearTimeout(timeoutId);
|
248 |
+
timeoutId = setTimeout(() => {
|
249 |
+
isThrottled = false;
|
250 |
+
document.querySelectorAll('#chuanhu-chatbot .message-wrap .message.bot').forEach(addChuanhuButton);
|
251 |
+
saveHistoryHtml();
|
252 |
+
disableSendBtn();
|
253 |
+
}, 1500);
|
254 |
+
}
|
255 |
+
}
|
256 |
+
});
|
257 |
+
|
258 |
var submitObserver = new MutationObserver(function (mutationsList) {
|
259 |
document.querySelectorAll('#chuanhu-chatbot .message-wrap .message.bot').forEach(addChuanhuButton);
|
260 |
saveHistoryHtml();
|
web_assets/javascript/message-button.js
CHANGED
@@ -95,36 +95,3 @@ function addChuanhuButton(botElement) {
|
|
95 |
}
|
96 |
|
97 |
|
98 |
-
let timeoutId;
|
99 |
-
let isThrottled = false;
|
100 |
-
// 监听chatWrap元素的变化,为 bot 消息添加复制按钮。
|
101 |
-
var mObserver = new MutationObserver(function (mutationsList) {
|
102 |
-
for (const mmutation of mutationsList) {
|
103 |
-
if (mmutation.type === 'childList') {
|
104 |
-
for (var node of mmutation.addedNodes) {
|
105 |
-
if (node.nodeType === 1 && node.classList.contains('message')) {
|
106 |
-
saveHistoryHtml();
|
107 |
-
disableSendBtn();
|
108 |
-
document.querySelectorAll('#chuanhu-chatbot .message-wrap .message.bot').forEach(addChuanhuButton);
|
109 |
-
}
|
110 |
-
}
|
111 |
-
for (var node of mmutation.removedNodes) {
|
112 |
-
if (node.nodeType === 1 && node.classList.contains('message')) {
|
113 |
-
saveHistoryHtml();
|
114 |
-
disableSendBtn();
|
115 |
-
document.querySelectorAll('#chuanhu-chatbot .message-wrap .message.bot').forEach(addChuanhuButton);
|
116 |
-
}
|
117 |
-
}
|
118 |
-
} else if (mmutation.type === 'attributes') {
|
119 |
-
if (isThrottled) break; // 为了防止重复不断疯狂渲染,加上等待_(:з」∠)_
|
120 |
-
isThrottled = true;
|
121 |
-
clearTimeout(timeoutId);
|
122 |
-
timeoutId = setTimeout(() => {
|
123 |
-
isThrottled = false;
|
124 |
-
document.querySelectorAll('#chuanhu-chatbot .message-wrap .message.bot').forEach(addChuanhuButton);
|
125 |
-
saveHistoryHtml();
|
126 |
-
disableSendBtn();
|
127 |
-
}, 1500);
|
128 |
-
}
|
129 |
-
}
|
130 |
-
});
|
|
|
95 |
}
|
96 |
|
97 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|