@xcrap/dom 0.0.1
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 +21 -0
- package/README.md +121 -0
- package/dist/errors.d.ts +13 -0
- package/dist/errors.d.ts.map +1 -0
- package/dist/errors.js +23 -0
- package/dist/errors.js.map +1 -0
- package/dist/extractors.d.ts +48 -0
- package/dist/extractors.d.ts.map +1 -0
- package/dist/extractors.js +87 -0
- package/dist/extractors.js.map +1 -0
- package/dist/index.d.ts +6 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +6 -0
- package/dist/index.js.map +1 -0
- package/dist/parser.d.ts +31 -0
- package/dist/parser.d.ts.map +1 -0
- package/dist/parser.js +55 -0
- package/dist/parser.js.map +1 -0
- package/dist/parsing-model.d.ts +29 -0
- package/dist/parsing-model.d.ts.map +1 -0
- package/dist/parsing-model.interface.d.ts +4 -0
- package/dist/parsing-model.interface.d.ts.map +1 -0
- package/dist/parsing-model.interface.js +2 -0
- package/dist/parsing-model.interface.js.map +1 -0
- package/dist/parsing-model.js +66 -0
- package/dist/parsing-model.js.map +1 -0
- package/package.json +30 -0
- package/tsconfig.json +33 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2025 Marcuth
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,121 @@
|
|
|
1
|
+
# 🕷️ Xcrap DOM Parser: Parsing HTML using declarative models
|
|
2
|
+
|
|
3
|
+
Xcrap DOM is a package from the Xcrap framework, designed to handle DOM data extraction (client-side) using declarative models. It is perfect for use in web scraping extensions and TamperMonkey user scripts.
|
|
4
|
+
|
|
5
|
+
---
|
|
6
|
+
|
|
7
|
+
## 📦 Installation
|
|
8
|
+
|
|
9
|
+
Installation is very simple; you can use NPM or any other package manager of your choice, such as PNPM, Yarn, etc.
|
|
10
|
+
|
|
11
|
+
```bash
|
|
12
|
+
npm i @xcrap/dom
|
|
13
|
+
|
|
14
|
+
```
|
|
15
|
+
|
|
16
|
+
---
|
|
17
|
+
|
|
18
|
+
## 🛠️ How to Use
|
|
19
|
+
|
|
20
|
+
There are several ways to use this parsing engine, from using pre-made models to expanding it by creating parsers for other file types and maintaining the interlocking of these models.
|
|
21
|
+
|
|
22
|
+
### Providing an HTML string
|
|
23
|
+
|
|
24
|
+
```ts
|
|
25
|
+
import { DomParser } from "@xcrap/dom"
|
|
26
|
+
|
|
27
|
+
const html = "<html><head><title>Page Title</title></head><body></body></html>" // or document.documentElement.outerHTML
|
|
28
|
+
const parser = new DomParser(html)
|
|
29
|
+
|
|
30
|
+
```
|
|
31
|
+
|
|
32
|
+
### Data extraction without using models
|
|
33
|
+
|
|
34
|
+
```ts
|
|
35
|
+
import { DomParser, extract } from "@xcrap/dom"
|
|
36
|
+
|
|
37
|
+
const html = `<html><head><title>Page Title</title></head><body><a href="https://example.com">Link</a></body></html>`
|
|
38
|
+
const parser = new DomParser(html)
|
|
39
|
+
|
|
40
|
+
// parseFirst() searches for and extracts something from the first element found
|
|
41
|
+
// extract(key: string, isAttribute?: boolean) is a generic extraction function; you can use some that are already created and ready for use by importing them from the same location :)
|
|
42
|
+
const title = parser.parseFirst({ query: "title", extractor: extract("innerText") })
|
|
43
|
+
|
|
44
|
+
// parseMany() searches for all elements matching a query (you can limit the number of results) and uses the extractor to get the data
|
|
45
|
+
const links = parser.parseMany({ query: "a", extractor: extract("href", true) })
|
|
46
|
+
|
|
47
|
+
console.log(title) // "Page Title"
|
|
48
|
+
console.log(links) // ["https://example.com"]
|
|
49
|
+
|
|
50
|
+
```
|
|
51
|
+
|
|
52
|
+
### Data extraction using models
|
|
53
|
+
|
|
54
|
+
ParsingModels are sufficiently decoupled so that you don't have to rely on Parser instances, but we will use them here nonetheless:
|
|
55
|
+
|
|
56
|
+
```ts
|
|
57
|
+
import { DomParser, DomParsingModel, extract } from "@xcrap/dom"
|
|
58
|
+
|
|
59
|
+
const html = `<html><body><h1>Header</h1><div><p id="id">1</p><p id="name">Name</p><p class="age">23</p></div></body></html>`
|
|
60
|
+
const parser = new DomParser(html)
|
|
61
|
+
|
|
62
|
+
const rootParsingModel = new DomParsingModel({
|
|
63
|
+
heading: {
|
|
64
|
+
query: "h1",
|
|
65
|
+
extractor: extract("innerText")
|
|
66
|
+
},
|
|
67
|
+
id: {
|
|
68
|
+
query: "#id",
|
|
69
|
+
extractor: extract("innerText")
|
|
70
|
+
},
|
|
71
|
+
name: {
|
|
72
|
+
query: "#name",
|
|
73
|
+
extractor: extract("innerText")
|
|
74
|
+
},
|
|
75
|
+
age: {
|
|
76
|
+
query: ".age",
|
|
77
|
+
extractor: extract("innerText")
|
|
78
|
+
}
|
|
79
|
+
})
|
|
80
|
+
|
|
81
|
+
const data = parser.extractFirst({ model: rootParsingModel })
|
|
82
|
+
|
|
83
|
+
console.log(data) // { heading: "Header", id: "1", name: "Name", age: "23" }
|
|
84
|
+
|
|
85
|
+
```
|
|
86
|
+
|
|
87
|
+
## 🧠 Create your own Parser: Concepts
|
|
88
|
+
|
|
89
|
+
### What is a Parser?
|
|
90
|
+
|
|
91
|
+
A Parser for this library is a class that handles a file type in some way, loads that file, and may or may not have methods to easily extract data.
|
|
92
|
+
|
|
93
|
+
A parser has a default method called `parseModel`, which is a wrapper that receives a `ParsingModel` and calls the `parse()` method, providing the internal `source` property.
|
|
94
|
+
|
|
95
|
+
### What is a ParsingModel?
|
|
96
|
+
|
|
97
|
+
A Parsing Model is a class that receives a `shape` in its constructor and stores it as a property. It must have a method called `parse()` that receives a `source`, which is the code/text containing the information to be extracted.
|
|
98
|
+
|
|
99
|
+
This `shape` is used to declare how the information will be extracted from the `source`.
|
|
100
|
+
|
|
101
|
+
## 🧪 Testing
|
|
102
|
+
|
|
103
|
+
Automated tests are located in `__tests__`. To run them:
|
|
104
|
+
|
|
105
|
+
```bash
|
|
106
|
+
npm run test
|
|
107
|
+
|
|
108
|
+
```
|
|
109
|
+
|
|
110
|
+
## 🤝 Contributing
|
|
111
|
+
|
|
112
|
+
* Want to contribute? Follow these steps:
|
|
113
|
+
* Fork the repository.
|
|
114
|
+
* Create a new branch (`git checkout -b feature-new`).
|
|
115
|
+
* Commit your changes (`git commit -m 'Add new feature'`).
|
|
116
|
+
* Push to the branch (`git push origin feature-new`).
|
|
117
|
+
* Open a Pull Request.
|
|
118
|
+
|
|
119
|
+
## 📝 License
|
|
120
|
+
|
|
121
|
+
This project is licensed under the MIT License.
|
package/dist/errors.d.ts
ADDED
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
export declare class HTMLElementNotFoundError extends Error {
|
|
2
|
+
constructor(query?: string);
|
|
3
|
+
}
|
|
4
|
+
export declare class MultipleQueryError extends Error {
|
|
5
|
+
constructor();
|
|
6
|
+
}
|
|
7
|
+
export declare class FieldNotFoundError extends Error {
|
|
8
|
+
constructor(key: string);
|
|
9
|
+
}
|
|
10
|
+
export declare class ExtractorNotFoundError extends Error {
|
|
11
|
+
constructor(name: string);
|
|
12
|
+
}
|
|
13
|
+
//# sourceMappingURL=errors.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"errors.d.ts","sourceRoot":"","sources":["../src/errors.ts"],"names":[],"mappings":"AAAA,qBAAa,wBAAyB,SAAQ,KAAK;gBACnC,KAAK,CAAC,EAAE,MAAM;CAI7B;AAED,qBAAa,kBAAmB,SAAQ,KAAK;;CAK5C;AAED,qBAAa,kBAAmB,SAAQ,KAAK;gBAC7B,GAAG,EAAE,MAAM;CAG1B;AAED,qBAAa,sBAAuB,SAAQ,KAAK;gBACjC,IAAI,EAAE,MAAM;CAG3B"}
|
package/dist/errors.js
ADDED
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
export class HTMLElementNotFoundError extends Error {
|
|
2
|
+
constructor(query) {
|
|
3
|
+
super(`Element with query "${query || 'no query provided'}" not found`);
|
|
4
|
+
this.name = "HTMLElementNotFoundError";
|
|
5
|
+
}
|
|
6
|
+
}
|
|
7
|
+
export class MultipleQueryError extends Error {
|
|
8
|
+
constructor() {
|
|
9
|
+
super("Multiple value must have a 'query'");
|
|
10
|
+
this.name = "MultipleQueryError";
|
|
11
|
+
}
|
|
12
|
+
}
|
|
13
|
+
export class FieldNotFoundError extends Error {
|
|
14
|
+
constructor(key) {
|
|
15
|
+
super(`Field with key "${key}" not found`);
|
|
16
|
+
}
|
|
17
|
+
}
|
|
18
|
+
export class ExtractorNotFoundError extends Error {
|
|
19
|
+
constructor(name) {
|
|
20
|
+
super(`Extractor with name "${name}" not found`);
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
//# sourceMappingURL=errors.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"errors.js","sourceRoot":"","sources":["../src/errors.ts"],"names":[],"mappings":"AAAA,MAAM,OAAO,wBAAyB,SAAQ,KAAK;IAC/C,YAAY,KAAc;QACtB,KAAK,CAAC,uBAAuB,KAAK,IAAI,mBAAmB,aAAa,CAAC,CAAA;QACvE,IAAI,CAAC,IAAI,GAAG,0BAA0B,CAAA;IAC1C,CAAC;CACJ;AAED,MAAM,OAAO,kBAAmB,SAAQ,KAAK;IACzC;QACI,KAAK,CAAC,oCAAoC,CAAC,CAAA;QAC3C,IAAI,CAAC,IAAI,GAAG,oBAAoB,CAAA;IACpC,CAAC;CACJ;AAED,MAAM,OAAO,kBAAmB,SAAQ,KAAK;IACzC,YAAY,GAAW;QACnB,KAAK,CAAC,mBAAmB,GAAG,aAAa,CAAC,CAAA;IAC9C,CAAC;CACJ;AAED,MAAM,OAAO,sBAAuB,SAAQ,KAAK;IAC7C,YAAY,IAAY;QACpB,KAAK,CAAC,wBAAwB,IAAI,aAAa,CAAC,CAAA;IACpD,CAAC;CACJ"}
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
export type ExtractorFunctionReturnType = string | undefined;
|
|
2
|
+
export type ExtractorFunction<T = ExtractorFunctionReturnType> = (element: HTMLElement) => T;
|
|
3
|
+
export type HtmlProperty = "innerText" | "textContent" | "innerHTML" | "outerHTML" | "tagName" | "classList" | "id" | "childElementCount" | "attributes" | "localName" | "nodeType";
|
|
4
|
+
export type HtmlAttribute = "href" | "src" | "value" | "style" | "role" | "alt" | "title" | "placeholder" | "disabled" | "readonly" | "checked" | "selected" | "name" | "type" | "autocomplete" | "maxlength" | "minlength" | "pattern" | "required" | "aria-label" | "aria-hidden" | "aria-expanded" | "aria-checked" | "aria-disabled" | "data-*" | (string & {});
|
|
5
|
+
export declare const propertyExtractors: Record<HtmlProperty, (element: HTMLElement) => unknown | undefined>;
|
|
6
|
+
export declare function extract<T extends HtmlProperty | HtmlAttribute, R = string>(key: T, isAttribute?: boolean): ExtractorFunction<R | undefined>;
|
|
7
|
+
export declare const extractInnerText: ExtractorFunction<string | undefined>;
|
|
8
|
+
export declare const extractTextContent: ExtractorFunction<string | undefined>;
|
|
9
|
+
export declare const extractText: ExtractorFunction<string | undefined>;
|
|
10
|
+
export declare const extractInnerHtml: ExtractorFunction<string | undefined>;
|
|
11
|
+
export declare const extractOuterHtml: ExtractorFunction<string | undefined>;
|
|
12
|
+
export declare const extractTagName: ExtractorFunction<string | undefined>;
|
|
13
|
+
export declare const extractClassList: ExtractorFunction<string[] | undefined>;
|
|
14
|
+
export declare const extractId: ExtractorFunction<string | undefined>;
|
|
15
|
+
export declare const extractHref: ExtractorFunction<string | undefined>;
|
|
16
|
+
export declare const extractSrc: ExtractorFunction<string | undefined>;
|
|
17
|
+
export declare const extractValue: ExtractorFunction<string | undefined>;
|
|
18
|
+
export declare const extractStyle: ExtractorFunction<string | undefined>;
|
|
19
|
+
export declare const extractRole: ExtractorFunction<string | undefined>;
|
|
20
|
+
export declare const extractTitle: ExtractorFunction<string | undefined>;
|
|
21
|
+
export declare const extractPlaceholder: ExtractorFunction<string | undefined>;
|
|
22
|
+
export declare const extractDisabled: ExtractorFunction<string | undefined>;
|
|
23
|
+
export declare const extractReadonly: ExtractorFunction<string | undefined>;
|
|
24
|
+
export declare const extractChecked: ExtractorFunction<string | undefined>;
|
|
25
|
+
export declare const extractSelected: ExtractorFunction<string | undefined>;
|
|
26
|
+
export declare const extractName: ExtractorFunction<string | undefined>;
|
|
27
|
+
export declare const extractType: ExtractorFunction<string | undefined>;
|
|
28
|
+
export declare const extractAutocomplete: ExtractorFunction<string | undefined>;
|
|
29
|
+
export declare const extractMaxLength: ExtractorFunction<string | undefined>;
|
|
30
|
+
export declare const extractMinLength: ExtractorFunction<string | undefined>;
|
|
31
|
+
export declare const extractPattern: ExtractorFunction<string | undefined>;
|
|
32
|
+
export declare const extractRequired: ExtractorFunction<string | undefined>;
|
|
33
|
+
export declare const extractAriaLabel: ExtractorFunction<string | undefined>;
|
|
34
|
+
export declare const extractAriaHidden: ExtractorFunction<string | undefined>;
|
|
35
|
+
export declare const extractAriaExpanded: ExtractorFunction<string | undefined>;
|
|
36
|
+
export declare const extractAriaChecked: ExtractorFunction<string | undefined>;
|
|
37
|
+
export declare const extractAriaDisabled: ExtractorFunction<string | undefined>;
|
|
38
|
+
export declare const extractAllData: ExtractorFunction<string | undefined>;
|
|
39
|
+
export declare const extractAttribute: <T extends string>(name: T) => ExtractorFunction<string | undefined>;
|
|
40
|
+
export declare const extractChildElementCount: ExtractorFunction<number | undefined>;
|
|
41
|
+
export declare const extractLocalName: ExtractorFunction<string | undefined>;
|
|
42
|
+
export declare const extractNodeType: ExtractorFunction<string | undefined>;
|
|
43
|
+
export type FromNextOrPreviousElementSiblingOptions = {
|
|
44
|
+
shouldExists?: boolean;
|
|
45
|
+
};
|
|
46
|
+
export declare const fromNextElementSibling: (extractor: ExtractorFunction, { shouldExists }?: FromNextOrPreviousElementSiblingOptions) => ExtractorFunction;
|
|
47
|
+
export declare const fromPreviousElementSibling: (extractor: ExtractorFunction, { shouldExists }?: FromNextOrPreviousElementSiblingOptions) => ExtractorFunction;
|
|
48
|
+
//# sourceMappingURL=extractors.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"extractors.d.ts","sourceRoot":"","sources":["../src/extractors.ts"],"names":[],"mappings":"AAEA,MAAM,MAAM,2BAA2B,GAAG,MAAM,GAAG,SAAS,CAAA;AAE5D,MAAM,MAAM,iBAAiB,CAAC,CAAC,GAAG,2BAA2B,IAAI,CAAC,OAAO,EAAE,WAAW,KAAK,CAAC,CAAA;AAE5F,MAAM,MAAM,YAAY,GAClB,WAAW,GACX,aAAa,GACb,WAAW,GACX,WAAW,GACX,SAAS,GACT,WAAW,GACX,IAAI,GACJ,mBAAmB,GACnB,YAAY,GACZ,WAAW,GACX,UAAU,CAAA;AAEhB,MAAM,MAAM,aAAa,GACnB,MAAM,GACN,KAAK,GACL,OAAO,G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eAAe,uCAAgD,CAAA;AAC5E,eAAO,MAAM,cAAc,uCAA8C,CAAA;AACzE,eAAO,MAAM,eAAe,uCAAgD,CAAA;AAC5E,eAAO,MAAM,WAAW,uCAAwC,CAAA;AAChE,eAAO,MAAM,WAAW,uCAAwC,CAAA;AAChE,eAAO,MAAM,mBAAmB,uCAAwD,CAAA;AACxF,eAAO,MAAM,gBAAgB,uCAAkD,CAAA;AAC/E,eAAO,MAAM,gBAAgB,uCAAkD,CAAA;AAC/E,eAAO,MAAM,cAAc,uCAA8C,CAAA;AACzE,eAAO,MAAM,eAAe,uCAAgD,CAAA;AAC5E,eAAO,MAAM,gBAAgB,uCAAoD,CAAA;AACjF,eAAO,MAAM,iBAAiB,uCAAsD,CAAA;AACpF,eAAO,MAAM,mBAAmB,uCAA0D,CAAA;AAC1F,eAAO,MAAM,kBAAkB,uCAAwD,CAAA;AACvF,eAAO,MAAM,mBAAmB,uCAA0D,CAAA;AAC1F,eAAO,MAAM,cAAc,uCAA4C,CAAA;AACvE,eAAO,MAAM,gBAAgB,GAAI,CAAC,SAAS,MAAM,EAAE,MAAM,CAAC,0CAAmC,CAAA;AAC7F,eAAO,MAAM,wBAAwB,uCAA4D,CAAA;AACjG,eAAO,MAAM,gBAAgB,uCAA4C,CAAA;AACzE,eAAO,MAAM,eAAe,uCAA0C,CAAA;AAEtE,MAAM,MAAM,uCAAuC,GAAG;IAClD,YAAY,CAAC,EAAE,OAAO,CAAA;CACzB,CAAA;AAED,eAAO,MAAM,sBAAsB,GAC/B,WAAW,iBAAiB,EAC5B,mBAAkB,uCAAgE,KACnF,iBAcF,CAAA;AAED,eAAO,MAAM,0BAA0B,GACnC,WAAW,iBAAiB,EAC5B,mBAAkB,uCAAgE,KACnF,iBAcF,CAAA"}
|
|
@@ -0,0 +1,87 @@
|
|
|
1
|
+
import { ExtractorNotFoundError, HTMLElementNotFoundError } from "./errors.js";
|
|
2
|
+
export const propertyExtractors = {
|
|
3
|
+
innerText: (element) => element.innerText,
|
|
4
|
+
textContent: (element) => element.textContent,
|
|
5
|
+
innerHTML: (element) => element.innerHTML,
|
|
6
|
+
outerHTML: (element) => element.outerHTML,
|
|
7
|
+
tagName: (element) => element.tagName,
|
|
8
|
+
classList: (element) => Array.from(element.classList.values()),
|
|
9
|
+
id: (element) => element.id,
|
|
10
|
+
attributes: (element) => element.attributes,
|
|
11
|
+
childElementCount: (element) => element.childElementCount,
|
|
12
|
+
localName: (element) => element.localName,
|
|
13
|
+
nodeType: (element) => element.localName,
|
|
14
|
+
};
|
|
15
|
+
export function extract(key, isAttribute = false) {
|
|
16
|
+
return (element) => {
|
|
17
|
+
if (isAttribute) {
|
|
18
|
+
return element.getAttribute(key);
|
|
19
|
+
}
|
|
20
|
+
const extractor = propertyExtractors[key];
|
|
21
|
+
if (!extractor) {
|
|
22
|
+
throw new ExtractorNotFoundError(key);
|
|
23
|
+
}
|
|
24
|
+
return extractor(element);
|
|
25
|
+
};
|
|
26
|
+
}
|
|
27
|
+
export const extractInnerText = extract("innerText");
|
|
28
|
+
export const extractTextContent = extract("textContent");
|
|
29
|
+
export const extractText = extract("text");
|
|
30
|
+
export const extractInnerHtml = extract("innerHTML");
|
|
31
|
+
export const extractOuterHtml = extract("outerHTML");
|
|
32
|
+
export const extractTagName = extract("tagName");
|
|
33
|
+
export const extractClassList = extract("classList");
|
|
34
|
+
export const extractId = extract("id");
|
|
35
|
+
export const extractHref = extract("href", true);
|
|
36
|
+
export const extractSrc = extract("src", true);
|
|
37
|
+
export const extractValue = extract("value", true);
|
|
38
|
+
export const extractStyle = extract("style", true);
|
|
39
|
+
export const extractRole = extract("role", true);
|
|
40
|
+
export const extractTitle = extract("title", true);
|
|
41
|
+
export const extractPlaceholder = extract("placeholder", true);
|
|
42
|
+
export const extractDisabled = extract("disabled", true);
|
|
43
|
+
export const extractReadonly = extract("readonly", true);
|
|
44
|
+
export const extractChecked = extract("checked", true);
|
|
45
|
+
export const extractSelected = extract("selected", true);
|
|
46
|
+
export const extractName = extract("name", true);
|
|
47
|
+
export const extractType = extract("type", true);
|
|
48
|
+
export const extractAutocomplete = extract("autocomplete", true);
|
|
49
|
+
export const extractMaxLength = extract("maxlength", true);
|
|
50
|
+
export const extractMinLength = extract("minlength", true);
|
|
51
|
+
export const extractPattern = extract("pattern", true);
|
|
52
|
+
export const extractRequired = extract("required", true);
|
|
53
|
+
export const extractAriaLabel = extract("aria-label", true);
|
|
54
|
+
export const extractAriaHidden = extract("aria-hidden", true);
|
|
55
|
+
export const extractAriaExpanded = extract("aria-expanded", true);
|
|
56
|
+
export const extractAriaChecked = extract("aria-checked", true);
|
|
57
|
+
export const extractAriaDisabled = extract("aria-disabled", true);
|
|
58
|
+
export const extractAllData = extract("data-*", true);
|
|
59
|
+
export const extractAttribute = (name) => extract(name, true);
|
|
60
|
+
export const extractChildElementCount = extract("childElementCount");
|
|
61
|
+
export const extractLocalName = extract("localName");
|
|
62
|
+
export const extractNodeType = extract("nodeType");
|
|
63
|
+
export const fromNextElementSibling = (extractor, { shouldExists } = { shouldExists: true }) => {
|
|
64
|
+
return (element) => {
|
|
65
|
+
const nextElementSibling = element.nextElementSibling;
|
|
66
|
+
if (!nextElementSibling) {
|
|
67
|
+
if (shouldExists) {
|
|
68
|
+
throw new HTMLElementNotFoundError();
|
|
69
|
+
}
|
|
70
|
+
return undefined;
|
|
71
|
+
}
|
|
72
|
+
return extractor(nextElementSibling);
|
|
73
|
+
};
|
|
74
|
+
};
|
|
75
|
+
export const fromPreviousElementSibling = (extractor, { shouldExists } = { shouldExists: true }) => {
|
|
76
|
+
return (element) => {
|
|
77
|
+
const previousElementSibling = element.previousElementSibling;
|
|
78
|
+
if (!previousElementSibling) {
|
|
79
|
+
if (shouldExists) {
|
|
80
|
+
throw new HTMLElementNotFoundError();
|
|
81
|
+
}
|
|
82
|
+
return undefined;
|
|
83
|
+
}
|
|
84
|
+
return extractor(previousElementSibling);
|
|
85
|
+
};
|
|
86
|
+
};
|
|
87
|
+
//# sourceMappingURL=extractors.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C1B,IAAI,YAAY,EAAE,CAAC;gBACf,MAAM,IAAI,wBAAwB,EAAE,CAAA;YACxC,CAAC;YAED,OAAO,SAAS,CAAA;QACpB,CAAC;QAED,OAAO,SAAS,CAAC,sBAAqC,CAAC,CAAA;IAC3D,CAAC,CAAA;AACL,CAAC,CAAA"}
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,aAAa,CAAA;AAC3B,cAAc,oBAAoB,CAAA;AAClC,cAAc,8BAA8B,CAAA;AAC5C,cAAc,iBAAiB,CAAA;AAC/B,cAAc,aAAa,CAAA"}
|
package/dist/index.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,aAAa,CAAA;AAC3B,cAAc,oBAAoB,CAAA;AAClC,cAAc,8BAA8B,CAAA;AAC5C,cAAc,iBAAiB,CAAA;AAC/B,cAAc,aAAa,CAAA"}
|
package/dist/parser.d.ts
ADDED
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
import { ParsingModel } from "./parsing-model.interface.js";
|
|
2
|
+
import { ExtractorFunction } from "./extractors.js";
|
|
3
|
+
export type ParseManyOptions = {
|
|
4
|
+
query: string;
|
|
5
|
+
extractor: ExtractorFunction;
|
|
6
|
+
limit?: number;
|
|
7
|
+
};
|
|
8
|
+
export type ParseFirstOptions = {
|
|
9
|
+
query?: string;
|
|
10
|
+
extractor: ExtractorFunction;
|
|
11
|
+
default?: string | null;
|
|
12
|
+
};
|
|
13
|
+
export type ExtractFirstOptions = {
|
|
14
|
+
query?: string;
|
|
15
|
+
model: ParsingModel;
|
|
16
|
+
};
|
|
17
|
+
export type ExtractManyOptions = {
|
|
18
|
+
query: string;
|
|
19
|
+
model: ParsingModel;
|
|
20
|
+
limit?: number;
|
|
21
|
+
};
|
|
22
|
+
export declare class DomParser {
|
|
23
|
+
readonly source: string;
|
|
24
|
+
readonly root: Document;
|
|
25
|
+
constructor(source: string);
|
|
26
|
+
parseMany({ query, extractor, limit }: ParseManyOptions): (string | undefined)[];
|
|
27
|
+
parseFirst({ query, extractor, default: default_ }: ParseFirstOptions): any | undefined | null;
|
|
28
|
+
extractFirst({ model, query }: ExtractFirstOptions): any;
|
|
29
|
+
extractMany({ model, query, limit }: ExtractManyOptions): any[];
|
|
30
|
+
}
|
|
31
|
+
//# sourceMappingURL=parser.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"parser.d.ts","sourceRoot":"","sources":["../src/parser.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,YAAY,EAAE,MAAM,8BAA8B,CAAA;AAE3D,OAAO,EAAE,iBAAiB,EAAE,MAAM,iBAAiB,CAAA;AAEnD,MAAM,MAAM,gBAAgB,GAAG;IAC3B,KAAK,EAAE,MAAM,CAAA;IACb,SAAS,EAAE,iBAAiB,CAAA;IAC5B,KAAK,CAAC,EAAE,MAAM,CAAA;CACjB,CAAA;AAED,MAAM,MAAM,iBAAiB,GAAG;IAC5B,KAAK,CAAC,EAAE,MAAM,CAAA;IACd,SAAS,EAAE,iBAAiB,CAAA;IAC5B,OAAO,CAAC,EAAE,MAAM,GAAG,IAAI,CAAA;CAC1B,CAAA;AAED,MAAM,MAAM,mBAAmB,GAAG;IAC9B,KAAK,CAAC,EAAE,MAAM,CAAA;IACd,KAAK,EAAE,YAAY,CAAA;CACtB,CAAA;AAED,MAAM,MAAM,kBAAkB,GAAG;IAC7B,KAAK,EAAE,MAAM,CAAA;IACb,KAAK,EAAE,YAAY,CAAA;IACnB,KAAK,CAAC,EAAE,MAAM,CAAA;CACjB,CAAA;AAED,qBAAa,SAAS;IAGN,QAAQ,CAAC,MAAM,EAAE,MAAM;IAFnC,QAAQ,CAAC,IAAI,EAAE,QAAQ,CAAA;gBAEF,MAAM,EAAE,MAAM;IAInC,SAAS,CAAC,EACN,KAAK,EACL,SAAS,EACT,KAAK,EACR,EAAE,gBAAgB,GAAG,CAAC,MAAM,GAAG,SAAS,CAAC,EAAE;IAc5C,UAAU,CAAC,EACP,KAAK,EACL,SAAS,EACT,OAAO,EAAE,QAAQ,EACpB,EAAE,iBAAiB,GAAG,GAAG,GAAG,SAAS,GAAG,IAAI;IAmB7C,YAAY,CAAC,EAAE,KAAK,EAAE,KAAK,EAAE,EAAE,mBAAmB;IAYlD,WAAW,CAAC,EAAE,KAAK,EAAE,KAAK,EAAE,KAAK,EAAE,EAAE,kBAAkB;CAa1D"}
|
package/dist/parser.js
ADDED
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
import { HTMLElementNotFoundError } from "./errors.js";
|
|
2
|
+
export class DomParser {
|
|
3
|
+
constructor(source) {
|
|
4
|
+
this.source = source;
|
|
5
|
+
this.root = new window.DOMParser().parseFromString(source, "text/html");
|
|
6
|
+
}
|
|
7
|
+
parseMany({ query, extractor, limit }) {
|
|
8
|
+
const elements = this.root.querySelectorAll(query);
|
|
9
|
+
const items = [];
|
|
10
|
+
for (const element of elements) {
|
|
11
|
+
if (limit !== undefined && items.length >= limit)
|
|
12
|
+
break;
|
|
13
|
+
const data = extractor(element);
|
|
14
|
+
items.push(data);
|
|
15
|
+
}
|
|
16
|
+
return items;
|
|
17
|
+
}
|
|
18
|
+
parseFirst({ query, extractor, default: default_ }) {
|
|
19
|
+
let data;
|
|
20
|
+
if (query) {
|
|
21
|
+
const element = this.root.querySelector(query);
|
|
22
|
+
if (!element) {
|
|
23
|
+
if (default_ !== undefined)
|
|
24
|
+
return default_;
|
|
25
|
+
throw new HTMLElementNotFoundError(query);
|
|
26
|
+
}
|
|
27
|
+
data = extractor(element);
|
|
28
|
+
}
|
|
29
|
+
else {
|
|
30
|
+
data = extractor(this.root.documentElement);
|
|
31
|
+
}
|
|
32
|
+
return data ?? default_;
|
|
33
|
+
}
|
|
34
|
+
extractFirst({ model, query }) {
|
|
35
|
+
const element = query
|
|
36
|
+
? this.root.querySelector(query)
|
|
37
|
+
: this.root.documentElement;
|
|
38
|
+
if (!element) {
|
|
39
|
+
throw new HTMLElementNotFoundError(query);
|
|
40
|
+
}
|
|
41
|
+
return model.parse(element.outerHTML);
|
|
42
|
+
}
|
|
43
|
+
extractMany({ model, query, limit }) {
|
|
44
|
+
const elements = this.root.querySelectorAll(query);
|
|
45
|
+
const dataList = [];
|
|
46
|
+
for (const element of elements) {
|
|
47
|
+
if (limit !== undefined && dataList.length >= limit)
|
|
48
|
+
break;
|
|
49
|
+
const data = model.parse(element.outerHTML);
|
|
50
|
+
dataList.push(data);
|
|
51
|
+
}
|
|
52
|
+
return dataList;
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
//# sourceMappingURL=parser.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"parser.js","sourceRoot":"","sources":["../src/parser.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,wBAAwB,EAAE,MAAM,aAAa,CAAA;AA0BtD,MAAM,OAAO,SAAS;IAGlB,YAAqB,MAAc;QAAd,WAAM,GAAN,MAAM,CAAQ;QAC/B,IAAI,CAAC,IAAI,GAAG,IAAI,MAAM,CAAC,SAAS,EAAE,CAAC,eAAe,CAAC,MAAM,EAAE,WAAW,CAAC,CAAA;IAC3E,CAAC;IAED,SAAS,CAAC,EACN,KAAK,EACL,SAAS,EACT,KAAK,EACU;QACf,MAAM,QAAQ,GAAG,IAAI,CAAC,IAAI,CAAC,gBAAgB,CAAC,KAAK,CAAC,CAAA;QAElD,MAAM,KAAK,GAA2B,EAAE,CAAA;QAExC,KAAK,MAAM,OAAO,IAAI,QAAQ,EAAE,CAAC;YAC7B,IAAI,KAAK,KAAK,SAAS,IAAI,KAAK,CAAC,MAAM,IAAI,KAAK;gBAAE,MAAK;YACvD,MAAM,IAAI,GAAG,SAAS,CAAC,OAAsB,CAAC,CAAA;YAC9C,KAAK,CAAC,IAAI,CAAC,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AE,CAAC;YAC7B,IAAI,KAAK,KAAK,SAAS,IAAI,QAAQ,CAAC,MAAM,IAAI,KAAK;gBAAE,MAAK;YAC1D,MAAM,IAAI,GAAG,KAAK,CAAC,KAAK,CAAC,OAAO,CAAC,SAAS,CAAC,CAAA;YAC3C,QAAQ,CAAC,IAAI,CAAC,IAAI,CAAC,CAAA;QACvB,CAAC;QAED,OAAO,QAAQ,CAAA;IACnB,CAAC;CACJ"}
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
import { ParsingModel } from "./parsing-model.interface.js";
|
|
2
|
+
import { ExtractorFunction } from "./extractors.js";
|
|
3
|
+
export type DomParsingModelShapeBaseValue = {
|
|
4
|
+
query?: string;
|
|
5
|
+
default?: string | string[] | null;
|
|
6
|
+
multiple?: boolean;
|
|
7
|
+
limit?: number;
|
|
8
|
+
extractor: ExtractorFunction;
|
|
9
|
+
};
|
|
10
|
+
export type DomParsingModelShapeNestedValue = {
|
|
11
|
+
query: string;
|
|
12
|
+
limit?: number;
|
|
13
|
+
multiple?: boolean;
|
|
14
|
+
model: ParsingModel;
|
|
15
|
+
extractor?: ExtractorFunction;
|
|
16
|
+
};
|
|
17
|
+
export type DomParsingModelValue = DomParsingModelShapeBaseValue | DomParsingModelShapeNestedValue;
|
|
18
|
+
export type DomParsingModelShape = {
|
|
19
|
+
[key: string]: DomParsingModelValue;
|
|
20
|
+
};
|
|
21
|
+
export type ParseBaseValueReturnType = (undefined | string)[] | string | null | undefined;
|
|
22
|
+
export declare class DomParsingModel implements ParsingModel {
|
|
23
|
+
readonly shape: DomParsingModelShape;
|
|
24
|
+
constructor(shape: DomParsingModelShape);
|
|
25
|
+
parse(source: string): any;
|
|
26
|
+
protected parseBaseValue(value: DomParsingModelShapeBaseValue, root: Element): ParseBaseValueReturnType;
|
|
27
|
+
protected parseNestedValue(value: DomParsingModelShapeNestedValue, root: Element): any;
|
|
28
|
+
}
|
|
29
|
+
//# sourceMappingURL=parsing-model.d.ts.map
|
|
@@ -0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tC,IAAI,EAAE,OAAO;CAwBpB"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"parsing-model.interface.d.ts","sourceRoot":"","sources":["../src/parsing-model.interface.ts"],"names":[],"mappings":"AAAA,MAAM,WAAW,YAAY;IACzB,KAAK,CAAC,MAAM,EAAE,MAAM,GAAG,GAAG,CAAA;CAC7B"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"parsing-model.interface.js","sourceRoot":"","sources":["../src/parsing-model.interface.ts"],"names":[],"mappings":""}
|
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
import { MultipleQueryError, HTMLElementNotFoundError } from "./errors.js";
|
|
2
|
+
export class DomParsingModel {
|
|
3
|
+
constructor(shape) {
|
|
4
|
+
this.shape = shape;
|
|
5
|
+
}
|
|
6
|
+
parse(source) {
|
|
7
|
+
const document = new window.DOMParser().parseFromString(source, "text/html");
|
|
8
|
+
const root = document.documentElement;
|
|
9
|
+
const data = {};
|
|
10
|
+
for (const key in this.shape) {
|
|
11
|
+
const value = this.shape[key];
|
|
12
|
+
const isNestedValue = "model" in value;
|
|
13
|
+
if (isNestedValue) {
|
|
14
|
+
data[key] = this.parseNestedValue(value, root);
|
|
15
|
+
}
|
|
16
|
+
else {
|
|
17
|
+
data[key] = this.parseBaseValue(value, root);
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
return data;
|
|
21
|
+
}
|
|
22
|
+
parseBaseValue(value, root) {
|
|
23
|
+
if (value.multiple) {
|
|
24
|
+
if (!value.query) {
|
|
25
|
+
throw new MultipleQueryError();
|
|
26
|
+
}
|
|
27
|
+
let elements = Array.from(root.querySelectorAll(value.query));
|
|
28
|
+
if (value.limit !== undefined) {
|
|
29
|
+
elements = elements.slice(0, value.limit);
|
|
30
|
+
}
|
|
31
|
+
return elements.map(element => value.extractor(element));
|
|
32
|
+
}
|
|
33
|
+
else {
|
|
34
|
+
const element = value.query
|
|
35
|
+
? root.querySelector(value.query)
|
|
36
|
+
: root;
|
|
37
|
+
if (!element) {
|
|
38
|
+
if (value.default === undefined) {
|
|
39
|
+
throw new HTMLElementNotFoundError(value.query);
|
|
40
|
+
}
|
|
41
|
+
return value.default;
|
|
42
|
+
}
|
|
43
|
+
return value.extractor(element);
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
parseNestedValue(value, root) {
|
|
47
|
+
if (value.multiple) {
|
|
48
|
+
let elements = Array.from(root.querySelectorAll(value.query));
|
|
49
|
+
if (value.limit !== undefined) {
|
|
50
|
+
elements = elements.slice(0, value.limit);
|
|
51
|
+
}
|
|
52
|
+
return elements.map(element => value.model.parse(element.outerHTML));
|
|
53
|
+
}
|
|
54
|
+
else {
|
|
55
|
+
const element = root.querySelector(value.query);
|
|
56
|
+
if (!element) {
|
|
57
|
+
throw new HTMLElementNotFoundError(value.query);
|
|
58
|
+
}
|
|
59
|
+
const source = value.extractor
|
|
60
|
+
? (value.extractor(element))
|
|
61
|
+
: element.outerHTML;
|
|
62
|
+
return value.model.parse(source);
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
//# sourceMappingURL=parsing-model.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"parsing-model.js","sourceRoot":"","sources":["../src/parsing-model.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,kBAAkB,EAAE,wBAAwB,EAAE,MAAM,aAAa,CAAA;AAkC1E,MAAM,OAAO,eAAe;IACxB,YAAqB,KAA2B;QAA3B,UAAK,GAAL,KAAK,CAAsB;IAAI,CAAC;IAErD,KAAK,CAAC,MAAc;QAChB,MAAM,QAAQ,GAAG,IAAI,MAAM,CAAC,SAAS,EAAE,CAAC,eAAe,CAAC,MAAM,EAAE,WAAW,CAAC,CAAA;QAC5E,MAAM,IAAI,GAAG,QAAQ,CAAC,eAAe,CAAA;QAErC,MAAM,IAAI,GAAyC,EAAE,CAAA;QAErD,KAAK,MAAM,GAAG,IAAI,IAAI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
2
|
+
"name": "@xcrap/dom",
|
|
3
|
+
"version": "0.0.1",
|
|
4
|
+
"description": "DOM parsing and extraction utilities",
|
|
5
|
+
"main": "./dist/index.js",
|
|
6
|
+
"module": "./dist/index.js",
|
|
7
|
+
"scripts": {
|
|
8
|
+
"build": "tsc"
|
|
9
|
+
},
|
|
10
|
+
"keywords": [
|
|
11
|
+
"dom",
|
|
12
|
+
"parsing",
|
|
13
|
+
"extraction",
|
|
14
|
+
"web",
|
|
15
|
+
"scraping",
|
|
16
|
+
"parser",
|
|
17
|
+
"browser",
|
|
18
|
+
"client-side",
|
|
19
|
+
"tampermonkey",
|
|
20
|
+
"user-script"
|
|
21
|
+
],
|
|
22
|
+
"author": "Marcuth",
|
|
23
|
+
"license": "MIT",
|
|
24
|
+
"type": "commonjs",
|
|
25
|
+
"devDependencies": {
|
|
26
|
+
"@types/jest": "^30.0.0",
|
|
27
|
+
"ts-jest": "^29.4.6",
|
|
28
|
+
"typescript": "^5.9.3"
|
|
29
|
+
}
|
|
30
|
+
}
|
package/tsconfig.json
ADDED
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
{
|
|
2
|
+
"compilerOptions": {
|
|
3
|
+
/* Target moderno, compatível com browsers atuais */
|
|
4
|
+
"target": "ES2020",
|
|
5
|
+
|
|
6
|
+
/* ESM puro */
|
|
7
|
+
"module": "ESNext",
|
|
8
|
+
|
|
9
|
+
/* CRÍTICO para permitir imports com .js no .ts */
|
|
10
|
+
"moduleResolution": "Bundler",
|
|
11
|
+
|
|
12
|
+
/* DOM real */
|
|
13
|
+
"lib": ["DOM", "DOM.Iterable", "ES2020"],
|
|
14
|
+
|
|
15
|
+
/* Lib pública */
|
|
16
|
+
"declaration": true,
|
|
17
|
+
"declarationMap": true,
|
|
18
|
+
|
|
19
|
+
/* DX */
|
|
20
|
+
"sourceMap": true,
|
|
21
|
+
"strict": true,
|
|
22
|
+
|
|
23
|
+
/* Necessário para bundlers (tsup) */
|
|
24
|
+
"isolatedModules": true,
|
|
25
|
+
|
|
26
|
+
/* Evita ruído */
|
|
27
|
+
"skipLibCheck": true,
|
|
28
|
+
|
|
29
|
+
/* Saída (opcional quando usa tsup, mas ok) */
|
|
30
|
+
"outDir": "./dist"
|
|
31
|
+
},
|
|
32
|
+
"include": ["src"]
|
|
33
|
+
}
|