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.?