首页 > 解决方案 > 覆盖 document.write

问题描述

我正在尝试覆盖 document.write,因此我将能够获取原始 html 解析它对代码进行一些操作并返回调用。我的整个过程是异步的,所以不用说当页面完成加载 document.write 时调用 document.write 将擦除整个文档,所以我不记得 document.write。我已经搜索并发现了很多关于此的讨论,但几乎每个讨论都非常古老,所以我找不到任何好的答案。我现在的代码非常基本:

 const prevDocWrite = document.write;
 document.write = function(str,patched) {
        if(patched){

            prevDocWrite.call(this, str);
        }else{
            Dom_Parser(str, context).then(newStr => {

                prevDocWrite.call(this, newStr);
            })};
    };

我添加了“修补”部分,因为我也在我的代码上调用 document.write,所以我像 document.write("str",true) 一样调用它,它会立即调用原始 document.write。

我非常感谢任何帮助或想法如何实现这一点(其他项目供参考会很棒)

顺便说一句,我看到了很多使用 innerHtml 的实现,但这搞砸了<script>标签:(

多谢

标签: javascripthtmldommonkeypatchingdocument.write

解决方案


推荐阅读