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 に答える