Discussions » Development

doc.documentElement.innerHTML only works in FireFox

§
Posted: 2014-10-31
Edited: 2014-10-31

doc.documentElement.innerHTML only works in FireFox

Raised here

I have a bug present in a number of my scripts. I want a way to convert GM_xmlhttpRequest responseText to a document object. Currently I am using the following method:

var dt = document.implementation.createDocumentType("html", "-//W3C//DTD HTML 4.01 Transitional//EN", "http://www.w3.org/TR/html4/loose.dtd"),
doc = document.implementation.createDocument("", "", dt),
documentElement = doc.createElement("html");
documentElement.innerHTML = xhr.responseText;
doc.appendChild(documentElement);

Works fine in FireFox but it seems all the other major browsers do not support it. Any other way I can fix this?
Current script that use it include "OUJS-1", "Citrus GFork", "deviantArt Gallery Pager", "GM_Update" and "Pixiv++".

Thanks in advance

§
Posted: 2014-10-31

I don't think you need to make another document. Just create a div from the existing document, set innerHTML, then append all its children to your document.

wOxxOmMod
§
Posted: 2014-10-31
Edited: 2014-10-31

MPIV userscript uses this code which works in FF/Chrome:

function createDoc(text) {
    var doc = d.implementation.createHTMLDocument('MPIV');
    doc.documentElement.innerHTML = text;
    return doc;
}
§
Posted: 2014-10-31

Thanks that works. :)

Still, I am wondering if there is a way. Currently if this is the only way, it will cause a bit of a pain in some of my scripts as they need some functionality present in the document object.

Pretty easy in xmlHttpRequest, it just lacks support of referer.

§
Posted: 2014-10-31

You could also try responseType and responseXML on your XHR.

§
Posted: 2014-10-31

Thank you both. I posted my response without seeing his post.

wOxxOm method seems to works.

> You could also try responseType and responseXML on your XHR.

How?

§
Posted: 2014-11-01

If you can use the native XMLHttpRequest method (i.e., the request meets the same-origin test), you can get a document object back by setting responseType="document". I haven't tested recently, but this old script uses that approach: http://userscripts-mirror.org/scripts/review/132341.html (see function fetchPreview()).

§
Posted: 2014-11-01
Edited: 2014-11-01
If you can use the native XMLHttpRequest method (i.e., the request meets the same-origin test), you can get a document object back by setting responseType="document". I haven't tested recently, but this old script uses that approach: http://userscripts-mirror.org/scripts/review/132341.html (see function fetchPreview()).

It works, I use that method in another script. I need the GM version because I need to set referer.

§
Posted: 2014-11-01
function parseHTML(str) {
    var doc;
    try {
        // firefox and chrome 30+,Opera 12 will error
        doc = new DOMParser().parseFromString(str, "text/html");
    } catch (ex) {}

    if (!doc) {
        doc = document.implementation.createHTMLDocument("");
        doc.querySelector("html").innerHTML = str;
    }
    return doc;
}
§
Posted: 2014-11-13

Thanks.

Post reply

Sign in to post a reply.