@localnerve/get-attribute 1.0.0 → 1.2.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/eslint.config.js +2 -1
- package/lib/cli.js +9 -0
- package/lib/get-attribute.js +24 -0
- package/lib/index.js +3 -0
- package/package.json +5 -4
package/eslint.config.js
CHANGED
package/lib/cli.js
CHANGED
|
@@ -2,13 +2,22 @@
|
|
|
2
2
|
* Get the command line args.
|
|
3
3
|
*
|
|
4
4
|
* Args are url, selector, attribute, and [useprop].
|
|
5
|
+
*
|
|
6
|
+
* Copyright (c) 2024, Alex Grant <alex@localNerve.com> (https://www.localnerve.com)
|
|
7
|
+
* Licensed under the MIT license.
|
|
5
8
|
*/
|
|
6
9
|
const yargs = require('yargs/yargs');
|
|
7
10
|
const { hideBin } = require('yargs/helpers');
|
|
11
|
+
const debug = require('debug')('cli');
|
|
8
12
|
|
|
9
13
|
function getCommandLineArgs (argv) {
|
|
14
|
+
debug('process argv', argv);
|
|
15
|
+
|
|
10
16
|
const args = yargs(hideBin(argv)).argv;
|
|
11
17
|
const prerequisite = args.url && args.selector && args.attribute;
|
|
18
|
+
|
|
19
|
+
debug('parsed args', args);
|
|
20
|
+
|
|
12
21
|
if (!prerequisite) {
|
|
13
22
|
return null;
|
|
14
23
|
}
|
package/lib/get-attribute.js
CHANGED
|
@@ -1,23 +1,43 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* Get an attribute from a web page given url, selector, and attributeName.
|
|
3
|
+
*
|
|
4
|
+
* Copyright (c) 2024, Alex Grant <alex@localNerve.com> (https://www.localnerve.com)
|
|
5
|
+
* Licensed under the MIT license.
|
|
3
6
|
*/
|
|
4
7
|
const {default: puppeteer, TimeoutError} = require('puppeteer');
|
|
8
|
+
const debug = require('debug')('get-attribute');
|
|
5
9
|
|
|
6
10
|
async function getAttribute (url, selector, attribute, {
|
|
7
11
|
useProp = false,
|
|
8
12
|
timeout = 10000
|
|
9
13
|
} = {}) {
|
|
14
|
+
debug('args',
|
|
15
|
+
`url=${url}`,
|
|
16
|
+
`selector=${selector}`,
|
|
17
|
+
`attribute=${attribute}`,
|
|
18
|
+
`useProp=${useProp}`,
|
|
19
|
+
`timeout=${timeout}`
|
|
20
|
+
);
|
|
21
|
+
|
|
10
22
|
const browser = await puppeteer.launch();
|
|
11
23
|
let attributeValue = null;
|
|
12
24
|
|
|
25
|
+
debug('launched successfully');
|
|
26
|
+
|
|
13
27
|
try {
|
|
14
28
|
const page = await browser.newPage();
|
|
15
29
|
await page.goto(url, {
|
|
16
30
|
timeout
|
|
17
31
|
});
|
|
32
|
+
|
|
33
|
+
debug('navigated to url successfully');
|
|
34
|
+
|
|
18
35
|
const sel = await page.waitForSelector(selector, {
|
|
19
36
|
timeout
|
|
20
37
|
});
|
|
38
|
+
|
|
39
|
+
debug('waited for selector successfully');
|
|
40
|
+
|
|
21
41
|
/* istanbul ignore next */
|
|
22
42
|
attributeValue = await sel?.evaluate((el, attrName, useProp) => {
|
|
23
43
|
if (useProp) {
|
|
@@ -25,10 +45,14 @@ async function getAttribute (url, selector, attribute, {
|
|
|
25
45
|
}
|
|
26
46
|
return el.getAttribute(attrName);
|
|
27
47
|
}, attribute, useProp);
|
|
48
|
+
|
|
49
|
+
debug('got attribute value', attributeValue);
|
|
50
|
+
|
|
28
51
|
} catch (e) {
|
|
29
52
|
if (!(e instanceof TimeoutError)) {
|
|
30
53
|
throw e;
|
|
31
54
|
}
|
|
55
|
+
debug('received TimeoutError', e.message);
|
|
32
56
|
} finally {
|
|
33
57
|
await browser.close();
|
|
34
58
|
}
|
package/lib/index.js
CHANGED
|
@@ -2,6 +2,9 @@
|
|
|
2
2
|
* Scrape a webpage for an attribute, write to standard out.
|
|
3
3
|
*
|
|
4
4
|
* get-attribute --url=<url> --selector=<selector> --attribute=<attribute-name> [--useprop=false] [--timeout=10000]
|
|
5
|
+
*
|
|
6
|
+
* Copyright (c) 2024, Alex Grant <alex@localNerve.com> (https://www.localnerve.com)
|
|
7
|
+
* Licensed under the MIT license.
|
|
5
8
|
*/
|
|
6
9
|
const getArgs = require('./cli');
|
|
7
10
|
const getAttr = require('./get-attribute');
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@localnerve/get-attribute",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.2.0",
|
|
4
4
|
"description": "Get an attribute from a webpage, echo to stdout",
|
|
5
5
|
"main": "lib/index.js",
|
|
6
6
|
"scripts": {
|
|
@@ -27,11 +27,12 @@
|
|
|
27
27
|
},
|
|
28
28
|
"homepage": "https://github.com/localnerve/get-attribute#readme",
|
|
29
29
|
"dependencies": {
|
|
30
|
-
"puppeteer": "^22.
|
|
31
|
-
"yargs": "^17.7.2"
|
|
30
|
+
"puppeteer": "^22.7.0",
|
|
31
|
+
"yargs": "^17.7.2",
|
|
32
|
+
"debug": "^4.3.4"
|
|
32
33
|
},
|
|
33
34
|
"devDependencies": {
|
|
34
|
-
"eslint": "^9.1.
|
|
35
|
+
"eslint": "^9.1.1",
|
|
35
36
|
"@eslint/js": "^9.1.1",
|
|
36
37
|
"eslint-plugin-jest": "^28.2.0",
|
|
37
38
|
"express": "^4.19.2",
|