Drafts Action Directory

stopwatch

Posted by dchar, Last updated: 2018-04-27 03:02:04 UTC

A simple stopwatch action.

Run the action to start and again at each lap.

Keep notes and comments in between laps.

13:01:02 >> Start

13:10:07 >> Lap 1  09:05 / 09:05

13:19:40 >> Lap 2  09:33 / 18:38

13:29:10 >> Lap 3  09:30 / 28:08

13:30:55 >> Lap 4  01:45 / 29:53
Install

Steps

  • script

    // simple drafts stopwatch
    
    function hmsToSec(s) {
        var timeCaptureRegex = /^\s*(\d{2}:\d{2}:\d{2})/;
        var m = timeCaptureRegex.exec(s);
        if (m == null) return 0;
    
        var a = m[1].split(':');
        return (+a[0])*60*60 + (+a[1])*60 + (+a[2]); 
    }
    
    function pad(s) {
        return ('0' + s).substr(-2);
    }
     
    function secToHMS(t) {
        var h = Math.floor(t/3600);
        t -= h*3600;
    
        var m = Math.floor(t/60);
        t -= m*60;
    
        var s = '';
        if (h)
            s+= pad(h) + ':';
    
        s += pad(m) + ':' + pad(t);
        return s;
    }
    
    function timestamp() {
        const now = new Date();
        return now.toString("HH:mm:ss");
    }
    
    var lap = 0;
    var firstTime = 0;
    var lastTime = 0;
    
    var lines = draft.content.split('\n');
    
    if ((lines.length == 1) && lines[0].length == 0)
        lines.pop();
    
    lines.push(timestamp());
    
    for (var i = 0; i < lines.length; i++) {
        var t = hmsToSec(lines[i]);
        if (t) {
            var l = lines[i].split(' >>')[0];
    
            if (lap) {
                l += " >> Lap " + lap;
                l += (lap < 10) ? '  ' : ' ';
                l += secToHMS(t-lastTime) + " / ";
                l += secToHMS(t-firstTime);
            } else {
                l += " >> Start";
                firstTime = t;
            }
    
            lap++;
            lastTime = t;
            lines[i] = l;
        }
    }
    
    draft.content = lines.join('\n');
    draft.update();
    
Actions available in the Action Directory are uploaded by community members. Use appropriate caution reviewing downloaded actions before use.