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 +75 -0
- package/_.js +10 -0
- package/_.json +12837 -0
- package/api.js +57 -0
- package/api.test.js +41 -0
- package/bun.lock +68 -0
- package/index.js +8 -0
- package/package-lock.json +291 -0
- package/package.json +23 -0
- package/parser/flight_data.js +333 -0
- package/parser/flight_data.test.js +59 -0
- package/parser/manifests.js +46 -0
- package/parser/manifests.test.js +36 -0
- package/parser/next_data.js +33 -0
- package/parser/next_data.test.js +15 -0
- package/parser/types.js +351 -0
- package/parser/types.test.js +261 -0
- package/parser/urls.js +72 -0
- package/parser/urls.test.js +26 -0
- package/test/src/index.js +16 -0
- package/tools.js +212 -0
- package/tools.test.js +153 -0
- package/utils.js +33 -0
- package/utils.test.js +38 -0
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)));
|