使用 GM_xmlhttpRequest 获取 xml

发布于 2024-09-12 16:56:38 字数 790 浏览 4 评论 0原文

我正在尝试使用greasemonkey 检索页面,然后从中提取链接,将链接插入当前页面。我遇到了一些麻烦:

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);
}

});

该页面是有效的,当我之前尝试时,GM_xmlhttpRequest 将其作为字符串正确返回,但我似乎不知道如何制作它,以便我可以对其使用节点操作。谢谢进步。

编辑 - 第二个相关问题

我应该如何引用当前页面,以便可以将其传递给函数,就像传递获取的页面一样?例如

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

,最初我传递文档,但后来我使用获取的页面。抱歉,如果措辞令人困惑。

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: "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 returned it correctly as a string when I tried previously, but I can't seem to figure out how to make it so I can use node operations on it.Thanks in advance.

Edit - a second, related question

How should I refer to the current page so that I could pass it a to function, just as I would pass my fetched page? Ex

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

where initially I pass it document, but later I use the fetched page. Sorry if the wording is confusing.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

蹲墙角沉默 2024-09-19 16:56:39

如果请求的页面是格式良好的 xml,那么您的方法是正确的。
但您应该将 data.response.xml 更改为 data.responseXML

并且我认为您无法使用 XMLDocument 执行此操作( xml 解析器),因为 .getElementByIdHTMLDocument 中工作。
但是,您可以执行以下操作以获得有效的 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 /评论/56489

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文