@osmn-byhn/htmlparser
inlines styles from style tags
and resolves JavaScript event handlers (like onclick) into their actual
function bodies.
are automatically mapped to
the elements they target in the .onclick="doSomething()",
this library searches the script tags, finds doSomething, and includes its
full source code in the JSON entry for that element.npm install @osmn-byhn/htmlparser
or
pnpm add @osmn-byhn/htmlparser
or
yarn add @osmn-byhn/htmlparser
import { extractUnifiedFromHTML } from "@osmn-byhn/htmlparser";
const html = `
`;
async function main() {
const result = await extractUnifiedFromHTML(html);
const button = result.body.children[0];
console.log(button.inlineStyle); // { color: 'red' }
console.log(button.events.click.function); // "function sayHi() { ... }"
}
main();
import { extractUnifiedFromHTML } from "@osmn-byhn/htmlparser";
const result = await extractUnifiedFromHTML('Hello');
console.log(result.body);
const { extractUnifiedFromHTML } = require("@osmn-byhn/htmlparser");
extractUnifiedFromHTML('Hello').then(result => {
console.log(result.body);
});
UnifiedExtraction object:
| Field | Description |
|---|---|
metadata |
Stats: totalElements, maxDepth, totalTextNodes, etc.
|
body |
The root UnifiedElement (usually
the tag). |
UnifiedElement object:
{
"tag": "div",
"id": "main-container",
"class": "active primitive",
"attrs": { "data-custom": "value" },
"inlineStyle": {
"color": "red",
"font-size": "16px"
},
"events": {
"click": {
"handler": "myFunc()",
"function": "function myFunc() { ... }"
}
},
"children": [ ... ],
"textContent": "Hello World"
}
Author: Osman Beyhan
❤️ Make via MDtoWeb