通过JavaScript 解决第三方运营商dns劫持,删除广告

通过JavaScript 解决第三方运营商dns劫持,删除广告

1、将给自己写的script 代码加上 data-res 标记为自己的javascript

<script data-res="trust">
</script> 

2、添加删除运营商动态添加script代码`

<script data-res="trust">
//原生版 解决联通dns劫持。添加了广告
        function clearAdv() {
            var head = document.getElementsByTagName('head')[0];
            var children = head.childNodes;
            var res;
            var source = 'trust'; //信任资源
            for (var i in children) {
                if (children.hasOwnProperty(i)) {
                    tagName = children[i].tagName;
                    if (tagName && tagName == 'SCRIPT') {
                        res = children[i].dataset['res'];
                        if (res != source) {
                            head.removeChild(children[i]);
                        }
                    }
                }
            }
            var body = document.getElementsByTagName('body')[0];
            if (body) {
                children = body.childNodes;
                for (var k in children) {
                    if (children.hasOwnProperty(k)) {
                        var tagName = children[k].tagName;
                        if (tagName) {
                            res = children[k].dataset['res'];
                            if (res != source) {
                                body.removeChild(children[k]);
                            }
                        }
                    }
                }
            }

        }
        clearAdv();
</script>

你可能感兴趣的:(H5)