Chrome扩展程序替换html单词 [英] Chrome extension to replace html words

查看:323
本文介绍了Chrome扩展程序替换html单词的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻求创建一个Google扩展程序,该扩展程序将允许我替换站点html中的特定单词.例如:将隐藏"一词替换为显示".我不尝试更改页面文本,仅更改html.我想将其设置为切换类型扩展名,以便在需要时使用.谢谢任何能帮助我的人.

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.

推荐答案

您可能要参考此

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);
            }
        }
    }
}

希望这会有所帮助!

这篇关于Chrome扩展程序替换html单词的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆