greasemonkey脚本,输出与xpath对应的所有数据

时间:2022-04-03 06:13:01

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.

我想写一个greasemonkey脚本,给出一个xpath返回在.txt文件中当前页面上执行的xpath的所有输出,每行一个结果。

How do I do this?

我该怎么做呢?

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

编辑:如果输出没有写入文件,则确定。我只是希望它显示出来。

1 个解决方案

#1


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.

这是一个将所有href链接的列表附加到html主体的示例。你可以用风格漂亮,并使它隐藏,浮动等。

// ==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);
    }
}())

#1


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.

这是一个将所有href链接的列表附加到html主体的示例。你可以用风格漂亮,并使它隐藏,浮动等。

// ==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);
    }
}())