Skip to content Skip to sidebar Skip to footer

Fetching Xml With GM_xmlhttpRequest

I'm trying to retrieve a page with greasemonkey and then extract a link from it, inserting the link into the current page. I'm having some trouble with: GM_xmlhttpRequest({ method:

Solution 1:

if the requested page is a well-formatted xml, then you are in the correct way.
but you should change data.response.xml to data.responseXML

and i THINK you can't do this with a XMLDocument (result of the xml parser) because .getElementById works in HTMLDocument.
however, you could do the following to have a valid HTMLDocument:

if (/^Content-Type: text\/xml/m.test(data.responseHeaders)) {
    data.responseXML = new DOMParser().parseFromString(data.responseText, "text/xml");
}
else if (/^Content-Type: text\/html/m.test(data.responseHeaders)) {
    var dt = document.implementation.createDocumentType("html", "-//W3C//DTD HTML 4.01 Transitional//EN", "http://www.w3.org/TR/html4/loose.dtd");
    var doc = document.implementation.createDocument(null, null, dt);

    // I have to find a workaround because this technique makes the html*/head/body tags to disappear.  
    var html = document.createElement('html');
    html.innerHTML = data.responseText;
    doc.appendChild(html);

    data.responseXML = doc;
}

source: http://userscripts.org/scripts/review/56489


Post a Comment for "Fetching Xml With GM_xmlhttpRequest"