-
Couldn't load subscription status.
- Fork 26
Description
This is a really cool. I looked at the code see you are referencing the innerHTML of a given node for text replacement. This is risky in that innerHTML returns all descendant code in addition to text, which means you could replace things like tag names or attribute values by accident. It would be safer if you just gathered the text. Fortunately, I have a solution for you.
This handy dandy tool allows gathering of DOM nodes by node type from any element. You can take most of the code out of this tool since you will always be looking for text node types and don't care about attribute values or other node types. Since the tool is native DOM walking it is really fast and also has no dependencies. https://github.com/prettydiff/getNodesByType
Using the mentioned solution will bloat your logic a bit as it will return an array of text nodes that you will have to iterate over instead of just a single string. You can modify the value of text node by reassigning to its textContent property and it will automagically update in the page. If you need to wrap a result in a span tag then innerHTML would be more efficient.