@localnerve/get-attribute 1.0.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/LICENSE.md ADDED
@@ -0,0 +1,7 @@
1
+ Copyright 2024 Alex Grant, LocalNerve LLC, https://www.localnerve.com
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
4
+
5
+ The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
6
+
7
+ THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,20 @@
1
+ # get-attribute
2
+
3
+ > A command to get a single attribute or property from a webpage, echo to stdout
4
+
5
+ [![npm version](https://badge.fury.io/js/@localnerve%2Fget-attribute.svg)](https://badge.fury.io/js/@localnerve%2Fget-attribute)
6
+ ![Verify](https://github.com/localnerve/get-attribute/workflows/Verify/badge.svg)
7
+ [![Coverage Status](https://coveralls.io/repos/github/localnerve/get-attribute/badge.svg?branch=main)](https://coveralls.io/github/localnerve/get-attribute?branch=main)
8
+
9
+ ## Example
10
+
11
+ Grab the full url from a specific anchor tag of interest:
12
+
13
+ ```shell
14
+ get-attribute --url=https://host.com/path --selector='a[href^="/videos"]' --attribute=href --useprop=true --timeout=5000
15
+
16
+ # echoes the first matching href with full url from property: 'https://host.com/path/videos/123456789'
17
+ ```
18
+
19
+ ## License
20
+ [MIT](LICENSE.md)
@@ -0,0 +1,3 @@
1
+ #!/usr/bin/env node
2
+
3
+ require('../lib');
@@ -0,0 +1,39 @@
1
+ const js = require('@eslint/js');
2
+ const globals = require('globals');
3
+ const jest = require('eslint-plugin-jest');
4
+
5
+ const ignores = {
6
+ name: 'ignores',
7
+ ignores: [
8
+ 'bin/**',
9
+ 'node_modules/**',
10
+ 'tmp/**'
11
+ ]
12
+ };
13
+
14
+ const tests = {
15
+ name: 'tests',
16
+ files: ['__tests__/**'],
17
+ ...jest.configs['flat/recommended']
18
+ };
19
+
20
+ const lib = {
21
+ name: 'lib',
22
+ files: ['lib/**'],
23
+ languageOptions: {
24
+ globals: {
25
+ ...globals.node
26
+ }
27
+ },
28
+ rules: {
29
+ ...js.configs.recommended.rules,
30
+ indent: [2, 2, {
31
+ SwitchCase: 1,
32
+ MemberExpression: 1
33
+ }],
34
+ quotes: [2, 'single'],
35
+ 'dot-notation': [2, {allowKeywords: true}]
36
+ }
37
+ }
38
+
39
+ module.exports = [ignores, tests, lib];
package/jest.config.js ADDED
@@ -0,0 +1,17 @@
1
+ module.exports = {
2
+ collectCoverage: true,
3
+ coverageDirectory: 'coverage',
4
+ coveragePathIgnorePatterns: [
5
+ '/__tests__/globals/'
6
+ ],
7
+ globalSetup: './__tests__/globals/setup',
8
+ globalTeardown: './__tests__/globals/teardown',
9
+ verbose: true,
10
+ testEnvironment: 'node',
11
+ testPathIgnorePatterns: [
12
+ '/node_modules/',
13
+ '/tmp/',
14
+ '__tests__/fixtures',
15
+ '__tests__/globals'
16
+ ]
17
+ };
package/lib/cli.js ADDED
@@ -0,0 +1,18 @@
1
+ /**
2
+ * Get the command line args.
3
+ *
4
+ * Args are url, selector, attribute, and [useprop].
5
+ */
6
+ const yargs = require('yargs/yargs');
7
+ const { hideBin } = require('yargs/helpers');
8
+
9
+ function getCommandLineArgs (argv) {
10
+ const args = yargs(hideBin(argv)).argv;
11
+ const prerequisite = args.url && args.selector && args.attribute;
12
+ if (!prerequisite) {
13
+ return null;
14
+ }
15
+ return args;
16
+ }
17
+
18
+ module.exports = getCommandLineArgs;
@@ -0,0 +1,39 @@
1
+ /**
2
+ * Get an attribute from a web page given url, selector, and attributeName.
3
+ */
4
+ const {default: puppeteer, TimeoutError} = require('puppeteer');
5
+
6
+ async function getAttribute (url, selector, attribute, {
7
+ useProp = false,
8
+ timeout = 10000
9
+ } = {}) {
10
+ const browser = await puppeteer.launch();
11
+ let attributeValue = null;
12
+
13
+ try {
14
+ const page = await browser.newPage();
15
+ await page.goto(url, {
16
+ timeout
17
+ });
18
+ const sel = await page.waitForSelector(selector, {
19
+ timeout
20
+ });
21
+ /* istanbul ignore next */
22
+ attributeValue = await sel?.evaluate((el, attrName, useProp) => {
23
+ if (useProp) {
24
+ return el[attrName];
25
+ }
26
+ return el.getAttribute(attrName);
27
+ }, attribute, useProp);
28
+ } catch (e) {
29
+ if (!(e instanceof TimeoutError)) {
30
+ throw e;
31
+ }
32
+ } finally {
33
+ await browser.close();
34
+ }
35
+
36
+ return attributeValue;
37
+ }
38
+
39
+ module.exports = getAttribute;
package/lib/index.js ADDED
@@ -0,0 +1,29 @@
1
+ /**
2
+ * Scrape a webpage for an attribute, write to standard out.
3
+ *
4
+ * get-attribute --url=<url> --selector=<selector> --attribute=<attribute-name> [--useprop=false] [--timeout=10000]
5
+ */
6
+ const getArgs = require('./cli');
7
+ const getAttr = require('./get-attribute');
8
+
9
+ const syntax = 'get-attribute --url=https://host.dom --selector=a[href^="/videos"] --attribute=href [--useprop=false] [--timeout=10000]';
10
+ const args = getArgs(process.argv);
11
+
12
+ if (args) {
13
+ (async () => {
14
+ const attributeValue =
15
+ await getAttr(args.url, args.selector, args.attribute, {
16
+ useProp: args.useprop,
17
+ timeout: args.timeout
18
+ });
19
+ if (!attributeValue) {
20
+ console.error('Failed to retrieve attribute');
21
+ process.exit(2);
22
+ } else {
23
+ console.log(attributeValue);
24
+ }
25
+ })();
26
+ } else {
27
+ console.error(`Argument error:\n\t${syntax}`);
28
+ process.exit(1);
29
+ }
package/package.json ADDED
@@ -0,0 +1,45 @@
1
+ {
2
+ "name": "@localnerve/get-attribute",
3
+ "version": "1.0.0",
4
+ "description": "Get an attribute from a webpage, echo to stdout",
5
+ "main": "lib/index.js",
6
+ "scripts": {
7
+ "lint": "eslint .",
8
+ "test:debug": "node --inspect-brk node_modules/.bin/jest --runInBand --testTimeout=300000",
9
+ "test": "jest . --runInBand"
10
+ },
11
+ "bin": {
12
+ "get-attribute": "./bin/get-attribute"
13
+ },
14
+ "repository": {
15
+ "type": "git",
16
+ "url": "git+https://github.com/localnerve/get-attribute.git"
17
+ },
18
+ "keywords": [
19
+ "puppeteer",
20
+ "web",
21
+ "attribute"
22
+ ],
23
+ "author": "Alex Grant <alex@localnerve.com> (https://www.localnerve.com)",
24
+ "license": "MIT",
25
+ "bugs": {
26
+ "url": "https://github.com/localnerve/get-attribute/issues"
27
+ },
28
+ "homepage": "https://github.com/localnerve/get-attribute#readme",
29
+ "dependencies": {
30
+ "puppeteer": "^22.6.5",
31
+ "yargs": "^17.7.2"
32
+ },
33
+ "devDependencies": {
34
+ "eslint": "^9.1.0",
35
+ "@eslint/js": "^9.1.1",
36
+ "eslint-plugin-jest": "^28.2.0",
37
+ "express": "^4.19.2",
38
+ "globals": "^15.0.0",
39
+ "jest": "^29.7.0",
40
+ "server-destroy": "^1.0.1"
41
+ },
42
+ "engines": {
43
+ "node": ">=18"
44
+ }
45
+ }