Chrome extension to replace html words

試著忘記壹切 提交于 2019-12-24 07:53:01

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!