npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

guardian4_api

v1.0.1

Published

Easy API module for Guardian4

Downloads

4

Readme

guardian4_api

An easy-to-use API which calls Guardian4 and returns the results.

Installation

The API requires G4 v1.11.2 or above and all prerequisites to be installed.

Install in a Node.js project using:

npm install guardian4_api

Basic usage

To process a single URL using the default parameters and async/await:

(async () => {

  const g4api = require('guardian4_api');

  try {
    let g4 = await g4api.run('siteurl.com');
    console.log(g4.result);
  }
  catch (e) {
    console.log(e);
  }

})();

Options

The following options can be set:

|option|description| |-:|-| |command|Guardian4 shell command (g4)| |timeout|maximum execution time in ms (0)| |opt|Guardian4 parameters (long names only - see G4 documentation)|

Example:

const g4api = require('guardian4_api');

g4api.timeout = 60000;                  // timeout at 60 seconds
g4api.opt.proxy = '1.2.3.4:5555';       // set proxy
g4api.opt.device = 'ip7';               // set device to iPhone 7
g4api.opt.individual = './profile1/';   // use a specific profile folder
g4api.opt.url = 'site1.com,site2.com';  // set URLs

Once any option is set, it remains set regardless of how many runs are initiated. It is possible to change options while a .run() is progressing, but it will have no effect on the current run.

By default, G4 has the following .opt parameters set:

  • .verbose = 0 - no output
  • .proxy = 'local' - local connection, no proxy
  • .device = 'wdc' - Windows desktop Chrome browser
  • .result = true - output result JSON

The .command need only be changed if G4 is not installed globally according to instructions with npm link and chmod, e.g.

g4api.command = 'node ~/guardian4/guardian4.js';

Run options

The .run() method executes G4. It only permits one operation at a time and can be passed two optional parameters:

  • a set of URLs in a single string (not necessary if g4api.opt.url or g4api.opt.series has been set)
  • a callback function

If set, the callback function is executed when processing completes. It is passed an error and a G4 result object, e.g.

g4api.run(null, (err, g4) => {
  if (!err) console.log( g4.result );
});

If no callback function is set, .run() returns a Promise which either triggers an error or resolves with a G4 result object. This can either be used in an async/await, e.g.

try {
  g4 = await g4api.run();
  console.log(g4.result);
}
catch (e) {}

or a Promise .then chain, e.g.

g4api.run()
  .then(g4 => {
    console.log(g4.result);
  })
  .catch(err => {
    console.log(err);
  })

G4 result object

The .run() method returns an object with two properties:

|property|description| |-:|-| |.result|G4 result JSON parsed into an object (see G4 documentation)| |.stdout|the G4 run output|

Example:

(async () => {

  const g4api = require('guardian4_api');

  try {
    const { result } = await g4api.run('siteurl.com');

    console.log('device    ', result.devicename);
    console.log('proxy     ', result.proxy);
    console.log('log folder', result.log);

    // loop processed URLs
    result.run.forEach((run, rIdx) => {

      console.log('URL     ', rIdx + 1, run.url);
      console.log('complete', run.complete);

      // loop journeys
      run.ad.forEach((ad, aIdx) => {

        console.log('ad           ', aIdx + 1);
        console.log('ad image file', result.log + ad.scrAd);
        console.log('ad log file  ', result.log + ad.log);

        // loop redirects
        ad.journey.forEach((redir, jIdx) => {

          console.log('redirect URL        ', jIdx + 1, redir.url);
          console.log('screenshots captured', redir.src.length);

        });

      });


    })

  }
  catch (e) {
    console.log(e);
  }

})();