njsparser 0.1.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 ADDED
@@ -0,0 +1,75 @@
1
+ # njsparser (JS)
2
+
3
+ A powerful parser and explorer for any website built with [NextJS](https://nextjs.org).
4
+ Ported from the Python library `njsparser`.
5
+
6
+ - Parses flight data (from the `self.__next_f.push` scripts).
7
+ - Parses next data from `__NEXT_DATA__` script.
8
+ - Parses build manifests.
9
+ - Searches for build id.
10
+
11
+ ## Installation
12
+
13
+ ```bash
14
+ bun install njsparser
15
+ # or
16
+ npm install njsparser
17
+ ```
18
+
19
+ ## Usage
20
+
21
+ ### Parsing Flight Data
22
+
23
+ ```javascript
24
+ import { BeautifulFD, get_flight_data } from 'njsparser';
25
+
26
+ const html = `...`; // Fetch your HTML
27
+
28
+ // Using BeautifulFD for easy searching
29
+ const fd = new BeautifulFD(html);
30
+
31
+ // Find specific data types
32
+ import { Data, RSCPayload } from 'njsparser';
33
+
34
+ const rsc = fd.find({ class_filters: [RSCPayload] });
35
+ console.log(rsc.build_id);
36
+
37
+ const data = fd.find_all({ class_filters: [Data] });
38
+ data.forEach(item => {
39
+ console.log(item.content);
40
+ });
41
+ ```
42
+
43
+ ### Parsing `__NEXT_DATA__`
44
+
45
+ ```javascript
46
+ import { get_next_data } from 'njsparser';
47
+
48
+ const data = get_next_data(html);
49
+ console.log(data.props.pageProps);
50
+ ```
51
+
52
+ ### Tools
53
+
54
+ ```javascript
55
+ import { has_nextjs, find_build_id } from 'njsparser';
56
+
57
+ if (has_nextjs(html)) {
58
+ console.log("NextJS detected!");
59
+ console.log("Build ID:", find_build_id(html));
60
+ }
61
+ ```
62
+
63
+ ## API Reference
64
+
65
+ Most functions from the Python library are available in JS with similar signatures.
66
+
67
+ - `BeautifulFD(html)`
68
+ - `get_flight_data(html)`
69
+ - `get_next_data(html)`
70
+ - `find_build_id(html)`
71
+ - `has_nextjs(html)`
72
+ - `get_next_static_urls(html)`
73
+ - `get_base_path(html)`
74
+
75
+ See source code for more details.
package/_.js ADDED
@@ -0,0 +1,10 @@
1
+ import {
2
+ BeautifulFD,
3
+ get_flight_data
4
+ } from './index.js';
5
+
6
+ const html = await Bun.file('/home/kaki/.config/JetBrains/IntelliJIdea2025.3/scratches/scratch_2.html').text();
7
+ const fd = new BeautifulFD(html);
8
+ console.log(fd.find_all());
9
+ console.log(await Bun.write('./_.json', JSON.stringify(fd.find_all(), null, 4)));
10
+ // console.log(await Bun.write('./_.json', JSON.stringify(get_flight_data(html), null, 4)));