Auto.js 开发经验总结

Auto.js 开发经验总结

// 点击一个object对象
function clickObject(obj,xoffset,yoffset){ 
    var b = obj.bounds();
    var x = Number(xoffset) || 0;
    var y = Number(yoffset) || 0;
    return click(b.centerX() + x, b.centerY() + y); 
}

// 刷视频
function swipeVideo(y1, y2) {
    toast('滑动');
    var x = this.width/2;
    y1 = y1 || this.height * 5 / 6
    y2 = y2 || this.height / 6;
    func.swipe(x,random(y1,y1+50), x, random(y2,y2+50),500);
}

// 寻找一个在某obj对象同一行的对象,且符合selector的要求,与obj的垂直距离不超过diff
function findItemNextToObj(obj, selector, diff) {
    var candidates = selector.find();
    diff = diff || 30;
    var o;
    for (var i = 0; i < candidates.length; i++) {
        var candidate = candidates[i];
        if (Math.abs(candidate.bounds().centerY() - obj.bounds().centerY()) < diff) {
            o = candidate;
            break;
        }
    }
    return o;
}

// 一直退后直到满足selector要求, 最多退后5次
function backUntil(selector, sleep) {
    var ii = 5;
    while(ii-- > 0 && !selector.exists()){
        func.back();
        func.sleep(sleep || 2000);
    }
    return selector.exists();
}

// 上下滑动直到满足s要求,各至多滑动5次, 代码省去了 swipeUp 和 swipeDown 函数的实现
function swipeUpAndDownUntilExist(s) {
    var tryTimes = 5;
    while (tryTimes-- > 0) {
        if (s.exists()) {
            return s.findOnce();
        }
        swipeUp();
    }
    tryTimes = 5;
    while (tryTimes-- > 0) {
        if (s.exists()) {
            return s.findOnce();
        }
        swipeDown();
    }
}