mirror of
https://github.com/fofolee/uTools-quickcommand.git
synced 2025-06-08 06:16:27 +08:00
15 lines
429 B
JavaScript
15 lines
429 B
JavaScript
const htmlParser = (html, selector = "", attr = "") => {
|
|
const parser = new DOMParser();
|
|
const doc = parser.parseFromString(html, "text/html");
|
|
if (!selector) return doc;
|
|
const elements = doc.querySelectorAll(selector);
|
|
if (!attr) return elements;
|
|
let result = Array.from(elements).map((element) => element[attr]);
|
|
if (result.length === 1) return result[0];
|
|
return result;
|
|
};
|
|
|
|
module.exports = {
|
|
htmlParser,
|
|
};
|