admin管理员组文章数量:1430516
How can I write a Greasemonkey script that will go through a list of URLs (on the same domain) and enable an XPath query to be performed on the resulting DOM?
Thanks
How can I write a Greasemonkey script that will go through a list of URLs (on the same domain) and enable an XPath query to be performed on the resulting DOM?
Thanks
Share Improve this question asked Feb 6, 2010 at 0:31 DaveDave 4331 gold badge5 silver badges7 bronze badges2 Answers
Reset to default 5Use GM_xmlhttpRequest for the request, and createContextualFragment for HTML parsing. See Best Addons for Greasemonkey for an example using createContextualFragment. For parsing of valid XML you can just use DOMParser.parseFromString.
EDIT: Here's a very simple but plete example to show how everything fits together:
// ==UserScript==
// @name Parse HTML demo
// @namespace
// @include *
// ==/UserScript==
GM_xmlhttpRequest({
method: 'GET',
url: 'http://www.google.',
onload: function(resp){
var range = document.createRange();
range.setStartAfter(document.body);
var xhr_frag = range.createContextualFragment(resp.responseText);
var xhr_doc = document.implementation.createDocument(null, 'html', null);
xhr_doc.adoptNode(xhr_frag);
xhr_doc.documentElement.appendChild(xhr_frag);
var node = xhr_doc.evaluate("//span//b[@class='gb1']", xhr_doc, null, XPathResult.FIRST_ORDERED_NODE_TYPE, null).singleNodeValue;
GM_log("node.localName: " + node.localName);
GM_log("node.textContent: " + node.textContent);
}
});
If you are working with xml or well written xhtml, you could do as follows:
// XMLDocument
var doc = new DOMParser().parseFromString(xhr.responseText, "text/xml");
Otherwise:
// HTMLDocument
var doc = document.implementation.createHTMLDocument("");
doc.documentElement.innerHTML = xhr.responseText;
Once you have the document, you may use anything just like a normal document.
本文标签: javascriptLoad and parse remote url with greasemonkeyStack Overflow
版权声明:本文标题:javascript - Load and parse remote url with greasemonkey - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745505941a2661245.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论