@localnerve/get-attribute 1.1.0 → 1.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -8,10 +8,10 @@
8
8
 
9
9
  ## Example
10
10
 
11
- Grab the full url from a specific anchor tag of interest:
11
+ Grab the full url from a specific anchor tag of interest (all options shown):
12
12
 
13
13
  ```shell
14
- get-attribute --url=https://host.com/path --selector='a[href^="/videos"]' --attribute=href --useprop=true --timeout=5000
14
+ get-attribute --url=https://host.com/path --selector='a[href^="/videos"]' --attribute=href --useprop=true --timeout=5000 --launchargs='{"headless":true}'
15
15
 
16
16
  # echoes the first matching href with full url from property: 'https://host.com/path/videos/123456789'
17
17
  ```
package/eslint.config.js CHANGED
@@ -7,7 +7,8 @@ const ignores = {
7
7
  ignores: [
8
8
  'bin/**',
9
9
  'node_modules/**',
10
- 'tmp/**'
10
+ 'tmp/**',
11
+ 'coverage/**'
11
12
  ]
12
13
  };
13
14
 
package/lib/cli.js CHANGED
@@ -21,6 +21,36 @@ function getCommandLineArgs (argv) {
21
21
  if (!prerequisite) {
22
22
  return null;
23
23
  }
24
+
25
+ if (args.useprop) {
26
+ const useprop = args.useprop.trim().toLowerCase();
27
+ if (useprop !== 'true' && useprop !== 'false') {
28
+ debug('useprop was not "true" or "false"', useprop);
29
+ return null;
30
+ }
31
+ args.useprop = useprop === 'true';
32
+ }
33
+
34
+ if (args.timeout) {
35
+ const timeout = parseInt(args.timeout, 10);
36
+ if (!timeout) {
37
+ debug('could not parse timeout to decimal integer', args.timeout);
38
+ return null;
39
+ }
40
+ args.timeout = timeout;
41
+ }
42
+
43
+ if (args.launchargs) {
44
+ let launchargs;
45
+ try {
46
+ launchargs = JSON.parse(args.launchargs);
47
+ } catch (e) {
48
+ debug('launchargs was not valid json', args.launchargs, e);
49
+ return null;
50
+ }
51
+ args.launchargs = launchargs;
52
+ }
53
+
24
54
  return args;
25
55
  }
26
56
 
@@ -9,17 +9,19 @@ const debug = require('debug')('get-attribute');
9
9
 
10
10
  async function getAttribute (url, selector, attribute, {
11
11
  useProp = false,
12
- timeout = 10000
12
+ timeout = 10000,
13
+ launchArgs = {}
13
14
  } = {}) {
14
15
  debug('args',
15
16
  `url=${url}`,
16
17
  `selector=${selector}`,
17
18
  `attribute=${attribute}`,
18
19
  `useProp=${useProp}`,
19
- `timeout=${timeout}`
20
+ `timeout=${timeout}`,
21
+ `launchargs=${JSON.stringify(launchArgs)}`
20
22
  );
21
23
 
22
- const browser = await puppeteer.launch();
24
+ const browser = await puppeteer.launch(launchArgs);
23
25
  let attributeValue = null;
24
26
 
25
27
  debug('launched successfully');
@@ -52,6 +54,7 @@ async function getAttribute (url, selector, attribute, {
52
54
  if (!(e instanceof TimeoutError)) {
53
55
  throw e;
54
56
  }
57
+ debug('received TimeoutError', e.message);
55
58
  } finally {
56
59
  await browser.close();
57
60
  }
package/lib/index.js CHANGED
@@ -9,7 +9,7 @@
9
9
  const getArgs = require('./cli');
10
10
  const getAttr = require('./get-attribute');
11
11
 
12
- const syntax = 'get-attribute --url=https://host.dom --selector=a[href^="/videos"] --attribute=href [--useprop=false] [--timeout=10000]';
12
+ const syntax = 'get-attribute --url=https://host.dom --selector=a[href^="/videos"] --attribute=href [--useprop=false] [--timeout=10000] [--launchargs=\'{"json":true}\']';
13
13
  const args = getArgs(process.argv);
14
14
 
15
15
  if (args) {
@@ -17,7 +17,8 @@ if (args) {
17
17
  const attributeValue =
18
18
  await getAttr(args.url, args.selector, args.attribute, {
19
19
  useProp: args.useprop,
20
- timeout: args.timeout
20
+ timeout: args.timeout,
21
+ launchArgs: args.launchargs
21
22
  });
22
23
  if (!attributeValue) {
23
24
  console.error('Failed to retrieve attribute');
@@ -28,5 +29,6 @@ if (args) {
28
29
  })();
29
30
  } else {
30
31
  console.error(`Argument error:\n\t${syntax}`);
32
+ console.error('Use DEBUG=cli for more info');
31
33
  process.exit(1);
32
34
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@localnerve/get-attribute",
3
- "version": "1.1.0",
3
+ "version": "1.3.0",
4
4
  "description": "Get an attribute from a webpage, echo to stdout",
5
5
  "main": "lib/index.js",
6
6
  "scripts": {
@@ -27,14 +27,14 @@
27
27
  },
28
28
  "homepage": "https://github.com/localnerve/get-attribute#readme",
29
29
  "dependencies": {
30
- "puppeteer": "^22.6.5",
30
+ "puppeteer": "^22.7.1",
31
31
  "yargs": "^17.7.2",
32
32
  "debug": "^4.3.4"
33
33
  },
34
34
  "devDependencies": {
35
- "eslint": "^9.1.0",
35
+ "eslint": "^9.1.1",
36
36
  "@eslint/js": "^9.1.1",
37
- "eslint-plugin-jest": "^28.2.0",
37
+ "eslint-plugin-jest": "^28.3.0",
38
38
  "express": "^4.19.2",
39
39
  "globals": "^15.0.0",
40
40
  "jest": "^29.7.0",