3

我正在使用 GreaseMonkey,所以使用 JavaScript。

字符串存在吗?

此 HTML 页面加载时,我想在特定位置查找字符串。

具体地方:XPath:/HTML[1]/BODY[1]/TABLE[1]/TBODY[1]/TR[3]/TD[1]/TABLE[1]/TBODY[1]/TR[1]/TD[1]/P[1]/B[1]

(包含在这个地方<b>Newsletter Issue Date: June 20th, 2013</b>:)

行动

如果字符串June 20在那里,则什么也不做。(无需任何操作)如果字符串 'June 20'存在,则重新加载页面。

4

2 回答 2

2

如果你想要更通用的东西,那么你可以使用这样的东西。

HTML

<table>
    <tbody>
        <tr>
            <td><p><b>Newsletter Issue Date: June 10th, 2013</b></p></td>
            <td><p><b>Newsletter Issue Date: June 11th, 2013</b></p></td>
            <td><p><b>Newsletter Issue Date: June 12th, 2013</b></p></td>
        </tr>
        <tr>
            <td><p><b>Newsletter Issue Date: June 13th, 2013</b></p></td>
            <td><p><b>Newsletter Issue Date: June 24th, 2013</b></p></td>
            <td><p><b>Newsletter Issue Date: June 25th, 2013</b></p></td>
        </tr>
        <tr>
            <td><p><b>Newsletter Issue Date: June 20th, 2013</b></p></td>
            <td><p><b>Newsletter Issue Date: June 21st, 2013</b></p></td>
            <td><p><b>Newsletter Issue Date: June 22nd, 2013</b></p></td>
        </tr>
    </tbody>
</table>

Javascript

/*jslint maxerr: 50, indent: 4, browser: true */

(function () {
    "use strict";

    function walkTheDOM(node, func) {
        if (node && node.nodeType) {
            if (typeof func === "function") {
                func(node);
            }

            node = node.firstChild;
            while (node) {
                walkTheDOM(node, func);
                node = node.nextSibling;
            }
        }
    }

    function escapeRegex(string) {
        return string.replace(/[\[\](){}?*+\^$\\.|]/g, "\\$&");
    }

    function filterElementsByContains(elements, string) {
        var toStringFN = {}.toString,
            text = toStringFN.call(elements),
            result,
            length,
            i,
            element;

        if (text !== "[object NodeList]" && text !== "[object Array]" && !($() instanceof jQuery)) {
            return result;
        }

        result = [];
        if (typeof string === "string") {
            string = new RegExp("^" + escapeRegex(string) + "$");
        } else if (toStringFN.call(string) !== "[object RegExp]") {
            return result;
        }

        function getText(node) {
            if (node.nodeType === 3) {
                text += node.nodeValue;
            }
        }

        length = elements.length;
        i = 0;
        while (i < length) {
            text = "";
            element = elements[i];
            walkTheDOM(element, getText);
            if (string.test(text)) {
                result.push(element);
            }

            i += 1;
        }

        return result;
    }

    if (filterElementsByContains([document.getElementsByTagName("table")[0]], /June 20/).length) {
        alert("exists");
    }
}());

jsfiddle 上

您可以在此答案中看到其使用的其他一些示例

如果您必须将 XPATH 转换为 CSS 选择器,请查看此GIST

于 2013-06-19T19:50:50.307 回答
1

您可以将选择器翻转为 css 选择器,然后使用document.querySelector(selector);

var xpath = '/HTML[1]/BODY[1]/TABLE[1]/TBODY[1]/TR[3]/TD[1]/TABLE[1]/TBODY[1]/TR[1]/TD[1]/P[1]/B[1]',
    selector = xpath.replace(/\/([a-z]+)\[([0-9]+)\]/gi, function(_, tag, num) {
        num--;
        return tag + ':nth-child(' + num + ')>';
    }).slice(0, -1);

var element = document.querySelector(selector);

获得元素后,您可以使用它.innerHTML来获取指定节点的 html。

var mySearch = 'June 20';
if ( element.innerHTML.indexOf(mySearch) == -1 ) {
    // the string isn't present
};
于 2013-06-19T19:33:18.807 回答