1

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

Dave
  • 433
  • 1
  • 5
  • 7

2 Answers2

5

Use 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 complete example to show how everything fits together:

// ==UserScript==
// @name           Parse HTML demo
// @namespace
// @include        *
// ==/UserScript==

GM_xmlhttpRequest({
    method: 'GET',
    url: 'http://www.google.com',
    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);
    }
});
Colt
  • 83
  • 2
  • 7
Matthew Flaschen
  • 278,309
  • 50
  • 514
  • 539
0

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.

w35l3y
  • 8,613
  • 3
  • 39
  • 51