【发布时间】:2021-01-16 11:54:35
【问题描述】:
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。
【问题讨论】:
-
Tampermonkey 在页面上下文中运行,而 GM4 在真正的沙箱中运行。为此,您需要使用 WrappedJSObject 和 exportFunction,请参阅more info(它用于扩展,但也适用于此处)。看看你能不能改用
new Document()。 -
@wOxxOm 感谢您的信息。如果我使用
new Document()而不是document.implementation.createHTMLDocument(),doc.open()上的结果是 DOMException: 尝试使用不可用或不再可用的对象。 -
@wOxxOm 我阅读了两个文档,属性和功能。它指的是内容脚本与页面脚本。不幸的是,我对 JS 的了解并没有那么深入,看这对我有什么帮助。
-
GM4 的沙盒就像一个内容脚本,而 document.implementation 指的是页面脚本上下文,我猜。顺便说一句,你为什么要使用这种超级神秘的方法,为什么不使用 insertAdjacentHTML?
-
如果 doctype 与当前文档的相同,您可能可以使用 DOMParser 并将 document.documentElement 替换为新的。
标签: tampermonkey userscripts domexception greasemonkey-4 htmldocumentclass