$x() function is not defined inside a Chrome extension, content script

前端 未结 2 2159
暗喜
暗喜 2021-02-15 03:44
$x(\"//a[contains(@href,\'.jpg\')]\");

works as expected from the developer tools command prompt. But, when in an extension\'s content-script I get a \

2条回答
  •  孤街浪徒
    2021-02-15 04:41

    I suggest adding this function to your code:

    var xpath = function (xpathToExecute) {
        var result = [];
        var nodesSnapshot = document.evaluate(xpathToExecute, document, null, XPathResult.ORDERED_NODE_SNAPSHOT_TYPE, null);
        for (var i = 0; i < nodesSnapshot.snapshotLength; i++) {
            result.push(nodesSnapshot.snapshotItem(i));
        }
        return result;
    }

    and Just calling it in place of $x

提交回复
热议问题