0

I'm trying to make an easy chrom extension that replaces several strings from websites with other strings. I've found a helpful piece of code in this stackoverflow answer, but it only works for replacing one string with another:

 // create a TreeWalker of all text nodes
var allTextNodes = document.createTreeWalker(document.body, NodeFilter.SHOW_TEXT),
// some temp references for performance
tmptxt,
tmpnode,
// compile the RE and cache the replace string, for performance
cakeRE = /cake/g,
replaceValue = "pie";

// iterate through all text nodes
while (allTextNodes.nextNode()) {
tmpnode = allTextNodes.currentNode;
tmptxt = tmpnode.nodeValue;
tmpnode.nodeValue = tmptxt.replace(cakeRE, replaceValue);
}

How would I modify this code if I have several strings to replace, e.g. I also want to replace "fork" with "spoon", "pasta" with "ice cream" etc.?

tobbog
  • 53
  • 7
  • I wrote [this answer](https://stackoverflow.com/a/70206690/4054683) earlier, with function example. You can call function multiple times with different words. Maybe put the tree walker inside the function to improve it. – Neea Dec 11 '21 at 07:40

0 Answers0