使用 GM_xmlhttpRequest 获取 xml
我正在尝试使用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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果请求的页面是格式良好的 xml,那么您的方法是正确的。
但您应该将
data.response.xml
更改为data.responseXML
并且我认为您无法使用
XMLDocument
执行此操作( xml 解析器),因为.getElementById
在HTMLDocument
中工作。但是,您可以执行以下操作以获得有效的 HTMLDocument:
来源: 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
todata.responseXML
and i THINK you can't do this with a
XMLDocument
(result of the xml parser) because.getElementById
works inHTMLDocument
.however, you could do the following to have a valid HTMLDocument:
source: http://userscripts.org/scripts/review/56489