1

I am trying to find and replace all the text on a webpage with JavaScript. I am currently using a variation of the following code from this answer which recursively processes every Text node in the document.

function replaceInText(element, pattern, replacement) {
    for (let node of element.childNodes) {
        switch (node.nodeType) {
            case Node.ELEMENT_NODE:
                replaceInText(node, pattern, replacement);
                break;
            case Node.TEXT_NODE:
                node.textContent = node.textContent.replace(pattern, replacement);
                break;
            case Node.DOCUMENT_NODE:
                replaceInText(node, pattern, replacement);
        }
    }
}

replaceInText(document, /e/gi, '');

Once I tested it out, I discovered that there's an exception: the placeholder attribute of <input> and <textarea> elements. These are not Text nodes, but are still displayed as visible text by the browser and searchable with CTRL-F. Try running the above on the Stack Overflow homepage and see that the search bar is left out.

Now I'm wondering if there's any other attributes out there like this too. All I can currently think of is value for <input> and <textarea>, alt for <img> and <area>, and the global title attribute (tooltips), and I'm worried I'm missing some obscure attribute since I couldn't find a comprehensive list of this sort anywhere.

Daniel Ting
  • 83
  • 1
  • 7

1 Answers1

1

It's been a while and I've discovered value for input[type="button"]and I will record my list so far below. I have marked the answer as community wiki so anybody who finds more can easily add them.

Daniel Ting
  • 83
  • 1
  • 7