views:

325

answers:

3

I would like to write a greasemonkey script that given an xpath returns all of the output of that xpath executed on the current page in a .txt file with one result per row.

How do I do this?

EDIT: Its ok if the output is not written to a file. I just want to have it displayed.

A: 

I don't think the greasemonkey script will be able to output a file. That would require write access to the machine, which is not allowed by the sandbox.

You may want to write it to a div (maybe hidden except when clicking a button or image) in the document?

John Weldon
Yes thats also fine.
A: 

Here is an example that appends a list of all href links to the body of the html. You can pretty it up with style, and make it hidden, floating, etc.

// ==UserScript==
// @name           test
// @namespace      johnweldon.com
// @description    test
// @include        *
// ==/UserScript==

(function() { 
    try {
     var xpath = "//a[@href]";                // get all links
     var res = document.evaluate(xpath, document, null, XPathResult.ORDERED_NODE_SNAPSHOT_TYPE,  null);
     var str = "<div><ul>";
     for ( var i = 0; i < res.snapshotLength; i++) {
      str = str + "\n<li>" + res.snapshotItem(i);
     }
     str += "</ul></div>";

     var ev = document.createElement("div");  // parent element for our display
     ev.innerHTML = str;                      //quick and dirty
     document.body.appendChild(ev);
    }
    catch (e) {
     alert(e.message);
    }
}())
John Weldon
A: 

may helps http://xpath.alephzarro.com/index

david