1.0.9 • Published 3 years ago

nvbtn v1.0.9

Weekly downloads
1
License
ISC
Repository
-
Last release
3 years ago

nvbtn

  • nvbtn is very simple util to simulate button actions
  • define your actions,such as double-click triple-click,press-down, in nodejs

install

  • npm install nvbtn

usage

  • const Btn = require('nvbtn').Btn
  • var btn = new Btn(policy,callback)

policy param format

action :
     D                  mouse-down
     U                  mouse-up
    (start,end)         duration  start default 0, end default Infinity
     click              D-()-U

condition:
     throttle(min,max)  round-throttle  min default 0, max default Infinity
    
examples:

    1s-2s-press                 D-(1000,2000)-U
    double-click-between-1s     click-(,1000)-click && throttle(,1100)
                                //&& throttle(,1100) is necessary
                                //because in browser click-time is not predictable 
    triple-click                click-()-click-()-click 
    triple-click-in-2s          click-(0,1500)-click-(0,1500)-click && throttle(,2000)

examples

double-click

const Btn = require('nvbtn').Btn
// define a double click in 1s


var btn = new Btn(
    "click-(0,1000)-click",
    function(r){
        if(r.error === null) {
            console.log("success")
        } else {
            console.log("fail")
        }
    }
)


btn.click()
setTimeout(function(){btn.click()},800)
//

> success

btn.click()
setTimeout(function(){btn.click()},2000)
//

> fail

long press

const Btn = require('nvbtn').Btn

// define a press   the press-down-duration is between 1s - 8s

var btn = new Btn(
    "D-(1000,8000)-U",
    function(r){
        if(r.error === null) {
            console.log("success")
        } else {
            console.log("fail")
        }
    }
)

btn.down()
setTimeout(function(){btn.up()},900)
//
> fail

btn.down()
setTimeout(function(){btn.up()},2000)
//
> success

btn.down()
setTimeout(function(){btn.up()},9000)
//
>fail

complicate action

const Btn = require('nvbtn').Btn
//define a action
//first-press-for-4-6s, then click

var btn = new Btn(
    "D-(4000,6000)-U-()-click",
    function(r){
        if(r.error === null) {
            console.log("success")
        } else {
            console.log("fail")
        }
    }
)

function combo() {
    btn.down();
    setTimeout(function(){btn.up();btn.click();},5000);

}

combo();
//
>> success

max throttle

//with throttle
var Btn = require("./btn").Btn
var err = undefined
var btn = new Btn(
    "click-(0,1500)-click-(0,1500)-click && throttle(,2000)",
    function(r){
        if(r.error === null) {
            console.log("success")
        } else {
            err = r
            console.log("fail")
        }
    }
)

var {Task} = require('pausable-setinterval')
var tsk = new Task(
    undefined,
    [1200,1200],
    (r)=>{if(err === undefined){btn.click()}},
    3,0
)
tsk.$exec()

> err.error
{
  action: {
    act: 'max_throttle_tmout',
    tmout_at: 1605466254553,
    prev_up_duration: 798,
    error: {
      name: 'Error',
      message: 'total_time_gt_max_throttle',
      total_time: 2002
    }
  },
  throttle: [ 0, 2000 ]
}
>

//

min throttle

var Btn = require("./btn").Btn
var err = undefined
var btn = new Btn(
    "click-()-click && throttle(2000,3000)",
    function(r){
        if(r.error === null) {
            console.log("success")
        } else {
            err = r
            console.log("fail")
        }
    }
)

btn.click()
btn.click()

> err.error
{
  action: {
    act: 'up',
    prev_down_duration: 2,
    up_at: 1605471942122,
    error: {
      name: 'Error',
      message: 'total_time_lt_min_throttle',
      total_time: 465
    }
  },
  throttle: [ 2000, 3000 ]
}

EXAMPLES

LICENSE

  • ISC
1.0.9

3 years ago

1.0.7

3 years ago

1.0.6

3 years ago

1.0.5

3 years ago

1.0.3

3 years ago

1.0.2

3 years ago

1.0.1

3 years ago

1.0.0

3 years ago