Spaces:
Sleeping
Sleeping
Fix: prevent protection reinjection loop causing visual jumps
Browse files
interface/components/image_protection.py
CHANGED
|
@@ -138,13 +138,19 @@ _PROTECTION_JS_HTML = """
|
|
| 138 |
}
|
| 139 |
lockImages(doc);
|
| 140 |
|
|
|
|
|
|
|
| 141 |
var obs = new MutationObserver(function (mutations) {
|
| 142 |
-
|
| 143 |
-
|
| 144 |
-
|
| 145 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 146 |
}
|
| 147 |
-
}
|
| 148 |
});
|
| 149 |
obs.observe(doc.body, { childList: true, subtree: true });
|
| 150 |
|
|
@@ -169,6 +175,12 @@ def inject_image_protection():
|
|
| 169 |
|
| 170 |
Call this ONCE near the top of main.py, after st.set_page_config().
|
| 171 |
"""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 172 |
# CSS — works natively via st.markdown
|
| 173 |
st.markdown(_PROTECTION_CSS, unsafe_allow_html=True)
|
| 174 |
|
|
|
|
| 138 |
}
|
| 139 |
lockImages(doc);
|
| 140 |
|
| 141 |
+
// Debounce to reduce excessive firing
|
| 142 |
+
var lockTimer = null;
|
| 143 |
var obs = new MutationObserver(function (mutations) {
|
| 144 |
+
if (lockTimer) return; // skip if already scheduled
|
| 145 |
+
lockTimer = setTimeout(function() {
|
| 146 |
+
lockTimer = null;
|
| 147 |
+
for (var m = 0; m < mutations.length; m++) {
|
| 148 |
+
var nodes = mutations[m].addedNodes;
|
| 149 |
+
for (var n = 0; n < nodes.length; n++) {
|
| 150 |
+
if (nodes[n].nodeType === 1) lockImages(nodes[n]);
|
| 151 |
+
}
|
| 152 |
}
|
| 153 |
+
}, 100); // debounce 100ms
|
| 154 |
});
|
| 155 |
obs.observe(doc.body, { childList: true, subtree: true });
|
| 156 |
|
|
|
|
| 175 |
|
| 176 |
Call this ONCE near the top of main.py, after st.set_page_config().
|
| 177 |
"""
|
| 178 |
+
# Guard: only inject once per session to avoid rerun loops
|
| 179 |
+
if "image_protection_injected" in st.session_state:
|
| 180 |
+
return
|
| 181 |
+
|
| 182 |
+
st.session_state.image_protection_injected = True
|
| 183 |
+
|
| 184 |
# CSS — works natively via st.markdown
|
| 185 |
st.markdown(_PROTECTION_CSS, unsafe_allow_html=True)
|
| 186 |
|