Spaces:
Running on CPU Upgrade
Running on CPU Upgrade
update htmlembed
Browse files
app/src/components/HtmlEmbed.astro
CHANGED
|
@@ -105,104 +105,106 @@ const htmlWithId =
|
|
| 105 |
<script>
|
| 106 |
// Re-execute <script> tags inside the injected fragment (innerHTML doesn't run scripts)
|
| 107 |
// Uses IntersectionObserver for lazy loading - only executes when embed is visible
|
| 108 |
-
|
| 109 |
-
|
| 110 |
-
|
|
|
|
| 111 |
|
| 112 |
-
|
| 113 |
|
| 114 |
-
|
| 115 |
-
|
| 116 |
-
|
| 117 |
-
|
| 118 |
|
| 119 |
-
|
| 120 |
-
|
| 121 |
-
|
| 122 |
-
|
| 123 |
-
|
| 124 |
-
|
| 125 |
-
|
| 126 |
-
|
| 127 |
-
|
| 128 |
-
|
| 129 |
-
|
| 130 |
-
|
| 131 |
-
|
| 132 |
-
|
| 133 |
-
|
| 134 |
-
|
| 135 |
-
|
| 136 |
-
|
| 137 |
-
|
| 138 |
-
|
| 139 |
-
|
| 140 |
-
|
| 141 |
-
|
| 142 |
-
|
|
|
|
| 143 |
}
|
| 144 |
-
}
|
| 145 |
-
});
|
| 146 |
|
| 147 |
-
|
| 148 |
-
|
| 149 |
-
|
| 150 |
|
| 151 |
-
|
| 152 |
-
|
| 153 |
-
|
| 154 |
-
|
| 155 |
-
|
| 156 |
-
|
| 157 |
-
|
| 158 |
-
|
| 159 |
-
|
| 160 |
-
|
| 161 |
-
|
| 162 |
-
|
| 163 |
-
|
| 164 |
-
|
| 165 |
-
|
| 166 |
-
|
| 167 |
-
|
|
|
|
| 168 |
}
|
| 169 |
-
}
|
| 170 |
-
}
|
| 171 |
-
|
| 172 |
-
|
| 173 |
-
|
| 174 |
-
|
| 175 |
-
|
| 176 |
-
|
| 177 |
-
);
|
| 178 |
|
| 179 |
-
|
| 180 |
|
| 181 |
-
|
| 182 |
-
|
| 183 |
-
|
| 184 |
-
|
| 185 |
-
|
| 186 |
-
|
| 187 |
-
|
| 188 |
-
|
| 189 |
-
|
| 190 |
-
|
| 191 |
-
|
| 192 |
-
|
|
|
|
| 193 |
}
|
| 194 |
-
}
|
| 195 |
-
}, 3000);
|
| 196 |
-
} else {
|
| 197 |
-
// Fallback for browsers without IntersectionObserver support
|
| 198 |
-
if (document.readyState === "loading") {
|
| 199 |
-
document.addEventListener("DOMContentLoaded", execute, { once: true });
|
| 200 |
} else {
|
| 201 |
-
|
| 202 |
-
|
| 203 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 204 |
}
|
| 205 |
-
}
|
| 206 |
</script>
|
| 207 |
|
| 208 |
<style is:global>
|
|
|
|
| 105 |
<script>
|
| 106 |
// Re-execute <script> tags inside the injected fragment (innerHTML doesn't run scripts)
|
| 107 |
// Uses IntersectionObserver for lazy loading - only executes when embed is visible
|
| 108 |
+
(() => {
|
| 109 |
+
const scriptEl = document.currentScript;
|
| 110 |
+
const figure = scriptEl?.previousElementSibling?.closest(".html-embed");
|
| 111 |
+
const mount = scriptEl ? scriptEl.previousElementSibling : null;
|
| 112 |
|
| 113 |
+
if (!mount || !figure) return;
|
| 114 |
|
| 115 |
+
let executed = false;
|
| 116 |
+
const execute = () => {
|
| 117 |
+
if (executed || !mount) return;
|
| 118 |
+
executed = true;
|
| 119 |
|
| 120 |
+
const scripts = mount.querySelectorAll("script");
|
| 121 |
+
scripts.forEach((old) => {
|
| 122 |
+
// ignore non-executable types (e.g., application/json)
|
| 123 |
+
if (
|
| 124 |
+
old.type &&
|
| 125 |
+
old.type !== "text/javascript" &&
|
| 126 |
+
old.type !== "module" &&
|
| 127 |
+
old.type !== ""
|
| 128 |
+
)
|
| 129 |
+
return;
|
| 130 |
+
if (old.dataset.executed === "true") return;
|
| 131 |
+
old.dataset.executed = "true";
|
| 132 |
+
if (old.src) {
|
| 133 |
+
const s = document.createElement("script");
|
| 134 |
+
Array.from(old.attributes).forEach((attr) =>
|
| 135 |
+
s.setAttribute(attr.name, attr.value),
|
| 136 |
+
);
|
| 137 |
+
document.body.appendChild(s);
|
| 138 |
+
} else {
|
| 139 |
+
try {
|
| 140 |
+
// run inline
|
| 141 |
+
(0, eval)(old.text || "");
|
| 142 |
+
} catch (e) {
|
| 143 |
+
console.error("HtmlEmbed inline script error:", e);
|
| 144 |
+
}
|
| 145 |
}
|
| 146 |
+
});
|
|
|
|
| 147 |
|
| 148 |
+
// Mark as loaded
|
| 149 |
+
figure.classList.add("html-embed--loaded");
|
| 150 |
+
};
|
| 151 |
|
| 152 |
+
// Check if IntersectionObserver is supported
|
| 153 |
+
if ("IntersectionObserver" in window) {
|
| 154 |
+
const observer = new IntersectionObserver(
|
| 155 |
+
(entries) => {
|
| 156 |
+
entries.forEach((entry) => {
|
| 157 |
+
if (entry.isIntersecting && !executed) {
|
| 158 |
+
observer.disconnect();
|
| 159 |
+
// Small delay to ensure DOM is ready
|
| 160 |
+
if (document.readyState === "loading") {
|
| 161 |
+
document.addEventListener("DOMContentLoaded", execute, {
|
| 162 |
+
once: true,
|
| 163 |
+
});
|
| 164 |
+
} else {
|
| 165 |
+
// Use requestAnimationFrame to ensure execution after DOM is fully ready
|
| 166 |
+
requestAnimationFrame(() => {
|
| 167 |
+
setTimeout(execute, 0);
|
| 168 |
+
});
|
| 169 |
+
}
|
| 170 |
}
|
| 171 |
+
});
|
| 172 |
+
},
|
| 173 |
+
{
|
| 174 |
+
// Start loading when element is 100px away from viewport
|
| 175 |
+
rootMargin: "100px",
|
| 176 |
+
threshold: 0.01,
|
| 177 |
+
},
|
| 178 |
+
);
|
|
|
|
| 179 |
|
| 180 |
+
observer.observe(figure);
|
| 181 |
|
| 182 |
+
// Fallback: if still not loaded after 3 seconds, load anyway (for edge cases)
|
| 183 |
+
setTimeout(() => {
|
| 184 |
+
if (!executed) {
|
| 185 |
+
observer.disconnect();
|
| 186 |
+
if (document.readyState === "loading") {
|
| 187 |
+
document.addEventListener("DOMContentLoaded", execute, {
|
| 188 |
+
once: true,
|
| 189 |
+
});
|
| 190 |
+
} else {
|
| 191 |
+
requestAnimationFrame(() => {
|
| 192 |
+
setTimeout(execute, 0);
|
| 193 |
+
});
|
| 194 |
+
}
|
| 195 |
}
|
| 196 |
+
}, 3000);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 197 |
} else {
|
| 198 |
+
// Fallback for browsers without IntersectionObserver support
|
| 199 |
+
if (document.readyState === "loading") {
|
| 200 |
+
document.addEventListener("DOMContentLoaded", execute, { once: true });
|
| 201 |
+
} else {
|
| 202 |
+
requestAnimationFrame(() => {
|
| 203 |
+
setTimeout(execute, 0);
|
| 204 |
+
});
|
| 205 |
+
}
|
| 206 |
}
|
| 207 |
+
})();
|
| 208 |
</script>
|
| 209 |
|
| 210 |
<style is:global>
|