从另一个XPath结果获取XPath结果

时间:2019-02-25 08:23:29

标签: javascript xpath greasemonkey

我似乎根本无法使它正常工作。我正在尝试使用xPath结果作为我的润滑脂脚本脚本页面的基本查询。

我正在使用的代码是:

var select=$tag('select');
this.basePath=xpath.single("/html/body/div/div/div/div",document);
// - /html/body/div/div/div/div/div[2]/div/div[2]/div[2]/table/tbody/tr/th[2]/a
if(select.length>0){
    this.location=xpath.single("/div[2]/div/div[2]/div[2]/table/tbody/tr/th[2]/a",this.basePath);
    console.log('location: '+this.location.singleNodeValue.textContent);
}

xPath函数是:

var xpath=function(){
    return {
    single:function easyXPath(path,baseNode){
        if(baseNode===undefined||!isHTML(baseNode)){
                baseNode=document;
        }
        return document.evaluate(path,baseNode,null,XPathResult.FIRST_ORDERED_NODE_TYPE,null);
    },
    count:function easyXPathCount(path,baseNode){
        if(baseNode===undefined||!isHTML(baseNode)){
            baseNode=document;
        }
        return document.evaluate("count("+path+")",baseNode,null,XPathResult.NUMBER_TYPE,null).numberValue;
    },
    number:function easyXPathNumber(path,baseNode){
        if(baseNode===undefined||!isHTML(baseNode)){
            baseNode=document;
        }
        return document.evaluate(path,baseNode,null,XPathResult.NUMBER_TYPE,null).numberValue;
    },
    multi:function easyXPathMultiNode(path,baseNode){
        if(baseNode==undefined||!isHTML(baseNode)){
            baseNode=document;
        }
        return document.evaluate(path,baseNode,null,XPathResult.ORDERED_NODE_SNAPSHOT_TYPE,null);
    }
};
}();
function isHTML(node){
    return node instanceof HTMLElement || node instanceof Element;
}

我已经为要尝试到达的元素留出了完整的路径,最后我将基本路径放在以下路径的前面

1 个答案:

答案 0 :(得分:0)

找到了一种方法。可能不是最有效的,但它可以工作。

void LateUpdate()
{
    if (Input.GetMouseButtonDown(0)) {
        firstTouchPos = movementCam.ScreenPointToRay(Input.mousePosition);
        playerPos = transform.position;
    }

    if (Input.GetMouseButton(0)) {
        // targetPosition will follow finger movements
        Ray currentTouchPos = movementCam.ScreenPointToRay(Input.mousePosition);
        Vector2 direction = currentTouchPos.origin - firstTouchPos.origin;
        targetPosition = playerPos + direction * touchSensitivity;
    }

    transform.position = Vector3.Lerp(transform.position, targetPosition, Time.deltaTime * followSpeed);
    transform.position = Vector3.ClampMagnitude(transform.position, radius);
}

xPath似乎需要一个文档库才能工作,并且不能在尚未属于文档类型或文档片段的任何文档上运行。

希望这对其他人也有帮助。