Spaces:
Runtime error
Runtime error
Update assets/custom.js
Browse files- assets/custom.js +32 -39
assets/custom.js
CHANGED
@@ -16,8 +16,18 @@ var chatbot = null;
|
|
16 |
|
17 |
var ga = document.getElementsByTagName("gradio-app");
|
18 |
var targetNode = ga[0];
|
|
|
|
|
19 |
|
20 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
21 |
function gradioLoaded(mutations) {
|
22 |
for (var i = 0; i < mutations.length; i++) {
|
23 |
if (mutations[i].addedNodes.length) {
|
@@ -26,18 +36,18 @@ function gradioLoaded(mutations) {
|
|
26 |
userInfoDiv = document.getElementById("user_info");
|
27 |
chatbot = document.querySelector('#chuanhu_chatbot');
|
28 |
|
29 |
-
if (gradioContainer) { //
|
30 |
adjustDarkMode();
|
31 |
}
|
32 |
if (user_input_tb) { // user_input_tb 加载出来了没?
|
33 |
selectHistory();
|
34 |
}
|
35 |
-
if (userInfoDiv) { //
|
36 |
setTimeout(showOrHideUserInfo(), 2000);
|
37 |
}
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
}
|
42 |
}
|
43 |
}
|
@@ -89,6 +99,7 @@ function selectHistory() {
|
|
89 |
});
|
90 |
}
|
91 |
}
|
|
|
92 |
function toggleUserInfoVisibility(shouldHide) {
|
93 |
if (userInfoDiv) {
|
94 |
if (shouldHide) {
|
@@ -179,51 +190,33 @@ function setChatbotHeight() {
|
|
179 |
const screenWidth = window.innerWidth;
|
180 |
const statusDisplay = document.querySelector('#status_display');
|
181 |
const statusDisplayHeight = statusDisplay ? statusDisplay.offsetHeight : 0;
|
182 |
-
|
183 |
const vh = window.innerHeight * 0.01;
|
184 |
document.documentElement.style.setProperty('--vh', `${vh}px`);
|
185 |
-
if (
|
186 |
-
|
|
|
|
|
|
|
187 |
chatbot.style.height = `calc(var(--vh, 1vh) * 100 - ${statusDisplayHeight + 150}px)`;
|
188 |
-
|
189 |
-
|
190 |
-
wrap.style.maxHeight = `calc(var(--vh, 1vh) * 100 - ${statusDisplayHeight + 150}px - var(--line-sm) * 1rem - 2 * var(--block-label-margin))`;
|
191 |
-
}
|
192 |
-
}
|
193 |
-
} else if (screenWidth <= 499) {
|
194 |
-
if (chatbot) {
|
195 |
chatbot.style.height = `calc(var(--vh, 1vh) * 100 - ${statusDisplayHeight + 100}px)`;
|
196 |
-
|
197 |
-
|
198 |
-
wrap.style.maxHeight = `calc(var(--vh, 1vh) * 100 - ${statusDisplayHeight + 100}px - var(--line-sm) * 1rem - 2 * var(--block-label-margin))`;
|
199 |
-
}
|
200 |
-
}
|
201 |
-
} else {
|
202 |
-
if (chatbot) {
|
203 |
chatbot.style.height = `calc(var(--vh, 1vh) * 100 - ${statusDisplayHeight + 160}px)`;
|
204 |
-
|
205 |
-
if (wrap) {
|
206 |
-
wrap.style.maxHeight = `calc(var(--vh, 1vh) * 100 - ${statusDisplayHeight + 160}px - var(--line-sm) * 1rem - 2 * var(--block-label-margin))`;
|
207 |
-
}
|
208 |
}
|
209 |
}
|
210 |
}
|
211 |
|
212 |
-
|
213 |
var observer = new MutationObserver(function (mutations) {
|
214 |
gradioLoaded(mutations);
|
215 |
});
|
216 |
observer.observe(targetNode, { childList: true, subtree: true });
|
217 |
|
218 |
-
|
219 |
-
window.addEventListener(
|
220 |
-
|
221 |
-
// setTimeout(function () {
|
222 |
-
// showOrHideUserInfo();
|
223 |
-
// setChatbotHeight();
|
224 |
-
// }, 2000);
|
225 |
-
// 本来是为了页面刷出来等两秒重试的,现在这么写不需要了~ 但框架先留着万一需要呢QAQ
|
226 |
-
});
|
227 |
-
// window.addEventListener('resize', setChatbotHeight);
|
228 |
-
// window.addEventListener('scroll', setChatbotHeight);
|
229 |
window.matchMedia("(prefers-color-scheme: dark)").addEventListener("change", adjustDarkMode);
|
|
|
16 |
|
17 |
var ga = document.getElementsByTagName("gradio-app");
|
18 |
var targetNode = ga[0];
|
19 |
+
var metaTags = document.getElementsByTagName('meta');
|
20 |
+
var isInHuggingFace = false;
|
21 |
|
22 |
+
for (var i = 0; i < metaTags.length; i++) {
|
23 |
+
var tag = metaTags[i];
|
24 |
+
if (tag.getAttribute('name') === 'twitter:site' && tag.getAttribute('content') === '@huggingface') {
|
25 |
+
isInHuggingFace = true;
|
26 |
+
}
|
27 |
+
break;
|
28 |
+
}
|
29 |
+
|
30 |
+
// gradio 页面加载好了么??? 我能动你的元素了么??
|
31 |
function gradioLoaded(mutations) {
|
32 |
for (var i = 0; i < mutations.length; i++) {
|
33 |
if (mutations[i].addedNodes.length) {
|
|
|
36 |
userInfoDiv = document.getElementById("user_info");
|
37 |
chatbot = document.querySelector('#chuanhu_chatbot');
|
38 |
|
39 |
+
if (gradioContainer) { // gradioCainter 加载出来了没?
|
40 |
adjustDarkMode();
|
41 |
}
|
42 |
if (user_input_tb) { // user_input_tb 加载出来了没?
|
43 |
selectHistory();
|
44 |
}
|
45 |
+
if (userInfoDiv) { // userInfoDiv 加载出来了没?
|
46 |
setTimeout(showOrHideUserInfo(), 2000);
|
47 |
}
|
48 |
+
if (chatbot) { // chatbot 加载出来了没?
|
49 |
+
setChatbotHeight()
|
50 |
+
}
|
51 |
}
|
52 |
}
|
53 |
}
|
|
|
99 |
});
|
100 |
}
|
101 |
}
|
102 |
+
|
103 |
function toggleUserInfoVisibility(shouldHide) {
|
104 |
if (userInfoDiv) {
|
105 |
if (shouldHide) {
|
|
|
190 |
const screenWidth = window.innerWidth;
|
191 |
const statusDisplay = document.querySelector('#status_display');
|
192 |
const statusDisplayHeight = statusDisplay ? statusDisplay.offsetHeight : 0;
|
193 |
+
const wrap = chatbot.querySelector('.wrap');
|
194 |
const vh = window.innerHeight * 0.01;
|
195 |
document.documentElement.style.setProperty('--vh', `${vh}px`);
|
196 |
+
if (isInHuggingFace) {
|
197 |
+
chatbot.style.height = `500px`;
|
198 |
+
wrap.style.maxHeight = `calc(500px - var(--line-sm) * 1rem - 2 * var(--block-label-margin))`
|
199 |
+
} else {
|
200 |
+
if (screenWidth <= 320) {
|
201 |
chatbot.style.height = `calc(var(--vh, 1vh) * 100 - ${statusDisplayHeight + 150}px)`;
|
202 |
+
wrap.style.maxHeight = `calc(var(--vh, 1vh) * 100 - ${statusDisplayHeight + 150}px - var(--line-sm) * 1rem - 2 * var(--block-label-margin))`;
|
203 |
+
} else if (screenWidth <= 499) {
|
|
|
|
|
|
|
|
|
|
|
204 |
chatbot.style.height = `calc(var(--vh, 1vh) * 100 - ${statusDisplayHeight + 100}px)`;
|
205 |
+
wrap.style.maxHeight = `calc(var(--vh, 1vh) * 100 - ${statusDisplayHeight + 100}px - var(--line-sm) * 1rem - 2 * var(--block-label-margin))`;
|
206 |
+
} else {
|
|
|
|
|
|
|
|
|
|
|
207 |
chatbot.style.height = `calc(var(--vh, 1vh) * 100 - ${statusDisplayHeight + 160}px)`;
|
208 |
+
wrap.style.maxHeight = `calc(var(--vh, 1vh) * 100 - ${statusDisplayHeight + 160}px - var(--line-sm) * 1rem - 2 * var(--block-label-margin))`;
|
|
|
|
|
|
|
209 |
}
|
210 |
}
|
211 |
}
|
212 |
|
213 |
+
// 监视页面内部 DOM 变动
|
214 |
var observer = new MutationObserver(function (mutations) {
|
215 |
gradioLoaded(mutations);
|
216 |
});
|
217 |
observer.observe(targetNode, { childList: true, subtree: true });
|
218 |
|
219 |
+
// 监视页面变化
|
220 |
+
window.addEventListener('resize', setChatbotHeight);
|
221 |
+
window.addEventListener('scroll', setChatbotHeight);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
222 |
window.matchMedia("(prefers-color-scheme: dark)").addEventListener("change", adjustDarkMode);
|