Spaces:
Running
Running
Martechsol commited on
Commit ·
77f8b73
1
Parent(s): ba1ada1
Fix styling and injection logic in loader
Browse files- static/chat-loader.js +18 -13
static/chat-loader.js
CHANGED
|
@@ -14,27 +14,32 @@
|
|
| 14 |
fetch(`${baseUrl}/widget?v=${Date.now()}`) // Cache busting
|
| 15 |
.then(response => response.text())
|
| 16 |
.then(html => {
|
| 17 |
-
//
|
| 18 |
-
const
|
| 19 |
-
|
| 20 |
-
.replace(/<html[^>]*>/gi, '')
|
| 21 |
-
.replace(/<\/html>/gi, '')
|
| 22 |
-
.replace(/<head[^>]*>[\s\S]*?<\/head>/gi, '')
|
| 23 |
-
.replace(/<body[^>]*>/gi, '')
|
| 24 |
-
.replace(/<\/body>/gi, '');
|
| 25 |
|
| 26 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 27 |
|
| 28 |
-
//
|
| 29 |
-
|
| 30 |
-
scripts.forEach(oldScript => {
|
| 31 |
const newScript = document.createElement('script');
|
| 32 |
Array.from(oldScript.attributes).forEach(attr => {
|
| 33 |
newScript.setAttribute(attr.name, attr.value);
|
| 34 |
});
|
| 35 |
newScript.innerHTML = oldScript.innerHTML;
|
| 36 |
document.body.appendChild(newScript);
|
| 37 |
-
oldScript.parentNode.removeChild(oldScript);
|
| 38 |
});
|
| 39 |
|
| 40 |
console.log("Martechsol Assistant Loaded Successfully");
|
|
|
|
| 14 |
fetch(`${baseUrl}/widget?v=${Date.now()}`) // Cache busting
|
| 15 |
.then(response => response.text())
|
| 16 |
.then(html => {
|
| 17 |
+
// Create a temporary element to parse the HTML
|
| 18 |
+
const parser = new DOMParser();
|
| 19 |
+
const doc = parser.parseFromString(html, 'text/html');
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 20 |
|
| 21 |
+
// 1. Extract and inject styles
|
| 22 |
+
doc.querySelectorAll('style').forEach(st => {
|
| 23 |
+
document.head.appendChild(st.cloneNode(true));
|
| 24 |
+
});
|
| 25 |
+
|
| 26 |
+
// 2. Extract and inject body content
|
| 27 |
+
const wrapper = doc.querySelector('#martech-chat-wrapper');
|
| 28 |
+
if (wrapper) {
|
| 29 |
+
container.innerHTML = wrapper.outerHTML;
|
| 30 |
+
} else {
|
| 31 |
+
// Fallback: just take the body content
|
| 32 |
+
container.innerHTML = doc.body.innerHTML;
|
| 33 |
+
}
|
| 34 |
|
| 35 |
+
// 3. Extract and execute scripts (critical for logic)
|
| 36 |
+
doc.querySelectorAll('script').forEach(oldScript => {
|
|
|
|
| 37 |
const newScript = document.createElement('script');
|
| 38 |
Array.from(oldScript.attributes).forEach(attr => {
|
| 39 |
newScript.setAttribute(attr.name, attr.value);
|
| 40 |
});
|
| 41 |
newScript.innerHTML = oldScript.innerHTML;
|
| 42 |
document.body.appendChild(newScript);
|
|
|
|
| 43 |
});
|
| 44 |
|
| 45 |
console.log("Martechsol Assistant Loaded Successfully");
|