njsparser 0.1.0 → 0.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/README.md +390 -40
- package/api.js +76 -50
- package/bun.lock +2 -48
- package/mod.js +148 -0
- package/package.json +11 -16
- package/parser/flight_data.js +189 -306
- package/parser/manifests.js +37 -37
- package/parser/next_data.js +29 -26
- package/parser/types.js +408 -296
- package/parser/urls.js +86 -56
- package/tests/api.test.js +96 -0
- package/tests/integration.test.js +68 -0
- package/tests/parser/flight_data.test.js +105 -0
- package/tests/parser/manifests.test.js +50 -0
- package/tests/parser/next_data.test.js +53 -0
- package/tests/parser/types.test.js +243 -0
- package/tests/parser/urls.test.js +84 -0
- package/tests/property.test.js +299 -0
- package/tests/setup.js +21 -0
- package/tests/utils.test.js +32 -0
- package/tools.js +263 -185
- package/utils.js +29 -24
- package/_.js +0 -10
- package/_.json +0 -12837
- package/api.test.js +0 -41
- package/index.js +0 -8
- package/package-lock.json +0 -291
- package/parser/flight_data.test.js +0 -59
- package/parser/manifests.test.js +0 -36
- package/parser/next_data.test.js +0 -15
- package/parser/types.test.js +0 -261
- package/parser/urls.test.js +0 -26
- package/test/src/index.js +0 -16
- package/tools.test.js +0 -153
- package/utils.test.js +0 -38
package/parser/next_data.js
CHANGED
|
@@ -1,33 +1,36 @@
|
|
|
1
|
-
|
|
1
|
+
/**
|
|
2
|
+
* Next data extraction from __NEXT_DATA__ script
|
|
3
|
+
*/
|
|
4
|
+
|
|
5
|
+
import { makeTree } from '../utils.js';
|
|
2
6
|
|
|
3
7
|
/**
|
|
4
|
-
*
|
|
5
|
-
* @param {
|
|
6
|
-
* @
|
|
8
|
+
* Extract and parse __NEXT_DATA__ script content
|
|
9
|
+
* @param {string} html - HTML string
|
|
10
|
+
* @param {DOMParser} DOMParser - DOMParser instance
|
|
11
|
+
* @returns {Object|null} Parsed JSON data or null
|
|
7
12
|
*/
|
|
8
|
-
export
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
console.error("Failed to parse __NEXT_DATA__ json", e);
|
|
20
|
-
return null;
|
|
21
|
-
}
|
|
22
|
-
}
|
|
13
|
+
export function getNextData(html, DOMParser) {
|
|
14
|
+
const doc = makeTree(html, DOMParser);
|
|
15
|
+
const script = doc.querySelector('script#__NEXT_DATA__');
|
|
16
|
+
|
|
17
|
+
if (!script || !script.textContent) {
|
|
18
|
+
return null;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
try {
|
|
22
|
+
return JSON.parse(script.textContent.trim());
|
|
23
|
+
} catch (e) {
|
|
23
24
|
return null;
|
|
24
|
-
}
|
|
25
|
+
}
|
|
26
|
+
}
|
|
25
27
|
|
|
26
28
|
/**
|
|
27
|
-
*
|
|
28
|
-
* @param {
|
|
29
|
-
* @
|
|
29
|
+
* Check if HTML contains __NEXT_DATA__ script
|
|
30
|
+
* @param {string} html - HTML string
|
|
31
|
+
* @param {DOMParser} DOMParser - DOMParser instance
|
|
32
|
+
* @returns {boolean} True if __NEXT_DATA__ exists
|
|
30
33
|
*/
|
|
31
|
-
export
|
|
32
|
-
|
|
33
|
-
}
|
|
34
|
+
export function hasNextData(html, DOMParser) {
|
|
35
|
+
return getNextData(html, DOMParser) !== null;
|
|
36
|
+
}
|