Solution 1 :

Got a solution from an answer to DOM parsing in JavaScript:

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

instead of:

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

Problem :

FF 84.0.2, GM 4.10.0

The code can be seen at GitLab. The relevant part is:

        ...
        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")
        ...

Console output:

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

Script works with Tampermonkey.

Comments

Comment posted by more info

Tampermonkey runs in page context whereas GM4 runs in a true sandbox. You’ll need to use wrappedJSObject and exportFunction for this, see

Comment posted by Gerold Broser

@wOxxOm Thanks for the info. If I use

Comment posted by Gerold Broser

@wOxxOm I read the doc for the two, property and function. It refers to content scripts vs. page scripts. Unfortunately I’m not that deep into JS to see how this can help me here.

Comment posted by wOxxOm

Sandbox of GM4 is like a content script whereas document.implementation refers to the page script context, I guess. BTW why do you use this super arcane method at all, why not insertAdjacentHTML?

Comment posted by wOxxOm

If doctype is the same as the current document’s you can probably use DOMParser and replace document.documentElement with the new one.

By