1.0.9 • Published 5 years ago
nvbtn v1.0.9
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)
//
> faillong 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)
//
>failcomplicate 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();
//
>> successmax 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
- https://github.com/navegador5/nv-angular-action-mat-button
- https://stackblitz.com/edit/action-mat-button?file=src%2Fapp%2Froutes%2Fmain%2Fmain.component.ts
LICENSE
- ISC