问题
I am looking to create a google extension that will allow me to replace a specific word in a sites html. Ex: replace the word "hidden" with "shown". I am not trying to change page text, just the html. I would like to make it a toggle type extension that I can use when needed. Thank you to anyone who can help me.
回答1:
You may want to refer with this blog tutorial. Here's a sample code of how to replace a specific text into another word.
var elements = document.getElementsByTagName('*');
for (var i = 0; i < elements.length; i++) {
var element = elements[i];
for (var j = 0; j < element.childNodes.length; j++) {
var node = element.childNodes[j];
if (node.nodeType === 3) {
var text = node.nodeValue;
var replacedText = text.replace(/[word or phrase to replace here]/gi, '[new word or phrase]');
if (replacedText !== text) {
element.replaceChild(document.createTextNode(replacedText), node);
}
}
}
}
Hope this helps!
来源:https://stackoverflow.com/questions/47688518/chrome-extension-to-replace-html-words