Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1004,109 +1004,174 @@ with gr.Blocks(css=custom_css, title="ChargeNode Kundtjänst") as app:
|
|
| 1004 |
with gr.Column(scale=1):
|
| 1005 |
support_btn = gr.Button("Behöver du mer hjälp?", elem_classes="support-btn")
|
| 1006 |
|
| 1007 |
-
# Lägg till anpassad JavaScript för att styra scrollning
|
| 1008 |
js_code = """
|
| 1009 |
-
//
|
| 1010 |
-
function
|
| 1011 |
-
|
|
|
|
|
|
|
|
|
|
| 1012 |
|
| 1013 |
-
//
|
| 1014 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1015 |
|
| 1016 |
-
//
|
| 1017 |
-
|
| 1018 |
-
|
| 1019 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1020 |
|
| 1021 |
-
|
| 1022 |
-
|
| 1023 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1024 |
}
|
| 1025 |
|
| 1026 |
-
|
| 1027 |
-
|
| 1028 |
-
|
| 1029 |
-
|
| 1030 |
-
|
| 1031 |
-
|
| 1032 |
-
|
| 1033 |
-
|
| 1034 |
-
|
| 1035 |
-
|
| 1036 |
-
|
| 1037 |
-
|
| 1038 |
-
|
| 1039 |
-
if (chatContainer) {
|
| 1040 |
-
const topPos = latestBotMessage.offsetTop;
|
| 1041 |
-
chatContainer.scrollTop = topPos;
|
| 1042 |
-
console.log("Manuell scrollning utförd");
|
| 1043 |
-
}
|
| 1044 |
-
} catch (e2) {
|
| 1045 |
-
console.error("Fallback-scrollning misslyckades:", e2);
|
| 1046 |
-
}
|
| 1047 |
-
}
|
| 1048 |
-
} else {
|
| 1049 |
-
console.log("Kunde inte hitta senaste botmeddelandet");
|
| 1050 |
}
|
| 1051 |
-
|
| 1052 |
-
|
| 1053 |
-
|
| 1054 |
-
|
| 1055 |
-
|
| 1056 |
-
|
| 1057 |
-
|
| 1058 |
-
|
|
|
|
|
|
|
| 1059 |
|
| 1060 |
-
|
|
|
|
|
|
|
| 1061 |
|
| 1062 |
-
//
|
| 1063 |
-
|
| 1064 |
-
setTimeout(
|
| 1065 |
-
|
|
|
|
|
|
|
|
|
|
| 1066 |
}
|
| 1067 |
-
}
|
| 1068 |
|
| 1069 |
-
//
|
| 1070 |
document.addEventListener('DOMContentLoaded', function() {
|
| 1071 |
-
console.log(
|
| 1072 |
-
|
| 1073 |
-
// Hitta chatcontainern
|
| 1074 |
-
const chatContainer = document.querySelector('.gradio-container');
|
| 1075 |
|
| 1076 |
-
|
| 1077 |
-
|
| 1078 |
-
|
| 1079 |
-
|
| 1080 |
-
|
| 1081 |
-
|
| 1082 |
-
|
| 1083 |
-
|
| 1084 |
-
|
| 1085 |
-
|
| 1086 |
-
|
| 1087 |
-
|
| 1088 |
-
|
| 1089 |
-
|
| 1090 |
}
|
| 1091 |
-
|
| 1092 |
-
|
| 1093 |
-
|
| 1094 |
-
|
| 1095 |
-
|
| 1096 |
-
|
| 1097 |
-
|
|
|
|
|
|
|
| 1098 |
}
|
| 1099 |
});
|
| 1100 |
|
| 1101 |
-
|
| 1102 |
-
|
| 1103 |
-
|
| 1104 |
-
|
| 1105 |
-
|
| 1106 |
-
|
| 1107 |
-
|
| 1108 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1109 |
});
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1110 |
"""
|
| 1111 |
app.load(js=js_code)
|
| 1112 |
|
|
|
|
| 1004 |
with gr.Column(scale=1):
|
| 1005 |
support_btn = gr.Button("Behöver du mer hjälp?", elem_classes="support-btn")
|
| 1006 |
|
| 1007 |
+
# Lägg till anpassad JavaScript för att styra scrollning och förhindra auto-scroll
|
| 1008 |
js_code = """
|
| 1009 |
+
// Förhindra Gradio's automatiska scrollning till botten
|
| 1010 |
+
(function() {
|
| 1011 |
+
// Spara originella scrollTo och scrollIntoView funktioner
|
| 1012 |
+
const originalScrollTo = window.scrollTo;
|
| 1013 |
+
const originalElementScrollIntoView = Element.prototype.scrollIntoView;
|
| 1014 |
+
const originalScrollIntoView = HTMLElement.prototype.scrollIntoView;
|
| 1015 |
|
| 1016 |
+
// Övervaka och förhindra automatisk scrollning till botten
|
| 1017 |
+
window.scrollTo = function(x, y) {
|
| 1018 |
+
console.log('scrollTo intercepted:', x, y);
|
| 1019 |
+
// Tillåt bara scrollning som inte är till botten av sidan
|
| 1020 |
+
if (typeof y !== 'number' || y < document.body.scrollHeight - window.innerHeight - 100) {
|
| 1021 |
+
originalScrollTo.apply(this, arguments);
|
| 1022 |
+
} else {
|
| 1023 |
+
console.log('Prevented automatic scroll to bottom');
|
| 1024 |
+
}
|
| 1025 |
+
};
|
| 1026 |
|
| 1027 |
+
// Övervaka scrollIntoView anrop
|
| 1028 |
+
Element.prototype.scrollIntoView = function(options) {
|
| 1029 |
+
console.log('Element scrollIntoView intercepted', this, options);
|
| 1030 |
+
// Kontrollera om elementet är ett botmeddelande eller en del av chattcontainern
|
| 1031 |
+
if (this.classList && (
|
| 1032 |
+
this.classList.contains('message') ||
|
| 1033 |
+
this.classList.contains('bot') ||
|
| 1034 |
+
this.closest('.message-wrap')
|
| 1035 |
+
)) {
|
| 1036 |
+
console.log('Handling chat element scrollIntoView');
|
| 1037 |
+
// Anropa vår egen scrollningsfunktion istället
|
| 1038 |
+
handleChatScroll();
|
| 1039 |
+
return;
|
| 1040 |
+
}
|
| 1041 |
+
// Annars, låt originella funktionen köra
|
| 1042 |
+
originalElementScrollIntoView.apply(this, arguments);
|
| 1043 |
+
};
|
| 1044 |
|
| 1045 |
+
HTMLElement.prototype.scrollIntoView = Element.prototype.scrollIntoView;
|
| 1046 |
+
|
| 1047 |
+
console.log('Scroll interception set up');
|
| 1048 |
+
})();
|
| 1049 |
+
|
| 1050 |
+
// Huvudfunktion för att hantera chattscrollning
|
| 1051 |
+
function handleChatScroll() {
|
| 1052 |
+
console.log('handleChatScroll called');
|
| 1053 |
+
|
| 1054 |
+
// Hitta chattcontainern
|
| 1055 |
+
const chatContainers = [
|
| 1056 |
+
document.querySelector('#chatbot_conversation .message-wrap'),
|
| 1057 |
+
document.querySelector('.gradio-container .chat'),
|
| 1058 |
+
document.querySelector('.gradio-container [id^="component-"] .chat'),
|
| 1059 |
+
document.querySelector('.gradio-container .message-wrap')
|
| 1060 |
+
].filter(Boolean);
|
| 1061 |
+
|
| 1062 |
+
if (chatContainers.length === 0) {
|
| 1063 |
+
console.log('No chat container found');
|
| 1064 |
+
return;
|
| 1065 |
}
|
| 1066 |
|
| 1067 |
+
const chatContainer = chatContainers[0];
|
| 1068 |
+
console.log('Found chat container:', chatContainer);
|
| 1069 |
+
|
| 1070 |
+
// Hitta alla botmeddelanden
|
| 1071 |
+
const botMessages = [
|
| 1072 |
+
...Array.from(document.querySelectorAll('.message.bot')),
|
| 1073 |
+
...Array.from(document.querySelectorAll('[data-testid="bot"]')),
|
| 1074 |
+
...Array.from(document.querySelectorAll('.message:not(.user)'))
|
| 1075 |
+
].filter(Boolean);
|
| 1076 |
+
|
| 1077 |
+
if (botMessages.length === 0) {
|
| 1078 |
+
console.log('No bot messages found');
|
| 1079 |
+
return;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1080 |
}
|
| 1081 |
+
|
| 1082 |
+
// Hitta det senaste botmeddelandet
|
| 1083 |
+
const latestBotMessage = botMessages[botMessages.length - 1];
|
| 1084 |
+
console.log('Latest bot message:', latestBotMessage);
|
| 1085 |
+
|
| 1086 |
+
// Beräkna position för att visa botmeddelandet i toppen
|
| 1087 |
+
try {
|
| 1088 |
+
const containerRect = chatContainer.getBoundingClientRect();
|
| 1089 |
+
const messageRect = latestBotMessage.getBoundingClientRect();
|
| 1090 |
+
const relativeTop = messageRect.top - containerRect.top;
|
| 1091 |
|
| 1092 |
+
// Scrolla så att botmeddelandet är i toppen
|
| 1093 |
+
chatContainer.scrollTop = chatContainer.scrollTop + relativeTop - 10; // 10px marginal
|
| 1094 |
+
console.log('Scrolled chat container to show bot message at top');
|
| 1095 |
|
| 1096 |
+
// Förhindra ytterligare scrollning under en kort period
|
| 1097 |
+
chatContainer.style.overflowY = 'hidden';
|
| 1098 |
+
setTimeout(() => {
|
| 1099 |
+
chatContainer.style.overflowY = '';
|
| 1100 |
+
}, 500);
|
| 1101 |
+
} catch (e) {
|
| 1102 |
+
console.error('Error during manual scrolling:', e);
|
| 1103 |
}
|
| 1104 |
+
}
|
| 1105 |
|
| 1106 |
+
// Lyssna på DOM-ändringar för att upptäcka nya meddelanden
|
| 1107 |
document.addEventListener('DOMContentLoaded', function() {
|
| 1108 |
+
console.log('DOM loaded, setting up observers');
|
|
|
|
|
|
|
|
|
|
| 1109 |
|
| 1110 |
+
// Använd MutationObserver för att upptäcka nya meddelanden
|
| 1111 |
+
const observer = new MutationObserver(function(mutations) {
|
| 1112 |
+
let newBotMessage = false;
|
| 1113 |
+
|
| 1114 |
+
mutations.forEach(function(mutation) {
|
| 1115 |
+
if (mutation.type === 'childList' && mutation.addedNodes.length) {
|
| 1116 |
+
mutation.addedNodes.forEach(function(node) {
|
| 1117 |
+
if (node.nodeType === 1) { // Element node
|
| 1118 |
+
// Kontrollera om det är ett botmeddelande
|
| 1119 |
+
if (node.classList && (
|
| 1120 |
+
node.classList.contains('bot') ||
|
| 1121 |
+
node.classList.contains('message')
|
| 1122 |
+
)) {
|
| 1123 |
+
newBotMessage = true;
|
| 1124 |
}
|
| 1125 |
+
// Eller om det innehåller ett botmeddelande
|
| 1126 |
+
else if (node.querySelector && (
|
| 1127 |
+
node.querySelector('.bot') ||
|
| 1128 |
+
node.querySelector('.message:not(.user)')
|
| 1129 |
+
)) {
|
| 1130 |
+
newBotMessage = true;
|
| 1131 |
+
}
|
| 1132 |
+
}
|
| 1133 |
+
});
|
| 1134 |
}
|
| 1135 |
});
|
| 1136 |
|
| 1137 |
+
if (newBotMessage) {
|
| 1138 |
+
console.log('New bot message detected');
|
| 1139 |
+
// Använd flera timeouts för att säkerställa att scrollningen fungerar
|
| 1140 |
+
// efter att alla DOM-uppdateringar är klara
|
| 1141 |
+
setTimeout(handleChatScroll, 100);
|
| 1142 |
+
setTimeout(handleChatScroll, 300);
|
| 1143 |
+
setTimeout(handleChatScroll, 500);
|
| 1144 |
+
setTimeout(handleChatScroll, 1000);
|
| 1145 |
+
}
|
| 1146 |
+
});
|
| 1147 |
+
|
| 1148 |
+
// Starta övervakning av hela dokumentet
|
| 1149 |
+
observer.observe(document.body, {
|
| 1150 |
+
childList: true,
|
| 1151 |
+
subtree: true
|
| 1152 |
+
});
|
| 1153 |
+
|
| 1154 |
+
console.log('MutationObserver started');
|
| 1155 |
+
|
| 1156 |
+
// Lyssna även på klickhändelser för att hantera scrollning efter användarinteraktion
|
| 1157 |
+
document.addEventListener('click', function(event) {
|
| 1158 |
+
// Fördröj för att låta eventuella UI-uppdateringar slutföras
|
| 1159 |
+
setTimeout(handleChatScroll, 300);
|
| 1160 |
+
});
|
| 1161 |
});
|
| 1162 |
+
|
| 1163 |
+
// Lägg till CSS för att förbättra scrollningsbeteendet
|
| 1164 |
+
const style = document.createElement('style');
|
| 1165 |
+
style.textContent = `
|
| 1166 |
+
#chatbot_conversation .message-wrap {
|
| 1167 |
+
scroll-behavior: smooth !important;
|
| 1168 |
+
overflow-anchor: none !important; /* Förhindra automatisk ankring */
|
| 1169 |
+
}
|
| 1170 |
+
.message.bot {
|
| 1171 |
+
scroll-margin-top: 10px !important;
|
| 1172 |
+
}
|
| 1173 |
+
`;
|
| 1174 |
+
document.head.appendChild(style);
|
| 1175 |
"""
|
| 1176 |
app.load(js=js_code)
|
| 1177 |
|