Extract xml using GM_xmlhttpRequest

I am trying to get a page with greasemonkey and then extract a link from it by pasting the link to the current page. I have problems with:

GM_xmlhttpRequest({
method: "GET",
url: "http://www.test.net/search.php?file=test",

onload: function(data) 
{
    if (!data.responseXML) 
    {
        data.responseXML = new DOMParser().parseFromString(data.responseText, "text/xml");
    }
    alert("!");
    var xmldata = data.response.xml;
    var tests = xmldata.getElementsByTagName('test');
    alert(tests[0].innerHTML);
}

});

The page is valid, and GM_xmlhttpRequest correctly returned it as a string when I tried to do this earlier, but I cannot figure out how to do this, so I can use node operations on it. Thanks in advance.

Change - the second related question

How can I link to the current page so that I can pass it to the functions, just as I would pass my loaded page? Ex

function FindTests(currentpage)
{
    currentpage.getElementById('blah');
}

where I transfer the document first, but later I use the selected page. Sorry if the wording is confusing.

+3
1

- xml, .
data.response.xml data.responseXML

, XMLDocument ( xml), .getElementById HTMLDocument.
, 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;
}

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

+2

Source: https://habr.com/ru/post/1758052/


All Articles