返回首頁 

讨论»开发

doc.documentElement.innerHTML only works in FireFox

发表于:2014-10-31
编辑于: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

woxxom管理员
发表于:2014-10-31
编辑于: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;}
发表于: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.

发表于: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?

发表于: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()).

发表于:2014-11-01
编辑于: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.

发表于:2014-11-01
function parseHTML(str) {var doc;try {// firefox and chrome 30+,Opera 12 will errordoc = new DOMParser().parseFromString(str, "text/html");} catch (ex) {}if (!doc) {doc = document.implementation.createHTMLDocument("");doc.querySelector("html").innerHTML = str;}return doc;}
发表于:2014-11-13

Thanks.

发表回复

登录以发表回复。