src/hdalab/scripts/capture-puppeteer.js
changeset 704 b5835dca2624
equal deleted inserted replaced
703:a988e44c92d5 704:b5835dca2624
       
     1 //from https://github.com/makinacorpus/django-screamshot
       
     2 
       
     3 const puppeteer = require('puppeteer');
       
     4 
       
     5 function delay(time) {
       
     6     return new Promise(function(resolve) { 
       
     7         setTimeout(resolve, time)
       
     8     });
       
     9  }
       
    10 
       
    11 /** 
       
    12  * arguments:
       
    13  * [1] => URL
       
    14  * [2] => output. Use /dev/stdout if you want to capture.
       
    15  * [3] => size
       
    16  */
       
    17 
       
    18 var address = process.argv[2],
       
    19     output  = process.argv[3];
       
    20 
       
    21 config = {}
       
    22 process.argv.forEach(function(arg, i) {
       
    23     if (i > 3) {
       
    24         namev = arg.split('=');
       
    25         config[namev[0].replace('--', '')] = namev[1];
       
    26     }
       
    27 });
       
    28 
       
    29 const width = parseInt(config.width || 1920),
       
    30       height = parseInt(config.height || 1080),
       
    31       wait = parseInt(config.wait || 1080);
       
    32 
       
    33 (async () => {
       
    34   const browser = await puppeteer.launch({
       
    35       defaultViewport: {width: width, height: height},
       
    36       ignoreHTTPSErrors: true,
       
    37   });
       
    38   const page = await browser.newPage();
       
    39   await page.goto(address, {waitUntil: 'domcontentloaded'});
       
    40   // Wait until page has loaded completely
       
    41   await delay(wait);
       
    42   // Make a screenshot
       
    43   await page.screenshot({path: output});
       
    44 
       
    45   await browser.close();
       
    46 })();