Greasemonkey 抛出“DOMException:操作不安全”关于 document.implementation.createHTMLDocument().open()

问题描述

FF 84.0.2,通用 4.10.0

代码GitLab。相关部分是:

        ...
        const doc = document.implementation.createHTMLDocument('http://www.w3.org/1999/xhtml','html');
console.debug("DOC CREATED")
        doc.open() // <-- with GM: DOMException: The operation is insecure.
console.debug("DOC OPENED")
        ...

控制台输出

...
DOC CREATED
DOMException: The operation is insecure.

脚本适用于 Tampermonkey。

解决方法

answer to DOM parsing in JavaScript 那里得到了解决方案:

        ...
        const doc = document.implementation.createHTMLDocument('http://www.w3.org/1999/xhtml','html');
        doc.documentElement.innerHTML = page.responseText
        ...

代替:

        ...
        const doc = document.implementation.createHTMLDocument('http://www.w3.org/1999/xhtml','html');
        doc.open()
        doc.write( page.responseText )
        ...

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...