Fix reveal mode not restoring substitute values on toggle off
The old unrevealInElement relied on a WeakMap (originalTexts) to restore text nodes to their pre-reveal state. This broke when SPA frameworks (React) re-rendered the DOM while reveal was active — new text nodes containing real values had no WeakMap entry to restore from, so real data stayed visible after turning reveal off. Fix: unrevealInElement now actively reverse-replaces real values back to their substitute counterparts using the reveal pairs, matching the same approach revealText uses in the forward direction. This works regardless of DOM re-renders or streaming content changes. https://claude.ai/code/session_01NNBEPuXMFGWezJb1f958nL
This commit is contained in:
+26
-4
@@ -1035,15 +1035,37 @@
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function unrevealText(text) {
|
||||||
|
const pairs = getRevealPairs();
|
||||||
|
let result = text;
|
||||||
|
for (const p of pairs) {
|
||||||
|
const escaped = esc(p.to); // p.to is the real value
|
||||||
|
const regex = new RegExp(escaped, p.caseSensitive ? 'g' : 'gi');
|
||||||
|
result = result.replace(regex, p.from); // p.from is the substitute
|
||||||
|
}
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
function unrevealInElement(el) {
|
function unrevealInElement(el) {
|
||||||
if (SKIP_REVEAL_TAGS.has(el.tagName)) return;
|
if (SKIP_REVEAL_TAGS.has(el.tagName)) return;
|
||||||
|
|
||||||
const walker = document.createTreeWalker(el, NodeFilter.SHOW_TEXT);
|
const walker = document.createTreeWalker(el, NodeFilter.SHOW_TEXT, {
|
||||||
|
acceptNode(node) {
|
||||||
|
const parent = node.parentElement;
|
||||||
|
if (parent && SKIP_REVEAL_TAGS.has(parent.tagName)) return NodeFilter.FILTER_REJECT;
|
||||||
|
if (parent?.closest?.('.ss-autodetect-warning, .ss-presend-warning, .ss-reveal-badge')) return NodeFilter.FILTER_REJECT;
|
||||||
|
return NodeFilter.FILTER_ACCEPT;
|
||||||
|
}
|
||||||
|
});
|
||||||
let textNode;
|
let textNode;
|
||||||
while ((textNode = walker.nextNode())) {
|
while ((textNode = walker.nextNode())) {
|
||||||
const original = originalTexts.get(textNode);
|
const text = textNode.textContent;
|
||||||
if (original && textNode.textContent !== original) {
|
if (!text || text.length < MIN_STRING_LENGTH) continue;
|
||||||
textNode.textContent = original;
|
const unrevealed = unrevealText(text);
|
||||||
|
if (unrevealed !== text) {
|
||||||
|
textNode.textContent = unrevealed;
|
||||||
|
// Update saved original so future reveals start from the right state
|
||||||
|
originalTexts.set(textNode, unrevealed);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user